Last active
August 29, 2015 14:06
-
-
Save Echocage/cbe71bcefb61110934e2 to your computer and use it in GitHub Desktop.
Primes "dummy exercise"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_next_prime(n): | |
while True: | |
n += 1 | |
prime = isPrime(n) | |
if prime: | |
return n | |
def isPrime(n): | |
for item in xrange(2, n): | |
if n % item == 0: | |
return False | |
return True | |
if __name__ == '__main__': | |
for x in [6, 10, 11]: | |
print find_next_prime(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import mock | |
import primes | |
class TestPrimes(unittest.TestCase): | |
def test_prime_finding(self): | |
with mock.patch('primes.isPrime') as prime_mock: | |
for input, desired_response in [(6, 7), (10, 11), (11, 13)]: | |
prime_mock.side_effect = lambda x: x is desired_response | |
self.assertEqual(primes.find_next_prime(input), desired_response) | |
def test_prime_identification(self): | |
for input, prime in [(6, False), (7, True), (8, False), (11, True), (13, True)]: | |
self.assertEqual(primes.isPrime(input), prime) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment