Live data from Hacker News

Show HN: Writing modern C unit tests with Criterion

github.com

11–14 of 14 posts

Re: Show HN: Writing modern C unit tests with Criterion

#11

This looks like some good stuff, but browsing through your code, it's honestly pretty opaque macro hell. Why so obfuscated?

Macros are used to handle platform specific details, for the most part. I don't think it's that opaque, but then again I might be biased.

Re: Show HN: Writing modern C unit tests with Criterion

#12
post #10
post #9

Earlier quoted context omitted.

Why did you rule out using Google Test ( https://code.google.com/p/googletest )? (In case you mulled over using it.)

Google Test requires you to write your tests in C++ rather than C; so there was times I just could not use it.

I'm having a little trouble imagining a situation where unit tests for a C codebase are appropriate that would be stymied by having to write the tests in C++. (Maybe relying on struct layout?)

Re: Show HN: Writing modern C unit tests with Criterion

#13
post #10

Earlier quoted context omitted.

Google Test requires you to write your tests in C++ rather than C; so there was times I just could not use it.

I'm having a little trouble imagining a situation where unit tests for a C codebase are appropriate that would be stymied by having to write the tests in C++. (Maybe relying on struct layout?)

Because C hasn't been a subset of C++ since C99, there are plenty of case where trying to do C in C++ is awkward or just not possible with standard C++:

First, you'd have to wrap your header inclusions in `extern "C"` to disable mangling, then you would have to make sure you static_cast all your pointers where normally `void*` conversions would have done its job.

Furthermore, all interfaces relying on designated initializers and compound literals are broken unless you decide to compile in nonstandard GNU C++.

And there are more incompatibilities, such as using `static` or `const` in array parameter declarations or using VLA in macros which are not recognized by C++.

It's all about using the right tool for the right job, ultimately.

Re: Show HN: Writing modern C unit tests with Criterion

#14
post #6
post #5

Earlier quoted context omitted.

Actually, I didn't know about it until now! The project uses a similar approach to mine, by parsing the DIE tree produced by dwarf, which means that it won't work if you're not compiling with -g. It also uses some very platform specific stuff, so windows & os x are out, too. Very well made nonetheless.

I think he's got OSX running now, but as you say -- it relies on some platform-specific stuff. Good to see C test frameworks doing some innovative stuff!

Darwin support is functional today, I'm just waiting for the 3.11 Valgrind release, which fixes a bug which is a showstopper for NovaProva.
Post reply on HN