CUnit CU_register_suites(suites) error

413 views Asked by At

I'm testing this code with CUnit. I get this error message:

/bin/sh: line 1: 44477 Segmentation fault: 11 ./build/Debug/GNU-MacOSX/tests/TestFiles/f2

Where is fault? I can't see it. Can anybody tell me why I am getting this error.
Thanks in advance

Where is fault? I can't see it. Can anybody tell me why I am getting this error.
Thanks in advance

#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/TestDB.h>

int add(int a, int b) {
    return a + b;
}

int init_suite(void) {
    return 0;
}

int clean_suite(void) {
    return 0;
}

void test1() {
     CU_ASSERT(add(2, 2) == 4);
}

void test2() {
    CU_ASSERT(add(2, 3) == 5);
}

CU_TestInfo test_array1[] = {
    { "add/test1", test1},
    { "add/test2", test2},
    CU_TEST_INFO_NULL,
};


CU_TestInfo test_array2[] = {
    { "add/test3", test1},
    { "add/test4", test2},
    CU_TEST_INFO_NULL,
};


CU_SuiteInfo suites[] = {
    { "suit1", init_suite, clean_suite, test_array1},
    { "suit2", init_suite, clean_suite, test_array2},
    CU_SUITE_INFO_NULL,
};

int main() {

/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
    return CU_get_error();

/* Add a suite to the registry */
if (CUE_SUCCESS != CU_register_suites(suites)) {
    CU_cleanup_registry();
    return CU_get_error();
}

/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_basic_show_failures(CU_get_failure_list());
printf("\n");

CU_cleanup_registry();
return CU_get_error();
}
2

There are 2 answers

0
JLHwung On

If you come across this issue on a recent cunit version, i.e. 2.1, the segmentation fault should come from the ill-defined CU_SuiteInfo on your code snippet.

CU_SuiteInfo suites[] = {
    { "suit1", init_suite, clean_suite, test_array1},
    { "suit2", init_suite, clean_suite, test_array2},
    CU_SUITE_INFO_NULL,
};

A recent change added two new property pSetUpFunc and pTearDownFunc to the CU_SuiteInfo interface, thus you have to declare your suites like

int setup_suite(void) { return 0; }
int teardown_suite(void) { return 0; }

CU_SuiteInfo suites[] = {
    { "suit1", init_suite, clean_suite, setup_suite, teardown_suite, test_array1},
    { "suit2", init_suite, clean_suite, setup_suite, teardown_suite, test_array2},
    CU_SUITE_INFO_NULL,
};

IMO, they should have maintained the interface compatibility on a 2.1 minor release. But it was a change made 9 years ago. Also note that the document website is out-of-dated regarding to the interface change of CU_SuiteInfo.

0
George Rodionov On

The signature of setup_suite and teardown_suite must be:

void setup_suite(void) { return; }
void teardown_suite(void) { return; }