Test Setup

Unit tests often need to perform the same tasks to initialize tests or to clean up after they have executed. To centralize this you can define setup and clean methods for each test suite. If the test suite calls sub suites, that also have setup or clean methods, they are chained, as you would expect it.

The following test setup and clean methods verify, that you have freed all your memory and obc-object resources after each test. Protecting your top level test suite like this, is useful to prevent memory leaks.

tests.c

    #include "../obc/obc.h"
    
    
    /*******************************************************************************
     test set up
    *******************************************************************************/
    
    static int allocatedBlocks;
    static int objRegisterCount;
    
    static void test_init() {
        obj_resetSystem();
        allocatedBlocks = HEAP_COUNTBLOCKS();
        objRegisterCount = obj_getRegisterCount();
    }
    
    static void test_clean() {
        test(objRegisterCount == obj_getRegisterCount(), "object registrations not properly freed");
        obj_resetSystem();
        test(allocatedBlocks == HEAP_COUNTBLOCKS(), "memory not properly freed");
    }
    
    
    /*******************************************************************************
     test suite
    *******************************************************************************/
    
    Test car_test();
    Test tunedcar_test();
    Test boat_test();
    
    Test tests() {
        Test t = test_create("ObcDemo", test_init, test_clean);
        test_addTest(t, car_test() );
        test_addTest(t, tunedcar_test() );
        test_addTest(t, boat_test() );
        return t;
    }