change gcc to clang in docs.yaml
[liba.git] / test / buf.h
blob75482a2b08555903b243d661c369b8d90d2057f8
1 #define MAIN_(x) A_CAST_2(x, _buf)
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 cmp(void const *lhs, void const *rhs)
73 return *a_int_(const *, lhs) - *a_int_(const *, rhs);
76 static int cmpr(void const *lhs, void const *rhs)
78 return *a_int_(const *, rhs) - *a_int_(const *, lhs);
81 static int rand10(void)
83 return a_cast_s(int, rand() / a_cast_s(double, RAND_MAX) * 10); // NOLINT
86 static void test_sort(void)
88 a_buf ctx;
89 int buf[10];
90 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
91 a_buf_ctor(&ctx, buf, sizeof(int), 10);
93 srand(t);
94 a_buf_drop(&ctx, A_NULL);
95 for (int i = 0; i != 10; ++i)
97 int *obj = a_buf_push_fore(int, &ctx);
98 if (obj)
100 *obj = rand10();
101 printf("%i ", *obj);
102 a_buf_sort_fore(&ctx, cmp);
105 printf("-> ");
106 a_buf_foreach(int, it, &ctx)
108 printf("%i ", *it);
110 putchar('\n');
112 srand(t);
113 a_buf_drop(&ctx, A_NULL);
114 for (int i = 0; i != 10; ++i)
116 int *obj = a_buf_push_back(int, &ctx);
117 if (obj)
119 *obj = rand10();
120 printf("%i ", *obj);
121 a_buf_sort_back(&ctx, cmp);
124 printf("-> ");
125 a_buf_foreach(int, it, &ctx)
127 printf("%i ", *it);
129 putchar('\n');
131 srand(t);
132 a_buf_foreach(int, it, &ctx)
134 *it = rand10();
135 printf("%i ", *it);
137 printf("-> ");
138 a_buf_sort(&ctx, cmp);
139 a_buf_foreach(int, it, &ctx)
141 printf("%i ", *it);
143 putchar('\n');
145 srand(t);
146 a_buf_foreach(int, it, &ctx)
148 *it = rand10();
149 printf("%i ", *it);
151 printf("-> ");
152 a_buf_sort(&ctx, cmpr);
153 a_buf_foreach(int, it, &ctx)
155 printf("%i ", *it);
157 putchar('\n');
160 a_str *ok = a_str_new();
161 a_str *no = a_str_new();
162 a_str_puts(ok, "finding ");
163 a_str_puts(no, "missing ");
164 for (int i = 0; i != 10; ++i)
166 int *obj = a_buf_search(int, &ctx, &i, cmp);
167 if (obj)
169 a_str_putf(ok, "%i ", *obj);
171 else
173 a_str_putf(no, "%i ", i);
176 printf("%s\n%s\n", a_str_ptr(ok), a_str_ptr(no));
177 a_str_die(ok);
178 a_str_die(no);
181 a_buf_dtor(&ctx, A_NULL);
184 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
186 (void)(argc);
187 (void)(argv);
188 printf("%s\n", A_FUNC);
189 back();
190 fore();
191 test_sort();
192 return 0;