by bjourne · 1y ago · 0 comments
void
test_load_uint8() {
npy_arr *arr = npy_load("tests/npy/uint8.npy");
assert(arr->n_dims == 1);
assert(arr->dims[0] == 100);
assert(arr->type == 'u');
npy_free(arr);
}
int
main(int argc, char *argv[]) {
PRINT_RUN(test_load_uint8);
...
}
I know I could have some pre-processor generate parts of the tests, but I prefer to KISS. struct S22079
{
int a, b, c;
};
_Static_assert(sizeof(struct S22079){1,2,3} == sizeof(int)*3, "ok");
_Static_assert(sizeof(struct S22079){1,2,3}.a == sizeof(int), "ok");
The semantics are checked at compile time, so no need to link & run. With the large volume of tests, this speeds things up considerably. The faster the test suite runs, the more productive I am.Could you have a look at fixing that?
I previously went down the rabbit hole of fancy unit test frameworks, and after a while I realised that they didn't really win much and settled on something almost identical to what you have (my PRINT_RUN macro has a different name, and requires the () to be passed in - and I only ever write it if the time to run all the tests is more than a second or so, just to make it really convenient to point the finger of blame).
The thing that I do which are potentially looked upon poorly by other people are:
1) I will happily #include a .c file that is being unit tested so I can call static functions in it (I will only #include a single .c file)
2) I do a tiny preprocessor dance before I #include <assert.h> to make sure NDEBUG is not defined (in case someone builds in a "release mode" which disables asserts)
If you can't write a test for functionality without fiddling with internal details the api is probably flawed
This logic is flawed. If you have an isolated implementation for some procedure that your api invokes in multiple places (or simply abstracted it out for clarity and SoC), it’s perfectly reasonable to test it separately even if it isn’t officially public.
I have something very similar.
https://github.com/ensisoft/detonator/blob/master/base/test_...
Borrowed heavily from boost.test.minimal and used to be a single header but but over the years I've had to add a single translation unit!
My takeaway is that if you keep your code base in a condition where tests are always passing you need much less complications in your testing tools and their error reporting and fault tolerance etc. !