X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fintmap%2Ftest%2Frun-order-smallsize.c;fp=ccan%2Fintmap%2Ftest%2Frun-order-smallsize.c;h=e9164413bb8d9561e9a1f9c622ca314009246160;hb=d9e93014a999102aa1cc9979e041cd58e6aca724;hp=0000000000000000000000000000000000000000;hpb=3f642347378afc9e1db1768d88c9f5b2baffe9ba;p=ccan diff --git a/ccan/intmap/test/run-order-smallsize.c b/ccan/intmap/test/run-order-smallsize.c new file mode 100644 index 00000000..e9164413 --- /dev/null +++ b/ccan/intmap/test/run-order-smallsize.c @@ -0,0 +1,92 @@ +#define intmap_index_t uint8_t + +#include +#include +#include + +#define NUM 100 + +typedef UINTMAP(uint8_t *) umap; +typedef SINTMAP(int8_t *) smap; + +static bool check_umap(const umap *map) +{ + /* This is a larger type than unsigned, and allows negative */ + int64_t i, prev; + + /* Must be in order, must contain value. */ + prev = -1; + for (i = uintmap_first(map); + i != UINTMAP_NONE || errno == 0; + i = uintmap_after(map, i)) { + if (i <= prev) + return false; + if (*(uint8_t *)uintmap_get(map, i) != i) + return false; + prev = i; + } + return true; +} + +static bool check_smap(const smap *map) +{ + /* This is a larger type than int, and allows negative */ + int64_t i, prev; + + /* Must be in order, must contain value. */ + prev = -0x80000001ULL; + for (i = sintmap_first(map); + i != 127 || errno == 0; + i = sintmap_after(map, i)) { + if (i <= prev) + return false; + if (*(int8_t *)sintmap_get(map, i) != i) + return false; + prev = i; + } + return true; +} + +int main(void) +{ + umap umap; + smap smap; + int i; + uint8_t urandoms[NUM]; + int8_t srandoms[NUM]; + + plan_tests(6 * NUM + 2); + uintmap_init(&umap); + sintmap_init(&smap); + + for (i = 0; i < NUM; i++) { + urandoms[i] = random(); + srandoms[i] = random(); + } + for (i = 0; i < NUM; i++) { + /* In case we have duplicates. */ + while (!uintmap_add(&umap, urandoms[i], urandoms+i)) + urandoms[i] = random(); + ok1(check_umap(&umap)); + } + for (i = 0; i < NUM; i++) { + ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i); + ok1(check_umap(&umap)); + } + ok1(uintmap_empty(&umap)); + + for (i = 0; i < NUM; i++) { + /* In case we have duplicates. */ + while (!sintmap_add(&smap, srandoms[i], srandoms+i)) + srandoms[i] = random(); + ok1(check_smap(&smap)); + } + for (i = 0; i < NUM; i++) { + ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i); + ok1(check_smap(&smap)); + } + ok1(sintmap_empty(&smap)); + + /* This exits depending on whether all tests passed */ + return exit_status(); +}