Skip to content

Commit 9e9eaa9

Browse files
committed
Restructure and add more tests
1 parent d746433 commit 9e9eaa9

File tree

1 file changed

+81
-177
lines changed

1 file changed

+81
-177
lines changed

test_pythagorean_tuples.py

Lines changed: 81 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -5,211 +5,115 @@
55
from pythagorean_tuples import pythagorean_triples
66

77

8+
def is_pythagorean_triple(t):
9+
return t[0] ** 2 + t[1] ** 2 == t[2] ** 2
10+
11+
812
def is_primitive(t):
913
return math.gcd(t[0], t[1]) == 1 and math.gcd(t[1], t[2]) == 1 and math.gcd(t[2], t[0]) == 1
1014

1115

12-
class TestExceptions(unittest.TestCase):
16+
class TestPythagoreanTriples(unittest.TestCase):
1317
def test_not_integer(self):
1418
self.assertRaises(TypeError, pythagorean_triples, 3.14, "TypeError has not been raised")
1519

1620
def test_not_positive(self):
1721
self.assertRaises(ValueError, pythagorean_triples, 0, "ValueError has not been raised")
1822

19-
20-
class TestAPrimitive(unittest.TestCase):
21-
def test_2(self):
23+
def test_number_AP(self):
2224
self.assertEqual(set(), pythagorean_triples(2, True))
23-
24-
def test_4(self):
2525
self.assertEqual({(4, 3, 5)}, pythagorean_triples(4, True))
26-
27-
def test_32(self):
2826
self.assertEqual({(32, 255, 257)}, pythagorean_triples(32, True))
29-
30-
def test_random(self):
31-
res = pythagorean_triples(2 ** randint(10, 20), True)
32-
for t in res:
33-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
34-
self.assertTrue(is_primitive(t), f"{t} is not primitive")
35-
for n in t:
36-
self.assertIs(int, type(n))
37-
38-
def test_random_huge(self):
39-
res = pythagorean_triples(2 ** randint(100, 200), True)
40-
for t in res:
41-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
42-
self.assertTrue(is_primitive(t), f"{t} is not primitive")
43-
for n in t:
44-
self.assertIs(int, type(n))
45-
46-
47-
class TestA(unittest.TestCase):
48-
def test_2(self):
49-
self.assertEqual(set(), pythagorean_triples(2, False))
50-
51-
def test_4(self):
52-
self.assertEqual({(4, 3, 5)}, pythagorean_triples(4, False))
53-
54-
def test_32(self):
55-
self.assertEqual({(32, 24, 40), (32, 60, 68), (32, 126, 130), (32, 255, 257)}, pythagorean_triples(32, False))
56-
57-
def test_random(self):
58-
res = pythagorean_triples(2 ** randint(10, 20), False)
59-
for t in res:
60-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
61-
for n in t:
62-
self.assertIs(int, type(n))
63-
64-
def test_random_huge(self):
65-
res = pythagorean_triples(2 ** randint(100, 200), False)
66-
for t in res:
67-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
68-
for n in t:
69-
self.assertIs(int, type(n))
70-
71-
72-
class TestBPrimitive(unittest.TestCase):
73-
def test_3(self):
27+
self.assertEqual({(512, 65535, 65537)}, pythagorean_triples(512, True))
28+
29+
def test_number_A(self):
30+
self.assertEqual(set(), pythagorean_triples(2))
31+
self.assertEqual({(4, 3, 5)}, pythagorean_triples(4))
32+
self.assertEqual({(32, 24, 40), (32, 60, 68), (32, 126, 130), (32, 255, 257)}, pythagorean_triples(32))
33+
self.assertEqual({(512, 384, 640), (512, 960, 1088), (512, 2016, 2080), (512, 4080, 4112), (512, 8184, 8200),
34+
(512, 16380, 16388), (512, 32766, 32770), (512, 65535, 65537)}, pythagorean_triples(512))
35+
36+
def test_random_AP(self):
37+
for _ in range(10):
38+
a = 2 ** randint(15, 50)
39+
primitive_triples = pythagorean_triples(a, True)
40+
for t in primitive_triples:
41+
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
42+
self.assertTrue(is_primitive(t), f'{t} is not primitive')
43+
44+
def test_random_A(self):
45+
for _ in range(10):
46+
a = 2 ** randint(15, 50)
47+
triples = pythagorean_triples(a)
48+
for t in triples:
49+
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
50+
51+
def test_number_BP(self):
7452
self.assertEqual({(3, 4, 5)}, pythagorean_triples(3, True))
75-
76-
def test_13(self):
7753
self.assertEqual({(13, 84, 85)}, pythagorean_triples(13, True))
78-
79-
def test_271(self):
8054
self.assertEqual({(271, 36720, 36721)}, pythagorean_triples(271, True))
81-
82-
def test_121(self):
8355
self.assertEqual({(121, 7320, 7321)}, pythagorean_triples(121, True))
84-
85-
def test_153(self):
8656
self.assertEqual({(153, 104, 185), (153, 11704, 11705)}, pythagorean_triples(153, True))
87-
88-
def test_235(self):
8957
self.assertEqual({(235, 1092, 1117), (235, 27612, 27613)}, pythagorean_triples(235, True))
9058

91-
def test_random(self):
92-
r = randint(10, 1000)
93-
while r % 2 == 0:
94-
r //= 2
95-
res = pythagorean_triples(r, True)
96-
for t in res:
97-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
98-
self.assertTrue(is_primitive(t), f"{t} is not primitive")
99-
for n in t:
100-
self.assertIs(int, type(n))
101-
102-
def test_random_huge(self):
103-
r = randint(100, 1000000)
104-
while r % 2 == 0:
105-
r //= 2
106-
res = pythagorean_triples(r, True)
107-
for t in res:
108-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
109-
self.assertTrue(is_primitive(t), f"{t} is not primitive")
110-
for n in t:
111-
self.assertIs(int, type(n))
112-
113-
114-
class TestB(unittest.TestCase):
115-
def test_3(self):
116-
self.assertEqual({(3, 4, 5)}, pythagorean_triples(3, False))
117-
118-
def test_13(self):
119-
self.assertEqual({(13, 84, 85)}, pythagorean_triples(13, False))
120-
121-
def test_271(self):
122-
self.assertEqual({(271, 36720, 36721)}, pythagorean_triples(271, False))
123-
124-
def test_121(self):
125-
self.assertEqual({(121, 660, 671), (121, 7320, 7321)}, pythagorean_triples(121, False))
126-
127-
def test_153(self):
59+
def test_number_B(self):
60+
self.assertEqual({(3, 4, 5)}, pythagorean_triples(3))
61+
self.assertEqual({(13, 84, 85)}, pythagorean_triples(13))
62+
self.assertEqual({(271, 36720, 36721)}, pythagorean_triples(271))
63+
self.assertEqual({(121, 660, 671), (121, 7320, 7321)}, pythagorean_triples(121))
12864
self.assertEqual({(153, 104, 185), (153, 204, 255), (153, 420, 447), (153, 680, 697), (153, 1296, 1305),
129-
(153, 3900, 3903), (153, 11704, 11705)}, pythagorean_triples(153, False))
130-
131-
def test_235(self):
65+
(153, 3900, 3903), (153, 11704, 11705)}, pythagorean_triples(153))
13266
self.assertEqual({(235, 564, 611), (235, 1092, 1117), (235, 5520, 5525), (235, 27612, 27613)},
133-
pythagorean_triples(235, False))
134-
135-
def test_random(self):
136-
r = randint(10, 1000)
137-
while r % 2 == 0:
138-
r //= 2
139-
res = pythagorean_triples(r, False)
140-
for t in res:
141-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
142-
for n in t:
143-
self.assertIs(int, type(n))
144-
145-
def test_random_huge(self):
146-
r = randint(100, 1000000)
147-
while r % 2 == 0:
148-
r //= 2
149-
res = pythagorean_triples(r, False)
150-
for t in res:
151-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
152-
for n in t:
153-
self.assertIs(int, type(n))
154-
155-
156-
class TestCPrimitive(unittest.TestCase):
157-
def test_12(self):
67+
pythagorean_triples(235))
68+
69+
def test_random_BP(self):
70+
for _ in range(10):
71+
a = randint(10_000_000, 100_000_000) * 2 + 1
72+
primitive_triples = pythagorean_triples(a, True)
73+
for t in primitive_triples:
74+
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
75+
self.assertTrue(is_primitive(t), f'{t} is not primitive')
76+
77+
def test_random_B(self):
78+
for _ in range(10):
79+
a = randint(10_000_000, 100_000_000) * 2 + 1
80+
triples = pythagorean_triples(a)
81+
for t in triples:
82+
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
83+
84+
def test_number_CP(self):
15885
self.assertEqual({(12, 5, 13), (12, 35, 37)}, pythagorean_triples(12, True))
159-
160-
def test_14(self):
16186
self.assertEqual(set(), pythagorean_triples(14, True))
162-
163-
def test_34(self):
16487
self.assertEqual(set(), pythagorean_triples(34, True))
165-
166-
def test_56(self):
16788
self.assertEqual({(56, 33, 65), (56, 783, 785)}, pythagorean_triples(56, True))
89+
self.assertEqual({(68, 285, 293), (68, 1155, 1157)}, pythagorean_triples(68, True))
90+
self.assertEqual(set(), pythagorean_triples(126, True))
16891

169-
def test_random(self):
170-
res = pythagorean_triples(randint(10, 100) * 2, True)
171-
for t in res:
172-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
173-
self.assertTrue(is_primitive(t), f"{t} is not primitive")
174-
for n in t:
175-
self.assertIs(int, type(n))
176-
177-
def test_random_huge(self):
178-
res = pythagorean_triples(randint(100, 100000) * 2, True)
179-
for t in res:
180-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
181-
self.assertTrue(is_primitive(t), f"{t} is not primitive")
182-
for n in t:
183-
self.assertIs(int, type(n))
184-
185-
186-
class TestC(unittest.TestCase):
187-
def test_12(self):
188-
self.assertEqual({(12, 5, 13), (12, 35, 37), (12, 9, 15), (12, 16, 20)}, pythagorean_triples(12, False))
189-
190-
def test_14(self):
191-
self.assertEqual({(14, 48, 50)}, pythagorean_triples(14, False))
192-
193-
def test_34(self):
194-
self.assertEqual({(34, 288, 290)}, pythagorean_triples(34, False))
195-
196-
def test_56(self):
92+
def test_number_C(self):
93+
self.assertEqual({(12, 5, 13), (12, 35, 37), (12, 9, 15), (12, 16, 20)}, pythagorean_triples(12))
94+
self.assertEqual({(14, 48, 50)}, pythagorean_triples(14))
95+
self.assertEqual({(34, 288, 290)}, pythagorean_triples(34))
19796
self.assertEqual({(56, 33, 65), (56, 42, 70), (56, 90, 106), (56, 105, 119), (56, 192, 200), (56, 390, 394),
198-
(56, 783, 785)}, pythagorean_triples(56, False))
199-
200-
def test_random(self):
201-
res = pythagorean_triples(randint(10, 100) * 2, False)
202-
for t in res:
203-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
204-
for n in t:
205-
self.assertIs(int, type(n))
206-
207-
def test_random_huge(self):
208-
res = pythagorean_triples(randint(100, 100000) * 2, False)
209-
for t in res:
210-
self.assertEqual(t[2] ** 2, t[0] ** 2 + t[1] ** 2, f"{t} is not Pythagorean triple")
211-
for n in t:
212-
self.assertIs(int, type(n))
97+
(56, 783, 785)}, pythagorean_triples(56))
98+
self.assertEqual({(68, 51, 85), (68, 285, 293), (68, 576, 580), (68, 1155, 1157)},
99+
pythagorean_triples(68))
100+
self.assertEqual({(126, 32, 130), (126, 120, 174), (126, 168, 210), (126, 432, 450), (126, 560, 574),
101+
(126, 1320, 1326), (126, 3968, 3970)}, pythagorean_triples(126))
102+
103+
def test_random_CP(self):
104+
for _ in range(10):
105+
a = randint(10_000_000, 100_000_000) * 2
106+
primitive_triples = pythagorean_triples(a, True)
107+
for t in primitive_triples:
108+
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
109+
self.assertTrue(is_primitive(t), f'{t} is not primitive')
110+
111+
def test_random_C(self):
112+
for _ in range(10):
113+
a = randint(10_000_000, 100_000_000) * 2
114+
triples = pythagorean_triples(a)
115+
for t in triples:
116+
self.assertTrue(is_pythagorean_triple(t), f"{t} is not Pythagorean triple")
213117

214118

215119
if __name__ == '__main__':

0 commit comments

Comments
 (0)