0

I had the following bit of code to determine if 2 periods intersect and wanted to prove it correct:

public static bool Intersect(this DateTime start1, DateTime end1, DateTime start2, DateTime end2)
{
    return (start1 == start2) || (start1 > start2 ? start1 <= end2 : start2 <= end1);
}

After a couple of attempts the best I could come up with is throwing a bunch of hardcoded values into it.

In the process of doing that research I discovered the SO answer on this problem which uses the magnificently simple syntax of

return (StartA <= EndB) and (EndA >= StartB) 

Either way could somebody tell me what would be the most effective way to unit test a date intersect method?

Community
  • 1
  • 1
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
  • This is an odd question ... you're not happy with some hardcoded values, so you're asking for a general solution to the problem. But once you have the general solution, why not just code the function correctly and skip the unit test? – McGarnagle Apr 25 '12 at 02:25

2 Answers2

1

From the description you have two date ranges

1) Start1...End1

2) Start2...End2

and you want to see if they overlap

So, what are the possibilities

A) Error cases

End1 < Start1
End2 < Start2

B) Exact overlap

Start1 == Start2, End1 == End2, Start1 == End1
Start1 == Start2, End1 == End2, Start1 < End1

C) No Overlap

Start1 < End1 < Start2 < End2
Start1 == End1 < Start2 < End2
Start1 < End1 < Start2 == End2
Start1 == End1 < Start2 == End2

D) Full overlap

Start1 == Start2 < End2 < End1
Start1 == Start2 == End2 < End1
Start1 < Start2 < End2 < End1
Start1 < Start2 == End2 < End1
Start1 < Start2 < End2 == End1
Start1 < Start2 == End2 == End1

E) Partial overlap

Start1 < Start2 < End1 < End2

I think that is the lot. It assumes that the ranges are in order (Start1 <= Start2).
You might need to double up the cases, passing in the second ranges first. The answers should still be the same (overlap is not order dependent) but it is possible that the implementation implicitly depends upon the (Start1 <= Start2) and fails if the dates are entered the other way.

Each of the above becomes a test case.
NOTE: if 0 length (start == end) ranges are not allowed, they become an error case and can be removed from other sections.

It's a lot of test cases for a simple function but you have 4 variables (each date) and a set of relationships between them and each combination of relationshships gives a different answer.

You need to decide if a full exaustive testing of this worth the effort or will a selection of the most common and 'most likely to cause the algorithm problems' (heavily subjective) cases suffice.

I hope this is what you were looking for and is useful,
Alan.

AlanT
  • 3,627
  • 20
  • 28
0

You should be testing your method with all combinations of input values that are possible. This way, not only are you testing valid input, but invalid input as well. Use sample input values that describe valid (expected) input as well as invalid (unexpected, but possible) input. This is how testing should be done in general so that one can prove that their code works as they intend it to.

Bernard
  • 7,908
  • 2
  • 36
  • 33