Skip to content

Commit

Permalink
Use destructor attribute instead of manual cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
madmann91 committed Apr 9, 2024
1 parent 83cdd4f commit b5f61c8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/overture/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ static bool summarize_tests(bool disable_colors) {
return !failed;
}

void cleanup_tests(void) {
[[gnu::destructor]]
static void cleanup_tests(void) {
test_vec_destroy(&tests);
memset(&tests, 0, sizeof(struct test_vec));
}
Expand Down
5 changes: 1 addition & 4 deletions src/overture/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/// is disabled by default, use @ref filter_test to enable it based on filters.
#define TEST(name) \
void test_##name(struct test_context*); \
__attribute__((constructor)) void register_##name() { register_test(#name, test_##name); } \
[[gnu::constructor]] void register_##name() { register_test(#name, test_##name); } \
void test_##name([[maybe_unused]] struct test_context* context)

/// Asserts that the given condition is `true`. Fails the test if it is not the case.
Expand Down Expand Up @@ -60,9 +60,6 @@ bool run_tests(bool disable_colors);
/// @param argv Prefixes to use for matching.
void filter_tests(int argc, char** argv);

/// Cleanup the memory associated with the tests.
void cleanup_tests(void);

/// Prints available test names, separated by new lines, on the given stream.
void print_tests(FILE*);

Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ target_link_libraries(unit_tests PRIVATE

add_test(NAME unit_tests COMMAND unit_tests)
add_test(NAME unit_tests_filter COMMAND unit_tests cli set str)
add_test(NAME unit_tests_list COMMAND unit_tests --list)
add_test(NAME unit_tests_usage COMMAND unit_tests -h)
set_tests_properties(unit_tests_usage unit_tests_list PROPERTIES WILL_FAIL TRUE)
add_custom_target(memcheck
COMMAND ${CMAKE_CTEST_COMMAND} -T memcheck
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
Expand Down
4 changes: 1 addition & 3 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ int main(int argc, char** argv) {
return 1;

filter_tests(argc, argv);
int status = run_tests(options.disable_colors) ? 0 : 1;
cleanup_tests();
return status;
return run_tests(options.disable_colors) ? 0 : 1;
}

0 comments on commit b5f61c8

Please sign in to comment.