How to test implementation details when there are multiple ways of implementing the algorithm, without reading the code?

I want to make sure that the code that the written code is precisely doing what it is expected to do.

For example, if I want to search in an array particularly using linear search, I don't want binary search to pass the test. Unit test to test the method "search" would pass irrespective of the implementation which is not what I want. (I understand that binary search won't work if the data is unsorted, but lets say the data is sorted and I still want to make sure that it is a linear search and not the binary. In this case, I know that for sufficiently large input, BS would be way quicker than LS, but I want a generic answer where there might not be considerable difference in performance.)

Is there a way to achieve this?

An idea comes to my mind - Divide the method in extremely small submethods and make sure that all the submethods are implemented correctly and are used in main method. However this is not desirable, this might not be feasible always.

• What do you want to do? Check if the code is well-written requires reading the code. I just don't see any way around it. – vonbrand Sep 13 '19 at 13:40

To find out if it is e.g. doing binary search (or using hashing, or whatever) inside, you have to look at what it is doing. No way around it.

Why would you insist on a particular algorithm? The whole point of modularity (and thus of much of modern programming languages and techniques) is to be able to hide such irrelevant details, and have users depend only on observable behaviour (so you can go back tomorrow and replace a simple, inefficient algorithm by a complex, efficient one, and noone is the wiser).

• You are correct about modularity. Think in terms of students. If someone only know, say merge sort and not quick sort. As long as the sort() method is needed, that person would get away with merge sort everytime. But there would be some problems where knowledge of quick sort would be critical. That person would struggle there. Thats something I want to avoid. In general, knowing internal details help in tweaking some things in future. Hence, sometimes its desirable that the implementation is in some specific way. – user3903448 Sep 5 '19 at 4:40
• If you want a specific añgorithm, read the code and check. – vonbrand Sep 7 '19 at 19:45
• Don't be salty please. The title of the question itself says "without reading the code". Of course, if there is no way to do it, one can always read the code. – user3903448 Sep 11 '19 at 18:15
• Best you can do is to request some "efficiency" measure, i.e., the algorithm has to sort with $O(n \log n)$ assignments/comparisons, instrument the data sorted, and run it on some (selected) examples. But that won't let you distinguish e.g. mergesort and quicksort. And they could cheat by sorting off-line by bubblesort and permuting into the result. – vonbrand Sep 13 '19 at 13:39
• Thanks for your input. Yeah, I can see that there just doesn't seem to be an automated way. One close enough way might be asking to write unit testable sub methods which combine to the whole algorithm. – user3903448 Sep 14 '19 at 21:34