def test_03(self): """OaS.indexUB() - within list range(3).""" self.assertEqual(0, OaS.indexUB(list(range(3)), -10)) self.assertEqual(0, OaS.indexUB(list(range(3)), 0)) self.assertEqual(1, OaS.indexUB(list(range(3)), 1)) self.assertEqual(2, OaS.indexUB(list(range(3)), 2)) self.assertEqual(-1, OaS.indexUB(list(range(3)), 3))
def test_random(self): """OaS.indexUB() - random sets of integers.""" CYCLES = 8 SIZE = 1024 LB = -1 * 2 << 16 UB = 2 << 16 TESTS = 1024 random.seed() for cyc in range(CYCLES): s = set() for r in range(SIZE): s.add(random.randint(LB, UB)) l = list(s) l.sort() # Test exact membership for i, v in enumerate(l): self.assertEqual(i, OaS.indexUB(l, v)) # Test lower bound for t in range(TESTS): #print t v = random.randint(LB, UB) result = OaS.indexUB(l, v) if result != -1: #if l[result] < v: # print 'result: %d l[result]: %d < v: %d l: %s' % (result, l[result], v, l) #assert(type(l[result]) == type(1)) #assert(type(v) == type(1)) #assert(l[result] >= v), 'l[result]: %d >= v: %d' % (l[result], v) self.assertTrue(l[result] >= v)
def test_11(self): """OaS.indexUB() - Special tests (2).""" myL = [1618, 2203, 12713, 15130, 47532, 48695, 68099, 79859, 82937, 92404, 110497, 125270] self.assertEqual(10, OaS.indexLB(myL, 116860)) self.assertEqual(11, OaS.indexUB(myL, 116860)) self.assertEqual(8, OaS.indexLB(myL, 90531)) self.assertEqual(9, OaS.indexUB(myL, 90531))
def test_10(self): """OaS.indexUB() - Special tests (1).""" myL = [62, 99, 291, 452, 621, 726, 739, 850, 859, 959] self.assertEqual(8, OaS.indexLB(myL, 936)) self.assertEqual(9, OaS.indexUB(myL, 936)) self.assertEqual(2, OaS.indexLB(myL, 380)) self.assertEqual(3, OaS.indexUB(myL, 380)) self.assertEqual(2, OaS.indexLB(myL, 373)) self.assertEqual(3, OaS.indexUB(myL, 373)) self.assertEqual(2, OaS.indexLB(myL, 450)) self.assertEqual(3, OaS.indexUB(myL, 450))
def href(self, theTuIndex, isLB): """Returns an href string for the TuIndex. If isLB is true returns the nearest lower bound, otherwise the nearest upper bound.""" if isLB: myIdx = OaS.indexLB(self._tuMarkerS, theTuIndex) else: myIdx = OaS.indexUB(self._tuMarkerS, theTuIndex) if myIdx >= len(self._tuMarkerS): raise ExceptionTuIndexer('Over-range index, isLB=%s: %s' % (isLB, theTuIndex)) if myIdx == -1: raise ExceptionTuIndexer('Under-range index, isLB=%s: %s' % (isLB, theTuIndex)) return '%s#_%d' % (self._tuName, self._tuMarkerS[myIdx])
def test_08_02(self): """OaS.indexUB() - within list range(8, 2).""" self.assertEqual(0, OaS.indexUB(list(range(0, 8, 2)), 0)) self.assertEqual(1, OaS.indexUB(list(range(0, 8, 2)), 1))
def test_00_01(self): """OaS.indexUB() - under range returns -1""" self.assertEqual(-1, OaS.indexUB([], 23)) self.assertEqual(-1, OaS.indexUB([ 23, ], 24))