cmocka free operation and catching exceptions

1.5k Views Asked by At

I started my adventure with cmocka library, and I have two questions.

  1. Is it possible to find out if free() operation was made correctly? I mean, I would like to test function which is cleaning up tree structure. I've read about test_free(), but honestly I don't understand idea behind that.

  2. The second thing is case of catching standard library exceptions. I know about function expect_assert_failure, but how to use it? For example I would to do something what will throw segmentation fault, but I would like to pass test anyway.

2

There are 2 best solutions below

0
MC93 On BEST ANSWER

I'd suggest just doing an additonal test with valgrind.

valgrind --error-exitcode=1 ./test

Without the option valgrind would always return the same exit code returned by your test program. This way if your test program succeeds, but valgrind's memory check reveals errors, it will return 1 to indicate an error.

0
asn On

You need to add

#define UNIT_TESTING 1

before you include the cmocka.h header file, then malloc, realloc and free get overridden and will warn you about memory leaks.

expect_assert_failure() if for checking that an assert() condition is really hit.