def test_or_operand(self): A = count(0, step=2) B = count(1, step=2) one = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) self.assertEqual(len(one), 200) self.assertTrue(hasattr(one, "__or__")) C = count(200, step=2) D = count(201, step=2) two = EdgeSet(UEdge(next(C), next(D)) for _ in range(200)) self.assertEqual(len(two), 200) three = one | two self.assertIsInstance(three, EdgeSet) self.assertEqual(len(three), 300) four = two | one self.assertEqual(len(four), len(three)) self.assertIsInstance(four, EdgeSet) self.assertEqual(three, four) E = count(0, step=2) F = count(1, step=2) five = EdgeSet(UEdge(next(E), next(F)) for _ in range(300)) self.assertEqual(three, five) self.assertEqual(four, five)
def test_discard_method(self): items = [ (8, 6), (7, 4), (5,3), (2, 1), (8, 7), (8, 4), (8, 5), (8, 2) ] result = EdgeSet(items) self.assertEqual(len(result), len(items)) self.assertTrue(hasattr(result, 'discard')) with self.subTest("without exception: tuple"): result.discard((5, 8)) self.assertEqual(len(result), len(items) - 1) with self.subTest("without exception: args"): result.discard(2, 1) self.assertEqual(len(result), len(items) - 2) with self.subTest("without exception: list"): result.discard([4, 7]) self.assertEqual(len(result), len(items) - 3) with self.subTest("without exception: list"): result.discard(UEdge(7, 8)) self.assertEqual(len(result), len(items) - 4) with self.subTest("without exception: alredy taken"): result.discard(UEdge(7, 8)) self.assertEqual(len(result), len(items) - 4) with self.subTest("without exception: non exists previously"): result.discard(UEdge(7, 8)) self.assertEqual(len(result), len(items) - 4)
def test_and_operand(self): A = count(0, step=2) B = count(1, step=2) one = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) self.assertEqual(len(one), 200) self.assertTrue(hasattr(one, "__and__")) C = count(200, step=2) D = count(201, step=2) two = EdgeSet(UEdge(next(C), next(D)) for _ in range(200)) self.assertEqual(len(two), 200) three = one & two self.assertIsInstance(three, EdgeSet) self.assertEqual(len(three), 100) four = two & one self.assertEqual(len(four), len(three)) self.assertIsInstance(four, EdgeSet) self.assertEqual(three, four)
def test_sub_operand(self): A = count(0, step=2) B = count(1, step=2) one = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) self.assertEqual(len(one), 200) self.assertTrue(hasattr(one, "__sub__")) C = count(200, step=2) D = count(201, step=2) two = EdgeSet(UEdge(next(C), next(D)) for _ in range(200)) self.assertEqual(len(two), 200) three = one - two self.assertIsInstance(three, EdgeSet) self.assertEqual(len(three), 100) A = count(0, step=2) B = count(1, step=2) three_expected = EdgeSet(UEdge(next(A), next(B)) for _ in range(100)) self.assertEqual(three, three_expected)
def test_contains(self): A = count(0, step=2) B = count(1, step=2) items = [ (next(A), next(B)) for _ in range(200)] result = EdgeSet(items) self.assertTrue(hasattr(result, "__contains__")) self.assertTrue(UEdge(8, 9) in result) self.assertTrue(UEdge(9, 8) in result) self.assertFalse(UEdge(8, 7) in result) self.assertFalse(UEdge(7, 8) in result)
def test_if_random_sample_works(self): A = count(0, step=2) B = count(1, step=2) edges = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) arbitrary = sample(edges, k=10)
def test_if_choices_works(self): A = count(0, step=2) B = count(1, step=2) edges = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) arbitrary_edges = choices(edges, k=5)
def test_remove_method(self): items = [ (8, 6), (7, 4), (5,3), (2, 1), (8, 7), (8, 4), (8, 5), (8, 2) ] result = EdgeSet(items) self.assertEqual(len(result), len(items)) self.assertTrue(hasattr(result, 'remove')) with self.subTest("without exception: tuple"): result.remove((5, 8)) self.assertEqual(len(result), len(items) - 1) with self.subTest("without exception: args"): result.remove(2, 1) self.assertEqual(len(result), len(items) - 2) with self.subTest("without exception: list"): result.remove([4, 7]) self.assertEqual(len(result), len(items) - 3) with self.subTest("without exception: list"): result.remove(UEdge(7, 8)) self.assertEqual(len(result), len(items) - 4) with self.subTest("with exception"): with self.assertRaises(KeyError): result.remove((101, 105)) with self.subTest("with exception: alredy removed"): with self.assertRaises(KeyError): result.remove((8, 5))
def test_convert_to_list(self): A = count(0, step=2) B = count(1, step=2) edges = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) result = list(edges) self.assertIsInstance(result, list) self.assertEqual(len(result), 200)
def test_xor_operand(self): A = count(0, step=2) B = count(1, step=2) one = EdgeSet(UEdge(next(A), next(B)) for _ in range(200)) self.assertEqual(len(one), 200) self.assertTrue(hasattr(one, "__xor__")) C = count(200, step=2) D = count(201, step=2) two = EdgeSet(UEdge(next(C), next(D)) for _ in range(200)) self.assertEqual(len(two), 200) three = one ^ two self.assertIsInstance(three, EdgeSet) self.assertEqual(len(three), 200) four = two ^ one self.assertEqual(three, four) self.assertIsInstance(four, EdgeSet) E = count(0, step=2) F = count(1, step=2) five = EdgeSet(UEdge(next(E), next(F)) for _ in range(100)) six = EdgeSet((next(E), next(F)) for _ in range(100)) items = [(next(E), next(F)) for _ in range(100)] for item in items: five.add(item) self.assertEqual(len(five), 200) self.assertEqual(three, five) self.assertEqual(four, five) seven = six & five self.assertEqual(len(seven), 0) self.assertFalse(seven) self.assertIsInstance(seven, EdgeSet)
def test_add_an_edge(self): items = [(8, 9), (7, 4),(5,3), (2,1)] result = EdgeSet(items) edge = UEdge(40, 50) self.assertFalse(edge in result) self.assertEqual(len(result), len(items)) self.assertTrue(hasattr(result, 'add')) result.add(edge) self.assertTrue(edge in result) self.assertEqual(len(result), len(items) + 1)
def test_init_with_set_of_edges(self): A = count(0, step=2) B = count(1, step=2) items = set([UEdge(next(A), next(B)) for _ in range(200)]) self.assertIsInstance(items, set) self.assertTrue(all(isinstance(item, UEdge) for item in items)) result = EdgeSet(items) self.assertIsInstance(result, EdgeSet) self.assertEqual(len(result), 200) self.assertIsInstance(result._edges, set) self.assertTrue(all(isinstance(edge, UEdge) for edge in result))