Skip to content

Commit

Permalink
Add more is_prime() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent 4c16712 commit d77bb3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions scripts/generate_int_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,13 @@ def save_pickle(d, folder, name):
Z[i] = int(z)
d = {"X": X, "Z": Z}
save_pickle(d, FOLDER, "next_prime.pkl")

set_seed(SEED + 305)
X = [random.randint(-100, 100) for _ in range(20)] + [random.randint(100, 1_000_000_000) for _ in range(20)]
Z = [0,]*len(X)
for i in range(len(X)):
x = X[i]
z = is_prime(x)
Z[i] = bool(z)
d = {"X": X, "Z": Z}
save_pickle(d, FOLDER, "is_prime.pkl")
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ def prev_prime():
@pytest.fixture(scope="session")
def next_prime():
return read_pickle("next_prime.pkl")


@pytest.fixture(scope="session")
def is_prime():
return read_pickle("is_prime.pkl")
Binary file added tests/data/is_prime.pkl
Binary file not shown.
8 changes: 7 additions & 1 deletion tests/test_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_is_prime_exceptions():
galois.is_prime(13.0)


def test_is_prime():
def test_is_prime_oeis():
# https://oeis.org/A000040
primes = np.array([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271])
n = np.arange(1, primes[-1] + 1)
Expand All @@ -123,6 +123,12 @@ def test_is_prime():
assert [galois.is_prime(ni) for ni in n] == is_prime.tolist()


def test_is_prime(is_prime):
X, Z = is_prime["X"], is_prime["Z"]
for i in range(len(X)):
assert galois.is_prime(X[i]) == Z[i]


def test_is_composite_exceptions():
with pytest.raises(TypeError):
galois.is_composite(13.0)
Expand Down

0 comments on commit d77bb3e

Please sign in to comment.