0

I have a header interface.h with a function f() and its two implementations, imp1.c and imp2.c. Both imp1.c, and imp2.c contain definitions for f(). I am checking f() with random inputs, but I want to make sure that the outputs from both imp1.c and imp2.c agree. How can I do that? Is there any way my main function can distinguish between the two implementations? Like

if(imp1.f() != imp2.f()) printf("The implementations differ on input %d", i); 
16
  • 3
    rename one of them. You can't link two identically defined different functions into one binary. Commented Feb 4, 2022 at 18:12
  • I expect this is theoretically impossible to do completely, for the classic Halting Problem reasons. Commented Feb 4, 2022 at 18:14
  • This would be only possible if you can exhaustively run them with every possible arguments combination (and this is only in case of pure functions). Commented Feb 4, 2022 at 18:16
  • No, you cannot be sure two methods behave in the same way. Even if you called them with all possible inputs they could be coded in a way to return something entirely the moment it is called with any input for a second time, or if is currently a Wednesday or it is rainy outside, ... Commented Feb 4, 2022 at 18:18
  • Suppose we have some function Q(f1, f2) that returns true if and only iff f1 and f2 return the same result for all inputs. Write f2: Given input x, if Q(f1, f2) is true, return !!f1(x). Otherwise, return f1(x). Then Q either must be broken or must never return in some cases, because, if it says f1 and f2 always return the same result, f2 returns a result different from f1, and, if it says they do not always return the same result, f2 returns the same result as f1. Commented Feb 4, 2022 at 18:19

0