summaryrefslogtreecommitdiffstats
path: root/src/test.c
blob: 41c9c9bd7d19658d47fe327d4ea0d6bce4cfa976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "list.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct arg {
	const char *str;

	struct list_link link;
};

bool
test_sort(const void *p1, const void *p2, void *u)
{
	const struct arg *a1 = p1, *a2 = p2;

	return strcmp(a1->str, a2->str) <= 0;
}

int
main(int argc, const char **argv)
{
	struct list list;
	struct arg *item;
	const char **arg;

	list_init(&list);

	if (argc < 3) {
		fprintf(stderr, "Usage: test REVERSE [STR]..\n");
		return 1;
	}

	for (arg = argv + 2; *arg; arg++) {
		item = malloc(sizeof(struct arg));
		if (!item) return 0;
		item->str = *arg;
		item->link = LIST_LINK_INIT;
		list_insert_back(&list, &item->link);
	}

	list_insertion_sort(&list, atoi(argv[1]), test_sort,
		LIST_OFFSET(struct arg, link), NULL);

	for (LIST_ITER_ITEMS(&list, item, struct arg, link))
		printf("%s\n", item->str);

	list_free_items(&list, free, LIST_OFFSET(struct arg, link));
}