Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two C implementations of the same function

Tags:

c

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?

like image 649
wwjoze Avatar asked Jul 12 '26 05:07

wwjoze


1 Answers

  1. Compile implem_1/f.c with the a preprocessor macro -Df=f_1.
  2. Compile implem_2/f.c with the a preprocessor macro -Df=f_2.
  3. 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;
         }
    }
    
like image 53
R Sahu Avatar answered Jul 13 '26 19:07

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!