4 ms·
Your function looks like it's doing I/O, which won't work at compile time test. Here's an example of a unittest for the ImportC compiler: struct S22079
by WalterBright 2y ago
Your function looks like it's doing I/O, which won't work at compile time test. Here's an example of a unittest for the ImportC compiler:
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.
- brabel 2y agoHey Walter, importC is great but on Mac it doesn't work right now because Apple seems to have added the type Float16 to math.h (probably due to this: https://developer.apple.com/documentation/swift/float16 https://developer.apple.com/documentation/swift/float16) and DMD breaks on that. Could you have a look at fixing that?
- WalterBright 2y agoAargh. Those sorts of extensions should not be in the system .h file.
- chikere232 2y agoThis works in regular C as sizeof() is a constant expression, but perhaps that was your point?
- uecker 2y agoIt is difficult to imagine that compile-time interpretation of tests is faster than compiling and running them for anything more complex. And for trivial stuff it should not matter. Not being able to do I/O is a limitation not a feature.
- WalterBright 2y agoLinkers are slow and clunky. Yes, there is a crossover point where executable tests are faster.
- uecker 2y agoFor one of my C projects (ca. 450 c-files), full rebuild time on my (not super fast) laptop is just below 10 seconds (incremental builds < 1s). Compiling and linking all unit tests takes a second, and running all unit tests takes 6-7 seconds. So even running the optimized code for the tests almost doubles the time for rebuilding the full project. Although I like the machine code to be tested that is actually used. (BTW: A single C++ file with templates for CUDA someone added to the project - when activated - almost doubles the build time.)
- pjmlp 2y agoWhat about putting common template parameters into an external template in a binary libray?
- uecker 2y agoNa, we will just remove this template nonsense.
- pjmlp 2y agoI guess preprocessor macros are much better alternative. /s
- uecker 2y agoProbably yes. Macros are about equally bad than templates in my experience (although C++ people will disagree, I do not see much difference). But mostly I plan to just let the compiler specialize the functions during optimization. I haven't looked at this specific problem though.
- pjmlp 2y agoFor one, templates can be stepped through on the debugger without additional effort, offer type checking, don't execute parameters multiple times, and don't require additional parenthesis and curly braces to protect their misuse.