Skip to content
Advertisement

Host testing C program with hard coded memory addresses

We will write functional/unit tests for C code. This C program will be run as embedded software. However we need to run tests on a Linux environment.

The problem is that parts of the code under test looks like this:

my_addresses.h:

JavaScript

my_code.c

JavaScript

Obviously, this will not run so well on Linux host environment.

Is there some way we can work around this issue during testing? Can we somehow “redirect” the program to use other addresses, that are valid addresses to memory allocated during test procedures?

Our first attempt was to replace “my_addresses.h” with another header file during tests, which (extern) declares variables instead of hard defines – then assign malloc’d memory to MY_BASE_ADDRESS, etc. The problem with that is the “static const” declaration in the c file. Of course you cannot assign a variable to a static const type.

Preferably, we should not modify the code under test (although in the worst case it may come to that).

Advertisement

Answer

You could check for e.g. the __linux__ macro and use conditional compilation. When on Linux use an array as base, and make it big enough to keep all the data needed in it.

Something like e.g.

JavaScript
Advertisement