Debian Codegraph @

gf16_test.go

raw
1 package reedsolomon
2
3 import "testing"
4
5 var gf16 LowLevel
6
7 func TestGF16MulZero(t *testing.T) {
8 // Anything multiplied by zero is zero.
9 for _, v := range []uint16{0, 1, 2, 100, 65535} {
10 if got := gf16.GF16Mul(v, 0); got != 0 {
11 t.Errorf("GF16Mul(%d, 0) = %d, want 0", v, got)
12 }
13 if got := gf16.GF16Mul(0, v); got != 0 {
14 t.Errorf("GF16Mul(0, %d) = %d, want 0", v, got)
15 }
16 }
17 }
18
19 func TestGF16MulOne(t *testing.T) {
20 initConstants()
21 // Multiplying by 1 (the multiplicative identity) is a no-op.
22 // The identity element in GF(2^16) is 1 (expLUT[0]).
23 one := uint16(expLUT[0])
24 for _, v := range []uint16{1, 2, 7, 255, 1000, 65535} {
25 if got := gf16.GF16Mul(v, one); got != v {
26 t.Errorf("GF16Mul(%d, 1) = %d, want %d", v, got, v)
27 }
28 if got := gf16.GF16Mul(one, v); got != v {
29 t.Errorf("GF16Mul(1, %d) = %d, want %d", v, got, v)
30 }
31 }
32 }
33
34 func TestGF16MulCommutativity(t *testing.T) {
35 cases := [][2]uint16{
36 {1, 2}, {3, 5}, {255, 256}, {1000, 2000}, {65534, 65535},
37 }
38 for _, c := range cases {
39 a, b := c[0], c[1]
40 ab := gf16.GF16Mul(a, b)
41 ba := gf16.GF16Mul(b, a)
42 if ab != ba {
43 t.Errorf("GF16Mul(%d, %d) = %d != GF16Mul(%d, %d) = %d", a, b, ab, b, a, ba)
44 }
45 }
46 }
47
48 func TestGF16MulAssociativity(t *testing.T) {
49 a, b, c := uint16(3), uint16(5), uint16(7)
50 // (a*b)*c == a*(b*c)
51 lhs := gf16.GF16Mul(gf16.GF16Mul(a, b), c)
52 rhs := gf16.GF16Mul(a, gf16.GF16Mul(b, c))
53 if lhs != rhs {
54 t.Errorf("associativity failed: (%d*%d)*%d=%d, %d*(%d*%d)=%d", a, b, c, lhs, a, b, c, rhs)
55 }
56 }
57
58 func TestGF16MulDistributivity(t *testing.T) {
59 // Addition in GF(2^16) is XOR. Distributivity: a*(b^c) == a*b ^ a*c.
60 cases := [][3]uint16{
61 {3, 5, 7}, {255, 256, 1000}, {1, 2, 3}, {65534, 65535, 1},
62 }
63 for _, c := range cases {
64 a, b, cc := c[0], c[1], c[2]
65 lhs := gf16.GF16Mul(a, b^cc)
66 rhs := gf16.GF16Mul(a, b) ^ gf16.GF16Mul(a, cc)
67 if lhs != rhs {
68 t.Errorf("distributivity failed: %d*(%d^%d)=%d, %d*%d^%d*%d=%d", a, b, cc, lhs, a, b, a, cc, rhs)
69 }
70 }
71 }
72
73 func TestGF16MulConsistentWithInternal(t *testing.T) {
74 initConstants()
75 // GF16Mul(a, b) should agree with the internal mulLog when b != 0.
76 cases := [][2]uint16{
77 {1, 2}, {3, 5}, {255, 256}, {1000, 2000},
78 }
79 for _, c := range cases {
80 a, b := c[0], c[1]
81 want := uint16(mulLog(ffe(a), logLUT[ffe(b)]))
82 got := gf16.GF16Mul(a, b)
83 if got != want {
84 t.Errorf("GF16Mul(%d, %d) = %d, internal mulLog gives %d", a, b, got, want)
85 }
86 }
87 }