2023-07-15 15:27:31 -07:00
|
|
|
# These tests are auto-generated with test data from:
|
|
|
|
|
# https://github.com/exercism/problem-specifications/tree/main/exercises/perfect-numbers/canonical-data.json
|
2023-07-21 16:54:40 -07:00
|
|
|
# File last updated on 2023-07-19
|
2023-07-15 15:27:31 -07:00
|
|
|
|
2015-11-26 15:05:58 +01:00
|
|
|
import unittest
|
|
|
|
|
|
2021-01-31 16:49:12 -05:00
|
|
|
from perfect_numbers import (
|
|
|
|
|
classify,
|
|
|
|
|
)
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
|
2015-11-26 15:05:58 +01:00
|
|
|
class PerfectNumbersTest(unittest.TestCase):
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_smallest_perfect_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(6), "perfect")
|
2015-11-26 15:05:58 +01:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_medium_perfect_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(28), "perfect")
|
2015-11-26 15:05:58 +01:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_large_perfect_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(33550336), "perfect")
|
2015-11-26 15:05:58 +01:00
|
|
|
|
2017-11-14 15:31:34 -05:00
|
|
|
|
|
|
|
|
class AbundantNumbersTest(unittest.TestCase):
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_smallest_abundant_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(12), "abundant")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_medium_abundant_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(30), "abundant")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_large_abundant_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(33550335), "abundant")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2015-11-26 15:05:58 +01:00
|
|
|
|
2017-11-14 15:31:34 -05:00
|
|
|
class DeficientNumbersTest(unittest.TestCase):
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_smallest_prime_deficient_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(2), "deficient")
|
2015-11-26 15:05:58 +01:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_smallest_non_prime_deficient_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(4), "deficient")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_medium_deficient_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(32), "deficient")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_large_deficient_number_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(33550337), "deficient")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2019-10-20 04:14:53 -07:00
|
|
|
def test_edge_case_no_factors_other_than_itself_is_classified_correctly(self):
|
2022-09-01 21:20:09 +03:00
|
|
|
self.assertEqual(classify(1), "deficient")
|
2017-11-14 15:31:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidInputsTest(unittest.TestCase):
|
2020-10-15 12:48:47 -04:00
|
|
|
def test_zero_is_rejected_as_it_is_not_a_positive_integer(self):
|
2021-11-02 03:46:05 -07:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2017-11-14 15:31:34 -05:00
|
|
|
classify(0)
|
2021-11-02 03:46:05 -07:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
err.exception.args[0],
|
|
|
|
|
"Classification is only possible for positive integers.",
|
|
|
|
|
)
|
2017-11-14 15:31:34 -05:00
|
|
|
|
2020-10-15 12:48:47 -04:00
|
|
|
def test_negative_integer_is_rejected_as_it_is_not_a_positive_integer(self):
|
2021-11-02 03:46:05 -07:00
|
|
|
with self.assertRaises(ValueError) as err:
|
2017-11-14 15:31:34 -05:00
|
|
|
classify(-1)
|
2021-11-02 03:46:05 -07:00
|
|
|
self.assertEqual(type(err.exception), ValueError)
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
err.exception.args[0],
|
|
|
|
|
"Classification is only possible for positive integers.",
|
|
|
|
|
)
|