1 #define MAIN_(x) A_CAST_2(x, _vec)
5 static void dtor(void *ptr
)
7 a_u32
*obj
= a_u32_(*, ptr
);
8 printf("%" PRIu32
" ", *obj
);
11 static int u32dup(void *dst
, void const *src
)
13 *a_u32_(*, dst
) = *a_u32_(const *, src
);
14 printf("%" PRIu32
" ", *a_u32_(const *, src
));
18 static void test(void)
20 a_vec
*ctx
= a_vec_new(sizeof(a_u64
));
21 a_vec_foreach(a_u64
, it
, ctx
);
22 a_vec_foreach_reverse(a_u64
, it
, ctx
);
23 for (a_u64 i
= 0; i
!= 10; ++i
)
25 a_u64
*obj
= a_vec_push(a_u64
, ctx
);
26 if (obj
) { *obj
= i
; }
28 a_vec_edit(ctx
, sizeof(a_u32
), A_NULL
);
29 for (a_u32 i
= 0; i
!= 20; ++i
)
31 a_u32
*obj
= a_vec_push(a_u32
, ctx
);
32 if (obj
) { *obj
= i
; }
34 for (a_u32 i
= 0; i
!= 10; ++i
)
36 (void)(a_vec_pull(a_u32
, ctx
));
40 a_u8
*end
= a_vec_end(a_u8
, ctx
);
41 a_u8
*top
= a_vec_top(a_u8
, ctx
);
42 TEST_BUG(a_vec_siz(ctx
) == a_size_c(end
- top
));
45 a_vec_swap(ctx
, 0, 0);
46 a_vec_swap(ctx
, 0, ~0U);
47 a_vec_swap(ctx
, ~0U, 0);
48 a_vec_swap(ctx
, 4, 6);
49 a_vec_swap(ctx
, 6, 4);
53 a_u32
*it
= a_vec_at(a_u32
, ctx
, i
);
54 TEST_BUG(a_vec_siz(ctx
) == sizeof(*it
));
55 if (it
) { printf("%" PRIu32
" ", *it
); }
58 a_vec_forenum_reverse(i
, ctx
)
60 a_u32
*it
= a_vec_at(a_u32
, ctx
, i
);
61 TEST_BUG(a_vec_siz(ctx
) == sizeof(*it
));
62 if (it
) { printf("%" PRIu32
" ", *it
); }
66 a_vec_foreach(a_u32
, it
, ctx
)
68 TEST_BUG(a_vec_siz(ctx
) == sizeof(*it
));
69 TEST_BUG(sizeof(a_u32
) == sizeof(*it
));
70 printf("%" PRIu32
" ", *it
);
73 a_vec_foreach_reverse(a_u32
, it
, ctx
)
75 TEST_BUG(a_vec_siz(ctx
) == sizeof(*it
));
76 TEST_BUG(sizeof(a_u32
) == sizeof(*it
));
77 printf("%" PRIu32
" ", *it
);
83 a_vec_copy(&obj
, ctx
, u32dup
);
85 a_vec_dtor(ctx
, A_NULL
);
86 a_vec_move(ctx
, &obj
);
93 ctx
= a_vec_new(sizeof(a_u32
));
94 for (a_u32 i
= 5; i
!= 10; ++i
)
96 a_u32
*obj
= a_vec_insert(a_u32
, ctx
, i
);
97 if (obj
) { *obj
= i
; }
99 for (a_u32 i
= 0; i
!= 5; ++i
)
101 a_u32
*obj
= a_vec_insert(a_u32
, ctx
, i
);
102 if (obj
) { *obj
= i
; }
104 a_vec_foreach(a_u32
, it
, ctx
)
106 printf("%" PRIu32
" ", *it
);
109 for (a_u32 i
= 0; i
!= 5; ++i
)
111 a_u32
*obj
= a_vec_remove(a_u32
, ctx
, i
);
112 if (obj
) { printf("%" PRIu32
" ", *obj
); }
114 for (a_u32 i
= 0; i
!= 5; ++i
)
116 a_u32
*obj
= a_vec_remove(a_u32
, ctx
, 0);
117 if (obj
) { printf("%" PRIu32
" ", *obj
); }
120 a_vec_die(ctx
, A_NULL
);
123 ctx
= a_vec_new(sizeof(a_u32
));
124 for (a_u32 i
= 5; i
!= 10; ++i
)
126 a_u32
*obj
= a_vec_push_back(a_u32
, ctx
);
127 if (obj
) { *obj
= i
; }
129 for (a_u32 i
= 5; i
!= 10; ++i
)
131 a_u32
*obj
= a_vec_pull_back(a_u32
, ctx
);
132 if (obj
) { printf("%" PRIu32
" ", *obj
); }
134 for (a_u32 i
= 0; i
!= 5; ++i
)
136 a_u32
*obj
= a_vec_push_fore(a_u32
, ctx
);
137 if (obj
) { *obj
= i
; }
139 for (a_u32 i
= 0; i
!= 5; ++i
)
141 a_u32
*obj
= a_vec_pull_fore(a_u32
, ctx
);
142 if (obj
) { printf("%" PRIu32
" ", *obj
); }
145 a_vec_die(ctx
, A_NULL
);
152 static int cmp(void const *lhs
, void const *rhs
)
154 return *a_int_(const *, lhs
) - *a_int_(const *, rhs
);
157 static int cmpr(void const *lhs
, void const *rhs
)
159 return *a_int_(const *, rhs
) - *a_int_(const *, lhs
);
162 static void test_sort(void)
164 unsigned int t
= a_cast_s(unsigned int, time(A_NULL
));
165 a_vec
*ctx
= a_vec_new(sizeof(int));
167 a_vec_make(ctx
, 10, A_NULL
);
170 a_vec_foreach(int, it
, ctx
)
172 *it
= rand() % 10; // NOLINT
176 a_vec_sort(ctx
, cmpr
);
177 a_vec_foreach(int, it
, ctx
)
184 a_vec_foreach(int, it
, ctx
)
186 *it
= rand() % 10; // NOLINT
190 a_vec_sort(ctx
, cmp
);
191 a_vec_foreach(int, it
, ctx
)
198 a_vec_drop(ctx
, A_NULL
);
199 for (int i
= 0; i
!= 10; ++i
)
201 int *obj
= a_vec_push_fore(int, ctx
);
204 *obj
= rand() % 10; // NOLINT
206 a_vec_sort_fore(ctx
, cmp
);
210 a_vec_foreach(int, it
, ctx
)
217 a_vec_drop(ctx
, A_NULL
);
218 for (int i
= 0; i
!= 10; ++i
)
220 int *obj
= a_vec_push_back(int, ctx
);
223 *obj
= rand() % 10; // NOLINT
225 a_vec_sort_back(ctx
, cmp
);
229 a_vec_foreach(int, it
, ctx
)
236 a_str
*ok
= a_str_new();
237 a_str
*no
= a_str_new();
238 a_str_puts(ok
, "finding ");
239 a_str_puts(no
, "missing ");
240 for (int i
= 0; i
!= 10; ++i
)
242 int *obj
= a_vec_search(int, ctx
, &i
, cmp
);
245 a_str_putf(ok
, "%i ", *obj
);
249 a_str_putf(no
, "%i ", i
);
252 printf("%s\n%s\n", a_str_ptr(ok
), a_str_ptr(no
));
257 a_vec_die(ctx
, A_NULL
);
260 int MAIN(int argc
, char *argv
[]) // NOLINT(misc-definitions-in-headers)
264 printf("%s\n", A_FUNC
);