How to structure static function tests in C?

I’m having trouble compiling any kind of reasonable structure for unit-testing a module’s helper/static functions. Nearly all of this module is static functions and it has many of them, so I’m trying not to put all my tests in the same file. The specific (large number of) error is:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11

But I’m interested in a general approach that will compile.

From the command line, first install Cunit:

# Install cunit
sudo apt-get install libcunit1 libcunit1-doc libcunit1-dev

In module_a.c:

#define UNIT_TEST
#include <stdio.h>
#ifndef UNIT_TEST
int main(void)
{
  // Do the real thing
  printf("The number 42: %dn", get_42());
  printf("The number 0: %dn", get_0());

  return 0;
}
#endif

static int32_t get_42(void)
{
  return 42;
}

static int32_t get_0(void)
{
  return 42;
}

In module_a_tests.c:

#define UNIT_TEST
#include "module_a.c"
#include "CUnit/Basic.h"
#ifdef UNIT_TEST

int set_up(void)
{
  return 0;
}

int tear_down(void)
{
  return 0;
}

void run_good_fn(void)
{
  CU_ASSERT(42 == get_42());
}

void run_bad_fn(void)
{
  CU_ASSERT(0 == get_0());
}

int main(void)
{
  CU_pSuite p_suite = NULL;

  // Initialize
  if (CU_initialize_registry() != CUE_SUCCESS) {
    return CU_get_error();
  }

  p_suite = CU_add_suite("First Suite", set_up, tear_down);
  if (p_suite == NULL) {
    goto exit;
  }
  CU_basic_set_mode(CU_BRM_VERBOSE);

  // Add tests
  if (CU_add_test(p_suite, "Testing run_good_fn", run_good_fn) == NULL) {
    goto exit;
  }

  if (CU_add_test(p_suite, "Testing run_bad_fn", run_bad_fn) == NULL) {
    goto exit;
  }

  // Run the tests
  CU_basic_run_tests();

  exit:
    CU_cleanup_registry();

  return CU_get_error();
}
#endif

Related:

How to test a static function


Source: unix

Leave a Reply