Compare two C implementations of the same function

c

Solution

- Compile `implem_1/f.c` with the a preprocessor macro `-Df=f_1`.

- Compile `implem_2/f.c` with the a preprocessor macro `-Df=f_2`.

Change the driver file to:

#define f f_1
#include "implem_1/f.h"
#undef f
#define f f_2
#include "implem_2/f.h" /* include second implem f_2 of f */

for(x=0; x<1000000; ++x) {
    if(f_1(x)!=f_2(x)) {
         printf("Implementations do not match\n");
         break;
     }
}

Problem

Assume I have two folders, `implem_1` and `implem_2`, each containing one file `f.c` implementing the same function f in two different ways and the corresponding header `f.h`. The function f takes one parameter x. I would like to compare the evalutations of the two functions in the two folders over many x values, to test whether the implementations match. The code would be look like this, at the exception that the header files do not define `f_1` and `f_2`, but twice `f`. ``` #include "implem_1/f.h" /* include first implem f_1 of f */ #include "implem_2/f.h" /* include second implem f_2 of f */ for(x=0; x<1000000; ++x) { if(f_1(x)!=f_2(x)) { printf("Implementations do not match\n"); break; } } ``` How can I achieve this without modify anything in the two folders `implem_1` and `implem_2`?

Original source