summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-31 12:53:46 +0100
committerTyge Løvset <[email protected]>2023-01-31 12:53:46 +0100
commit5bbcae2a3add163ea3b7a91d65fda6836c18f410 (patch)
tree304ab8ca8f632f56e53ee2bc568fb834da91b13c
parent209bf743e0c1253a4bc81d2ffb6897f657a84c8a (diff)
downloadSTC-modified-5bbcae2a3add163ea3b7a91d65fda6836c18f410.tar.gz
STC-modified-5bbcae2a3add163ea3b7a91d65fda6836c18f410.zip
Updates, and prepare for the big unsigned ==> signed transformation.
-rw-r--r--include/c11/fmt.h9
-rw-r--r--include/stc/ccommon.h18
-rw-r--r--include/stc/cspan.h110
-rw-r--r--misc/benchmarks/build_all.sh2
-rw-r--r--misc/examples/multidim.c20
-rw-r--r--misc/examples/new_map.c17
-rw-r--r--misc/tests/cspan_test.c53
-rw-r--r--misc/tests/ctest.h51
8 files changed, 173 insertions, 107 deletions
diff --git a/include/c11/fmt.h b/include/c11/fmt.h
index ff0cb25c..435193f8 100644
--- a/include/c11/fmt.h
+++ b/include/c11/fmt.h
@@ -59,6 +59,7 @@ int main() {
}
*/
#include <stdio.h>
+#include <stdint.h>
#include <assert.h>
#define fmt_OVERLOAD(name, ...) \
@@ -86,7 +87,7 @@ int main() {
typedef struct {
char* data;
- size_t cap, len;
+ intptr_t cap, len;
_Bool stream;
} fmt_buffer;
@@ -202,11 +203,11 @@ FMT_API void _fmt_bprint(fmt_buffer* buf, const char* fmt, ...) {
va_copy(args2, args);
const int n = vsnprintf(NULL, 0U, fmt, args);
if (n < 0) goto done2;
- const size_t pos = buf->stream ? buf->len : 0U;
+ const intptr_t pos = buf->stream ? buf->len : 0;
buf->len = pos + n;
if (buf->len > buf->cap) {
buf->cap = buf->len + buf->cap/2;
- buf->data = (char*)realloc(buf->data, buf->cap + 1);
+ buf->data = (char*)realloc(buf->data, (size_t)buf->cap + 1U);
}
vsprintf(buf->data + pos, fmt, args2);
done2: va_end(args2);
@@ -252,7 +253,7 @@ FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) {
if (!strchr("csdioxXufFeEaAgGnp", fmt[-1]))
while (*arg) *p++ = *arg++;
if (p0 && (p[-1] == 's' || p[-1] == 'c')) /* left-align str */
- memmove(p0 + 1, p0, p++ - p0), *p0 = '-';
+ memmove(p0 + 1, p0, (size_t)(p++ - p0)), *p0 = '-';
fmt += *fmt == '}';
continue;
}
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 34d297e3..7c25d397 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -78,18 +78,34 @@
#define c_NEW(T, ...) ((T*)memcpy(c_ALLOC(T), (T[]){__VA_ARGS__}, sizeof(T)))
#define c_LITERAL(T) (T)
#endif
+
#ifndef c_MALLOC
#define c_MALLOC(sz) malloc(sz)
#define c_CALLOC(n, sz) calloc(n, sz)
#define c_REALLOC(p, sz) realloc(p, sz)
#define c_FREE(p) free(p)
#endif
+#ifndef c_malloc
+ #define c_malloc(sz) malloc(c_i2u(sz))
+ #define c_calloc(n, sz) calloc(c_i2u(n), c_i2u(sz))
+ #define c_realloc(p, sz) realloc(p, c_i2u(sz))
+ #define c_free(p) free(p)
+#endif
#define c_static_assert(b) ((int)(0*sizeof(int[(b) ? 1 : -1])))
#define c_container_of(p, T, m) ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m)))
#define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); c_FREE(_tp); } while (0)
#define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \
_tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0)
+#define c_LTu(a, b) ((size_t)(a) < (size_t)(b))
+#define c_u2i(u) ((intptr_t)((u) + 0*sizeof((u) == 1U)))
+#define c_i2u(i) ((size_t)(i) + 0*sizeof((i) == 1))
+#define c_sizeof(x) (intptr_t)sizeof(x)
+#define c_memcpy(d, s, ilen) memcpy(d, s, c_i2u(ilen))
+#define c_memmove(d, s, ilen) memmove(d, s, c_i2u(ilen))
+#define c_memcmp(d, s, ilen) memcmp(d, s, c_i2u(ilen))
+#define c_strlen(str) (intptr_t)strlen(str)
+#define c_strncmp(d, s, ilen) strncmp(d, s, c_i2u(ilen))
// x and y are i_keyraw* type, defaults to i_key*:
#define c_default_cmp(x, y) (c_default_less(y, x) - c_default_less(x, y))
@@ -122,7 +138,7 @@ typedef const char* crawstr;
#define crawstr_hash(p) cstrhash(*(p))
#define crawstr_len(literal) (sizeof("" literal) - 1U)
-#define c_ARRAYLEN(a) (sizeof(a)/sizeof 0[a])
+#define c_ARRAYLEN(a) (intptr_t)(sizeof(a)/sizeof 0[a])
#define c_SV(...) c_MACRO_OVERLOAD(c_SV, __VA_ARGS__)
#define c_SV_1(lit) c_SV_2(lit, crawstr_len(lit))
#define c_SV_2(str, n) (c_LITERAL(csview){str, n})
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index 7886c9f6..a5f3fdc7 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -32,8 +32,8 @@ int demo1() {
float raw[4*5];
Span2f ms = cspan_md(raw, 4, 5);
- for (size_t i=0; i<ms.dim[0]; i++)
- for (size_t j=0; j<ms.dim[1]; j++)
+ for (int i=0; i<ms.dim[0]; i++)
+ for (int j=0; j<ms.dim[1]; j++)
*cspan_at(&ms, i, j) = i*1000 + j;
printf("%f\n", *cspan_at(&ms, 3, 4));
@@ -60,17 +60,19 @@ int demo2() {
#include "ccommon.h"
-#define using_cspan(Self, T, RANK) \
+#define using_cspan(...) c_MACRO_OVERLOAD(using_cspan, __VA_ARGS__)
+#define using_cspan_2(Self, T) using_cspan_3(Self, T, 1)
+#define using_cspan_3(Self, T, RANK) \
typedef T Self##_value; typedef T Self##_raw; \
typedef struct { \
Self##_value *data; \
- uint32_t dim[RANK]; \
+ int32_t dim[RANK]; \
cspan_idx##RANK stride; \
} Self; \
- typedef struct { Self##_value *ref; uint32_t pos[RANK]; const Self *_s; } Self##_iter; \
+ typedef struct { Self##_value *ref; int32_t pos[RANK]; const Self *_s; } Self##_iter; \
\
- STC_INLINE Self Self##_from_n(Self##_raw* raw, const size_t n) { \
- return (Self){.data=raw, .dim={(uint32_t)n}}; \
+ STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \
+ return (Self){.data=raw, .dim={(int32_t)n}}; \
} \
STC_INLINE Self##_iter Self##_begin(const Self* self) { \
Self##_iter it = {.ref=self->data, .pos={0}, ._s=self}; \
@@ -86,13 +88,15 @@ int demo2() {
} \
struct stc_nostruct
-#define using_cspan2(Self, T) using_cspan(Self, T, 1); using_cspan(Self##2, T, 2)
-#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan(Self##3, T, 3)
-#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan(Self##4, T, 4)
-typedef struct { uint32_t d[1]; } cspan_idx1;
-typedef struct { uint32_t d[2]; } cspan_idx2;
-typedef struct { uint32_t d[3]; } cspan_idx3;
-typedef struct { uint32_t d[4]; } cspan_idx4;
+#define using_cspan2(Self, T) using_cspan_3(Self, T, 1); using_cspan_3(Self##2, T, 2)
+#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan_3(Self##3, T, 3)
+#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan_3(Self##4, T, 4)
+#define using_cspan5(Self, T) using_cspan4(Self, T); using_cspan_4(Self##4, T, 5)
+typedef struct { int32_t d[1]; } cspan_idx1;
+typedef struct { int32_t d[2]; } cspan_idx2;
+typedef struct { int32_t d[3]; } cspan_idx3;
+typedef struct { int32_t d[4]; } cspan_idx4;
+typedef struct { int32_t d[5]; } cspan_idx5;
#define cspan_md(array, ...) \
{.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}}
@@ -103,7 +107,7 @@ typedef struct { uint32_t d[4]; } cspan_idx4;
/* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */
#define cspan_from(container) \
- {.data=(container)->data, .dim={(uint32_t)(container)->_len}}
+ {.data=(container)->data, .dim={(int32_t)(container)->_len}}
#define cspan_from_array(array) \
{.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}}
@@ -117,20 +121,16 @@ typedef struct { uint32_t d[4]; } cspan_idx4;
#define cspan_front(self) ((self)->data)
#define cspan_back(self) ((self)->data + cspan_size(self) - 1)
-// cspan_subspanN:
+// cspan_subspanN. for N > 3, use cspan_slice(&ms4, {offset, offset + count}, {0}, {0}, {0});
#define cspan_subspan(self, offset, count) \
{.data=cspan_at(self, offset), .dim={count}}
#define cspan_subspan2(self, offset, count) \
{.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[1]}, .stride={(self)->stride}}
#define cspan_subspan3(self, offset, count) \
- {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}, \
- .stride={(self)->stride}}
-#define cspan_subspan4(self, offset, count) \
- {.data=cspan_at(self, offset, 0, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2], (self)->dim[3]}, \
- .stride={(self)->stride}}
+ {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}, .stride={(self)->stride}}
-// cspan_submdN:
+// cspan_submdN: return reduced rank
#define cspan_submd4(...) c_MACRO_OVERLOAD(cspan_submd4, __VA_ARGS__)
#define cspan_submd3(...) c_MACRO_OVERLOAD(cspan_submd3, __VA_ARGS__)
@@ -145,40 +145,55 @@ typedef struct { uint32_t d[4]; } cspan_idx4;
{.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}, \
.stride={.d={0, (self)->stride.d[2], (self)->stride.d[3]}}}
#define cspan_submd4_3(self, x, y) \
- {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}, \
- .stride={.d={0, (self)->stride.d[3]}}}
+ {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}, .stride={.d={0, (self)->stride.d[3]}}}
#define cspan_submd4_4(self, x, y, z) \
{.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}}
+#define cspan_submd5_2(self, x) \
+ {.data=cspan_at(self, x, 0, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3], (self)->dim[4]}, \
+ .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3], (self)->stride.d[4]}}}
+#define cspan_submd5_3(self, x, y) \
+ {.data=cspan_at(self, x, y, 0, 0, 0), .dim={(self)->dim[2], (self)->dim[3], (self)->dim[4]}, \
+ .stride={.d={0, (self)->stride.d[3], (self)->stride.d[4]}}}
+#define cspan_submd5_4(self, x, y, z) \
+ {.data=cspan_at(self, x, y, z, 0, 0), .dim={(self)->dim[3], (self)->dim[4]}, .stride={.d={0, (self)->stride.d[4]}}}
+#define cspan_submd5_5(self, x, y, z, w) \
+ {.data=cspan_at(self, x, y, z, w, 0), .dim={(self)->dim[4]}}
+
// cspan_slice:
// e.g.: cspan_slice(&ms3, {1,3}, {0}, {1,4});
#define cspan_slice(self, ...) \
((void)((self)->data += _cspan_slice(cspan_rank(self), (self)->dim, (self)->stride.d, \
- (const uint32_t[][2]){__VA_ARGS__}) + \
+ (const int32_t[][2]){__VA_ARGS__}) + \
c_static_assert(cspan_rank(self) == \
- sizeof((const uint32_t[][2]){__VA_ARGS__})/8)))
+ sizeof((const int32_t[][2]){__VA_ARGS__})/8)))
// FUNCTIONS
-STC_INLINE size_t _cspan_i1(const uint32_t dim[1], const cspan_idx1 stri, uint32_t x)
- { c_ASSERT(x < dim[0]); return x; }
+STC_INLINE intptr_t _cspan_i1(const int32_t dim[1], const cspan_idx1 stri, int32_t x)
+ { c_ASSERT(c_LTu(x, dim[0])); return x; }
-STC_INLINE size_t _cspan_i2(const uint32_t dim[2], const cspan_idx2 stri, uint32_t x, uint32_t y)
- { c_ASSERT(x < dim[0] && y < dim[1]); return stri.d[1]*x + y; }
+STC_INLINE intptr_t _cspan_i2(const int32_t dim[2], const cspan_idx2 stri, int32_t x, int32_t y)
+ { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1])); return (intptr_t)stri.d[1]*x + y; }
-STC_INLINE size_t _cspan_i3(const uint32_t dim[3], const cspan_idx3 stri, uint32_t x, uint32_t y, uint32_t z) {
- c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2]);
- return stri.d[2]*(stri.d[1]*x + y) + z;
+STC_INLINE intptr_t _cspan_i3(const int32_t dim[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) {
+ c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]));
+ return (intptr_t)stri.d[2]*(stri.d[1]*x + y) + z;
+}
+STC_INLINE intptr_t _cspan_i4(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y,
+ int32_t z, int32_t w) {
+ c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3]));
+ return (intptr_t)stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w;
}
-STC_INLINE size_t _cspan_i4(const uint32_t dim[4], const cspan_idx4 stri, uint32_t x, uint32_t y, \
- uint32_t z, uint32_t w) {
- c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2] && w < dim[3]);
- return stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w;
+STC_INLINE intptr_t _cspan_i5(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, int32_t z,
+ int32_t w, int32_t v) {
+ c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3]) && c_LTu(v, dim[4]));
+ return (intptr_t)stri.d[4]*(stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w) + v;
}
-STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) {
- size_t sz = dim[0];
+STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) {
+ intptr_t sz = dim[0];
while (rank-- > 1) sz *= dim[rank];
return sz;
}
@@ -186,30 +201,31 @@ STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) {
#define _cspan_next_1(r, pos, d, s) (++pos[0], 1)
#define _cspan_next_3 _cspan_next_2
#define _cspan_next_4 _cspan_next_2
+#define _cspan_next_5 _cspan_next_2
-static inline size_t _cspan_next_2(int rank, uint32_t pos[], const uint32_t dim[], const uint32_t stride[]) {
- size_t off = 1, rs = 1;
+STC_INLINE intptr_t _cspan_next_2(int rank, int32_t pos[], const int32_t dim[], const int32_t stride[]) {
+ intptr_t off = 1, rs = 1;
++pos[rank - 1];
while (--rank && pos[rank] == dim[rank]) {
pos[rank] = 0, ++pos[rank - 1];
- const size_t ds = rs*dim[rank];
+ const intptr_t ds = rs*dim[rank];
rs *= stride[rank];
off += rs - ds;
}
return off;
}
-STC_INLINE size_t _cspan_slice(int rank, uint32_t dim[], const uint32_t stri[], const uint32_t a[][2]) {
- uint32_t t = a[0][1] ? a[0][1] : dim[0];
- c_ASSERT(t <= dim[0]);
+STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[], const int32_t a[][2]) {
+ int32_t t = a[0][1] ? a[0][1] : dim[0];
+ c_ASSERT(!c_LTu(dim[0], t));
dim[0] = t - a[0][0];
- size_t off = a[0][0];
+ intptr_t off = a[0][0];
for (int i = 1; i < rank; ++i) {
off *= stri[i];
off += a[i][0];
t = a[i][1] ? a[i][1] : dim[i];
- c_ASSERT(t <= dim[i]);
+ c_ASSERT(!c_LTu(dim[i], t));
dim[i] = t - a[i][0];
}
return off;
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 <stc/cstack.h>
+
+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 <typename T> void CTEST_IMPL_SETUP_FNAME(sname)(T* self) { } \
- template <typename T> void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* self) { } \
+ template <typename T> void CTEST_IMPL_SETUP_FNAME(sname)(T* fix) { } \
+ template <typename T> 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)<struct CTEST_IMPL_DATA_SNAME(sname)>; \
static void (*CTEST_IMPL_TEARDOWN_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname)<struct CTEST_IMPL_DATA_SNAME(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<expsize; i++) {
- if (exp[i] != real[i]) {
- CTEST_ERR("%s:%d expected 0x%02x at offset %" PRIuMAX " got 0x%02x",
- caller, line, exp[i], (uintmax_t) i, real[i]);
- }
- }
-}
-
static bool get_compare_result(const char* cmp, int c3, bool eq) {
if (cmp[0] == '<')
return c3 < 0 || ((cmp[1] == '=') & eq);
@@ -443,7 +422,7 @@ void assert_fail(const char* caller, int line) {
static int suite_all(struct ctest* t) {
- (void) t; // fix unused parameter warning
+ (void) t; // avoid unused parameter warning
return 1;
}
@@ -466,8 +445,8 @@ static void sighandler(int signum)
const char msg_nocolor[] = "[SIGSEGV: Segmentation fault]\n";
const char* msg = color_output ? msg_color : msg_nocolor;
- write(STDOUT_FILENO, msg, (unsigned int)strlen(msg));
-
+ intptr_t n = write(STDOUT_FILENO, msg, (unsigned int)strlen(msg));
+ (void)n;
/* "Unregister" the signal handler and send the signal back to the process
* so it can terminate as expected */
signal(signum, SIG_DFL);