Trying to unit test with cmocka;
#include <cmocka.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
long long coloredCells(int n) { return (long long)2 * n * n - 2 * n + 1; }
static void test_coloredCells(void **state) {
(void)state;
// Test cases
assert_int_equal(coloredCells(1), 1);
assert_int_equal(coloredCells(2), 5);
assert_int_equal(coloredCells(3), 13);
assert_int_equal(coloredCells(100), 19901);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_coloredCells),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
gcc:
gcc -v -E -x c++ -
Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-apple-darwin22
Configured with: ../configure --prefix=/usr/local/opt/gcc --libdir=/usr/local/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran,m2 --program-suffix=-14 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 14.2.0_1' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=x86_64-apple-darwin22 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.0 (Homebrew GCC 14.2.0_1)
COLLECT_GCC_OPTIONS='-v' '-E' '-mmacosx-version-min=13.0.0' '-asm_macosx_version_min=13.0' '-nodefaultexport' '-mtune=core2'
/usr/local/Cellar/gcc/14.2.0_1/bin/../libexec/gcc/x86_64-apple-darwin22/14/cc1plus -E -quiet -v -iprefix /usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/ -D__DYNAMIC__ - -fPIC -mmacosx-version-min=13.0.0 -mtune=core2 -dumpbase -
ignoring nonexistent directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../x86_64-apple-darwin22/include"
ignoring duplicate directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/../../../../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../include/c++/14"
ignoring duplicate directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/../../../../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../include/c++/14/x86_64-apple-darwin22"
ignoring duplicate directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/../../../../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../include/c++/14/backward"
ignoring duplicate directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/../../../../lib/gcc/current/gcc/x86_64-apple-darwin22/14/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/local/include"
ignoring duplicate directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/../../../../lib/gcc/current/gcc/x86_64-apple-darwin22/14/include-fixed"
ignoring nonexistent directory "/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/../../../../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../x86_64-apple-darwin22/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../include/c++/14
/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../include/c++/14/x86_64-apple-darwin22
/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/../../../../../../include/c++/14/backward
/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/include
/usr/local/Cellar/gcc/14.2.0_1/bin/../lib/gcc/current/gcc/x86_64-apple-darwin22/14/include-fixed
/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/System/Library/Frameworks
End of search list.
getting:
/usr/local/Cellar/cmocka/1.1.7/include/cmocka.h:2211:11: error: unknown type name 'size_t' 2211 | const size_t number_of_tests; | ^~~~~~ /usr/local/Cellar/cmocka/1.1.7/include/cmocka.h:1:1: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>' +++ |+#include <stddef.h> 1 | /* /usr/local/Cellar/cmocka/1.1.7/include/cmocka.h:2244:8: error: unknown type name 'jmp_buf' 2244 | extern jmp_buf global_expect_assert_env; | ^~~~~~~ /usr/local/Cellar/cmocka/1.1.7/include/cmocka.h:2270:11: error: unknown type name 'size_t' 2270 | const size_t number_of_values, const int count); | ^~~~~~ /usr/local/Cellar/cmocka/1.1.7/include/cmocka.h:2270:11: note: 'size_t' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>' /usr/local/Cellar/cmocka/1.1.7/include/cmocka.h:2274:11: error: unknown type name 'size_t' 2274 | const size_t number_of_values, const int count);
Tried to reorder the headers; VS code automatically orders it as above.
How to fix this?
According to a comment in cmocka.h
itself,
/**
* @defgroup cmocka The CMocka API
*
* These headers or their equivalents MUST be included prior to including
* this header file.
* @code
* #include <stdarg.h>
* #include <stddef.h>
* #include <setjmp.h>
* #include <stdint.h>
* @endcode
*
* This allows test applications to use custom definitions of C standard
* library functions and types.
*
* @{
*/
So your includes should look like
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
As you might guess from the @
markup, the documentation generator they're using also embeds this in the generated docs. But the documentation isn't organized very well, so it was a lot easier to find the comment than the corresponding section of the docs. For reference, the corresponding section of the docs is most of the way down this page. It doesn't even say what "this header file" is. (Also stdint.h
is in the list twice in the docs for some reason.)