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);
  • 3
    rename one of them. You can't link two identically defined different functions into one binary. – littleadv Feb 04 '22 at 18:12
  • I expect this is theoretically impossible to do completely, for the classic Halting Problem reasons. – Eric Postpischil Feb 04 '22 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). – Eugene Sh. Feb 04 '22 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, ... – luk2302 Feb 04 '22 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. – Eric Postpischil Feb 04 '22 at 18:19
  • You could use 2 different names for each implementation. So you can check that they gives same results. Then for each one create an interface file that will define a macro f that call one specific implementation. So for testing you use real name, then you can choose which implementation using the implementation interface you want. – Ptit Xav Feb 04 '22 at 18:23
  • @GoldCredential What's your actual question? Are you trying to figure out a necessary/sufficient set of test cases to try your two implementations on? Or are you trying to figure out how to call two functions with the same name in the same program? (So far, some of the comments here have addressed one of those questions, some the other.) – Steve Summit Feb 04 '22 at 18:25
  • @SteveSummit Latter. –  Feb 04 '22 at 18:27
  • There are various ways of linking identically named functions into an executable. They depend on the platform you are using and particular requirements you have. One method is [here](https://stackoverflow.com/questions/48881047/how-to-link-a-project-to-two-different-versions-of-the-same-c-static-library/48885000#48885000). The partial linking described there could be used to wrap one function `f` into another function `g`, and then the `main` routine could get the separate functions by calling one `f` directly or calling the wrapper `g` to get the other `f`. – Eric Postpischil Feb 04 '22 at 18:43
  • @GoldCredential Far and away the easiest way, as littleadv already suggested, would simply be to (temporarily) rename one of the functions, assuming you'e allowed to modify imp1.c or imp2.c. – Steve Summit Feb 04 '22 at 18:44
  • But, if your question is how to link two functions of the same name into a program, why does your question ask about checking if they agree? – Eric Postpischil Feb 04 '22 at 18:45
  • @littleadv: Re “You can't link two identically defined different functions into one binary”: Do not mistake “The C standard does not provide a way to do this” for “This cannot be done.” It is entirely possible to link two functions of the same name into one executable file. – Eric Postpischil Feb 04 '22 at 18:48
  • @EricPostpischil well, I'm sure you can put both binary blobs in, but how would the compiler determine which one to call? Somewhere somehow they will be tagged differently one way or another and it has to be defined in code (namespaces? scopes? using parameters added to the function name by the compiler? by original source file? don't know, but some renaming will have to happen somewhere) – littleadv Feb 04 '22 at 18:51
  • I assume the pseudocode was supposed to be something more like: `for(i = 0; i < infinity; i++) if(imp1.f(i) != imp2.f(i)) printf("The implementations differ on input %d\n", i);` – Steve Summit Feb 04 '22 at 18:51
  • @littleadv The source code would say something like `if(f() != g())`. But then you'd use linker magic to arrange that function `f` was function `f` from `one.o`, but function `g` was function `f` from `two.o`. – Steve Summit Feb 04 '22 at 18:54
  • @littleadv: One way is you link them separately, per [the question](https://stackoverflow.com/questions/48881047/how-to-link-a-project-to-two-different-versions-of-the-same-c-static-library/48885000#48885000) I linked to above. Another way is you build one or both of them into a dynamic library that adds another level of namespace when linking, as Apple does. – Eric Postpischil Feb 04 '22 at 18:55

0 Answers0