r/programmerchat • u/gxm492lor • Mar 10 '19
Testing any complex program completely is practically impossible
Someone made this argument after a staff meeting a few days ago. What's wrong with this argument?
- Every IF statement in a program doubles the number of possible states of the program (ignoring time)
- Which means every IF statement doubles the number of test conditions
- A 1 million line program might, conservatively estimating, have 100k IF statements (conditionals)
- That is 2100000 which is more seconds than have elapsed since the beginning of the universe.
- No project has 2100000 seconds to test
- So complete test coverage of complex programs is impossible
1
Upvotes
5
u/[deleted] Mar 10 '19
A computer doesn't evaluate one conditional per second...
4
u/Iskendarian Mar 10 '19
The implicit assumption is that you need to integration test every permutation all the way through, by multiplying them to get your number of test cases. What we do in the real world is unit test each component, which means we add the number of possibilities, which is an achieveable target.
Software and unit tests are both written by imperfect humans, so we still need integration testing to check unintended assumptions about the environment that the software is run in and because people do miss interactions when writing unit tests, but having both gets you a lot further than one or the other will.
4
u/Profix Mar 10 '19
Based on your title I thought "resonable", based on his arguments; ridiculous.
If it's possible for a developer to write an if statement, how can it be impossible for a test to evaluate it?
Close to every branch of a program should ideally have a unit test.
Testing the real world scenarios a program will have to deal with is much harder to test, and it is likely impractical to have an integration test for each scenario. You should have integration tests for the scenarios you explicitly intend to support though.
2
u/bluefootedpig Mar 10 '19
The doubling would only make sense if it was a parameter maybe that really did change entire branches of code. If you organizing it in a OOD manner, then it isn't that much.
I write if statements to check for a connection being open. But that doesn't branch my code two different ways, it is a check and notification to the user of a fail state.
Also, If statements aren't that common, or at least shouldn't be. In OOD we use polymorphism to allow objects to manage that complexity.
Here is an example that might prove it more wrong. Take your basic switch statement. Say you have an Enum to get determine if the value should be "max", "min", "average". That is 3 if statements, but only 3 states. If it doubled each time, then that is 4 branches? but that isn't true. What if you have more states, mean, mode, high resolution, low resolution, etc. For the switch, it is maybe 10 if statements but that is for 10 states.
1
u/[deleted] Jun 08 '19
The problem with your thinking is this:
Your math is making the assumption that for ever if statement it is possible to still touch every other single if statement in the program. If the first if switch is as simple as "user chooses game else user exits program" then hitting that "exits program" is the end of that if and you don't have to test the other 99,999 IF statements based on the first one being false.
simple fact is most of those 100k if statements are not dependent on what the other 100k have done. So ultimately you are talking about testing closer to 100k, not 2100000
2
u/zfundamental Mar 11 '19
'coverage' is typically defined in that every line of code is executed, not every combination of branches are executed. If you use the former definition and a reasonable per branch test time of perhaps 100us, then the total test time would be in the ballpark of 10 seconds, rather than exceeding the life of the universe.
For testing you want to establish when there are interesting relationships between conditional blocks (doesn't matter if they're if statements, function pointers, subclass instantiations, etc) and test those scenarios. Sometimes you'll get things wrong as the space to test programs in is absurdly large when you think of it in an 'all-possible-states' view rather than a test coverage view. As such, tools like fuzzing libs/applications can be helpful in exposing parts of the program state which are not explored by existing tests.
8
u/[deleted] Mar 10 '19
[removed] — view removed comment