fork download
  1. import unittest
  2. import sys
  3.  
  4.  
  5. def is_prime(n):
  6. """Checks if a number is prime."""
  7. if not isinstance(n, int) or n <= 1:
  8. return False
  9. for i in range(2, int(n**0.5) + 1):
  10. if n % i == 0:
  11. return False
  12. return True
  13.  
  14.  
  15. def sum_of_first_n_primes(n):
  16. """Calculates the sum of the first n prime numbers."""
  17. if not isinstance(n, int) or n <= 0:
  18. return 0
  19. count = 0
  20. num = 2
  21. prime_sum = 0
  22. while count < n:
  23. if is_prime(num):
  24. prime_sum += num
  25. count += 1
  26. num += 1
  27. return prime_sum
  28.  
  29.  
  30. class TestPrimeFunctions(unittest.TestCase):
  31.  
  32. def test_is_prime_small_primes(self):
  33. self.assertTrue(is_prime(2))
  34. self.assertTrue(is_prime(3))
  35. self.assertTrue(is_prime(5))
  36. self.assertTrue(is_prime(7))
  37. self.assertTrue(is_prime(11))
  38.  
  39. def test_is_prime_small_composites(self):
  40. self.assertFalse(is_prime(1))
  41. self.assertFalse(is_prime(4))
  42. self.assertFalse(is_prime(6))
  43. self.assertFalse(is_prime(8))
  44. self.assertFalse(is_prime(9))
  45.  
  46. def test_is_prime_edge_cases(self):
  47. self.assertFalse(is_prime(0))
  48. self.assertFalse(is_prime(-1))
  49. self.assertTrue(is_prime(101))
  50.  
  51. def test_sum_of_first_n_primes_small_n(self):
  52. self.assertEqual(sum_of_first_n_primes(1), 2)
  53. self.assertEqual(sum_of_first_n_primes(2), 5)
  54. self.assertEqual(sum_of_first_n_primes(3), 10)
  55. self.assertEqual(sum_of_first_n_primes(4), 17)
  56. self.assertEqual(sum_of_first_n_primes(5), 28)
  57.  
  58. def test_sum_of_first_n_primes_zero_n(self):
  59. self.assertEqual(sum_of_first_n_primes(0), 0)
  60.  
  61. def test_sum_of_first_n_primes_larger_n(self):
  62. self.assertEqual(sum_of_first_n_primes(10), 129)
  63.  
  64. def test_sum_of_first_n_primes_none(self):
  65. self.assertEqual(sum_of_first_n_primes(None), 0)
  66.  
  67. def test_is_prime_none(self):
  68. self.assertEqual(is_prime(None), False)
  69.  
  70. def test_sum_of_first_n_primes_string(self):
  71. self.assertEqual(sum_of_first_n_primes(''), 0)
  72.  
  73. def test_is_prime_string(self):
  74. self.assertEqual(is_prime(''), False)
  75.  
  76. def test_sum_of_first_n_primes_boolean(self):
  77. self.assertEqual(sum_of_first_n_primes(False), 0)
  78.  
  79. def test_is_prime_boolean(self):
  80. self.assertEqual(is_prime(False), False)
  81.  
  82.  
  83. class CustomTestResult(unittest.TestResult):
  84. def __init__(self, stream):
  85. super().__init__()
  86. self.stream = stream
  87.  
  88. def addSuccess(self, test):
  89. self.stream.write("*** test_passed ***\n")
  90.  
  91. def addFailure(self, test, err):
  92. self.stream.write("*** assertion_failed ***\n")
  93.  
  94. def addError(self, test, err):
  95. error_message = str(err[1])
  96. self.stream.write(
  97. f"*** exception_raised ***, error message: {error_message}\n")
  98.  
  99.  
  100. class CustomTestRunner(unittest.TextTestRunner):
  101. def run(self, test):
  102. result = CustomTestResult(self.stream)
  103. test(result)
  104. return result
  105.  
  106.  
  107. if __name__ == '__main__':
  108. suite = unittest.TestLoader().loadTestsFromTestCase(TestPrimeFunctions)
  109. runner = CustomTestRunner(stream=sys.stdout)
  110. runner.run(suite)
  111.  
Success #stdin #stdout 0.06s 10700KB
stdin
Standard input is empty
stdout
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***
*** test_passed ***