diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-10-03 00:36:26 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-10-03 00:36:26 +0200 |
commit | d874d01e8e9a30f0073a6e559cbae07244dec7bf (patch) | |
tree | dbde18d7ada337b74fe6cfb8ccc35b52008e6aa0 /test | |
parent | 1e721a413f1d4fc7f7f4e1e691a0a37168f3b302 (diff) |
Huge library overhaul
Diffstat (limited to 'test')
-rw-r--r-- | test/_brk-test.h | 41 | ||||
-rw-r--r-- | test/_case-test.h | 21 | ||||
-rw-r--r-- | test/_norm-test.h | 31 | ||||
-rw-r--r-- | test/array-test.c | 82 | ||||
-rw-r--r-- | test/wbrk-human-test.c | 22 |
5 files changed, 137 insertions, 60 deletions
diff --git a/test/_brk-test.h b/test/_brk-test.h index 00b84ff..40098c3 100644 --- a/test/_brk-test.h +++ b/test/_brk-test.h @@ -7,7 +7,7 @@ #include <stdlib.h> #include <alloc.h> -#include <dynarr.h> +#include <array.h> #include <errors.h> #include <macros.h> #include <mbstring.h> @@ -57,14 +57,10 @@ test(u8view_t sv, int id) bool rv = true; size_t total = 0; - arena a = mkarena(0); - struct arena_ctx ctx = {.a = &a}; + arena_ctx_t ctx = {}; + allocator_t mem = init_arena_allocator(&ctx, nullptr); - typedef dynarr(char8_t) item; - dynarr(item) items = { - .alloc = alloc_arena, - .ctx = &ctx, - }; + char8_t **items = array_new(mem, typeof(*items), 64); rune op; u8view_t sv_cpy = sv; @@ -77,23 +73,26 @@ test(u8view_t sv, int id) total += w; if (op == U'÷') - DAPUSH(&items, ((item){.alloc = alloc_arena, .ctx = &ctx})); - DAEXTEND(&items.buf[items.len - 1], buf, w); + array_push(&items, array_new(mem, char8_t, 64)); + array_extend(items[array_len(items) - 1], buf, w); } size_t off = 0; - char8_t *p = bufalloc(nullptr, 1, total); - da_foreach (items, g) { - memcpy(p + off, g->buf, g->len); - off += g->len; + char8_t *p = malloc(total); + if (p == nullptr) + err("malloc:"); + for (ptrdiff_t i = 0; i < array_len(items); i++) { + char8_t *g = items[i]; + memcpy(p + off, g, array_len(g)); + off += array_len(g); } u8view_t buf = {p, total}; /* Assert the item count is correct */ - size_t items_got = CNTFUNC(buf); - if (items_got != items.len) { - warn("case %d: expected %zu %s(s) but got %zu: ‘%s’", id, items.len, + ptrdiff_t items_got = CNTFUNC(buf); + if (items_got != array_len(items)) { + warn("case %d: expected %zu %s(s) but got %zu: ‘%s’", id, array_len(items), STR(BRKTYPE_LONG), items_got, sv.p); rv = false; goto out; @@ -102,17 +101,17 @@ test(u8view_t sv, int id) /* Assert the individual items are correct */ u8view_t it1, buf_cpy = buf; for (size_t i = 0; ITERFUNC(&it1, &buf_cpy); i++) { - item it2 = items.buf[i]; - if (!ucseq(it1, ((u8view_t){it2.buf, it2.len}))) { + char8_t *it2 = items[i]; + if (!ucseq(it1, ((u8view_t){it2, array_len(it2)}))) { warn("case %d: expected %s ‘%.*s’ but got ‘%.*s’", id, - STR(BRKTYPE_LONG), (int)it2.len, it2.buf, SV_PRI_ARGS(it1)); + STR(BRKTYPE_LONG), (int)array_len(it2), it2, SV_PRI_ARGS(it1)); rv = false; goto out; } } out: - arena_free(&a); + deleteall(mem); free(p); return rv; } diff --git a/test/_case-test.h b/test/_case-test.h index 299f079..3230f18 100644 --- a/test/_case-test.h +++ b/test/_case-test.h @@ -58,24 +58,23 @@ test(const char8_t *line, int id) ucscut(&after, &sv, U";", 1); ucscut(&flags, &sv, U";", 1); - enum caseflags cf = ucseq(flags, U8("ẞ")) ? CF_ẞ - : ucseq(flags, U8("AZ")) ? CF_LANG_AZ - : ucseq(flags, U8("LT")) ? CF_LANG_LT - : ucseq(flags, U8("NL")) ? CF_LANG_NL - : 0; + caseflags_t cf = ucseq(flags, U8("ẞ")) ? CF_ẞ + : ucseq(flags, U8("AZ")) ? CF_LANG_AZ + : ucseq(flags, U8("LT")) ? CF_LANG_LT + : ucseq(flags, U8("NL")) ? CF_LANG_NL + : /* no flags */ 0; - arena a = mkarena(0); - mapped.p = FUNC(&mapped.len, before, cf, alloc_arena, &((struct arena_ctx){ - .a = &a, - })); + arena_ctx_t ctx = {}; + allocator_t mem = init_arena_allocator(&ctx, nullptr); + mapped.p = FUNC(&mapped.len, before, cf, mem); if (!ucseq(mapped, after)) { warn("case %d: expected ‘%.*s’ but got ‘%.*s’", id, SV_PRI_ARGS(after), SV_PRI_ARGS(mapped)); - arena_free(&a); + deleteall(mem); return false; } - arena_free(&a); + deleteall(mem); return true; } diff --git a/test/_norm-test.h b/test/_norm-test.h index 744bc93..e4da609 100644 --- a/test/_norm-test.h +++ b/test/_norm-test.h @@ -7,7 +7,7 @@ #include <stdlib.h> #include <alloc.h> -#include <dynarr.h> +#include <array.h> #include <errors.h> #include <macros.h> #include <mbstring.h> @@ -53,20 +53,14 @@ bool test(u8view_t sv, int id) { bool rv = true; - arena a = mkarena(0); - struct arena_ctx ctx = {.a = &a}; - - dynarr(u8view_t) columns = { - .alloc = alloc_arena, - .ctx = &ctx, - }; + arena_ctx_t ctx = {}; + allocator_t mem = init_arena_allocator(&ctx, nullptr); u8view_t column; + u8view_t *columns = array_new(mem, typeof(*columns), 64); + while (ucscut(&column, &sv, U";", 1) != MBEND) { - dynarr(char8_t) s = { - .alloc = alloc_arena, - .ctx = &ctx, - }; + char8_t *s = array_new(mem, typeof(*s), 64); rune _; u8view_t cp; @@ -76,10 +70,10 @@ test(u8view_t sv, int id) sscanf(cp.p, "%" SCNxRUNE, &ch); char8_t buf[U8_LEN_MAX]; int w = rtoucs(buf, sizeof(buf), ch); - DAEXTEND(&s, buf, w); + array_extend(s, buf, w); } while (_ != MBEND); - DAPUSH(&columns, ((u8view_t){s.buf, s.len})); + array_push(&columns, ((u8view_t){s, array_len(s)})); } for (size_t i = 0; i < 5; i++) { @@ -97,17 +91,16 @@ test(u8view_t sv, int id) err("invalid NORMTYPE ‘%s’", nt); u8view_t normd = {}; - normd.p = - ucsnorm(&normd.len, columns.buf[i], alloc_arena, &ctx, NORMTYPE); - if (!ucseq(columns.buf[base], normd)) { + normd.p = ucsnorm(&normd.len, columns[i], mem, NORMTYPE); + if (!ucseq(columns[base], normd)) { warn("case %d: expected c%zu to be ‘%.*s’ but got ‘%.*s’", id, - i + 1, SV_PRI_ARGS(columns.buf[base]), SV_PRI_ARGS(normd)); + i + 1, SV_PRI_ARGS(columns[base]), SV_PRI_ARGS(normd)); rv = false; goto out; } } out: - arena_free(&a); + deleteall(mem); return rv; } diff --git a/test/array-test.c b/test/array-test.c new file mode 100644 index 0000000..3eb955a --- /dev/null +++ b/test/array-test.c @@ -0,0 +1,82 @@ +#include <stdbit.h> +#include <stdlib.h> + +#include <array.h> +#include <alloc.h> +#include <errors.h> + +typedef struct { + allocator_t inner; + ptrdiff_t num_allocs, last_alloc_size; + bool didfree; +} dbg_ctx_t; + +static void *dbg_alloc(allocator_t, alloc_mode_t, void *, ptrdiff_t, ptrdiff_t, + ptrdiff_t, ptrdiff_t); + +int +main(int, char **argv) +{ + mlib_setprogname(argv[0]); + + dbg_ctx_t ctx = {.inner = init_heap_allocator(nullptr)}; + allocator_t mem = { + .alloc = dbg_alloc, + .ctx = &ctx, + }; + + int *xs = array_new(mem, int, 4); + if (ctx.num_allocs != 1) + err("ctx.num_allocs == %td\n", ctx.num_allocs); + /* Integers don’t need padding. It simplifies the test */ + if (ctx.last_alloc_size != sizeof(_mlib_arr_hdr_t) + sizeof(int) * 4) + err("ctx.last_alloc_size == %td\n", ctx.last_alloc_size); + + xs[0] = 1; xs[1] = 2; + xs[2] = 3; xs[3] = 4; + array_hdr(xs)->len = 4; + + for (int i = 4; i < 69; i++) { + array_push(&xs, i + 1); + if (stdc_count_ones((unsigned)i) == 1) { + /* Integers don’t need padding. It simplifies the test */ + if ((size_t)ctx.last_alloc_size + != sizeof(_mlib_arr_hdr_t) + sizeof(int) * i * 2) + { + err("ctx.last_alloc_size == %td\n", ctx.last_alloc_size); + } + } + } + for (int i = 0; i < 69; i++) { + if (xs[i] != i + 1) + err("xs[%d] == %d", i, xs[i]); + } + + if (array_len(xs) != 69) + err("array_len(xs) == %td", array_len(xs)); + + array_free(xs); + if (!ctx.didfree) + err("ctx.did_free == false"); + + return EXIT_SUCCESS; +} + +void * +dbg_alloc(allocator_t mem, alloc_mode_t mode, void *ptr, ptrdiff_t oldnmemb, + ptrdiff_t newnmemb, ptrdiff_t elemsz, ptrdiff_t align) +{ + dbg_ctx_t *p = mem.ctx; + switch (mode) { + case ALLOC_NEW: + case ALLOC_RESIZE: + p->didfree = false; + p->num_allocs++; + p->last_alloc_size = newnmemb * elemsz; + break; + case ALLOC_FREE: + case ALLOC_FREEALL: + p->didfree = true; + } + return p->inner.alloc(p->inner, mode, ptr, oldnmemb, newnmemb, elemsz, align); +} diff --git a/test/wbrk-human-test.c b/test/wbrk-human-test.c index 955cf4f..45dcaef 100644 --- a/test/wbrk-human-test.c +++ b/test/wbrk-human-test.c @@ -2,7 +2,8 @@ #include <stdio.h> #include <stdlib.h> -#include <dynarr.h> +#include <alloc.h> +#include <array.h> #include <errors.h> #include <macros.h> #include <mbstring.h> @@ -49,30 +50,33 @@ test(u8view_t sv, int id) u8view_t src; ucscut(&src, &sv, U";", 1); + allocator_t mem = init_heap_allocator(nullptr); + u8view_t w; - dynarr(u8view_t) ws = {.alloc = alloc_heap}; + u8view_t *ws = array_new(mem, typeof(*ws), 64); while (ucscut(&w, &sv, U"|", 1) != MBEND) - DAPUSH(&ws, w); + array_push(&ws, w); if (w.len > 0) - DAPUSH(&ws, w); + array_push(&ws, w); /* Assert the word count is correct */ size_t n; - if ((n = ucswcnt_human(src)) != ws.len) { - warn("case %d: expected %zu words but got %zu", id, ws.len, n); + /* TODO: Fix return type and remove cast */ + if ((ptrdiff_t)(n = ucswcnt_human(src)) != array_len(ws)) { + warn("case %d: expected %tu words but got %zu", id, array_len(ws), n); return false; } /* Assert the individual words are correct */ for (size_t i = 0; ucswnext_human(&w, &src) != 0; i++) { - if (!ucseq(w, ws.buf[i])) { + if (!ucseq(w, ws[i])) { warn("case %d: expected word %zu to be ‘%.*s’ but got ‘%.*s’", id, - i, SV_PRI_ARGS(ws.buf[i]), SV_PRI_ARGS(w)); + i, SV_PRI_ARGS(ws[i]), SV_PRI_ARGS(w)); return false; } } - free(ws.buf); + array_free(ws); return true; } |