import unittest
def is_prime(n):
"""Checks if a number is prime."""
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def sum_of_first_n_primes(n):
"""Calculates the sum of the first n prime numbers."""
count = 0
num = 2
prime_sum = 0
while count < n:
if is_prime(num):
prime_sum += num
count += 1
num += 1
return prime_sum
class TestPrimes(unittest.TestCase):
def test_is_prime_non_positive(self):
test_name = "test_is_prime_non_positive"
try:
self.assertFalse(is_prime(0))
self.assertFalse(is_prime(-1))
self.assertFalse(is_prime(-2))
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
def test_is_prime_non_integer(self):
test_name = "test_is_prime_non_integer"
try:
self.assertFalse(is_prime("a"))
self.assertFalse(is_prime(1.5))
self.assertFalse(is_prime([1, 2, 3]))
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
def test_is_prime_true(self):
test_name = "test_is_prime_true"
try:
self.assertTrue(is_prime(2))
self.assertTrue(is_prime(3))
self.assertTrue(is_prime(5))
self.assertTrue(is_prime(7))
self.assertTrue(is_prime(11))
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
def test_is_prime_false(self):
test_name = "test_is_prime_false"
try:
self.assertFalse(is_prime(4))
self.assertFalse(is_prime(6))
self.assertFalse(is_prime(8))
self.assertFalse(is_prime(9))
self.assertFalse(is_prime(10))
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
def test_sum_of_first_n_primes_invalid_input(self):
test_name = "test_sum_of_first_n_primes_invalid_input"
try:
self.assertEqual(sum_of_first_n_primes(0), 0)
self.assertEqual(sum_of_first_n_primes(-1), 0)
self.assertEqual(sum_of_first_n_primes("abc"), 0)
self.assertEqual(sum_of_first_n_primes(3.14), 0)
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
def test_sum_of_first_n_primes_small_n(self):
test_name = "test_sum_of_first_n_primes_small_n"
try:
self.assertEqual(sum_of_first_n_primes(1), 2)
self.assertEqual(sum_of_first_n_primes(2), 5)
self.assertEqual(sum_of_first_n_primes(3), 10)
self.assertEqual(sum_of_first_n_primes(4), 17)
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
def test_sum_of_first_n_primes_larger_n(self):
test_name = "test_sum_of_first_n_primes_larger_n"
try:
self.assertEqual(sum_of_first_n_primes(5), 28)
print(f"{test_name}: *** test_passed ***")
except AssertionError:
print(f"{test_name}: *** assertion_failed ***")
except Exception as e:
print(f"{test_name}: *** exception_raised ***, error message: {e}")
if __name__ == '__main__':
unittest.main()