improve a_version_parse and implement a_version_tostr
[liba.git] / test / que.h
blobf66b77d6a248c445d56e4500f263e55b316d11eb
1 #define MAIN_(x) A_CAST_2(x, _que)
2 #include "test.h"
3 #include "a/que.h"
5 static void dtor(void *ptr)
7 a_u32 *obj = a_u32_(*, ptr);
8 printf("%" PRIu32 " ", *obj);
11 static void test(void)
13 a_que *ctx = a_que_new(sizeof(a_u64));
14 for (a_u64 i = 0; i != 10; ++i)
16 a_u64 *obj = a_que_push_back(a_u64, ctx);
17 if (obj) { *obj = i; }
20 a_u64 *fore = a_que_fore_(a_u64, ctx);
21 a_u64 *back = a_que_back_(a_u64, ctx);
22 printf("%" PRIu64 " %" PRIu64 " ", *fore, *back);
25 a_u64 *fore = a_que_fore(a_u64, ctx);
26 a_u64 *back = a_que_back(a_u64, ctx);
27 if (fore && back)
29 printf("%" PRIu64 " %" PRIu64 " ", *fore, *back);
33 a_u64 *fore = a_que_at(a_u64, ctx, 0);
34 a_u64 *back = a_que_at(a_u64, ctx, ~0);
35 if (fore && back)
37 printf("%" PRIu64 " %" PRIu64 "\n", *fore, *back);
40 for (a_u64 i = 0; i != 5; ++i)
42 (void)(a_que_pull_fore(a_u64, ctx));
44 a_que_edit(ctx, sizeof(a_u32), A_NULL);
45 for (a_u32 i = 5; i--;)
47 a_u32 *obj = a_que_push_fore(a_u32, ctx);
48 if (obj) { *obj = i; }
50 for (a_u32 i = 5; i != 10; ++i)
52 a_u32 *obj = a_que_push_back(a_u32, ctx);
53 if (obj) { *obj = i; }
56 a_u32 *obj = a_que_insert(a_u32, ctx, 0);
57 if (obj) { *obj = A_U32_MAX; }
60 a_u32 *obj = a_que_insert(a_u32, ctx, A_SIZE_MAX);
61 if (obj) { *obj = A_U32_MAX; }
63 (void)(a_que_remove(a_u32, ctx, 0));
64 (void)(a_que_remove(a_u32, ctx, A_SIZE_MAX));
65 a_que_swap(ctx, 0, A_SIZE_MAX);
67 a_u32 *lhs = a_que_fore_(a_u32, ctx);
68 a_u32 *rhs = a_que_back_(a_u32, ctx);
69 a_que_swap_(ctx, lhs, rhs);
71 a_que_foreach(a_u32, it, ctx)
73 TEST_BUG(a_que_siz(ctx) == sizeof(*it));
74 printf("%" PRIu32 " ", *it);
76 putchar('\n');
77 a_que_foreach_reverse(a_u32, it, ctx)
79 TEST_BUG(a_que_siz(ctx) == sizeof(*it));
80 printf("%" PRIu32 " ", *it);
82 putchar('\n');
83 for (a_u32 i = 0; i != 5; ++i)
85 (void)(a_que_pull_back(a_u32, ctx));
87 for (a_u32 i = 0; i != 5; ++i)
89 (void)(a_que_pull_fore(a_u32, ctx));
91 a_que_die(ctx, dtor);
92 putchar('\n');
95 #include <time.h>
97 static int cmp(void const *lhs, void const *rhs)
99 return *a_int_(const *, lhs) - *a_int_(const *, rhs);
102 static int cmpr(void const *lhs, void const *rhs)
104 return *a_int_(const *, rhs) - *a_int_(const *, lhs);
107 static void test_sort(void)
109 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
110 a_que *ctx = a_que_new(sizeof(int));
112 srand(t);
113 a_que_drop(ctx, A_NULL);
114 for (int i = 0; i != 10; ++i)
116 int *obj = a_que_push_fore(int, ctx);
117 if (obj)
119 *obj = rand() % 10; // NOLINT
120 printf("%i ", *obj);
121 a_que_sort_fore(ctx, cmp);
124 printf("-> ");
125 a_que_foreach(int, it, ctx)
127 printf("%i ", *it);
130 srand(t);
131 printf("-> ");
132 a_que_drop(ctx, dtor);
133 putchar('\n');
134 for (int i = 0; i != 10; ++i)
136 int *obj = a_que_push_back(int, ctx);
137 if (obj)
139 *obj = rand() % 10; // NOLINT
140 printf("%i ", *obj);
141 a_que_sort_back(ctx, cmpr);
144 printf("-> ");
145 a_que_foreach(int, it, ctx)
147 printf("%i ", *it);
150 printf("-> ");
151 a_que_die(ctx, dtor);
152 putchar('\n');
155 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
157 (void)(argc);
158 (void)(argv);
159 printf("%s\n", A_FUNC);
160 test();
161 test_sort();
162 return 0;