File: matrix_test.go

package info (click to toggle)
golang-github-templexxx-reedsolomon 0.1.1%2Bgit20170927.7092926-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 636 kB
  • sloc: asm: 352; makefile: 3
file content (146 lines) | stat: -rw-r--r-- 2,744 bytes parent folder | download
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
package reedsolomon

import (
	"bytes"
	"testing"
)

func TestVerifyEncMatrixVand(t *testing.T) {
	a, err := genEncMatrixVand(4, 4)
	if err != nil {
		t.Fatal(err)
	}
	e := []byte{1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
		27, 28, 18, 20,
		28, 27, 20, 18,
		18, 20, 27, 28,
		20, 18, 28, 27}
	if !bytes.Equal(a, e) {
		t.Fatal("mismatch")
	}
}

func TestVerifyEncMatrixCauchy(t *testing.T) {
	a := genEncMatrixCauchy(4, 4)
	e := []byte{1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
		71, 167, 122, 186,
		167, 71, 186, 122,
		122, 186, 71, 167,
		186, 122, 167, 71}
	if !bytes.Equal(a, e) {
		t.Fatal("mismatch")
	}
}

func TestMatrixInvert(t *testing.T) {
	testCases := []struct {
		matrixData  []byte
		cols        int
		expect      []byte
		ok          bool
		expectedErr error
	}{
		{
			[]byte{56, 23, 98,
				3, 100, 200,
				45, 201, 123},
			3,
			[]byte{175, 133, 33,
				130, 13, 245,
				112, 35, 126},
			true,
			nil,
		},
		{
			[]byte{0, 23, 98,
				3, 100, 200,
				45, 201, 123},
			3,
			[]byte{245, 128, 152,
				188, 64, 135,
				231, 81, 239},
			true,
			nil,
		},
		{
			[]byte{1, 0, 0, 0, 0,
				0, 1, 0, 0, 0,
				0, 0, 0, 1, 0,
				0, 0, 0, 0, 1,
				7, 7, 6, 6, 1},
			5,
			[]byte{1, 0, 0, 0, 0,
				0, 1, 0, 0, 0,
				123, 123, 1, 122, 122,
				0, 0, 1, 0, 0,
				0, 0, 0, 1, 0},
			true,
			nil,
		},
		{
			[]byte{4, 2,
				12, 6},
			2,
			nil,
			false,
			errSingular,
		},
	}

	for i, c := range testCases {
		m := matrix(c.matrixData)
		raw := make([]byte, 2*c.cols*c.cols)
		actual := make([]byte, c.cols*c.cols)
		actualErr := m.invert(raw, c.cols, actual)
		if actualErr != nil && c.ok {
			t.Errorf("case.%d, expected to pass, but failed with: <ERROR> %s", i+1, actualErr.Error())
		}
		if actualErr == nil && !c.ok {
			t.Errorf("case.%d, expected to fail with <ERROR> \"%s\", but passed", i+1, c.expectedErr)
		}
		if actualErr != nil && !c.ok {
			if c.expectedErr != actualErr {
				t.Errorf("case.%d, expected to fail with error \"%s\", but instead failed with error \"%s\"", i+1, c.expectedErr, actualErr)
			}
		}
		if actualErr == nil && c.ok {
			if !bytes.Equal(c.expect, actual) {
				t.Errorf("case.%d, mismatch", i+1)
			}
		}
	}
}

func benchmarkInvert(b *testing.B, size int) {
	m := genEncMatrixCauchy(size, 2)
	m.swap(0, size, size)
	m.swap(1, size+1, size)
	b.ResetTimer()
	buf := make([]byte, 3*size*size)
	raw := buf[:2*size*size]
	r := buf[2*size*size:]
	for i := 0; i < b.N; i++ {
		err := m.invert(raw, size, r)
		if err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkInvert5x5(b *testing.B) {
	benchmarkInvert(b, 5)
}

func BenchmarkInvert10x10(b *testing.B) {
	benchmarkInvert(b, 10)
}

func BenchmarkInvert20x20(b *testing.B) {
	benchmarkInvert(b, 20)
}