def test_difference(self): ps1 = PowerSet() ps2 = PowerSet() ps3 = PowerSet() ps4 = PowerSet() for n in range(0, 10): ps1.put("test" + str(n)) for n in range(5, 15): ps2.put("test" + str(n)) for n in range(10, 20): ps3.put("test" + str(n)) for n in range(0, 10): ps4.put("test" + str(n)) self.assertEqual(ps1.size(), 10) self.assertEqual(ps2.size(), 10) self.assertEqual(ps3.size(), 10) # все элементы текущего множества не входят в множество-параметр res = ps1.difference(ps3) self.assertEqual(res.size(), 10) for n in range(0, 10): self.assertTrue(res.get("test" + str(n))) self.assertFalse(res.get("test10")) # в результате пустое множество res = ps1.difference(ps4) self.assertEqual(res.size(), 0) # в результате не пустое множество res = ps1.difference(ps2) self.assertEqual(res.size(), 5) for n in range(0, 5): self.assertTrue(res.get("test" + str(n))) for n in range(5, 10): self.assertFalse(res.get("test" + str(n))) res = ps2.difference(ps1) self.assertEqual(res.size(), 5) for n in range(10, 15): self.assertTrue(res.get("test" + str(n))) for n in range(5, 10): self.assertFalse(res.get("test" + str(n)))
def test_timing(self): from timeit import default_timer as timer from datetime import timedelta from random import randrange # операции над множествами из десятков тысяч элементов # должны укладываться в пару секунд REFERENCE = 2 ps1 = PowerSet() ps2 = PowerSet() values = [self.get_random_string(100) for n in range(100)] print("Loop 19900: ", timedelta(seconds=timer())) start = timer() for n in range(0, 19800): ps1.put(self.get_random_string(randrange(10, 150))) for val in values: ps1.put(val) end = timer() print("created 19900: " + str(timedelta(seconds=end - start))) print("Loop 21100: ", timedelta(seconds=timer())) start = timer() for n in range(0, 21000): ps2.put(self.get_random_string(randrange(10, 150))) for val in values: ps2.put(val) end = timer() print("created 21100: " + str(timedelta(seconds=end - start))) self.assertEqual(ps1.size(), 19900) self.assertEqual(ps2.size(), 21100) # put start = timer() ps1.put(self.get_random_string(randrange(10, 150))) end = timer() print("put: " + str(timedelta(seconds=end - start))) self.assertTrue(end - start <= REFERENCE) # get val = self.get_random_string(randrange(10, 150)) ps1.put(val) start = timer() ps1.get(val) end = timer() print("get: " + str(timedelta(seconds=end - start))) self.assertTrue(end - start <= REFERENCE) # remove val = self.get_random_string(randrange(10, 150)) ps1.put(val) start = timer() ps1.remove(val) end = timer() print("remove: " + str(timedelta(seconds=end - start))) self.assertTrue(end - start <= REFERENCE) # intersection print("\nIntersection start: ", timedelta(seconds=timer())) start = timer() ps1.intersection(ps2) end = timer() print(timedelta(seconds=end - start)) self.assertTrue(end - start <= REFERENCE) # union print("\nUnion start: ", timedelta(seconds=timer())) start = timer() ps1.union(ps2) end = timer() print(timedelta(seconds=end - start)) self.assertTrue(end - start <= REFERENCE) # difference print("\nDifference start: ", timedelta(seconds=timer())) start = timer() ps1.difference(ps2) end = timer() print(timedelta(seconds=end - start)) self.assertTrue(end - start <= REFERENCE) # issubset print("\nIssubset start: ", timedelta(seconds=timer())) start = timer() ps1.issubset(ps1) end = timer() print(timedelta(seconds=end - start)) self.assertTrue(end - start <= REFERENCE)