From d77bb3e3d361045addb47d00480de47c1abba62b Mon Sep 17 00:00:00 2001 From: mhostetter Date: Tue, 8 Feb 2022 12:00:18 -0500 Subject: [PATCH] Add more `is_prime()` unit tests --- scripts/generate_int_test_vectors.py | 10 ++++++++++ tests/conftest.py | 5 +++++ tests/data/is_prime.pkl | Bin 0 -> 248 bytes tests/test_primes.py | 8 +++++++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/data/is_prime.pkl diff --git a/scripts/generate_int_test_vectors.py b/scripts/generate_int_test_vectors.py index 36aabfb42..1b60673d8 100644 --- a/scripts/generate_int_test_vectors.py +++ b/scripts/generate_int_test_vectors.py @@ -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") diff --git a/tests/conftest.py b/tests/conftest.py index 5847a41fb..6dd1f574f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/data/is_prime.pkl b/tests/data/is_prime.pkl new file mode 100644 index 0000000000000000000000000000000000000000..16659c7e7346865f33c8f4e8a8e0304bd9dcf80c GIT binary patch literal 248 zcmZo*nfjIi0&1sd^e{$DiJhY1_5A<;|6b>SgtxfYJP;d*|9ia$321ul1hEf+h|M74 zH%L_9YcYiS2BHjr(8_BCh-vC|M0lsLSJSI$%3h0}Dyw>J`+AGji>Yggf!E^=wwhjA zM*gf`DyI8cy!;Nda(F$ybd}31@#sqnud@Pj)?S~gflQ4VT=N~yR);S6M}&Z5EIG)vyh0?Qau1`3VZkf literal 0 HcmV?d00001 diff --git a/tests/test_primes.py b/tests/test_primes.py index fcbc716b1..1834f5c40 100644 --- a/tests/test_primes.py +++ b/tests/test_primes.py @@ -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) @@ -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)