From 2bed88914afe24dc3a245f398b321e8f236d23d7 Mon Sep 17 00:00:00 2001 From: tylo Date: Fri, 17 Sep 2021 11:24:39 +0200 Subject: Moved tests and v1 files --- tests/test_new_arr.c | 47 +++++++++++++++++++++++++++++++++++ tests/test_new_deq.c | 57 ++++++++++++++++++++++++++++++++++++++++++ tests/test_new_list.c | 57 ++++++++++++++++++++++++++++++++++++++++++ tests/test_new_map.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_new_pque.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test_new_queue.c | 43 ++++++++++++++++++++++++++++++++ tests/test_new_smap.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_new_sptr.c | 45 +++++++++++++++++++++++++++++++++ tests/test_new_vec.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 496 insertions(+) create mode 100644 tests/test_new_arr.c create mode 100644 tests/test_new_deq.c create mode 100644 tests/test_new_list.c create mode 100644 tests/test_new_map.c create mode 100644 tests/test_new_pque.c create mode 100644 tests/test_new_queue.c create mode 100644 tests/test_new_smap.c create mode 100644 tests/test_new_sptr.c create mode 100644 tests/test_new_vec.c (limited to 'tests') diff --git a/tests/test_new_arr.c b/tests/test_new_arr.c new file mode 100644 index 00000000..7513903f --- /dev/null +++ b/tests/test_new_arr.c @@ -0,0 +1,47 @@ +#include + +#define i_tag i +#define i_val int +#include + +#define i_val int +#include +#include + +int main() +{ + int w = 7, h = 5, d = 3; + + c_forvar (carr2_i image = carr2_i_init(w, h), carr2_i_del(&image)) + { + int *dat = carr2_i_data(&image); + for (int i = 0; i < carr2_i_size(image); ++i) + dat[i] = i; + + for (int x = 0; x < image.xdim; ++x) + for (int y = 0; y < image.ydim; ++y) + printf(" %d", image.data[x][y]); + puts(""); + + c_foreach (i, carr2_i, image) + printf(" %d", *i.ref); + } + puts("\n"); + + c_forvar (carr3_int image = carr3_int_init(w, h, d), carr3_int_del(&image)) + { + int *dat = carr3_int_data(&image); + for (int i = 0; i < carr3_int_size(image); ++i) + dat[i] = i; + + for (int x = 0; x < image.xdim; ++x) + for (int y = 0; y < image.ydim; ++y) + for (int z = 0; z < image.zdim; ++z) + printf(" %d", image.data[x][y][z]); + puts(""); + + c_foreach (i, carr3_int, image) + printf(" %d", *i.ref); + } + puts(""); +} diff --git a/tests/test_new_deq.c b/tests/test_new_deq.c new file mode 100644 index 00000000..d1a5c2a6 --- /dev/null +++ b/tests/test_new_deq.c @@ -0,0 +1,57 @@ +#include +#include + +forward_cdeq(i32, int); +forward_cdeq(pnt, struct Point); + +struct MyStruct { + cdeq_i32 intvec; + cdeq_pnt pntvec; +} typedef MyStruct; + + +#define F_tag i32 +#define i_val int +#include + +struct Point { int x, y; } typedef Point; +int point_compare(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} +#define F_tag pnt +#define i_val Point +#define i_cmp point_compare +#include + +#define i_val float +#include + +#define i_val_str +#include + + +int main() +{ + cdeq_i32 vec = cdeq_i32_init(); + cdeq_i32_push_back(&vec, 123); + cdeq_i32_del(&vec); + + cdeq_float fvec = cdeq_float_init(); + cdeq_float_push_back(&fvec, 123.3); + cdeq_float_del(&fvec); + + cdeq_pnt pvec = cdeq_pnt_init(); + cdeq_pnt_push_back(&pvec, (Point){42, 14}); + cdeq_pnt_push_back(&pvec, (Point){32, 94}); + cdeq_pnt_push_front(&pvec, (Point){62, 81}); + cdeq_pnt_sort(&pvec); + c_foreach (i, cdeq_pnt, pvec) + printf(" (%d %d)", i.ref->x, i.ref->y); + puts(""); + cdeq_pnt_del(&pvec); + + cdeq_str svec = cdeq_str_init(); + cdeq_str_emplace_back(&svec, "Hello, friend"); + cdeq_str_del(&svec); +} \ No newline at end of file diff --git a/tests/test_new_list.c b/tests/test_new_list.c new file mode 100644 index 00000000..f0983b76 --- /dev/null +++ b/tests/test_new_list.c @@ -0,0 +1,57 @@ +#include "cstr.h" +#include "forward.h" + +forward_clist(i32, int); +forward_clist(pnt, struct Point); + +struct MyStruct { + clist_i32 intlst; + clist_pnt pntlst; +} typedef MyStruct; + + +#define F_tag i32 +#define i_val int +#include "clist.h" + +struct Point { int x, y; } typedef Point; +int point_compare(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} +#define F_tag pnt +#define i_val Point +#define i_cmp point_compare +#include "clist.h" + +#define i_val float +#include "clist.h" + +#define i_val_str +#include "clist.h" + + +int main() +{ + clist_i32 lst = clist_i32_init(); + clist_i32_push_back(&lst, 123); + clist_i32_del(&lst); + + clist_float flst = clist_float_init(); + clist_float_push_back(&flst, 123.3); + clist_float_del(&flst); + + clist_pnt plst = clist_pnt_init(); + clist_pnt_push_back(&plst, (Point){42, 14}); + clist_pnt_push_back(&plst, (Point){32, 94}); + clist_pnt_push_back(&plst, (Point){62, 81}); + clist_pnt_sort(&plst); + c_foreach (i, clist_pnt, plst) + printf(" (%d %d)", i.ref->x, i.ref->y); + puts(""); + clist_pnt_del(&plst); + + clist_str slst = clist_str_init(); + clist_str_emplace_back(&slst, "Hello, friend"); + clist_str_del(&slst); +} \ No newline at end of file diff --git a/tests/test_new_map.c b/tests/test_new_map.c new file mode 100644 index 00000000..2c4ee3bb --- /dev/null +++ b/tests/test_new_map.c @@ -0,0 +1,60 @@ +#include +#include + +forward_cmap(pnt, struct Point, int); + +struct MyStruct { + cmap_pnt pntmap; + cstr name; +} typedef MyStruct; + +// int => int map +#define i_key int +#define i_val int +#include + +// Point => int map +struct Point { int x, y; } typedef Point; +int point_compare(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} +#define F_tag pnt // F=forward declared +#define i_key Point +#define i_val int +#define i_cmp point_compare +#include + +// int => int map +#define i_key_str +#define i_val_str +#include + +// string set +#define i_key_str +#include + + +int main() +{ + cmap_int map = cmap_int_init(); + cmap_int_insert(&map, 123, 321); + cmap_int_del(&map); + + cmap_pnt pmap = cmap_pnt_init(); + cmap_pnt_insert(&pmap, (Point){42, 14}, 1); + cmap_pnt_insert(&pmap, (Point){32, 94}, 2); + cmap_pnt_insert(&pmap, (Point){62, 81}, 3); + c_foreach (i, cmap_pnt, pmap) + printf(" (%d,%d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second); + puts(""); + cmap_pnt_del(&pmap); + + cmap_str smap = cmap_str_init(); + cmap_str_emplace(&smap, "Hello, friend", "this is the mapped value"); + cmap_str_del(&smap); + + cset_str sset = cset_str_init(); + cset_str_emplace(&sset, "Hello, friend"); + cset_str_del(&sset); +} \ No newline at end of file diff --git a/tests/test_new_pque.c b/tests/test_new_pque.c new file mode 100644 index 00000000..b9750c44 --- /dev/null +++ b/tests/test_new_pque.c @@ -0,0 +1,63 @@ +#include + +forward_cpque(pnt, struct Point); + +struct MyStruct { + cpque_pnt priority_queue; + int id; +}; + +#define i_val int +#include "cstack.h" + +#define i_val int +#include "cpque.h" + +struct Point { int x, y; } typedef Point; + +int Point_cmp(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} +#define F_tag pnt // F: was forward declared. +#define i_val Point +#define i_cmp Point_cmp +#include "cpque.h" + +#include + +int main() +{ + cstack_int istk = cstack_int_init(); + cstack_int_push(&istk, 123); + cstack_int_push(&istk, 321); + // print + c_foreach (i, cstack_int, istk) + printf(" %d", *i.ref); + cstack_int_del(&istk); + puts(""); + + cpque_pnt pque = cpque_pnt_init(); + cpque_pnt_push(&pque, (Point){23, 80}); + cpque_pnt_push(&pque, (Point){12, 32}); + cpque_pnt_push(&pque, (Point){54, 74}); + cpque_pnt_push(&pque, (Point){12, 62}); + // print + while (!cpque_pnt_empty(pque)) { + cpque_pnt_value_t *v = cpque_pnt_top(&pque); + printf(" (%d,%d)", v->x, v->y); + cpque_pnt_pop(&pque); + } + // free + cpque_pnt_del(&pque); + puts(""); + + cpque_int ique = cpque_int_init(); + cpque_int_push(&ique, 123); + cpque_int_push(&ique, 321); + // print + for (int i=0; i +#include +#include + +forward_cqueue(pnt, struct Point); + +struct Point { int x, y; } typedef Point; +int point_compare(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} +#define F_tag pnt +#define i_val Point +#define i_cmp point_compare +#include + +#define i_val int +#include +#include + +int main() { + int n = 60000000; + stc64_t rng = stc64_init(time(NULL)); + stc64_uniform_t dist = stc64_uniform_init(0, n); + + c_forauto (cqueue_int, Q) + { + // Push eight million random numbers onto the queue. + for (int i=0; i0; --i) { + int r = stc64_uniform(&rng, &dist); + if (r & 1) + cqueue_int_push(&Q, r); + else + cqueue_int_pop(&Q); + } + printf("after: size %zu, capacity %zu\n", cqueue_int_size(Q), cqueue_int_capacity(Q)); + } +} \ No newline at end of file diff --git a/tests/test_new_smap.c b/tests/test_new_smap.c new file mode 100644 index 00000000..033749ce --- /dev/null +++ b/tests/test_new_smap.c @@ -0,0 +1,67 @@ +#include +#include + +forward_csmap(pnt, struct Point, int); + +struct MyStruct { + csmap_pnt pntmap; + cstr name; +} typedef MyStruct; + +// int => int map +#define i_key int +#define i_val int +#include + +// Point => int map +struct Point { int x, y; } typedef Point; +int point_compare(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} + +#define i_fwd +#define i_tag pnt +#define i_key Point +#define i_val int +#define i_cmp point_compare +#include + +// int => int map +#define i_key_str +#define i_val_str +#include + +// string set +#define i_key_str +#include + + +int main() +{ + c_forauto (csmap_int, map) + csmap_int_insert(&map, 123, 321); + + c_forauto (csmap_pnt, pmap) { + csmap_pnt_insert(&pmap, (Point){42, 14}, 1); + csmap_pnt_insert(&pmap, (Point){32, 94}, 2); + csmap_pnt_insert(&pmap, (Point){62, 81}, 3); + c_foreach (i, csmap_pnt, pmap) + printf(" (%d,%d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second); + puts(""); + } + + c_forauto (csmap_str, smap) { + csmap_str_emplace(&smap, "Hello, friend", "this is the mapped value"); + csmap_str_emplace(&smap, "The brown fox", "jumped"); + csmap_str_emplace(&smap, "This is the time", "for all good things"); + c_foreach (i, csmap_str, smap) + printf(" (%s: %s)\n", i.ref->first.str, i.ref->second.str); + } + + c_forauto (csset_str, sset) { + csset_str_emplace(&sset, "Hello, friend"); + csset_str_emplace(&sset, "Goodbye, foe"); + printf("Found? %s\n", csset_str_contains(&sset, "Hello, friend") ? "true" : "false"); + } +} \ No newline at end of file diff --git a/tests/test_new_sptr.c b/tests/test_new_sptr.c new file mode 100644 index 00000000..2ef31790 --- /dev/null +++ b/tests/test_new_sptr.c @@ -0,0 +1,45 @@ +#include + +#include +forward_csptr(person, struct Person); + +struct Person { cstr name, last; } typedef Person; + +Person Person_init(const char* name, const char* last) { + return (Person){.name = cstr_from(name), .last = cstr_from(last)}; +} +void Person_del(Person* p) { + printf("del: %s %s\n", p->name.str, p->last.str); + c_del(cstr, &p->name, &p->last); +} + +#define F_tag person +#define i_val Person +#define i_valdel Person_del +#define i_cmp c_no_compare +#include + +#define i_val int +#include + +#define i_tag iptr +#define i_key csptr_int +#define i_cmp csptr_int_compare +#include + +int main(void) { + c_forvar (csptr_person p = csptr_person_make(Person_init("John", "Smiths")), csptr_person_del(&p)) + c_forvar (csptr_person q = csptr_person_clone(p), csptr_person_del(&q)) // share the pointer + { + printf("%s %s. uses: %zu\n", q.get->name.str, q.get->last.str, *q.use_count); + + c_forauto (csset_iptr, map) { + csset_iptr_insert(&map, csptr_int_make(2021)); + csset_iptr_insert(&map, csptr_int_make(2033)); + + c_foreach (i, csset_iptr, map) + printf(" %d", *i.ref->get); + puts(""); + } + } +} \ No newline at end of file diff --git a/tests/test_new_vec.c b/tests/test_new_vec.c new file mode 100644 index 00000000..ae1fd155 --- /dev/null +++ b/tests/test_new_vec.c @@ -0,0 +1,57 @@ +#include +#include + +forward_cvec(i32, int); +forward_cvec(pnt, struct Point); + +struct MyStruct { + cvec_i32 intvec; + cvec_pnt pntvec; +} typedef MyStruct; + + +#define F_tag i32 +#define i_val int +#include + +struct Point { int x, y; } typedef Point; +int point_compare(const Point* a, const Point* b) { + int c = c_default_compare(&a->x, &b->x); + return c ? c : c_default_compare(&a->y, &b->y); +} +#define F_tag pnt +#define i_val Point +#define i_cmp point_compare +#include + +#define i_val float +#include + +#define i_val_str +#include + + +int main() +{ + cvec_i32 vec = cvec_i32_init(); + cvec_i32_push_back(&vec, 123); + cvec_i32_del(&vec); + + cvec_float fvec = cvec_float_init(); + cvec_float_push_back(&fvec, 123.3); + cvec_float_del(&fvec); + + cvec_pnt pvec = cvec_pnt_init(); + cvec_pnt_push_back(&pvec, (Point){42, 14}); + cvec_pnt_push_back(&pvec, (Point){32, 94}); + cvec_pnt_push_back(&pvec, (Point){62, 81}); + cvec_pnt_sort(&pvec); + c_foreach (i, cvec_pnt, pvec) + printf(" (%d %d)", i.ref->x, i.ref->y); + puts(""); + cvec_pnt_del(&pvec); + + cvec_str svec = cvec_str_init(); + cvec_str_emplace_back(&svec, "Hello, friend"); + cvec_str_del(&svec); +} \ No newline at end of file -- cgit v1.2.3