Project

General

Profile

Bug #89 ยป test.c

Jason King, 2011-01-20 04:34 PM

 
1
#include <libuutil.h>
2
#include <stdio.h>
3
#include <stddef.h>
4
#include <err.h>
5
#include <stdlib.h>
6
#include <sys/types.h>
7

    
8
typedef struct test {
9
	uu_list_node_t	node;
10
	int		val;
11
} test_t;
12

    
13
static uu_list_pool_t	*sorted_p, *unsorted_p;
14

    
15
static int
16
test_compare(const void *l_arg, const void *r_arg, void *private)
17
{
18
	test_t *l = (test_t *)l;
19
	test_t *r = (test_t *)r;
20

    
21
	if (l->val > r->val)
22
		return (1);
23
	if (l->val < r->val)
24
		return (-1);
25
	return (0);
26
}
27

    
28
static test_t *
29
test_new(boolean_t sorted)
30
{
31
	test_t *t = malloc(sizeof (test_t));
32
	uu_list_pool_t *pool = (sorted) ? sorted_p : unsorted_p;
33

    
34
	if (t == NULL)
35
		err(EXIT_FAILURE, "%s: no memory!", __func__);
36

    
37
	uu_list_node_init(t, &t->node, pool);
38
	t->val = rand();
39
	return (t);
40
}
41

    
42
int
43
main(int argc, char **argv)
44
{
45
	uu_list_t *sorted_l, *unsorted_l;
46
	void *res;
47
	test_t cmp;
48

    
49
	sorted_p = uu_list_pool_create("sorted", sizeof (test_t),
50
	    offsetof(test_t, node), test_compare, 0);
51
	unsorted_p = uu_list_pool_create("unsorted", sizeof (test_t),
52
	    offsetof(test_t, node), NULL, 0);
53
	if (sorted_p == NULL || unsorted_p == NULL)
54
		err(EXIT_FAILURE, "%s: no memory!", __func__);
55

    
56
	sorted_l = uu_list_create(sorted_p, NULL, UU_LIST_SORTED);
57
	unsorted_l = uu_list_create(unsorted_p, NULL, 0);
58
	if (sorted_l == NULL || unsorted_l == NULL)
59
		err(EXIT_FAILURE, "%s(2): no memory!", __func__);
60

    
61
	cmp.val = 5;
62

    
63
	printf("uu_error = %s (%d)\n", uu_strerror(uu_error()), uu_error());
64
	res = uu_list_find(unsorted_l, NULL, NULL, NULL);
65
	printf("Unsorted:\n");
66
	printf("    uu_error = %s (%d); res = 0x%p\n", uu_strerror(uu_error()),
67
	    uu_error(), res);
68
	res = uu_list_find(sorted_l, &cmp, NULL, NULL);
69
	printf("Sorted:\n");
70
	printf("    uu_error = %s (%d); res = 0x%p\n", uu_strerror(uu_error()),
71
	    uu_error(), res);
72

    
73
	return (0);
74
}
75

    
    (1-1/1)