def test_span(self): if debug: print("Testing span") range1 = Range.closed(1., 3.) range2 = Range.closed(5., 7.) self.assertEqual(range1.span(range2), Range.closed(1., 7.)) range2 = Range.closed(2., 5.) self.assertEqual(range1.span(range2), Range.closed(1., 5.))
def test_get_bugfix1(self): if debug: print("Testing get under first bugfix") buckets = RangeBucketMap() buckets.put(Range.closed(67432367, 67434244), 'G') buckets.put(Range.closed(67432367, 67434244), 'T1') buckets.put(Range.closed(67432375, 67434015), 'T2') buckets_dict = dict((v, k) for k, v in buckets.iteritems()) self.assertEqual(buckets_dict['T2'], Range.closed(67432375, 67434015))
def test_get_bugfix1(self): if debug: print("Testing get under first bugfix") buckets = RangeBucketMap() buckets.put(Range.closed(67432367,67434244),'G') buckets.put(Range.closed(67432367,67434244),'T1') buckets.put(Range.closed(67432375,67434015),'T2') buckets_dict = dict((v,k) for k,v in buckets.iteritems()) self.assertEqual(buckets_dict['T2'], Range.closed(67432375,67434015))
def test_span(self): if debug: print("Testing span") range1 = Range.closed(1.,3.) range2 = Range.closed(5.,7.) self.assertEqual(range1.span(range2), Range.closed(1.,7.)) range2 = Range.closed(2.,5.) self.assertEqual(range1.span(range2), Range.closed(1.,5.))
def test_isConnected(self): if debug: print("Testing isConnected") range1 = Range.closed(2., 4.) range2 = Range.closed(5., 7.) self.assertFalse(range1.isConnected(range2)) range2 = Range.closed(3., 5.) self.assertTrue(range1.isConnected(range2)) range2 = Range.closed(4., 6.) self.assertTrue(range1.isConnected(range2))
def test_isConnected(self): if debug: print("Testing isConnected") range1 = Range.closed(2.,4.) range2 = Range.closed(5.,7.) self.assertFalse(range1.isConnected(range2)) range2 = Range.closed(3.,5.) self.assertTrue(range1.isConnected(range2)) range2 = Range.closed(4.,6.) self.assertTrue(range1.isConnected(range2))
def test_get(self): if debug: print("Testing get") buckets = RangeBucketMap() buckets.put(Range.closed(3,5),'a') buckets.put(Range.closed(7,10),'b') buckets.put(Range.closed(4,8),'c') self.assertEqual(buckets.get(6),set(['c'])) self.assertEqual(buckets.get(4),set(['a','c'])) self.assertEquals(buckets.get(Range.closed(0,20)),set(['a','b','c'])) self.assertEquals(buckets.get(Range.openClosed(5,8)),set(['b','c']))
def test_get(self): if debug: print("Testing get") buckets = RangeBucketMap() buckets.put(Range.closed(3, 5), 'a') buckets.put(Range.closed(7, 10), 'b') buckets.put(Range.closed(4, 8), 'c') self.assertEqual(buckets.get(6), set(['c'])) self.assertEqual(buckets.get(4), set(['a', 'c'])) self.assertEquals(buckets.get(Range.closed(0, 20)), set(['a', 'b', 'c'])) self.assertEquals(buckets.get(Range.openClosed(5, 8)), set(['b', 'c']))
def test_getDistanceFromPoint(self): if debug: print("Testing getDistanceFromPoint") range1 = Range.closed(1.,3.) self.assertAlmostEqual(range1.getDistanceFromPoint(1.),0.) self.assertAlmostEqual(range1.getDistanceFromPoint(2.),0.) self.assertAlmostEqual(range1.getDistanceFromPoint(3.),0.) self.assertAlmostEqual(range1.getDistanceFromPoint(100.),97.) self.assertAlmostEqual(range1.getDistanceFromPoint(0.99),0.01) range1 = Range.openClosed(1.,3.) with self.assertRaises(TypeError): range1.getDistanceFromPoint(0.99)
def test_getDistanceFromPoint(self): if debug: print("Testing getDistanceFromPoint") range1 = Range.closed(1., 3.) self.assertAlmostEqual(range1.getDistanceFromPoint(1.), 0.) self.assertAlmostEqual(range1.getDistanceFromPoint(2.), 0.) self.assertAlmostEqual(range1.getDistanceFromPoint(3.), 0.) self.assertAlmostEqual(range1.getDistanceFromPoint(100.), 97.) self.assertAlmostEqual(range1.getDistanceFromPoint(0.99), 0.01) range1 = Range.openClosed(1., 3.) with self.assertRaises(TypeError): range1.getDistanceFromPoint(0.99)
def test_openClosed(self): if debug: print("Testing openClosed") # Floats floatRange = Range.openClosed(2.,5.) self.assertFalse(floatRange.contains(1.99)) self.assertFalse(floatRange.contains(2.)) self.assertTrue(floatRange.contains(3.)) self.assertTrue(floatRange.contains(5.)) self.assertFalse(floatRange.contains(5.01)) # Letters letterRange = Range.openClosed('b','e') self.assertFalse(letterRange.contains('a')) self.assertFalse(letterRange.contains('b')) self.assertTrue(letterRange.contains('c')) self.assertTrue(letterRange.contains('e')) self.assertFalse(letterRange.contains('f'))
def test_greaterThan(self): if debug: print("Testing greaterThan") # Floats floatRange = Range.greaterThan(5.) self.assertFalse(floatRange.contains(1.99)) self.assertFalse(floatRange.contains(2.)) self.assertFalse(floatRange.contains(3.)) self.assertFalse(floatRange.contains(5.)) self.assertTrue(floatRange.contains(5.01)) # Letters letterRange = Range.greaterThan('e') self.assertFalse(letterRange.contains('a')) self.assertFalse(letterRange.contains('b')) self.assertFalse(letterRange.contains('c')) self.assertFalse(letterRange.contains('e')) self.assertTrue(letterRange.contains('f'))
def test_openClosed(self): if debug: print("Testing openClosed") # Floats floatRange = Range.openClosed(2., 5.) self.assertFalse(floatRange.contains(1.99)) self.assertFalse(floatRange.contains(2.)) self.assertTrue(floatRange.contains(3.)) self.assertTrue(floatRange.contains(5.)) self.assertFalse(floatRange.contains(5.01)) # Letters letterRange = Range.openClosed('b', 'e') self.assertFalse(letterRange.contains('a')) self.assertFalse(letterRange.contains('b')) self.assertTrue(letterRange.contains('c')) self.assertTrue(letterRange.contains('e')) self.assertFalse(letterRange.contains('f'))
def test_atMost(self): if debug: print("Testing atMost") # Floats floatRange = Range.atMost(5.) self.assertTrue(floatRange.contains(1.99)) self.assertTrue(floatRange.contains(2.)) self.assertTrue(floatRange.contains(3.)) self.assertTrue(floatRange.contains(5.)) self.assertFalse(floatRange.contains(5.01)) # Letters letterRange = Range.atMost('e') self.assertTrue(letterRange.contains('a')) self.assertTrue(letterRange.contains('b')) self.assertTrue(letterRange.contains('c')) self.assertTrue(letterRange.contains('e')) self.assertFalse(letterRange.contains('f'))
def test_lessThan(self): if debug: print("Testing lessThan") # Floats floatRange = Range.lessThan(5.) self.assertTrue(floatRange.contains(1.99)) self.assertTrue(floatRange.contains(2.)) self.assertTrue(floatRange.contains(3.)) self.assertFalse(floatRange.contains(5.)) self.assertFalse(floatRange.contains(5.01)) # Letters letterRange = Range.lessThan('e') self.assertTrue(letterRange.contains('a')) self.assertTrue(letterRange.contains('b')) self.assertTrue(letterRange.contains('c')) self.assertFalse(letterRange.contains('e')) self.assertFalse(letterRange.contains('f'))
def test_atLeast(self): if debug: print("Testing atLeast") # Floats floatRange = Range.atLeast(5.) self.assertFalse(floatRange.contains(1.99)) self.assertFalse(floatRange.contains(2.)) self.assertFalse(floatRange.contains(3.)) self.assertTrue(floatRange.contains(5.)) self.assertTrue(floatRange.contains(5.01)) # Letters letterRange = Range.atLeast('e') self.assertFalse(letterRange.contains('a')) self.assertFalse(letterRange.contains('b')) self.assertFalse(letterRange.contains('c')) self.assertTrue(letterRange.contains('e')) self.assertTrue(letterRange.contains('f'))
def test_overlaps(self): if debug: print("Testing overlaps") theMap = RangeMap() theMap.put(Range.closed(3,5),'foo') theMap.put(Range.closed(7,10),'bar') self.assertTrue(theMap.overlaps(4)) self.assertFalse(theMap.overlaps(Range.closedOpen(1,3))) self.assertFalse(theMap.overlaps(Range.open(5,7))) self.assertFalse(theMap.overlaps(2)) self.assertTrue(theMap.overlaps(3)) self.assertTrue(theMap.overlaps(10)) self.assertTrue(theMap.overlaps(Range.closed(4,5))) self.assertTrue(theMap.overlaps(Range.closed(8,9))) self.assertTrue(theMap.overlaps(Range.closed(1,4))) self.assertFalse(theMap.overlaps(Range.closed(6,6))) self.assertTrue(theMap.overlaps(Range.closed(8,12))) self.assertTrue(theMap.overlaps(Range.closed(1,12)))
def test_overlaps(self): if debug: print("Testing overlaps") theMap = RangeMap() theMap.put(Range.closed(3, 5), 'foo') theMap.put(Range.closed(7, 10), 'bar') self.assertTrue(theMap.overlaps(4)) self.assertFalse(theMap.overlaps(Range.closedOpen(1, 3))) self.assertFalse(theMap.overlaps(Range.open(5, 7))) self.assertFalse(theMap.overlaps(2)) self.assertTrue(theMap.overlaps(3)) self.assertTrue(theMap.overlaps(10)) self.assertTrue(theMap.overlaps(Range.closed(4, 5))) self.assertTrue(theMap.overlaps(Range.closed(8, 9))) self.assertTrue(theMap.overlaps(Range.closed(1, 4))) self.assertFalse(theMap.overlaps(Range.closed(6, 6))) self.assertTrue(theMap.overlaps(Range.closed(8, 12))) self.assertTrue(theMap.overlaps(Range.closed(1, 12)))
def test_overlaps(self): if debug: print("Testing overlaps") theSet = RangeSet() theSet.add(Range.closed(3,5)) theSet.add(Range.closed(7,10)) self.assertTrue(theSet.overlaps(4)) self.assertFalse(theSet.overlaps(Range.closedOpen(1,3))) self.assertFalse(theSet.overlaps(Range.open(5,7))) self.assertFalse(theSet.overlaps(2)) self.assertTrue(theSet.overlaps(3)) self.assertTrue(theSet.overlaps(10)) self.assertTrue(theSet.overlaps(Range.closed(4,5))) self.assertTrue(theSet.overlaps(Range.closed(8,9))) self.assertTrue(theSet.overlaps(Range.closed(1,4))) self.assertFalse(theSet.overlaps(Range.closed(6,6))) self.assertTrue(theSet.overlaps(Range.closed(8,12))) self.assertTrue(theSet.overlaps(Range.closed(1,12)))
def test_overlaps(self): if debug: print("Testing overlaps") theSet = RangeSet() theSet.add(Range.closed(3, 5)) theSet.add(Range.closed(7, 10)) self.assertTrue(theSet.overlaps(4)) self.assertFalse(theSet.overlaps(Range.closedOpen(1, 3))) self.assertFalse(theSet.overlaps(Range.open(5, 7))) self.assertFalse(theSet.overlaps(2)) self.assertTrue(theSet.overlaps(3)) self.assertTrue(theSet.overlaps(10)) self.assertTrue(theSet.overlaps(Range.closed(4, 5))) self.assertTrue(theSet.overlaps(Range.closed(8, 9))) self.assertTrue(theSet.overlaps(Range.closed(1, 4))) self.assertFalse(theSet.overlaps(Range.closed(6, 6))) self.assertTrue(theSet.overlaps(Range.closed(8, 12))) self.assertTrue(theSet.overlaps(Range.closed(1, 12)))
def test_getDistanceFromRange(self): if debug: print("Testing getDistanceFromRange") range1 = Range.closed(1.,3.) self.assertAlmostEqual(range1.getDistanceFromRange(Range.closed(5.,7.)),2.) self.assertAlmostEqual(range1.getDistanceFromRange(Range.closed(-1.,0.)),1.) self.assertAlmostEqual(range1.getDistanceFromRange(Range.closed(-5.,10.)),0.) self.assertAlmostEqual(range1.getDistanceFromRange(Range.closed(-5.,2.)),0.) self.assertAlmostEqual(range1.getDistanceFromRange(Range.closed(2.,10.)),0.) self.assertAlmostEqual(range1.getDistanceFromRange(Range.closed(1.5,2.1)),0.) with self.assertRaises(TypeError): range1.getDistanceFromRange(Range.closedOpen(1.5,2.1))
def test_union(self): if debug: print("Testing union") firstSet = RangeSet([Range.closed(3,5), Range.closed(7,10)]) secondSet = RangeSet([Range.closed(2,4), Range.closed(5, 11), Range.closed(13, 15)]) union = firstSet.union(secondSet) self.assertEqual(union, RangeSet([Range.closed(2,11), Range.closed(13,15)]))
def test_iteritems(self): if debug: print("Testing iteritems") buckets = RangeBucketMap() buckets.put(Range.closed(3, 5), 'a') buckets.put(Range.closed(7, 10), 'b') buckets.put(Range.closed(4, 8), 'c') iterator = buckets.iteritems(2, 10) self.assertEquals(next(iterator), (Range.closed(3, 5), 'a')) self.assertEquals(next(iterator), (Range.closed(4, 8), 'c')) self.assertEquals(next(iterator), (Range.closed(7, 10), 'b')) with self.assertRaises(StopIteration): next(iterator) iterator = buckets.iteritems(3, 8) self.assertEquals(next(iterator), (Range.closed(3, 5), 'a')) self.assertEquals(next(iterator), (Range.closed(4, 8), 'c')) self.assertEquals(next(iterator), (Range.closed(7, 8), 'b')) with self.assertRaises(StopIteration): next(iterator)
def test_encloses(self): if debug: print("Testing encloses") range1 = Range.closed(3., 6.) range2 = Range.closed(4., 5.) self.assertTrue(range1.encloses(range2)) range1 = Range.open(3., 6.) range2 = Range.open(3., 6.) self.assertTrue(range1.encloses(range2)) range2 = Range.closed(4., 4.) self.assertTrue(range1.encloses(range2)) range1 = Range.openClosed(3., 6.) range2 = Range.closed(3., 6.) self.assertFalse(range1.encloses(range2)) self.assertTrue(range2.encloses(range1)) range1 = Range.closed(4., 5.) range2 = Range.open(3., 6.) self.assertFalse(range1.encloses(range2)) self.assertTrue(range2.encloses(range1))
def test_encloses(self): if debug: print("Testing encloses") range1 = Range.closed(3.,6.) range2 = Range.closed(4.,5.) self.assertTrue(range1.encloses(range2)) range1 = Range.open(3.,6.) range2 = Range.open(3.,6.) self.assertTrue(range1.encloses(range2)) range2 = Range.closed(4.,4.) self.assertTrue(range1.encloses(range2)) range1 = Range.openClosed(3.,6.) range2 = Range.closed(3.,6.) self.assertFalse(range1.encloses(range2)) self.assertTrue(range2.encloses(range1)) range1 = Range.closed(4.,5.) range2 = Range.open(3.,6.) self.assertFalse(range1.encloses(range2)) self.assertTrue(range2.encloses(range1))
def test_iteritems(self): if debug: print("Testing iteritems") buckets = RangeBucketMap() buckets.put(Range.closed(3,5),'a') buckets.put(Range.closed(7,10),'b') buckets.put(Range.closed(4,8),'c') iterator = buckets.iteritems(2,10) self.assertEquals(next(iterator), (Range.closed(3,5), 'a')) self.assertEquals(next(iterator), (Range.closed(4,8), 'c')) self.assertEquals(next(iterator), (Range.closed(7,10), 'b')) with self.assertRaises(StopIteration): next(iterator) iterator = buckets.iteritems(3,8) self.assertEquals(next(iterator), (Range.closed(3,5), 'a')) self.assertEquals(next(iterator), (Range.closed(4,8), 'c')) self.assertEquals(next(iterator), (Range.closed(7,8), 'b')) with self.assertRaises(StopIteration): next(iterator)
def test_union(self): if debug: print("Testing union") firstSet = RangeSet([Range.closed(3, 5), Range.closed(7, 10)]) secondSet = RangeSet( [Range.closed(2, 4), Range.closed(5, 11), Range.closed(13, 15)]) union = firstSet.union(secondSet) self.assertEqual(union, RangeSet([Range.closed(2, 11), Range.closed(13, 15)]))
def test_contains(self): if debug: print("Testing contains") theMap = RangeMap() theMap.put(Range.closed(3,5),'foo') theMap.put(Range.closed(7,10),'bar') self.assertTrue(theMap.contains(4)) self.assertFalse(theMap.contains(2)) self.assertTrue(theMap.contains(Range.closed(4,5))) self.assertTrue(theMap.contains(Range.closed(8,9))) self.assertFalse(theMap.contains(Range.closed(1,4))) self.assertFalse(theMap.contains(Range.closed(6,6))) self.assertFalse(theMap.contains(Range.closed(8,12)))
def test_contains(self): if debug: print("Testing contains") theSet = RangeSet() theSet.add(Range.closed(3,5)) theSet.add(Range.closed(7,10)) self.assertTrue(theSet.contains(4)) self.assertFalse(theSet.contains(2)) self.assertTrue(theSet.contains(Range.closed(4,5))) self.assertTrue(theSet.contains(Range.closed(8,9))) self.assertFalse(theSet.contains(Range.closed(1,4))) self.assertFalse(theSet.contains(Range.closed(6,6))) self.assertFalse(theSet.contains(Range.closed(8,12)))
def test_contains(self): if debug: print("Testing contains") theSet = RangeSet() theSet.add(Range.closed(3, 5)) theSet.add(Range.closed(7, 10)) self.assertTrue(theSet.contains(4)) self.assertFalse(theSet.contains(2)) self.assertTrue(theSet.contains(Range.closed(4, 5))) self.assertTrue(theSet.contains(Range.closed(8, 9))) self.assertFalse(theSet.contains(Range.closed(1, 4))) self.assertFalse(theSet.contains(Range.closed(6, 6))) self.assertFalse(theSet.contains(Range.closed(8, 12)))
def test_contains(self): if debug: print("Testing contains") theMap = RangeMap() theMap.put(Range.closed(3, 5), 'foo') theMap.put(Range.closed(7, 10), 'bar') self.assertTrue(theMap.contains(4)) self.assertFalse(theMap.contains(2)) self.assertTrue(theMap.contains(Range.closed(4, 5))) self.assertTrue(theMap.contains(Range.closed(8, 9))) self.assertFalse(theMap.contains(Range.closed(1, 4))) self.assertFalse(theMap.contains(Range.closed(6, 6))) self.assertFalse(theMap.contains(Range.closed(8, 12)))
def test_getDistanceFromRange(self): if debug: print("Testing getDistanceFromRange") range1 = Range.closed(1., 3.) self.assertAlmostEqual( range1.getDistanceFromRange(Range.closed(5., 7.)), 2.) self.assertAlmostEqual( range1.getDistanceFromRange(Range.closed(-1., 0.)), 1.) self.assertAlmostEqual( range1.getDistanceFromRange(Range.closed(-5., 10.)), 0.) self.assertAlmostEqual( range1.getDistanceFromRange(Range.closed(-5., 2.)), 0.) self.assertAlmostEqual( range1.getDistanceFromRange(Range.closed(2., 10.)), 0.) self.assertAlmostEqual( range1.getDistanceFromRange(Range.closed(1.5, 2.1)), 0.) with self.assertRaises(TypeError): range1.getDistanceFromRange(Range.closedOpen(1.5, 2.1))
def test_intersection(self): if debug: print("Testing intersection") range1 = Range.closed(1., 5.) range2 = Range.closed(3., 7.) self.assertEqual(range1.intersection(range2), Range.closed(3., 5.)) range2 = Range.closed(5., 7.) self.assertEqual(range1.intersection(range2), Range.closed(5., 5.)) range2 = Range.closed(6., 7.) with self.assertRaises(ValueError): range1.intersection(range2)
def test_get(self): if debug: print("Testing get") rangeMap = RangeMap() rangeMap.put(Range.closed(1,10),'foo') rangeMap.put(Range.open(3,6), 'bar') rangeMap.put(Range.open(10,20), 'foo') self.assertEqual(rangeMap.get(1),set(['foo'])) self.assertEquals(rangeMap.get(4),set(['bar'])) with self.assertRaises(KeyError): rangeMap.get(20) self.assertEqual(rangeMap.get(Range.closed(11,15)),set(['foo'])) self.assertEquals(rangeMap.get(Range.closed(5,15)),set(['foo','bar'])) with self.assertRaises(KeyError): rangeMap.get(Range.closed(20,30)) self.assertEquals(rangeMap.get(Range.closed(15,100)),set(['foo']))
def test_intersection(self): if debug: print("Testing intersection") range1 = Range.closed(1.,5.) range2 = Range.closed(3.,7.) self.assertEqual(range1.intersection(range2), Range.closed(3.,5.)) range2 = Range.closed(5.,7.) self.assertEqual(range1.intersection(range2), Range.closed(5.,5.)) range2 = Range.closed(6.,7.) with self.assertRaises(ValueError): range1.intersection(range2)
def test_get(self): if debug: print("Testing get") rangeMap = RangeMap() rangeMap.put(Range.closed(1, 10), 'foo') rangeMap.put(Range.open(3, 6), 'bar') rangeMap.put(Range.open(10, 20), 'foo') self.assertEqual(rangeMap.get(1), set(['foo'])) self.assertEquals(rangeMap.get(4), set(['bar'])) with self.assertRaises(KeyError): rangeMap.get(20) self.assertEqual(rangeMap.get(Range.closed(11, 15)), set(['foo'])) self.assertEquals(rangeMap.get(Range.closed(5, 15)), set(['foo', 'bar'])) with self.assertRaises(KeyError): rangeMap.get(Range.closed(20, 30)) self.assertEquals(rangeMap.get(Range.closed(15, 100)), set(['foo']))
def test_put(self): if debug: print("Testing put") rangeMap = RangeMap() rangeMap.put(Range.closed(1, 10), 'foo') self.assertEqual(rangeMap.ranges[0], Range.closed(1, 10)) self.assertEqual(rangeMap.items[0], 'foo') rangeMap.put(Range.open(3, 6), 'bar') self.assertEqual(rangeMap.ranges[0], Range.closed(1, 3)) self.assertEqual(rangeMap.ranges[1], Range.open(3, 6)) self.assertEqual(rangeMap.ranges[2], Range.closed(6, 10)) self.assertEqual(rangeMap.items[0], 'foo') self.assertEqual(rangeMap.items[1], 'bar') self.assertEqual(rangeMap.items[2], 'foo') rangeMap.put(Range.open(10, 20), 'foo') self.assertEqual(len(rangeMap), 4) self.assertEqual(rangeMap.ranges[3], Range.open(10, 20)) self.assertEqual(rangeMap.items[0], 'foo') self.assertEqual(rangeMap.items[1], 'bar') self.assertEqual(rangeMap.items[2], 'foo') self.assertEqual(rangeMap.items[3], 'foo')
def test_put(self): if debug: print("Testing put") rangeMap = RangeMap() rangeMap.put(Range.closed(1,10),'foo') self.assertEqual(rangeMap.ranges[0], Range.closed(1,10)) self.assertEqual(rangeMap.items[0], 'foo') rangeMap.put(Range.open(3,6), 'bar') self.assertEqual(rangeMap.ranges[0], Range.closed(1,3)) self.assertEqual(rangeMap.ranges[1], Range.open(3,6)) self.assertEqual(rangeMap.ranges[2], Range.closed(6,10)) self.assertEqual(rangeMap.items[0],'foo') self.assertEqual(rangeMap.items[1], 'bar') self.assertEqual(rangeMap.items[2],'foo') rangeMap.put(Range.open(10,20), 'foo') self.assertEqual(len(rangeMap),4) self.assertEqual(rangeMap.ranges[3], Range.open(10,20)) self.assertEqual(rangeMap.items[0],'foo') self.assertEqual(rangeMap.items[1], 'bar') self.assertEqual(rangeMap.items[2],'foo') self.assertEqual(rangeMap.items[3],'foo')
def test_open(self): if debug: print("Testing open") # Floats floatRange = Range.open(2., 5.) self.assertFalse(floatRange.contains(1.99)) self.assertFalse(floatRange.contains(2.)) self.assertTrue(floatRange.contains(3.)) self.assertFalse(floatRange.contains(5.)) self.assertFalse(floatRange.contains(5.01)) with self.assertRaises(TypeError): Range.open(3., 3.) # Letters letterRange = Range.open('b', 'e') self.assertFalse(letterRange.contains('a')) self.assertFalse(letterRange.contains('b')) self.assertTrue(letterRange.contains('c')) self.assertFalse(letterRange.contains('e')) self.assertFalse(letterRange.contains('f')) with self.assertRaises(TypeError): Range.open('b', 'b')
def test_open(self): if debug: print("Testing open") # Floats floatRange = Range.open(2.,5.) self.assertFalse(floatRange.contains(1.99)) self.assertFalse(floatRange.contains(2.)) self.assertTrue(floatRange.contains(3.)) self.assertFalse(floatRange.contains(5.)) self.assertFalse(floatRange.contains(5.01)) with self.assertRaises(TypeError): Range.open(3.,3.) # Letters letterRange = Range.open('b','e') self.assertFalse(letterRange.contains('a')) self.assertFalse(letterRange.contains('b')) self.assertTrue(letterRange.contains('c')) self.assertFalse(letterRange.contains('e')) self.assertFalse(letterRange.contains('f')) with self.assertRaises(TypeError): Range.open('b','b')
def test_hasUpperBound(self): if debug: print("Testing hasUpperBound") self.assertFalse(Range.atLeast(5.).hasUpperBound()) self.assertTrue(Range.atMost(5.).hasUpperBound()) self.assertTrue(Range.closed(2.,5.).hasUpperBound())
def test_upperEndpoint(self): if debug: print("Testing upperEndpoint") with self.assertRaises(TypeError): Range.atLeast(5.).upperEndpoint() self.assertEqual(Range.atMost(5.).upperEndpoint(),5.) self.assertEqual(Range.closed(5.,10.).upperEndpoint(),10.)
def test_isUpperBoundClosed(self): if debug: print("Testing isUpperBoundClosed") with self.assertRaises(TypeError): Range.atLeast(5.).isUpperBoundClosed() self.assertFalse(Range.closedOpen(5.,10.).isUpperBoundClosed()) self.assertTrue(Range.openClosed(5.,10.).isUpperBoundClosed())
def test_difference(self): if debug: print("Testing difference") startSet = RangeSet([Range.closed(3,5),Range.closed(7,10)]) diffSet = startSet.difference(RangeSet([Range.closed(4,6)])) self.assertEqual(diffSet, RangeSet([Range.closedOpen(3,4), Range.closed(7,10)])) diffSet = startSet.difference(RangeSet([Range.closed(2,6)])) self.assertEqual(diffSet, RangeSet([Range.closed(7,10)])) diffSet = startSet.difference(RangeSet([Range.closed(-2,1)])) self.assertEqual(diffSet, startSet) diffSet = startSet.difference(RangeSet([Range.closed(1,3), Range.closed(6,9)])) self.assertEqual(diffSet, RangeSet([Range.openClosed(3,5), Range.openClosed(9,10)])) diffSet = startSet.difference(RangeSet([Range.closed(1,11)])) self.assertEqual(len(diffSet),0)
def test_intersection(self): if debug: print("Testing intersection") startSet = RangeSet([Range.closed(3, 5), Range.closed(7, 10)]) intersect = startSet.intersection(RangeSet([Range.closed(4, 6)])) self.assertEqual(intersect, RangeSet([Range.closed(4, 5)])) intersect = startSet.intersection( RangeSet( [Range.closed(1, 3), Range.closed(8, 9), Range.closed(12, 15)])) self.assertEqual(intersect, RangeSet([Range.closed(3, 3), Range.closed(8, 9)])) intersect = startSet.intersection(RangeSet([Range.openClosed(8, 1000)])) self.assertEqual(intersect, RangeSet([Range.openClosed(8, 10)]))
def iteritems(self, start = None, end = None): """ Iterates over pairs of (Range, value) Parameters ---------- start : comparable, optional The starting point for iterating, inclusive end : comparable, optional The ending point for iterating, inclusive Returns ------- Generator of (Range intersecting [start,end], value), ordered by start point """ if start is None: start = self.lower_cuts[0] else: start = Cut.belowValue(start) if end is None: end = self.upper_cuts[-1] else: end = Cut.aboveValue(end) bounding_range = Range(start, end) # Get the bounding indices ovlapLowerInd = max(bisect_left(self.lower_cuts, start)-1,0) ovlapUpperInd = bisect_left(self.lower_cuts, end) # Create queue of values that need to be generated yield_vals = deque() # Create dictionary of values to be generated -> indices containing them vals_inds_dict = {} for i in range(ovlapLowerInd, ovlapUpperInd): # Check if anything can be released from the queue while len(yield_vals) > 0: if vals_inds_dict[yield_vals[0]][-1] < i-1: # Yield the full range, value. Remove value from queue val = yield_vals.popleft() yield Range(max(self.lower_cuts[vals_inds_dict[val][0]],start), min(self.upper_cuts[vals_inds_dict[val][-1]],end)), val # Remove value from dict del vals_inds_dict[val] else: break try: # Get intersection of the ranges intersect = bounding_range.intersection(self.ranges[i]) if not intersect.isEmpty(): # If overlapping with this range, put into queue for val in self.items[i]: if val not in vals_inds_dict: yield_vals.append(val) vals_inds_dict[val] = deque() vals_inds_dict[val].append(i) except ValueError: # Continue if no overlap with this range continue ## Yield remaining values while len(yield_vals) > 0: # Yield the full range, value. Remove value from queue val = yield_vals.popleft() yield Range(max(self.lower_cuts[vals_inds_dict[val][0]],start), min(self.upper_cuts[vals_inds_dict[val][-1]],end)), val # Remove value from dict del vals_inds_dict[val]
def test_containsAll(self): if debug: print("Testing containsAll") self.assertTrue(Range.openClosed(3., 5.).containsAll([4., 4.5, 5.])) self.assertFalse(Range.closedOpen(3., 5.).containsAll([4., 4.5, 5.])) self.assertFalse(Range.closedOpen(3., 5.).containsAll([3., 4., 5.])) self.assertFalse(Range.closedOpen(3., 5.).containsAll([2., 4., 5.]))
def test_isEmpty(self): if debug: print("Testing isEmpty") self.assertTrue(Range.closedOpen(3., 3.).isEmpty()) self.assertTrue(Range.openClosed(3., 3.).isEmpty()) self.assertFalse(Range.openClosed(3., 3.001).isEmpty())
def test_isUpperBoundClosed(self): if debug: print("Testing isUpperBoundClosed") with self.assertRaises(TypeError): Range.atLeast(5.).isUpperBoundClosed() self.assertFalse(Range.closedOpen(5., 10.).isUpperBoundClosed()) self.assertTrue(Range.openClosed(5., 10.).isUpperBoundClosed())
def test_upperEndpoint(self): if debug: print("Testing upperEndpoint") with self.assertRaises(TypeError): Range.atLeast(5.).upperEndpoint() self.assertEqual(Range.atMost(5.).upperEndpoint(), 5.) self.assertEqual(Range.closed(5., 10.).upperEndpoint(), 10.)
def test_hasUpperBound(self): if debug: print("Testing hasUpperBound") self.assertFalse(Range.atLeast(5.).hasUpperBound()) self.assertTrue(Range.atMost(5.).hasUpperBound()) self.assertTrue(Range.closed(2., 5.).hasUpperBound())
def test_add(self): if debug: print("Testing add with integers") theSet = RangeSet() # Adding initial part theSet.add(Range.closed(3,5)) self.assertEqual(theSet.lower_cuts[0], Cut.belowValue(3)) self.assertEqual(theSet.upper_cuts[0], Cut.aboveValue(5)) self.assertEqual(len(theSet),1) # Adding distinct range above initial one theSet.add(Range.closed(7,10)) self.assertEqual(len(theSet),2) self.assertEqual(theSet.ranges[1],Range.closed(7,10)) self.assertEqual(theSet.ranges[0],Range.closed(3,5)) # Adding range below/overlapping with initial one theSet.add(Range.closed(2,3)) self.assertEqual(len(theSet),2) self.assertEqual(Range.closed(2,5), theSet.ranges[0]) self.assertEqual(Cut.belowValue(2), theSet.lower_cuts[0]) self.assertEqual(Cut.aboveValue(5), theSet.upper_cuts[0]) self.assertEqual(Range.closed(7,10), theSet.ranges[1]) self.assertEqual(Cut.belowValue(7), theSet.lower_cuts[1]) self.assertEqual(Cut.aboveValue(10), theSet.upper_cuts[1]) # Adding range above/overlapping second one theSet.add(Range.closed(9,11)) self.assertEqual(len(theSet),2) self.assertEqual(Range.closed(7,11), theSet.ranges[1]) self.assertEqual(Cut.belowValue(7), theSet.lower_cuts[1]) self.assertEqual(Cut.aboveValue(11), theSet.upper_cuts[1]) # Adding range encompasing second one theSet.add(Range.closed(6,12)) self.assertEqual(len(theSet),2) self.assertEqual(Range.closed(6,12), theSet.ranges[1]) self.assertEqual(Cut.belowValue(6), theSet.lower_cuts[1]) self.assertEqual(Cut.aboveValue(12), theSet.upper_cuts[1]) # Adding range encompassing all theSet.add(Range.closed(3, 11)) self.assertEqual(len(theSet),1) self.assertEqual(len(theSet.lower_cuts),1) self.assertEqual(len(theSet.upper_cuts),1) self.assertEqual(Range.closed(2,12), theSet.ranges[0]) self.assertEqual(Cut.belowValue(2), theSet.lower_cuts[0]) self.assertEqual(Cut.aboveValue(12), theSet.upper_cuts[0])
def test_add(self): if debug: print("Testing add with integers") theSet = RangeSet() # Adding initial part theSet.add(Range.closed(3, 5)) self.assertEqual(theSet.lower_cuts[0], Cut.belowValue(3)) self.assertEqual(theSet.upper_cuts[0], Cut.aboveValue(5)) self.assertEqual(len(theSet), 1) # Adding distinct range above initial one theSet.add(Range.closed(7, 10)) self.assertEqual(len(theSet), 2) self.assertEqual(theSet.ranges[1], Range.closed(7, 10)) self.assertEqual(theSet.ranges[0], Range.closed(3, 5)) # Adding range below/overlapping with initial one theSet.add(Range.closed(2, 3)) self.assertEqual(len(theSet), 2) self.assertEqual(Range.closed(2, 5), theSet.ranges[0]) self.assertEqual(Cut.belowValue(2), theSet.lower_cuts[0]) self.assertEqual(Cut.aboveValue(5), theSet.upper_cuts[0]) self.assertEqual(Range.closed(7, 10), theSet.ranges[1]) self.assertEqual(Cut.belowValue(7), theSet.lower_cuts[1]) self.assertEqual(Cut.aboveValue(10), theSet.upper_cuts[1]) # Adding range above/overlapping second one theSet.add(Range.closed(9, 11)) self.assertEqual(len(theSet), 2) self.assertEqual(Range.closed(7, 11), theSet.ranges[1]) self.assertEqual(Cut.belowValue(7), theSet.lower_cuts[1]) self.assertEqual(Cut.aboveValue(11), theSet.upper_cuts[1]) # Adding range encompasing second one theSet.add(Range.closed(6, 12)) self.assertEqual(len(theSet), 2) self.assertEqual(Range.closed(6, 12), theSet.ranges[1]) self.assertEqual(Cut.belowValue(6), theSet.lower_cuts[1]) self.assertEqual(Cut.aboveValue(12), theSet.upper_cuts[1]) # Adding range encompassing all theSet.add(Range.closed(3, 11)) self.assertEqual(len(theSet), 1) self.assertEqual(len(theSet.lower_cuts), 1) self.assertEqual(len(theSet.upper_cuts), 1) self.assertEqual(Range.closed(2, 12), theSet.ranges[0]) self.assertEqual(Cut.belowValue(2), theSet.lower_cuts[0]) self.assertEqual(Cut.aboveValue(12), theSet.upper_cuts[0])
def test_containsAll(self): if debug: print("Testing containsAll") self.assertTrue(Range.openClosed(3.,5.).containsAll([4.,4.5,5.])) self.assertFalse(Range.closedOpen(3.,5.).containsAll([4.,4.5,5.])) self.assertFalse(Range.closedOpen(3.,5.).containsAll([3.,4.,5.])) self.assertFalse(Range.closedOpen(3.,5.).containsAll([2.,4.,5.]))
def test_remove(self): if debug: print("Testing remove") startSet = RangeSet([Range.closed(3, 5), Range.closed(7, 10)]) # Try removing something outside all ranges startSet.remove(Range.closed(0, 1)) self.assertEqual(startSet, RangeSet([Range.closed(3, 5), Range.closed(7, 10)])) # Remove left part of left range startSet.remove(Range.closed(2, 3)) self.assertEqual( startSet, RangeSet([Range.openClosed(3, 5), Range.closed(7, 10)])) # Remove from middle of right range startSet.remove(Range.closed(8, 9)) self.assertEqual( startSet, RangeSet([ Range.openClosed(3, 5), Range.closedOpen(7, 8), Range.openClosed(9, 10) ])) # Remove through both ranges startSet.remove(Range.closed(5, 7)) self.assertEqual( startSet, RangeSet( [Range.open(3, 5), Range.open(7, 8), Range.openClosed(9, 10)])) # Remove over the whole second set of ranges startSet.remove(Range.closed(5, 100)) self.assertEqual(startSet, RangeSet([Range.open(3, 5)]))
def test_isEmpty(self): if debug: print("Testing isEmpty") self.assertTrue(Range.closedOpen(3.,3.).isEmpty()) self.assertTrue(Range.openClosed(3.,3.).isEmpty()) self.assertFalse(Range.openClosed(3.,3.001).isEmpty())
def test_remove(self): if debug: print("Testing remove") rangeMap = RangeMap() rangeMap.put(Range.closed(1, 10), 'foo') rangeMap.put(Range.open(3, 6), 'bar') rangeMap.put(Range.open(10, 20), 'foo') rangeMap.remove(Range.closed(5, 11)) self.assertEqual(len(rangeMap), 3) self.assertEqual(rangeMap.ranges[0], Range.closed(1, 3)) self.assertEqual(rangeMap.ranges[1], Range.open(3, 5)) self.assertEqual(rangeMap.ranges[2], Range.open(11, 20)) self.assertEqual(rangeMap.items[0], 'foo') self.assertEqual(rangeMap.items[1], 'bar') self.assertEqual(rangeMap.items[2], 'foo') rangeMap.remove(Range.closed(0, 1)) self.assertEqual(len(rangeMap), 3) self.assertEqual(rangeMap.ranges[0], Range.openClosed(1, 3)) self.assertEqual(rangeMap.ranges[1], Range.open(3, 5)) self.assertEqual(rangeMap.ranges[2], Range.open(11, 20)) self.assertEqual(rangeMap.items[0], 'foo') self.assertEqual(rangeMap.items[1], 'bar') self.assertEqual(rangeMap.items[2], 'foo')
def test_remove(self): if debug: print("Testing remove") startSet = RangeSet([Range.closed(3,5),Range.closed(7,10)]) # Try removing something outside all ranges startSet.remove(Range.closed(0,1)) self.assertEqual(startSet, RangeSet([ Range.closed(3,5),Range.closed(7,10) ])) # Remove left part of left range startSet.remove(Range.closed(2,3)) self.assertEqual(startSet, RangeSet([ Range.openClosed(3,5),Range.closed(7,10) ])) # Remove from middle of right range startSet.remove(Range.closed(8,9)) self.assertEqual(startSet, RangeSet([ Range.openClosed(3,5),Range.closedOpen(7,8), Range.openClosed(9,10) ])) # Remove through both ranges startSet.remove(Range.closed(5,7)) self.assertEqual(startSet, RangeSet([ Range.open(3,5),Range.open(7,8), Range.openClosed(9,10) ])) # Remove over the whole second set of ranges startSet.remove(Range.closed(5,100)) self.assertEqual(startSet, RangeSet([ Range.open(3,5) ]))
def test_difference(self): if debug: print("Testing difference") startSet = RangeSet([Range.closed(3, 5), Range.closed(7, 10)]) diffSet = startSet.difference(RangeSet([Range.closed(4, 6)])) self.assertEqual( diffSet, RangeSet([Range.closedOpen(3, 4), Range.closed(7, 10)])) diffSet = startSet.difference(RangeSet([Range.closed(2, 6)])) self.assertEqual(diffSet, RangeSet([Range.closed(7, 10)])) diffSet = startSet.difference(RangeSet([Range.closed(-2, 1)])) self.assertEqual(diffSet, startSet) diffSet = startSet.difference( RangeSet([Range.closed(1, 3), Range.closed(6, 9)])) self.assertEqual( diffSet, RangeSet([Range.openClosed(3, 5), Range.openClosed(9, 10)])) diffSet = startSet.difference(RangeSet([Range.closed(1, 11)])) self.assertEqual(len(diffSet), 0)
def test_intersection(self): if debug: print("Testing intersection") startSet = RangeSet([Range.closed(3,5),Range.closed(7,10)]) intersect = startSet.intersection(RangeSet([Range.closed(4,6)])) self.assertEqual(intersect,RangeSet([Range.closed(4,5)])) intersect = startSet.intersection(RangeSet([ Range.closed(1,3),Range.closed(8,9),Range.closed(12,15) ])) self.assertEqual(intersect, RangeSet([ Range.closed(3,3),Range.closed(8,9) ])) intersect = startSet.intersection(RangeSet([ Range.openClosed(8,1000) ])) self.assertEqual(intersect, RangeSet([ Range.openClosed(8,10) ]))