-
Notifications
You must be signed in to change notification settings - Fork 37
/
polynomialtest.py
155 lines (113 loc) · 4.35 KB
/
polynomialtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import unittest
from polynomial import Polynomial
from ff import GF256int
class TestGFPoly(unittest.TestCase):
"""Tests that the Polynomial class works when given GF256int objects
instead of regular integers
"""
def test_add(self):
one = Polynomial(map(GF256int, (8,3,5,1)))
two = Polynomial(map(GF256int, (5,3,1,1,6,8)))
r = one + two
self.assertEqual(r.coefficients, (5,3,9,2,3,9))
def test_sub(self):
one = Polynomial(map(GF256int, (8,3,5,1)))
two = Polynomial(map(GF256int, (5,3,1,1,6,8)))
r = one - two
self.assertEqual(r.coefficients, (5,3,9,2,3,9))
def test_mul(self):
one = Polynomial(map(GF256int, (8,3,5,1)))
two = Polynomial(map(GF256int, (5,3,1,1,6,8)))
r = one * two
self.assertEqual(r.coefficients, (40,23,28,1,53,78,7,46,8))
def test_div(self):
one = Polynomial(map(GF256int, (8,3,5,1)))
two = Polynomial(map(GF256int, (5,3,1,1,6,8)))
q, r = divmod(two,one)
self.assertEqual(q.coefficients, (101, 152, 11))
self.assertEqual(r.coefficients, (183, 185, 3))
# Make sure they multiply back out okay
self.assertEqual(q*one + r, two)
def test_div_scalar(self):
"""Tests division by a scalar"""
numbers = map(GF256int, (5,20,50,100,134,158,0,148,233,254,4,5,2))
scalar = GF256int(17)
poly = Polynomial(numbers)
scalarpoly = Polynomial(x0=scalar)
self.assertEqual(
(poly // scalarpoly).coefficients,
tuple(map(lambda x: x / scalar, numbers))
)
def test_div_scalar2(self):
"""Test that dividing by a scalar is the same as multiplying by the
scalar's inverse"""
a = Polynomial(map(GF256int, (5,3,1,1,6,8)))
scalar = GF256int(50)
self.assertEqual(
a * Polynomial(x0=scalar),
a // Polynomial(x0=scalar.inverse())
)
class TestPolynomial(unittest.TestCase):
def test_add_1(self):
one = Polynomial((2,4,7,3))
two = Polynomial((5,2,4,2))
r = one + two
self.assertEqual(r.coefficients, (7, 6, 11, 5))
def test_add_2(self):
one = Polynomial((2,4,7,3,5,2))
two = Polynomial((5,2,4,2))
r = one + two
self.assertEqual(r.coefficients, (2,4,12,5,9,4))
def test_add_3(self):
one = Polynomial((7,3,5,2))
two = Polynomial((6,8,5,2,4,2))
r = one + two
self.assertEqual(r.coefficients, (6,8,12,5,9,4))
def test_mul_1(self):
one = Polynomial((2,4,7,3))
two = Polynomial((5,2,4,2))
r = one * two
self.assertEqual(r.coefficients,
(10,24,51,49,42,26,6))
def test_div_1(self):
one = Polynomial((1,4,0,3))
two = Polynomial((1,0,1))
q, r = divmod(one, two)
self.assertEqual(q, one // two)
self.assertEqual(r, one % two)
self.assertEqual(q.coefficients, (1,4))
self.assertEqual(r.coefficients, (-1,-1))
def test_div_2(self):
one = Polynomial((1,0,0,2,2,0,1,2,1))
two = Polynomial((1,0,-1))
q, r = divmod(one, two)
self.assertEqual(q, one // two)
self.assertEqual(r, one % two)
self.assertEqual(q.coefficients, (1,0,1,2,3,2,4))
self.assertEqual(r.coefficients, (4,5))
def test_div_3(self):
# 0 quotient
one = Polynomial((1,0,-1))
two = Polynomial((1,1,0,0,-1))
q, r = divmod(one, two)
self.assertEqual(q, one // two)
self.assertEqual(r, one % two)
self.assertEqual(q.coefficients, (0,))
self.assertEqual(r.coefficients, (1,0,-1))
def test_div_4(self):
# no remander
one = Polynomial((1,0,0,2,2,0,1,-2,-4))
two = Polynomial((1,0,-1))
q, r = divmod(one, two)
self.assertEqual(q, one // two)
self.assertEqual(r, one % two)
self.assertEqual(q.coefficients, (1,0,1,2,3,2,4))
self.assertEqual(r.coefficients, (0,))
def test_getcoeff(self):
p = Polynomial((9,3,3,2,2,3,1,-2,-4))
self.assertEqual(p.get_coefficient(0), -4)
self.assertEqual(p.get_coefficient(2), 1)
self.assertEqual(p.get_coefficient(8), 9)
self.assertEqual(p.get_coefficient(9), 0)
if __name__ == "__main__":
unittest.main()