Skip to content

Commit

Permalink
Add unit tests for integer logarithms
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Feb 10, 2022
1 parent d77b1ee commit d68f537
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion galois/_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def ilog(n: int, b: int) -> int:
n : int
A positive integer.
b : int
The logarithm base :math:`b`.
The logarithm base :math:`b`, must be at least 2.
Returns
-------
Expand Down
14 changes: 13 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, xgcd, lcm, prod, isqrt
from sage.all import Integer, xgcd, lcm, prod, isqrt, log

PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tests")
FOLDER = os.path.join(PATH, "data")
Expand Down Expand Up @@ -108,3 +108,15 @@ def save_pickle(d, folder, name):
Z[i] = int(z)
d = {"X": X, "R": R, "Z": Z}
save_pickle(d, FOLDER, "iroot.pkl")

set_seed(SEED + 107)
X = [random.randint(1, 1000) for _ in range(20)] + [random.randint(1000, 1_000_000_000) for _ in range(20)]
B = [random.randint(2, 6) for _ in range(40)]
Z = [0,]*len(X)
for i in range(len(X)):
x = X[i]
b = B[i]
z = log(Integer(x), b)
Z[i] = int(z)
d = {"X": X, "B": B, "Z": Z}
save_pickle(d, FOLDER, "ilog.pkl")
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ def iroot():
with open(os.path.join(FOLDER, "iroot.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
return d
return d


@pytest.fixture(scope="session")
def ilog():
with open(os.path.join(FOLDER, "ilog.pkl"), "rb") as f:
print(f"Loading {f}...")
d = pickle.load(f)
return d
Binary file added tests/data/ilog.pkl
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,24 @@ def test_iroot(iroot):

galois.iroot(10, 1) == 10
assert galois.iroot(0, 2) == 0


def test_ilog_exceptions():
with pytest.raises(TypeError):
galois.ilog(9.0, 2)
with pytest.raises(TypeError):
galois.ilog(9, 2.0)
with pytest.raises(ValueError):
galois.ilog(0, 2)
with pytest.raises(ValueError):
galois.ilog(-9, 2)
with pytest.raises(ValueError):
galois.ilog(9, 1)


def test_ilog(ilog):
X, B, Z = ilog["X"], ilog["B"], ilog["Z"]
for i in range(len(X)):
assert galois.ilog(X[i], B[i]) == Z[i]

assert galois.ilog(10, 10) == 1
33 changes: 0 additions & 33 deletions tests/test_number_theory.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,36 +203,3 @@ def test_is_cyclic():
assert galois.is_cyclic(2*5*7*9) == False

assert all(galois.is_cyclic(n) == (galois.euler_phi(n) == galois.carmichael_lambda(n)) for n in range(1, 100))


###############################################################################
# Integer arithmetic
###############################################################################

def test_ilog_exceptions():
with pytest.raises(TypeError):
galois.ilog(9.0, 2)
with pytest.raises(TypeError):
galois.ilog(9, 2.0)
with pytest.raises(ValueError):
galois.ilog(-9, 2)
with pytest.raises(ValueError):
galois.ilog(9, 1)


# TODO: Find a way to generate test vectors with Sage
def test_ilog():
p = galois.mersenne_primes(2000)[-1]
exponent = galois.ilog(p, 17)
assert isinstance(exponent, int)
assert 17**exponent <= p and not 17**(exponent + 1) <= p

p = galois.mersenne_primes(2000)[-1] - 1
exponent = galois.ilog(p, 17)
assert isinstance(exponent, int)
assert 17**exponent <= p and not 17**(exponent + 1) <= p

p = galois.mersenne_primes(2000)[-1] + 1
exponent = galois.ilog(p, 17)
assert isinstance(exponent, int)
assert 17**exponent <= p and not 17**(exponent + 1) <= p

0 comments on commit d68f537

Please sign in to comment.