1
|
|
|
""" |
2
|
|
|
:mod:`tests.unit.test_grouptheory` -- Unit Tests |
3
|
|
|
================================================ |
4
|
|
|
|
5
|
|
|
.. module:: tests.unit.test_grouptheory |
6
|
|
|
:synopsis: Unit tests for the lib.grouptheory module. |
7
|
|
|
|
8
|
|
|
.. moduleauthor:: Bill Maroney <[email protected]> |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
import pytest |
12
|
|
|
|
13
|
|
|
from lib.grouptheory import multiplicative_order |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
@pytest.mark.parametrize("a,n,order", [(2, 7, 3), (10, 99, 2), (937, 9008, 562), (2, 11111111111, 6158856)]) |
|
|
|
|
17
|
|
|
def test_multiplicative_order_correctness(a: int, n: int, order: int): |
|
|
|
|
18
|
|
|
""" Test the correctness of :func:`lib.grouptheory.multiplicative_order` using known answer tests |
|
|
|
|
19
|
|
|
|
20
|
|
|
:param a: the base input value |
21
|
|
|
:param n: the modulus defining the multiplicative group |
22
|
|
|
:param order: the expected answer, i.e. :math:`ord_n(a)` |
23
|
|
|
:raises AssertionError: if :func:`lib.grouptheory.multiplicative_order` returns the wrong type |
24
|
|
|
:raises AssertionError: if :func:`lib.grouptheory.multiplicative_order` returns the wrong value |
25
|
|
|
""" |
26
|
|
|
|
27
|
|
|
computed_answer = multiplicative_order(a, n) |
28
|
|
|
assert isinstance(computed_answer, multiplicative_order.__annotations__["return"]), "wrong type" |
29
|
|
|
assert computed_answer == order, "wrong answer" |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def test_multiplicative_order_bad_a_type_str(): |
|
|
|
|
33
|
|
|
""" Test that :func:`lib.grouptheory.multiplicative_order` raises a ``TypeError`` for a non-``int`` value for `a` |
|
|
|
|
34
|
|
|
(``str``) |
35
|
|
|
""" |
36
|
|
|
with pytest.raises(TypeError): |
37
|
|
|
multiplicative_order("123", 10) |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_multiplicative_order_bad_a_type_float(): |
|
|
|
|
41
|
|
|
""" Test that :func:`lib.grouptheory.multiplicative_order` raises a ``TypeError`` for a non-``int`` value for `a` |
|
|
|
|
42
|
|
|
(``float``) |
43
|
|
|
""" |
44
|
|
|
with pytest.raises(TypeError): |
45
|
|
|
multiplicative_order(123.0, 10) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def test_multiplicative_order_bad_n_type_str(): |
|
|
|
|
49
|
|
|
""" Test that :func:`lib.grouptheory.multiplicative_order` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
50
|
|
|
(``str``) |
51
|
|
|
""" |
52
|
|
|
with pytest.raises(TypeError): |
53
|
|
|
multiplicative_order(2, "123") |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_multiplicative_order_bad_n_type_float(): |
|
|
|
|
57
|
|
|
""" Test that :func:`lib.grouptheory.multiplicative_order` raises a ``TypeError`` for a non-``int`` value for `n` |
|
|
|
|
58
|
|
|
(``float``) |
59
|
|
|
""" |
60
|
|
|
with pytest.raises(TypeError): |
61
|
|
|
multiplicative_order(2, 123.0) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_multiplicative_order_bad_n_zero(): |
|
|
|
|
65
|
|
|
""" Test that :func:`lib.grouptheory.multiplicative_order` raises a ``ValueError`` for a non-positive value for `n` |
|
|
|
|
66
|
|
|
""" |
67
|
|
|
with pytest.raises(ValueError): |
68
|
|
|
multiplicative_order(2, 0) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_multiplicative_order_bad_a_n_not_coprime(): |
|
|
|
|
72
|
|
|
""" Test that :func:`lib.grouptheory.multiplicative_order` raises a ``ValueError`` for non co-prime `a,n` """ |
|
|
|
|
73
|
|
|
with pytest.raises(ValueError): |
74
|
|
|
multiplicative_order(2, 4) |
75
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.