release 0.1.15
[liba.git] / test / buf.h
blob949bd1a9c97c8b166ac4d41902d935a583cc69ee
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];
16 a_buf_ctor(&ctx, buf, sizeof(a_u32), 10);
18 for (a_u32 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, &ctx)
26 printf("%" PRIu32 " ", *it);
28 putchar('\n');
30 for (a_u32 i = 0; i != 20; ++i)
32 a_u32 *obj = A_BUF_PULL_BACK(a_u32, &ctx);
33 if (obj) { printf("%" PRIu32 " ", *obj); }
35 putchar('\n');
37 a_buf_dtor(&ctx, dtor);
40 static void fore(void)
42 a_buf ctx;
43 a_u32 buf[10];
44 a_buf_ctor(&ctx, buf, sizeof(a_u32), 10);
46 for (a_u32 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, &ctx)
54 printf("%" PRIu32 " ", *it);
56 putchar('\n');
58 for (a_u32 i = 0; i != 20; ++i)
60 a_u32 *obj = A_BUF_PULL_FORE(a_u32, &ctx);
61 if (obj) { printf("%" PRIu32 " ", *obj); }
63 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 x;
93 a_buf ctx;
94 int buf[10];
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 (int 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, &ctx)
114 if (x >= 0) { TEST_BUG(x <= *it); }
115 printf("%i ", *it);
116 x = *it;
118 putchar('\n');
120 x = -1;
121 srand(t);
122 a_buf_drop(&ctx, A_NULL);
123 for (int 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, &ctx)
136 if (x >= 0) { TEST_BUG(x <= *it); }
137 printf("%i ", *it);
138 x = *it;
140 putchar('\n');
142 x = -1;
143 srand(t);
144 a_buf_drop(&ctx, A_NULL);
145 for (int 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, &ctx)
158 if (x >= 0) { TEST_BUG(x <= *it); }
159 printf("%i ", *it);
160 x = *it;
162 putchar('\n');
164 x = -1;
165 srand(t);
166 a_buf_foreach(int, *, it, &ctx)
168 *it = rand10();
169 printf("%i ", *it);
171 printf("-> ");
172 a_buf_sort(&ctx, small);
173 a_buf_foreach(int, *, it, &ctx)
175 if (x >= 0) { TEST_BUG(x <= *it); }
176 printf("%i ", *it);
177 x = *it;
179 putchar('\n');
181 x = -1;
182 srand(t);
183 a_buf_foreach(int, *, it, &ctx)
185 *it = rand10();
186 printf("%i ", *it);
188 printf("-> ");
189 a_buf_sort(&ctx, large);
190 a_buf_foreach(int, *, it, &ctx)
192 if (x >= 0) { TEST_BUG(x >= *it); }
193 printf("%i ", *it);
194 x = *it;
196 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 (int 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 (void)argc;
226 (void)argv;
227 printf("%s\n", A_FUNC);
228 back();
229 fore();
230 test_sort();
231 return 0;