Skip to content

Commit

Permalink
Add more is_prime_power() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent 2153cef commit a70d95b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
22 changes: 21 additions & 1 deletion scripts/generate_int_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import sage
import numpy as np
from sage.all import Integer, Integers, xgcd, lcm, prod, isqrt, log, crt, euler_phi, prime_range, nth_prime, previous_prime, next_prime, is_prime
from sage.all import Integer, Integers, xgcd, lcm, prod, isqrt, log, crt, euler_phi, prime_range, nth_prime, previous_prime, next_prime, is_prime, is_prime_power
from sage.crypto.util import carmichael_lambda

PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tests")
Expand Down Expand Up @@ -230,3 +230,23 @@ def save_pickle(d, folder, name):
Z[i] = bool(z)
d = {"X": X, "Z": Z}
save_pickle(d, FOLDER, "is_prime.pkl")

# set_seed(SEED + 306)
# 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_composite.pkl")

set_seed(SEED + 307)
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_power(x)
Z[i] = bool(z)
d = {"X": X, "Z": Z}
save_pickle(d, FOLDER, "is_prime_power.pkl")
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ def next_prime():
@pytest.fixture(scope="session")
def is_prime():
return read_pickle("is_prime.pkl")


@pytest.fixture(scope="session")
def is_prime_power():
return read_pickle("is_prime_power.pkl")
Binary file added tests/data/is_prime_power.pkl
Binary file not shown.
9 changes: 0 additions & 9 deletions tests/test_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,6 @@ def test_divisor_sigma():
assert [galois.divisor_sigma(n, k=3) for n in range(1, 43)] == sigma_3


def test_is_prime_power():
# https://oeis.org/A246655
prime_powers = np.array([2,3,4,5,7,8,9,11,13,16,17,19,23,25,27,29,31,32,37,41,43,47,49,53,59,61,64,67,71,73,79,81,83,89,97,101,103,107,109,113,121,125,127,128,131,137,139,149,151,157,163,167,169,173,179,181,191,193,197,199,211])
n = np.arange(1, prime_powers[-1] + 1)
is_prime_power = np.zeros(n.size, dtype=bool)
is_prime_power[prime_powers - 1] = True # -1 for 1-indexed
assert [galois.is_prime_power(ni) for ni in n] == is_prime_power.tolist()


def test_is_perfect_power():
# https://oeis.org/A001597
perfect_powers = np.array([1,4,8,9,16,25,27,32,36,49,64,81,100,121,125,128,144,169,196,216,225,243,256,289,324,343,361,400,441,484,512,529,576,625,676,729,784,841,900,961,1000,1024,1089,1156,1225,1296,1331,1369,1444,1521,1600,1681,1728,1764])
Expand Down
11 changes: 11 additions & 0 deletions tests/test_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ def test_is_composite():
assert [galois.is_composite(ni) for ni in n] == is_composite.tolist()


def test_is_prime_power_exceptions():
with pytest.raises(TypeError):
galois.is_prime_power(13.0)


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


def test_fermat_primality_test_exceptions():
with pytest.raises(TypeError):
galois.fermat_primality_test(13.0)
Expand Down

0 comments on commit a70d95b

Please sign in to comment.