55from pythagorean_tuples import pythagorean_triples
66
77
8+ def is_primitive (t ):
9+ return math .gcd (t [0 ], t [1 ]) == 1 and math .gcd (t [1 ], t [2 ]) == 1 and math .gcd (t [2 ], t [0 ]) == 1
10+
11+
812class TestExceptions (unittest .TestCase ):
913 def test_not_integer (self ):
1014 self .assertRaises (TypeError , pythagorean_triples , 3.14 , "TypeError has not been raised" )
@@ -27,15 +31,15 @@ def test_random(self):
2731 res = pythagorean_triples (2 ** randint (10 , 20 ), True )
2832 for t in res :
2933 self .assertEqual (t [2 ] ** 2 , t [0 ] ** 2 + t [1 ] ** 2 , f"{ t } is not Pythagorean triple" )
30- self .assertEqual ( 1 , math . gcd ( * t ), f"{ t } is not primitive" )
34+ self .assertTrue ( is_primitive ( t ), f"{ t } is not primitive" )
3135 for n in t :
3236 self .assertIs (int , type (n ))
3337
3438 def test_random_huge (self ):
3539 res = pythagorean_triples (2 ** randint (100 , 200 ), True )
3640 for t in res :
3741 self .assertEqual (t [2 ] ** 2 , t [0 ] ** 2 + t [1 ] ** 2 , f"{ t } is not Pythagorean triple" )
38- self .assertEqual ( 1 , math . gcd ( * t ), f"{ t } is not primitive" )
42+ self .assertTrue ( is_primitive ( t ), f"{ t } is not primitive" )
3943 for n in t :
4044 self .assertIs (int , type (n ))
4145
@@ -91,7 +95,7 @@ def test_random(self):
9195 res = pythagorean_triples (r , True )
9296 for t in res :
9397 self .assertEqual (t [2 ] ** 2 , t [0 ] ** 2 + t [1 ] ** 2 , f"{ t } is not Pythagorean triple" )
94- self .assertEqual ( 1 , math . gcd ( * t ), f"{ t } is not primitive" )
98+ self .assertTrue ( is_primitive ( t ), f"{ t } is not primitive" )
9599 for n in t :
96100 self .assertIs (int , type (n ))
97101
@@ -102,7 +106,7 @@ def test_random_huge(self):
102106 res = pythagorean_triples (r , True )
103107 for t in res :
104108 self .assertEqual (t [2 ] ** 2 , t [0 ] ** 2 + t [1 ] ** 2 , f"{ t } is not Pythagorean triple" )
105- self .assertEqual ( 1 , math . gcd ( * t ), f"{ t } is not primitive" )
109+ self .assertTrue ( is_primitive ( t ), f"{ t } is not primitive" )
106110 for n in t :
107111 self .assertIs (int , type (n ))
108112
@@ -166,15 +170,15 @@ def test_random(self):
166170 res = pythagorean_triples (randint (10 , 100 ) * 2 , True )
167171 for t in res :
168172 self .assertEqual (t [2 ] ** 2 , t [0 ] ** 2 + t [1 ] ** 2 , f"{ t } is not Pythagorean triple" )
169- self .assertEqual ( 1 , math . gcd ( * t ), f"{ t } is not primitive" )
173+ self .assertTrue ( is_primitive ( t ), f"{ t } is not primitive" )
170174 for n in t :
171175 self .assertIs (int , type (n ))
172176
173177 def test_random_huge (self ):
174178 res = pythagorean_triples (randint (100 , 100000 ) * 2 , True )
175179 for t in res :
176180 self .assertEqual (t [2 ] ** 2 , t [0 ] ** 2 + t [1 ] ** 2 , f"{ t } is not Pythagorean triple" )
177- self .assertEqual ( 1 , math . gcd ( * t ), f"{ t } is not primitive" )
181+ self .assertTrue ( is_primitive ( t ), f"{ t } is not primitive" )
178182 for n in t :
179183 self .assertIs (int , type (n ))
180184
0 commit comments