diff options
Diffstat (limited to 'misc/include')
| -rw-r--r-- | misc/include/c11/fmt.h (renamed from misc/include/fmt.h) | 37 | ||||
| -rw-r--r-- | misc/include/old/carr2.h | 152 | ||||
| -rw-r--r-- | misc/include/old/carr3.h | 157 | ||||
| -rw-r--r-- | misc/include/old/csmap.h (renamed from misc/include/alt/csmap.h) | 0 | ||||
| -rw-r--r-- | misc/include/old/cstr.h (renamed from misc/include/alt/cstr.h) | 11 | ||||
| -rw-r--r-- | misc/include/old/new_arr.c | 57 | ||||
| -rw-r--r-- | misc/include/stctest.h | 203 |
7 files changed, 394 insertions, 223 deletions
diff --git a/misc/include/fmt.h b/misc/include/c11/fmt.h index df1fe990..d5ba3575 100644 --- a/misc/include/fmt.h +++ b/misc/include/c11/fmt.h @@ -30,12 +30,14 @@ int main() { const double pi = 3.141592653589793;
const size_t x = 1234567890;
const char* string = "Hello world";
+ const wchar_t* wstr = L"The whole";
const char z = 'z';
_Bool flag = 1;
unsigned char r = 123, g = 214, b = 90, w = 110;
char buffer[64];
fmt_print(1, "Color: ({} {} {}), {}\n", r, g, b, flag);
+ fmt_print(1, "Wide: {}, {}\n", wstr, L"wide world");
fmt_print(1, "{:10} {:10} {:10.2f}\n", 42ull, 43, pi);
fmt_print(stdout, "{:>10} {:>10} {:>10}\n", z, z, w);
fmt_print(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World");
@@ -72,6 +74,11 @@ int main() { #else
# define FMT_API static inline
#endif
+#if defined FMT_NDEBUG || defined NDEBUG
+# define fmt_OK(exp) (void)(exp)
+#else
+# define fmt_OK(exp) assert(exp)
+#endif
typedef struct {
char* data;
@@ -92,48 +99,48 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); #define fmt_print(...) fmt_OVERLOAD(fmt_print, __VA_ARGS__)
#define fmt_print2(to, fmt) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 0, fmt); \
- assert(_n == 0); _fmt_fn(to)(to, fmt); } while (0)
+ fmt_OK(_n == 0); _fmt_fn(to)(to, fmt); } while (0)
#define fmt_print3(to, fmt, c) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 1, fmt, _fc(c)); \
- assert(_n == 1); _fmt_fn(to)(to, _fs, c); } while (0)
+ fmt_OK(_n == 1); _fmt_fn(to)(to, _fs, c); } while (0)
#define fmt_print4(to, fmt, c, d) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 2, fmt, _fc(c), _fc(d)); \
- assert(_n == 2); _fmt_fn(to)(to, _fs, c, d); } while (0)
+ fmt_OK(_n == 2); _fmt_fn(to)(to, _fs, c, d); } while (0)
#define fmt_print5(to, fmt, c, d, e) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 3, fmt, _fc(c), _fc(d), _fc(e)); \
- assert(_n == 3); _fmt_fn(to)(to, _fs, c, d, e); } while (0)
+ fmt_OK(_n == 3); _fmt_fn(to)(to, _fs, c, d, e); } while (0)
#define fmt_print6(to, fmt, c, d, e, f) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 4, fmt, _fc(c), _fc(d), _fc(e), _fc(f)); \
- assert(_n == 4); _fmt_fn(to)(to, _fs, c, d, e, f); } while (0)
+ fmt_OK(_n == 4); _fmt_fn(to)(to, _fs, c, d, e, f); } while (0)
#define fmt_print7(to, fmt, c, d, e, f, g) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 5, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g)); \
- assert(_n == 5); _fmt_fn(to)(to, _fs, c, d, e, f, g); } while (0)
+ fmt_OK(_n == 5); _fmt_fn(to)(to, _fs, c, d, e, f, g); } while (0)
#define fmt_print8(to, fmt, c, d, e, f, g, h) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 6, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h)); \
- assert(_n == 6); _fmt_fn(to)(to, _fs, c, d, e, f, g, h); } while (0)
+ fmt_OK(_n == 6); _fmt_fn(to)(to, _fs, c, d, e, f, g, h); } while (0)
#define fmt_print9(to, fmt, c, d, e, f, g, h, i) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 7, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), _fc(i)); \
- assert(_n == 7); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i); } while (0)
+ fmt_OK(_n == 7); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i); } while (0)
#define fmt_print10(to, fmt, c, d, e, f, g, h, i, j) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 8, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j)); \
- assert(_n == 8); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j); } while (0)
+ fmt_OK(_n == 8); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j); } while (0)
#define fmt_print11(to, fmt, c, d, e, f, g, h, i, j, k) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 9, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k)); \
- assert(_n == 9); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k); } while (0)
+ fmt_OK(_n == 9); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k); } while (0)
#define fmt_print12(to, fmt, c, d, e, f, g, h, i, j, k, m) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 10, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m)); \
- assert(_n == 10); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m); } while (0)
+ fmt_OK(_n == 10); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m); } while (0)
#define fmt_print13(to, fmt, c, d, e, f, g, h, i, j, k, m, n) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 11, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m), _fc(n)); \
- assert(_n == 11); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n); } while (0)
+ fmt_OK(_n == 11); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n); } while (0)
#define fmt_print14(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 12, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m), _fc(n), _fc(o)); \
- assert(_n == 12); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n, o); } while (0)
+ fmt_OK(_n == 12); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n, o); } while (0)
#define _fmt_fn(x) _Generic ((x), \
FILE*: fprintf, \
@@ -164,8 +171,10 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); double: "@g", \
long double: "@Lg", \
char*: "s", \
+ wchar_t*: "ls", \
void*: "p", \
const char*: "s", \
+ const wchar_t*: "ls", \
const void*: "p")
#if defined FMT_IMPLEMENT || !(defined FMT_HEADER || defined FMT_IMPLEMENT)
@@ -250,4 +259,4 @@ FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) { return n;
}
#endif
-#endif
\ No newline at end of file +#endif
diff --git a/misc/include/old/carr2.h b/misc/include/old/carr2.h new file mode 100644 index 00000000..aebaec81 --- /dev/null +++ b/misc/include/old/carr2.h @@ -0,0 +1,152 @@ +/* MIT License + * + * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include <stc/ccommon.h> + +#ifndef CARR2_H_INCLUDED +#define CARR2_H_INCLUDED +#include <stc/forward.h> +#include <stdlib.h> +#endif +/* +// carr2- 2D dynamic array in one memory block with easy indexing. +#define i_key int +#include <stc/carr2.h> +#include <stdio.h> + +int main() { + int w = 7, h = 5; + c_with (carr2_int image = carr2_int_new_uninit(w, h), carr2_int_drop(&image)) + { + int *dat = carr2_int_data(&image); + for (int i = 0; i < carr2_int_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("\n"); + + c_foreach (i, carr2_int, image) + printf(" %d", *i.ref); + puts(""); + } +} +*/ + +#ifndef _i_prefix +#define _i_prefix carr2_ +#endif +#include <stc/priv/template.h> +#if !c_option(c_is_forward) +_cx_deftypes(_c_carr2_types, _cx_self, i_key); +#endif + +STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null); +STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* storage); +STC_API _cx_value* _cx_memb(_release)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); +#if !defined i_no_clone +STC_API _cx_self _cx_memb(_clone)(_cx_self src); +STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); +#endif + +STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim) { + return _cx_memb(_with_data)(xdim, ydim, c_alloc_n(_cx_value, xdim*ydim)); +} +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->xdim*self->ydim; } + +STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self) + { return *self->data; } + +STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t y) { + assert(x < self->xdim && y < self->ydim); + return *self->data + self->ydim*x + y; +} + +STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) { + return self->ydim*x + y; +} + + +STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { + size_t n = self->xdim*self->ydim; + return c_INIT(_cx_iter){n ? *self->data : NULL, *self->data + n}; +} + +STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) + { return c_INIT(_cx_iter){NULL, *self->data + self->xdim*self->ydim}; } + +STC_INLINE void _cx_memb(_next)(_cx_iter* it) + { if (++it->ref == it->end) it->ref = NULL; } + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) + +STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* block) { + _cx_self _arr = {c_alloc_n(_cx_value*, xdim), xdim, ydim}; + for (size_t x = 0; x < xdim; ++x, block += ydim) + _arr.data[x] = block; + return _arr; +} + +STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null) { + _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim); + for (_cx_value* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) + *p = null; + return _arr; +} + +#if !defined i_no_clone + +STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { + _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim); + for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) + *p = i_keyclone((*q)); + return _arr; +} + +STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); +} +#endif + +STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) { + _cx_value *values = self->data[0]; + c_free(self->data); + self->data = NULL; + return values; +} + +STC_DEF void _cx_memb(_drop)(_cx_self* self) { + if (!self->data) return; + for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(self); p != q; ) { + --q; i_keydrop(q); + } + c_free(self->data[0]); /* values */ + c_free(self->data); /* pointers */ +} + +#endif +#include <stc/priv/template.h> diff --git a/misc/include/old/carr3.h b/misc/include/old/carr3.h new file mode 100644 index 00000000..a0148992 --- /dev/null +++ b/misc/include/old/carr3.h @@ -0,0 +1,157 @@ +/* MIT License + * + * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include <stc/ccommon.h> + +#ifndef CARR3_H_INCLUDED +#define CARR3_H_INCLUDED +#include <stc/forward.h> +#include <stdlib.h> +#endif +/* +// carr3 - 3D dynamic array in one memory block with easy indexing. +#define i_key int +#include <stc/carr3.h> +#include <stdio.h> + +int main() { + int w = 7, h = 5, d = 3; + c_with (carr3_int image = carr3_int_new_uninit(w, h, d), carr3_int_drop(&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("\n"); + + c_foreach (i, carr3_int, image) + printf(" %d", *i.ref); + puts(""); + } +} +*/ + +#ifndef _i_prefix +#define _i_prefix carr3_ +#endif +#include <stc/priv/template.h> + +#if !c_option(c_is_forward) +_cx_deftypes(_c_carr3_types, _cx_self, i_key); +#endif + +STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null); +STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage); +STC_API _cx_value* _cx_memb(_release)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); +#if !defined i_no_clone +STC_API _cx_self _cx_memb(_clone)(_cx_self src); +STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); +#endif + +STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim, size_t zdim) { + return _cx_memb(_with_data)(xdim, ydim, zdim, c_alloc_n(_cx_value, xdim*ydim*zdim)); +} + +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->xdim*self->ydim*self->zdim; } + +STC_INLINE _cx_value* _cx_memb(_data)(_cx_self* self) + { return **self->data; } + +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t x, size_t y, size_t z) { + assert(x < self->xdim && y < self->ydim && z < self->zdim); + return **self->data + self->zdim*(self->ydim*x + y) + z; +} + +STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y, size_t z) { + return self->zdim*(self->ydim*x + y) + z; +} + + +STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { + size_t n = _cx_memb(_size)(self); + return c_INIT(_cx_iter){n ? **self->data : NULL, **self->data + n}; +} + +STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) + { return c_INIT(_cx_iter){NULL, **self->data + _cx_memb(_size)(self)}; } + +STC_INLINE void _cx_memb(_next)(_cx_iter* it) + { if (++it->ref == it->end) it->ref = NULL; } + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) + +STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* block) { + _cx_self _arr = {c_alloc_n(_cx_value**, xdim*(ydim + 1)), xdim, ydim, zdim}; + _cx_value** p = (_cx_value**) &_arr.data[xdim]; + for (size_t x = 0, y; x < xdim; ++x, p += ydim) + for (y = 0, _arr.data[x] = p; y < ydim; ++y, block += zdim) + p[y] = block; + return _arr; +} + +STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null) { + _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim, zdim); + for (_cx_value* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) + *p = null; + return _arr; +} + +#if !defined i_no_clone + +STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { + _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim, src.zdim); + for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) + *p = i_keyclone((*q)); + return _arr; +} + +STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); +} +#endif + +STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) { + _cx_value *values = self->data[0][0]; + c_free(self->data); + self->data = NULL; + return values; +} + +STC_DEF void _cx_memb(_drop)(_cx_self* self) { + if (!self->data) return; + for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(self); p != q; ) { + --q; i_keydrop(q); + } + c_free(self->data[0][0]); /* data */ + c_free(self->data); /* pointers */ +} + +#endif +#include <stc/priv/template.h> diff --git a/misc/include/alt/csmap.h b/misc/include/old/csmap.h index 4b4e764c..4b4e764c 100644 --- a/misc/include/alt/csmap.h +++ b/misc/include/old/csmap.h diff --git a/misc/include/alt/cstr.h b/misc/include/old/cstr.h index ecb9c9fb..8de6c4b0 100644 --- a/misc/include/alt/cstr.h +++ b/misc/include/old/cstr.h @@ -182,12 +182,12 @@ STC_INLINE uint64_t cstr_hash(const cstr *self) { return cfasthash(self->str, _cstr_p(self)->size); } -STC_INLINE void -cstr_replace(cstr* self, const char* find, const char* repl, unsigned count) { - csview in = cstr_sv(self); - cstr_take(self, cstr_replace_sv(in, c_SV(find, strlen(find)), - c_SV(repl, strlen(repl)), count)); +STC_INLINE void cstr_replace_ex(cstr* self, const char* find, const char* repl, unsigned count) { + cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(find, strlen(find)), + c_SV(repl, strlen(repl)), count)); } +STC_INLINE void cstr_replace(cstr* self, const char* search, const char* repl) + { cstr_replace_ex(self, search, repl, ~0U); } /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) @@ -324,7 +324,6 @@ STC_DEF cstr cstr_replace_sv(csview str, csview find, csview repl, unsigned count) { cstr out = cstr_NULL; size_t from = 0; char* res; - if (count == 0) count = ~0; if (find.size) while (count-- && (res = cstrnstrn(str.str + from, find.str, str.size - from, find.size))) { const size_t pos = res - str.str; diff --git a/misc/include/old/new_arr.c b/misc/include/old/new_arr.c new file mode 100644 index 00000000..1006439d --- /dev/null +++ b/misc/include/old/new_arr.c @@ -0,0 +1,57 @@ +#include <stc/cstr.h> + +#define i_val int +#include "carr2.h" + +#define i_val int +#include "carr3.h" + +#define i_val_str +#include "carr2.h" + +int main() +{ + int w = 7, h = 5, d = 3; + + c_WITH (carr2_int volume = carr2_int_new_uninit(w, h), carr2_int_drop(&volume)) + { + int *dat = carr2_int_data(&volume); + for (size_t i = 0; i < carr2_int_size(&volume); ++i) + dat[i] = i; + + for (size_t x = 0; x < volume.xdim; ++x) + for (size_t y = 0; y < volume.ydim; ++y) + printf(" %d", volume.data[x][y]); + puts(""); + + c_FOREACH (i, carr2_int, volume) + printf(" %d", *i.ref); + puts("\n"); + } + + c_WITH (carr3_int volume = carr3_int_new_uninit(w, h, d), carr3_int_drop(&volume)) + { + int *dat = carr3_int_data(&volume); + for (size_t i = 0; i < carr3_int_size(&volume); ++i) + dat[i] = i; + + for (size_t x = 0; x < volume.xdim; ++x) + for (size_t y = 0; y < volume.ydim; ++y) + for (size_t z = 0; z < volume.zdim; ++z) + printf(" %d", volume.data[x][y][z]); + puts(""); + + c_FOREACH (i, carr3_int, volume) + printf(" %d", *i.ref); + puts(""); + } + + c_WITH (carr2_str text2d = carr2_str_with_size(h, d, cstr_NULL), carr2_str_drop(&text2d)) + { + cstr_assign(&text2d.data[2][1], "hello"); + cstr_assign(&text2d.data[4][0], "world"); + + c_FOREACH (i, carr2_str, text2d) + printf("line: %s\n", cstr_str(i.ref)); + } +} diff --git a/misc/include/stctest.h b/misc/include/stctest.h deleted file mode 100644 index 9718af7f..00000000 --- a/misc/include/stctest.h +++ /dev/null @@ -1,203 +0,0 @@ -/* MIT License
- *
- * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-/* stctest: A small and simple C11 unit-testing framework.
-
- Features:
- - Requires C11. Should work with any C compiler supporting _Generic.
- - No library dependencies. Not even itself. Just a header file.
- - Reports assertion failures, including expressions and line numbers.
- - ANSI color output for maximum visibility.
- - Easy to embed in apps for runtime tests (e.g. environment tests).
-
- Example:
-
- #include "stctest.h"
- #include "mylib.h"
-
- void test_sheep()
- {
- EXPECT_EQ("Sheep are cool", are_sheep_cool());
- EXPECT_EQ(4, sheep.legs);
- }
-
- void test_cheese()
- {
- EXPECT_GT(cheese.tanginess, 0);
- EXPECT_EQ("Wensleydale", cheese.name);
- }
-
- int main()
- {
- RUN_TEST(test_sheep);
- RUN_TEST(test_cheese);
- return REPORT_TESTS();
- }
- */
-
-#ifndef STCTEST_INCLUDED
-#define STCTEST_INCLUDED
-
-#include <stdio.h>
-#include <time.h>
-#include <string.h>
-#include <float.h>
-#include <stdarg.h>
-#include <inttypes.h>
-
-#define STC_FLOAT_EPSILON 1e-6
-#define STC_DOUBLE_EPSILON 1e-13
-
-
-#define EXPECT_TRUE(expr) \
- do { if (!_stctest_assert(__FILE__, __LINE__, #expr, (expr) != 0)) puts(""); } while (0)
-
-#define EXPECT_TRUE1(expr, v) \
- do { if (!_stctest_assert(__FILE__, __LINE__, #expr, (expr) != 0)) { \
- char _fmt[32]; sprintf(_fmt, " ; %%s --> %s\n", _stctest_FMT(v)); \
- printf(_fmt, #v, v); \
- }} while (0)
-
-#define EXPECT_FALSE(expr) EXPECT_TRUE(!(expr))
-#define EXPECT_FALSE1(expr, v) EXPECT_TRUE1(!(expr), v)
-
-/* NB! (char*) are compared as strings. Cast to (void*) to compare pointers only */
-#define EXPECT_EQ(a, b) _stctest_CHECK(a, ==, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_NE(a, b) _stctest_CHECK(a, !=, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_GT(a, b) _stctest_CHECK(a, >, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_LT(a, b) _stctest_CHECK(a, <, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_LE(a, b) _stctest_CHECK(a, <=, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_GE(a, b) _stctest_CHECK(a, >=, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_FLOAT_EQ(a, b) _stctest_CHECK((float)(a), ==, (float)(b), -STC_FLOAT_EPSILON)
-#define EXPECT_DOUBLE_EQ(a, b) _stctest_CHECK((double)(a), ==, (double)(b), -STC_DOUBLE_EPSILON)
-#define EXPECT_NEAR(a, b, abs_error) _stctest_CHECK((double)(a), ==, (double)(b), abs_error)
-
-/* Run a test() function */
-#define RUN_TEST(test, ...) do { \
- puts(#test "(" #__VA_ARGS__ "):"); \
- const int ps = _stctest_s.passes; \
- const int fs = _stctest_s.fails; \
- const clock_t _start = clock(); \
- test(__VA_ARGS__); \
- const int _sum = (clock() - _start)*1000 / CLOCKS_PER_SEC; \
- _stctest_s.total_ms += _sum; \
- printf(" passed: %d/%d. duration: %d ms\n", \
- _stctest_s.passes - ps, _stctest_s.passes + _stctest_s.fails - (ps + fs), _sum); \
-} while (0)
-
-#define REPORT_TESTS() stctest_report()
-
-/* ----------------------------------------------------------------------------- */
-
-#define _stctest_CHECK(a, OP, b, e) \
- do { if (!_stctest_assert(__FILE__, __LINE__, #a " " #OP " " #b, _stctest_CMP(a, OP, b, e))) { \
- char _fmt[32]; sprintf(_fmt, " ; %s %s %s\n", _stctest_FMT(a), #OP, _stctest_FMT(b)); \
- printf(_fmt, a, b); \
- }} while (0)
-
-#define _stctest_CMP(a, OP, b, e) _Generic((a), \
- const char*: _stctest_strcmp, char*: _stctest_strcmp, \
- double: _Generic((b), double: _stctest_dblcmp, float: _stctest_dblcmp, default: _stctest_valcmp), \
- float: _Generic((b), double: _stctest_dblcmp, float: _stctest_dblcmp, default: _stctest_valcmp), \
- default: _stctest_valcmp)((a) OP (b), #OP, a, b, (double)(e))
-
-#define _stctest_FMT(a) _Generic((a), \
- float: "%.8gf", double: "%.15g", \
- int64_t: "%" PRId64, int32_t: "%" PRId32, int16_t: "%" PRId16, int8_t: "%" PRId8, \
- uint64_t: "%" PRIu64 "u", uint32_t: "%" PRIu32 "u", uint16_t: "%" PRIu16 "u", uint8_t: "%" PRIu8 "u", \
- char*: "`%s`", const char*: "`%s`", \
- default: "%p")
-
-static int _stctest_strcmp(int res, const char* OP, ...) {
- va_list ap;
- va_start(ap, OP);
- const char* a = va_arg(ap, const char *);
- const char* b = va_arg(ap, const char *);
- va_end(ap);
- int c = strcmp(a, b);
- if (OP[0] == '<') return OP[1] == '=' ? c <= 0 : c < 0;
- if (OP[0] == '>') return OP[1] == '=' ? c >= 0 : c > 0;
- return (OP[0] == '!') ^ (c == 0);
-}
-
-// Knuth:
-static int approximately_equal(double a, double b, double epsilon) {
- double d = a - b; if (d < 0) d = -d;
- if (a < 0) a = -a; if (b < 0) b = -b;
- return d <= ((a < b ? b : a) * epsilon); // (a > b ? b : a) => essentially_equal:
-}
-
-static int _stctest_dblcmp(int res, const char* OP, ...) {
- va_list ap;
- va_start(ap, OP);
- double a = va_arg(ap, double);
- double b = va_arg(ap, double);
- double e = va_arg(ap, double);
- double c = a - b;
- va_end(ap);
- if (OP[0] == '<') return OP[1] == '=' ? c <= 0 : c < 0;
- if (OP[0] == '>') return OP[1] == '=' ? c >= 0 : c > 0;
- return (OP[0] == '!') ^ (e < 0 ? approximately_equal(a, b, -e) : (c < 0 ? -c : c) <= e);
-}
-
-static int _stctest_valcmp(int res, const char* OP, ...)
- { return res; }
-
-#define _stctest_COLOR_CODE 0x1B
-#define _stctest_COLOR_RED "[1;31m"
-#define _stctest_COLOR_GREEN "[1;32m"
-#define _stctest_COLOR_RESET "[0m"
-
-static struct stctest_s {
- int passes;
- int fails;
- const char* current_file;
- int total_ms;
-} _stctest_s = {0};
-
-static int _stctest_assert(const char* file, int line, const char* expression, int pass) {
- if (pass)
- _stctest_s.passes++;
- else {
- _stctest_s.fails++;
- printf(" failed \"%s:%d\": (%s)", file, line, expression);
- }
- _stctest_s.current_file = file;
- return pass;
-}
-
-static int stctest_report(void) {
- if (_stctest_s.fails) {
- printf("%c%sFAILED%c%s: \"%s\": (failed %d, passed %d, total %d)\n",
- _stctest_COLOR_CODE, _stctest_COLOR_RED, _stctest_COLOR_CODE, _stctest_COLOR_RESET,
- _stctest_s.current_file, _stctest_s.fails, _stctest_s.passes, _stctest_s.passes + _stctest_s.fails);
- } else {
- printf("%c%sPASSED%c%s: \"%s\": (total %d)\n",
- _stctest_COLOR_CODE, _stctest_COLOR_GREEN, _stctest_COLOR_CODE, _stctest_COLOR_RESET,
- _stctest_s.current_file, _stctest_s.passes);
- }
- printf(" duration: %d ms\n", _stctest_s.total_ms); \
- return -_stctest_s.fails;
-}
-
-#endif
|
