Skip to content

Commit c628f7a

Browse files
committed
Use Hypothesis for n_primes tests (#44)
1 parent 493ca65 commit c628f7a

File tree

2 files changed

+7
-40
lines changed

2 files changed

+7
-40
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
- python: nightly
1313
install:
1414
- pip install mypy
15+
- pip install hypothesis
1516
- pip install coverage
1617
- pip install pylint
1718
script:

test.py

+6-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import unittest
22
import random
3+
from hypothesis import given, settings
4+
import hypothesis.strategies as st
35
from math import sqrt
46
from operator import mul
57
from primes import *
@@ -143,46 +145,10 @@ def testNPrimes5(self):
143145
def testNPrimes100(self):
144146
self.assertEqual(n_primes(100), primes_up_to(542))
145147

146-
def testNPrimes500(self):
147-
for i in range(500):
148-
with self.subTest(i=i):
149-
p = primes_up_to(i)
150-
self.assertEqual(n_primes(len(p)), p)
151-
152-
def testNPrimes1000(self):
153-
for i in range(1000):
154-
with self.subTest(i=i):
155-
self.assertEqual(len(n_primes(i)), i)
156-
157-
def testNPrimes10000(self):
158-
for i in range(10000, 10050):
159-
with self.subTest(i=i):
160-
self.assertEqual(len(n_primes(i)), i)
161-
162-
def testNPrimes16000(self):
163-
for i in range(16000, 16005):
164-
with self.subTest(i=i):
165-
self.assertEqual(len(n_primes(i)), i)
166-
167-
def testNPrimes40000(self):
168-
for i in range(40000, 40005):
169-
with self.subTest(i=i):
170-
self.assertEqual(len(n_primes(i)), i)
171-
172-
def testNPrimes180000(self):
173-
for i in range(180000, 180005):
174-
with self.subTest(i=i):
175-
self.assertEqual(len(n_primes(i)), i)
176-
177-
def testNPrimes700000(self):
178-
for i in range(700000, 700005):
179-
with self.subTest(i=i):
180-
self.assertEqual(len(n_primes(i)), i)
181-
182-
def testNPrimes8010000(self):
183-
for i in range(8010000, 8010002):
184-
with self.subTest(i=i):
185-
self.assertEqual(len(n_primes(i)), i)
148+
@given(st.integers(min_value=0, max_value=100_000))
149+
@settings(max_examples=20)
150+
def testNPrimes(self, i):
151+
self.assertEqual(len(n_primes(i)), i)
186152

187153
class TestNthPrime(unittest.TestCase):
188154
def testNthPrime1000(self):

0 commit comments

Comments
 (0)