fork download
  1. import unittest
  2.  
  3.  
  4. def is_prime(n):
  5. """Checks if a number is prime."""
  6. if n <= 1:
  7. return False
  8. for i in range(2, int(n**0.5) + 1):
  9. if n % i == 0:
  10. return False
  11. return True
  12.  
  13.  
  14. def sum_of_first_n_primes(n):
  15. """Calculates the sum of the first n prime numbers."""
  16. count = 0
  17. num = 2
  18. prime_sum = 0
  19. while count < n:
  20. if is_prime(num):
  21. prime_sum += num
  22. count += 1
  23. num += 1
  24. return prime_sum
  25.  
  26.  
  27. class TestPrimes(unittest.TestCase):
  28.  
  29. def test_is_prime_non_positive(self):
  30. test_name = "test_is_prime_non_positive"
  31. try:
  32. self.assertFalse(is_prime(0))
  33. self.assertFalse(is_prime(-1))
  34. self.assertFalse(is_prime(-2))
  35. print(f"{test_name}: *** test_passed ***")
  36. except AssertionError:
  37. print(f"{test_name}: *** assertion_failed ***")
  38. except Exception as e:
  39. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  40.  
  41. def test_is_prime_non_integer(self):
  42. test_name = "test_is_prime_non_integer"
  43. try:
  44. self.assertFalse(is_prime("a"))
  45. self.assertFalse(is_prime(1.5))
  46. self.assertFalse(is_prime([1, 2, 3]))
  47. print(f"{test_name}: *** test_passed ***")
  48. except AssertionError:
  49. print(f"{test_name}: *** assertion_failed ***")
  50. except Exception as e:
  51. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  52.  
  53. def test_is_prime_true(self):
  54. test_name = "test_is_prime_true"
  55. try:
  56. self.assertTrue(is_prime(2))
  57. self.assertTrue(is_prime(3))
  58. self.assertTrue(is_prime(5))
  59. self.assertTrue(is_prime(7))
  60. self.assertTrue(is_prime(11))
  61. print(f"{test_name}: *** test_passed ***")
  62. except AssertionError:
  63. print(f"{test_name}: *** assertion_failed ***")
  64. except Exception as e:
  65. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  66.  
  67. def test_is_prime_false(self):
  68. test_name = "test_is_prime_false"
  69. try:
  70. self.assertFalse(is_prime(4))
  71. self.assertFalse(is_prime(6))
  72. self.assertFalse(is_prime(8))
  73. self.assertFalse(is_prime(9))
  74. self.assertFalse(is_prime(10))
  75. print(f"{test_name}: *** test_passed ***")
  76. except AssertionError:
  77. print(f"{test_name}: *** assertion_failed ***")
  78. except Exception as e:
  79. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  80.  
  81. def test_sum_of_first_n_primes_invalid_input(self):
  82. test_name = "test_sum_of_first_n_primes_invalid_input"
  83. try:
  84. self.assertEqual(sum_of_first_n_primes(0), 0)
  85. self.assertEqual(sum_of_first_n_primes(-1), 0)
  86. self.assertEqual(sum_of_first_n_primes("abc"), 0)
  87. self.assertEqual(sum_of_first_n_primes(3.14), 0)
  88. print(f"{test_name}: *** test_passed ***")
  89. except AssertionError:
  90. print(f"{test_name}: *** assertion_failed ***")
  91. except Exception as e:
  92. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  93.  
  94. def test_sum_of_first_n_primes_small_n(self):
  95. test_name = "test_sum_of_first_n_primes_small_n"
  96. try:
  97. self.assertEqual(sum_of_first_n_primes(1), 2)
  98. self.assertEqual(sum_of_first_n_primes(2), 5)
  99. self.assertEqual(sum_of_first_n_primes(3), 10)
  100. self.assertEqual(sum_of_first_n_primes(4), 17)
  101. print(f"{test_name}: *** test_passed ***")
  102. except AssertionError:
  103. print(f"{test_name}: *** assertion_failed ***")
  104. except Exception as e:
  105. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  106.  
  107. def test_sum_of_first_n_primes_larger_n(self):
  108. test_name = "test_sum_of_first_n_primes_larger_n"
  109. try:
  110. self.assertEqual(sum_of_first_n_primes(5), 28)
  111. print(f"{test_name}: *** test_passed ***")
  112. except AssertionError:
  113. print(f"{test_name}: *** assertion_failed ***")
  114. except Exception as e:
  115. print(f"{test_name}: *** exception_raised ***, error message: {e}")
  116.  
  117.  
  118. if __name__ == '__main__':
  119. unittest.main()
  120.  
Success #stdin #stdout #stderr 0.07s 11872KB
stdin
Standard input is empty
stdout
test_is_prime_false: *** test_passed ***
test_is_prime_non_integer: *** exception_raised ***, error message: '<=' not supported between instances of 'str' and 'int'
test_is_prime_non_positive: *** test_passed ***
test_is_prime_true: *** test_passed ***
test_sum_of_first_n_primes_invalid_input: *** exception_raised ***, error message: '<' not supported between instances of 'int' and 'str'
test_sum_of_first_n_primes_larger_n: *** test_passed ***
test_sum_of_first_n_primes_small_n: *** test_passed ***
stderr
.......
----------------------------------------------------------------------
Ran 7 tests in 0.001s

OK