Search code examples
cunit-testingcunit

CUnit assertion Assertion `((void *)0) != f_pCurSuite' failed


My code looks like this:

#include <CUnit/CUnit.h>


int maxi(int i1, int i2)
{
    return (i1 > i2) ? i1 : i2;
}

void test_maxi(void)
{
    CU_ASSERT(maxi(0,2) == 2);
}

int main() {
    test_maxi();
    return 0;
}

I compiled it using gcc test.c -o test -lcunit on Ubuntu.

I get this error when trying to launch it:

test: TestRun.c:159: CU_assertImplementation: Assertion `((void *)0) != f_pCurSuite' failed. Aborted (core dumped)

What does it mean? I found nothing about it on the internet.


Solution

  • CUnit works on test suites, you need to create before you can run the application.

    A very basic way to make your test to work is like the following:

    #include <CUnit/CUnit.h>
    #include <CUnit/Basic.h>
    
    int maxi(int i1, int i2)
    {
        return (i1 > i2) ? i1 : i2;
    }
    
    void test_maxi(void)
    {
        CU_ASSERT(maxi(0,2) == 2);
    }
    
    int main() {
        CU_initialize_registry();
        CU_pSuite suite = CU_add_suite("maxi_test", 0, 0);
    
        CU_add_test(suite, "maxi_fun", test_maxi);
    
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
        CU_cleanup_registry();
    
        return 0;
    }
    

    without all the required checks, but as Joachim Pileborg suggested in the comments, it's safer to follow the example code provided.