From 5bbcae2a3add163ea3b7a91d65fda6836c18f410 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 31 Jan 2023 12:53:46 +0100 Subject: Updates, and prepare for the big unsigned ==> signed transformation. --- misc/benchmarks/build_all.sh | 2 +- misc/examples/multidim.c | 20 ++++++++--------- misc/examples/new_map.c | 17 +++++++------- misc/tests/cspan_test.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ misc/tests/ctest.h | 51 +++++++++++++----------------------------- 5 files changed, 88 insertions(+), 55 deletions(-) (limited to 'misc') diff --git a/misc/benchmarks/build_all.sh b/misc/benchmarks/build_all.sh index 54340998..869559ba 100644 --- a/misc/benchmarks/build_all.sh +++ b/misc/benchmarks/build_all.sh @@ -1,5 +1,5 @@ #!/bin/bash -cc='g++ -I../../include -s -O3 -Wall -pedantic -x c++ -std=c++20' +cc='g++ -I../../include -s -O3 -Wall -pedantic -x c++ -std=c++2a' #cc='clang++ -I../include -s -O3 -Wall -pedantic -x c++ -std=c++20' #cc='cl -nologo -I../include -O2 -TP -EHsc -std:c++20' run=0 diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index b7ee46fb..3bc1feec 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -9,7 +9,7 @@ using_cspan3(ispan, int); int main() { cstack_int v = {0}; - c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}) + c_FORLIST (i, int, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}) cstack_int_push(&v, *i.ref); // View data as contiguous memory representing 24 ints @@ -19,9 +19,9 @@ int main() ispan3 ms3 = cspan_md(v.data, 2, 3, 4); puts("ms3:"); - for (unsigned i=0; i != ms3.dim[0]; i++) { - for (unsigned j=0; j != ms3.dim[1]; j++) { - for (unsigned k=0; k != ms3.dim[2]; k++) { + for (int i=0; i != ms3.dim[0]; i++) { + for (int j=0; j != ms3.dim[1]; j++) { + for (int k=0; k != ms3.dim[2]; k++) { printf(" %2d", *cspan_at(&ms3, i, j, k)); } puts(""); @@ -32,9 +32,9 @@ int main() ispan3 ss3 = ms3; cspan_slice(&ss3, {0}, {1,3}, {1,3}); - for (unsigned i=0; i != ss3.dim[0]; i++) { - for (unsigned j=0; j != ss3.dim[1]; j++) { - for (unsigned k=0; k != ss3.dim[2]; k++) { + for (int i=0; i != ss3.dim[0]; i++) { + for (int j=0; j != ss3.dim[1]; j++) { + for (int k=0; k != ss3.dim[2]; k++) { printf(" %2d", *cspan_at(&ss3, i, j, k)); } puts(""); @@ -50,12 +50,12 @@ int main() ispan2 ms2 = cspan_submd3(&ms3, 0); // write data using 2D view - for (unsigned i=0; i != ms2.dim[0]; i++) - for (unsigned j=0; j != ms2.dim[1]; j++) + for (int i=0; i != ms2.dim[0]; i++) + for (int j=0; j != ms2.dim[1]; j++) *cspan_at(&ms2, i, j) = i*1000 + j; puts("\nview data as 1D view:"); - for (unsigned i=0; i != cspan_size(&ms1); i++) + for (int i=0; i != cspan_size(&ms1); i++) printf(" %d", *cspan_at(&ms1, i)); puts(""); diff --git a/misc/examples/new_map.c b/misc/examples/new_map.c index 72705eb2..9cee987d 100644 --- a/misc/examples/new_map.c +++ b/misc/examples/new_map.c @@ -48,24 +48,25 @@ int main() c_AUTO (cset_str, sset) { cmap_int_insert(&map, 123, 321); + cmap_int_insert(&map, 456, 654); + cmap_int_insert(&map, 789, 987); - c_FORLIST (i, cmap_pnt_raw, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}) - cmap_pnt_insert(&pmap, c_PAIR(i.ref)); + pmap = c_make(cmap_pnt, {{{42, 14}, 1}, {{32, 94}, 2}, {{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(""); - c_FORLIST (i, cmap_str_raw, { + smap = c_make(cmap_str, { {"Hello, friend", "long time no see"}, - {"So long, friend", "see you around"}, - }) cmap_str_emplace(&smap, c_PAIR(i.ref)); + {"So long", "see you around"}, + }); - c_FORLIST (i, const char*, { + sset = c_make(cset_str, { "Hello, friend", "Nice to see you again", - "So long, friend", - }) cset_str_emplace(&sset, *i.ref); + "So long", + }); c_FOREACH (i, cset_str, sset) printf(" %s\n", cstr_str(i.ref)); diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 32634795..3f03ef0f 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -72,3 +72,56 @@ CTEST(cspan, slice2) { ASSERT_EQ(65112, sum); } } + + +#define i_type Tiles +#define i_val intspan3 +#include + +CTEST_FIXTURE(cspan_cube) { + cstack_int stack; + Tiles tiles; +}; + +CTEST_SETUP(cspan_cube) { + enum {TSIZE=4, CUBE=64, N=CUBE*CUBE*CUBE}; + + fix->stack = cstack_int_init(); + fix->tiles = Tiles_init(); + + cstack_int_reserve(&fix->stack, N); + c_FORRANGE (i, N) + cstack_int_push(&fix->stack, i+1); + + intspan3 ms3 = cspan_md(fix->stack.data, CUBE, CUBE, CUBE); + + c_FORRANGE (i, 0, ms3.dim[0], TSIZE) { + c_FORRANGE (j, 0, ms3.dim[1], TSIZE) { + c_FORRANGE (k, 0, ms3.dim[2], TSIZE) { + intspan3 tile = ms3; + cspan_slice(&tile, {i, i + TSIZE}, {j, j + TSIZE}, {k, k + TSIZE}); + Tiles_push(&fix->tiles, tile); + } + } + } +} + +// Optional teardown function for suite, called after every test in suite +CTEST_TEARDOWN(cspan_cube) { + cstack_int_drop(&fix->stack); + Tiles_drop(&fix->tiles); +} + + +CTEST_F(cspan_cube, slice3) { + intptr_t n = cstack_int_size(&fix->stack); + //printf("\ntiles: %zi, cells: %zi\n", Tiles_size(&fix->tiles), n); + + int64_t sum = 0; + // iterate each 3d tile in sequence + c_FOREACH (i, Tiles, fix->tiles) + c_FOREACH (t, intspan3, *i.ref) + sum += *t.ref; + + ASSERT_EQ(n*(n + 1)/2, sum); +} diff --git a/misc/tests/ctest.h b/misc/tests/ctest.h index 294a6ca2..6cfb507a 100644 --- a/misc/tests/ctest.h +++ b/misc/tests/ctest.h @@ -113,14 +113,14 @@ struct ctest { #ifdef __cplusplus #define CTEST_SETUP(sname) \ - template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_TEARDOWN(sname) \ - template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_FIXTURE(sname) \ - template void CTEST_IMPL_SETUP_FNAME(sname)(T* self) { } \ - template void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* self) { } \ + template void CTEST_IMPL_SETUP_FNAME(sname)(T* fix) { } \ + template void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* fix) { } \ struct CTEST_IMPL_DATA_SNAME(sname) #define CTEST_IMPL_CTEST(sname, tname, tskip) \ @@ -130,23 +130,23 @@ struct ctest { #define CTEST_IMPL_CTEST_F(sname, tname, tskip) \ static struct CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_DATA_TNAME(sname, tname); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ static void (*CTEST_IMPL_SETUP_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_SETUP_FNAME(sname); \ static void (*CTEST_IMPL_TEARDOWN_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname); \ CTEST_IMPL_STRUCT(sname, tname, tskip, &CTEST_IMPL_DATA_TNAME(sname, tname), &CTEST_IMPL_SETUP_TPNAME(sname, tname), &CTEST_IMPL_TEARDOWN_TPNAME(sname, tname)); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #else #define CTEST_SETUP(sname) \ - static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ static void (*CTEST_IMPL_SETUP_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_SETUP_FNAME(sname); \ - static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_TEARDOWN(sname) \ - static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ static void (*CTEST_IMPL_TEARDOWN_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname); \ - static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_FIXTURE(sname) \ struct CTEST_IMPL_DATA_SNAME(sname); \ @@ -161,9 +161,9 @@ struct ctest { #define CTEST_IMPL_CTEST_F(sname, tname, tskip) \ static struct CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_DATA_TNAME(sname, tname); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ CTEST_IMPL_STRUCT(sname, tname, tskip, &CTEST_IMPL_DATA_TNAME(sname, tname), &CTEST_IMPL_SETUP_FPNAME(sname), &CTEST_IMPL_TEARDOWN_FPNAME(sname)); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #endif @@ -186,12 +186,6 @@ void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const #define ASSERT_WSTREQ(exp, real) assert_wstr("==", exp, real, __FILE__, __LINE__) #define ASSERT_WSTRNE(exp, real) assert_wstr("!=", exp, real, __FILE__, __LINE__) -void assert_data(const unsigned char* exp, size_t expsize, - const unsigned char* real, size_t realsize, - const char* caller, int line); -#define ASSERT_DATA(exp, expsize, real, realsize) \ - assert_data(exp, expsize, real, realsize, __FILE__, __LINE__) - #define CTEST_FLT_EPSILON 1e-5 #define CTEST_DBL_EPSILON 1e-12 @@ -363,21 +357,6 @@ void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const } } -void assert_data(const unsigned char* exp, size_t expsize, - const unsigned char* real, size_t realsize, - const char* caller, int line) { - size_t i; - if (expsize != realsize) { - CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize); - } - for (i=0; i