remove warning C6031
[liba.git] / test / buf.h
blobf1a218712a34ffe421bb51652651493b8814c243
1 #define MAIN(x) buf##x
2 #include "test.h"
3 #include "a/buf.h"
4 #include <stdlib.h>
6 static void dtor(void *ptr)
8 a_u32 *obj = a_u32_(*, ptr);
9 printf("%" PRIu32 " ", *obj);
12 static void back(void)
14 a_buf ctx;
15 a_u32 buf[10], i, *it, *at;
16 a_buf_ctor(&ctx, buf, sizeof(a_u32), 10);
18 for (i = 0; i != 20; ++i)
20 a_u32 *obj = A_BUF_PUSH_BACK(a_u32, &ctx);
21 if (obj) { *obj = i; }
24 A_BUF_FOREACH(a_u32 *, it, at, &ctx)
26 printf("%" PRIu32 " ", *it);
28 (void)putchar('\n');
30 for (i = 0; i != 20; ++i)
32 a_u32 *obj = A_BUF_PULL_BACK(a_u32, &ctx);
33 if (obj) { printf("%" PRIu32 " ", *obj); }
35 (void)putchar('\n');
37 a_buf_dtor(&ctx, dtor);
40 static void fore(void)
42 a_buf ctx;
43 a_u32 buf[10], i, *it, *at;
44 a_buf_ctor(&ctx, buf, sizeof(a_u32), 10);
46 for (i = 0; i != 20; ++i)
48 a_u32 *obj = A_BUF_PUSH_FORE(a_u32, &ctx);
49 if (obj) { *obj = i; }
52 A_BUF_FOREACH_REVERSE(a_u32 *, it, at, &ctx)
54 printf("%" PRIu32 " ", *it);
56 (void)putchar('\n');
58 for (i = 0; i != 20; ++i)
60 a_u32 *obj = A_BUF_PULL_FORE(a_u32, &ctx);
61 if (obj) { printf("%" PRIu32 " ", *obj); }
63 (void)putchar('\n');
65 a_buf_dtor(&ctx, dtor);
68 #include "a/str.h"
69 #include <time.h>
71 static int small(void const *lhs, void const *rhs)
73 int a = *a_int_(const *, lhs);
74 int b = *a_int_(const *, rhs);
75 return (a > b) - (a < b);
78 static int large(void const *lhs, void const *rhs)
80 int a = *a_int_(const *, lhs);
81 int b = *a_int_(const *, rhs);
82 return (a < b) - (a > b);
85 static int rand10(void)
87 return a_cast_s(int, rand() / a_cast_s(double, RAND_MAX) * 10); /* NOLINT */
90 static void test_sort(void)
92 int i, x;
93 a_buf ctx;
94 int buf[10], *it, *at;
95 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
96 a_buf_ctor(&ctx, buf, sizeof(int), 10);
98 x = -1;
99 srand(t);
100 a_buf_drop(&ctx, A_NULL);
101 for (i = 0; i != 10; ++i)
103 int *obj = A_BUF_PUSH_FORE(int, &ctx);
104 if (obj)
106 *obj = rand10();
107 printf("%i ", *obj);
108 a_buf_sort_fore(&ctx, small);
111 printf("-> ");
112 A_BUF_FOREACH(int *, it, at, &ctx)
114 if (x >= 0) { TEST_BUG(x <= *it); }
115 printf("%i ", *it);
116 x = *it;
118 (void)putchar('\n');
120 x = -1;
121 srand(t);
122 a_buf_drop(&ctx, A_NULL);
123 for (i = 0; i != 10; ++i)
125 int *obj = A_BUF_PUSH_BACK(int, &ctx);
126 if (obj)
128 *obj = rand10();
129 printf("%i ", *obj);
130 a_buf_sort_back(&ctx, small);
133 printf("-> ");
134 A_BUF_FOREACH(int *, it, at, &ctx)
136 if (x >= 0) { TEST_BUG(x <= *it); }
137 printf("%i ", *it);
138 x = *it;
140 (void)putchar('\n');
142 x = -1;
143 srand(t);
144 a_buf_drop(&ctx, A_NULL);
145 for (i = 0; i != 10; ++i)
147 int key = rand10();
148 int *obj = A_BUF_PUSH_SORT(int, &ctx, &key, small);
149 if (obj)
151 printf("%i ", key);
152 *obj = key;
155 printf("-> ");
156 A_BUF_FOREACH(int *, it, at, &ctx)
158 if (x >= 0) { TEST_BUG(x <= *it); }
159 printf("%i ", *it);
160 x = *it;
162 (void)putchar('\n');
164 x = -1;
165 srand(t);
166 A_BUF_FOREACH(int *, it, at, &ctx)
168 *it = rand10();
169 printf("%i ", *it);
171 printf("-> ");
172 a_buf_sort(&ctx, small);
173 A_BUF_FOREACH(int *, it, at, &ctx)
175 if (x >= 0) { TEST_BUG(x <= *it); }
176 printf("%i ", *it);
177 x = *it;
179 (void)putchar('\n');
181 x = -1;
182 srand(t);
183 A_BUF_FOREACH(int *, it, at, &ctx)
185 *it = rand10();
186 printf("%i ", *it);
188 printf("-> ");
189 a_buf_sort(&ctx, large);
190 A_BUF_FOREACH(int *, it, at, &ctx)
192 if (x >= 0) { TEST_BUG(x >= *it); }
193 printf("%i ", *it);
194 x = *it;
196 (void)putchar('\n');
199 a_str *ok = a_str_new();
200 a_str *no = a_str_new();
201 a_str_puts(ok, "finding ");
202 a_str_puts(no, "missing ");
203 for (i = 0; i != 10; ++i)
205 int *obj = A_BUF_SEARCH(int, &ctx, &i, large);
206 if (obj)
208 a_str_putf(ok, "%i ", *obj);
210 else
212 a_str_putf(no, "%i ", i);
215 printf("%s\n%s\n", a_str_ptr(ok), a_str_ptr(no));
216 a_str_die(ok);
217 a_str_die(no);
220 a_buf_dtor(&ctx, A_NULL);
223 int main(int argc, char *argv[]) /* NOLINT(misc-definitions-in-headers) */
225 puts(A_FUNC);
226 back();
227 fore();
228 test_sort();
229 (void)argc;
230 (void)argv;
231 return 0;