Skip to main content

C

Tests

Tests are to be written with the dedicated CUnit test framework, which is located in the repository. First, the framework must be included in the test file:

my_test.c
#include "CUnit.h"

Then, the test file must define a main function:

my_test.c
int main(int argc, char** args){

// Optional: declare all test cases in advance so that they appear in the analysis in case of
// infinite loops/segmentation faults - even if the first test case already leads to an abort
testDeclare("fac1");
testDeclare("eratosthenes 1");

// Start of the test case named fac1
// After each `testStart` and after each assert, the test result is persisted to the filesystem.
// If a test case runs into an infinite loop or a segmentation fault, the analysis is aborted.
// Thanks to the persistence, the test case in which this occurred can be identified.
// Note: setup logic that calls methods in the solution should preferably be run after testStart.
// Otherwise, it is not possible to trace later where the cause of the crash or the infinite loop
// lies.
testStart("fac1");
assertIntEq("Fakultät von 5", 120, factorial(5));

testStart("eratosthenes 1");
bool ar [10] = {false,false,false,false,false,false,false,false,false,false};
eratosthenes(ar,10);
// As soon as an assertion is triggered for a test case, all subsequent assertions are ignored
// until a new test case is opened with `testStart`. This behavior thus corresponds to that of
// JUnit.
assertTrue("array[0] muss true sein, denn 2 ist Primzahl",ar[0]);
assertTrue("array[1] muss true sein, denn 3 ist Primzahl",ar[1]);

// Do not forget to free the memory reserved by the test framework
shutdown();
return 0;
}

The following assertions can be used:

  • bool assertTrue(char* message, bool val);
  • bool assertIntEq(char* message, int expected, int found);
  • bool assertDoubleEq(char* message, double expected, double found);
  • bool assertStringEq(char* message,char* expected,char* this);

Memory Analysis

By including the file MemoryTest.h, calls to free as well as malloc and calloc are counted, and the result is provided as feedback after test execution.

MemoryTest.h
#include <stdlib.h>

extern unsigned int speicherAlloc;
extern unsigned int speicherFree;

#define free(m)((speicherFree++,free(m)))
#define malloc(m)((speicherAlloc++,malloc(m)))
#define calloc(m,s)((speicherAlloc++,calloc(m,s)))

Conventions

  • Source files must have the .c extension.
  • Header files can be named arbitrarily.
  • Only one of the files may have a main function.

Environments

Currently, the only available environment is c17, which includes GCC 12.