You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

42 lines
1.4 KiB

  1. from nilmdb import Interval, IntervalSet, IntervalException
  2. from datetime import datetime
  3. from nose.tools import assert_raises
  4. import itertools
  5. def test_interval():
  6. """Test the Interval class"""
  7. start = datetime.strptime("19801205","%Y%m%d")
  8. end = datetime.strptime("20110216","%Y%m%d")
  9. # basic construction
  10. i = Interval(start, end)
  11. assert(i.start == start)
  12. assert(i.end == end)
  13. # end before start
  14. assert_raises(Exception, Interval, end, start)
  15. def test_interval_intersect():
  16. """Test Interval intersections"""
  17. dates = [ datetime.strptime(year, "%y") for year in [ "00", "01", "02", "03" ] ]
  18. perm = list(itertools.permutations(dates, 2))
  19. prod = list(itertools.product(perm, perm))
  20. should_intersect = {
  21. False: [4, 5, 8, 20, 48, 56, 60, 96, 97, 100],
  22. True: [0, 1, 2, 12, 13, 14, 16, 17, 24, 25, 26, 28, 29,
  23. 32, 49, 50, 52, 53, 61, 62, 64, 65, 68, 98, 101, 104]}
  24. for i,((a,b),(c,d)) in enumerate(prod):
  25. try:
  26. i1 = Interval(a, b)
  27. i2 = Interval(c, d)
  28. assert(i1.intersects(i2) == i2.intersects(i1))
  29. assert(i in should_intersect[i1.intersects(i2)])
  30. except IntervalException:
  31. assert(i not in should_intersect[True] and
  32. i not in should_intersect[False])
  33. def test_intervalset():
  34. """Test interval sets"""
  35. #iset = IntervalSet("hi")