Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am wondering, What would be the correct condition coverage test cases for the following condition:

if(A && E && (B || C || D))

Considering short circuiting, what cases would I need?

EDIT: Sorry I put only a half of the expression

share|improve this question
    
if you want it easier then split up the condition... –  ratchet freak Feb 22 '13 at 14:21
    
rathchet freak: Well this is how is the program written. Are my cases correct? –  user970696 Feb 22 '13 at 14:25
add comment

4 Answers

up vote 1 down vote accepted

Short answer is yes.

There are many more cases but since you are taking into account short circuiting, then only need to consider one case where A is False and only one case in which A is True and B is True and you don't need to bother checking C. The only time you need to check all three values is if A is True and B is False.

EDIT** With the new expression (A && E && (B || C || D)) With this new expression and still taking into account short cuircuiting, you will only be concerned with the instances where both A is true and E is true. From there, you only need to test B || C || D in that order until you find a true value. So these instances:

T--
FT-
FFT

So D will only be checked if B and C are False. C will only be checked in B is false. If B is true, no need to check any further.

share|improve this answer
add comment

Assuming that each of the values is independent of the other (that the value of B does not depend on the value A), then the minimum number of test cases is (true values are cap, false are lower case):

  • a
  • Ae
  • AEB
  • AEbC
  • AEbcD
  • AEbcd

The difficulty with implementing just the minimum number is that if the code changes (extract method refactoring on the conditional) but the business logic does not, it may miss an incorrect result.

share|improve this answer
    
+1 test for all possible (reasonable) combinations not just coverage –  ratchet freak Feb 23 '13 at 0:54
    
ratchet freak: that is what condition coverage is about..to test both outcomes of all conditions. –  user970696 Feb 23 '13 at 11:24
add comment

In the general case you'll need to check every combination. The code under the if may depend on any number of variables, whose state is reflected in tests A-E. What if variable x is set whenever condition C is true, but the execution path through condition D doesn't set it? What if calling D has side-effects itself that affect the conditional block?

In practice, this kind of code may encapsulate small commonalities between diverse execution paths, and it's hard to get all paths to set up the state correctly. So mistakes of this sort are likely to happen.

share|improve this answer
add comment

i think when both of (a,e) must be true and any one of b,c,d must be true so when any of a,e is false everything is false. when both a,e are true and any one of b,c,d must be true then result is false. if all b,c,d is false then the result is false.

share|improve this answer
    
Your answer could be strengthened by explicitly stating "You need test cases for _, _, _ conditions" after your logic explanation. –  GlenH7 Mar 1 '13 at 19:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.