aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/compressor/compressor.go
blob: cac61581729d165cbedf08987517d1102f3d9c62 (plain) (blame)
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
package compressor

import (
	"fmt"
	"os"
	"testing"
	"testing/internal/testdeps"
)



func TestCompressor_Compress(t *testing.T) {
	x := 0 // an empty value

	allCompressors := func() []Compressor {
		return []Compressor{
			NewUniqueEntriesTable(),
			NewRowDisplacementTable(x),
		}
	}

	tests := []struct {
		original    []int
		rowCount    int
		colCount    int
		compressors []Compressor
	}{
		{
			original: []int{
				1, 1, 1, 1, 1,
				1, 1, 1, 1, 1,
				1, 1, 1, 1, 1,
			},
			rowCount:    3,
			colCount:    5,
			compressors: allCompressors(),
		},
		{
			original: []int{
				x, x, x, x, x,
				x, x, x, x, x,
				x, x, x, x, x,
			},
			rowCount:    3,
			colCount:    5,
			compressors: allCompressors(),
		},
		{
			original: []int{
				1, 1, 1, 1, 1,
				x, x, x, x, x,
				1, 1, 1, 1, 1,
			},
			rowCount:    3,
			colCount:    5,
			compressors: allCompressors(),
		},
		{
			original: []int{
				1, x, 1, 1, 1,
				1, 1, x, 1, 1,
				1, 1, 1, x, 1,
			},
			rowCount:    3,
			colCount:    5,
			compressors: allCompressors(),
		},
	}
	for i, tt := range tests {
		for _, comp := range tt.compressors {
			t.Run(fmt.Sprintf("%T #%v", comp, i), func(t *testing.T) {
				dup := make([]int, len(tt.original))
				copy(dup, tt.original)

				orig, err := NewOriginalTable(tt.original, tt.colCount)
				if err != nil {
					t.Fatal(err)
				}
				err = comp.Compress(orig)
				if err != nil {
					t.Fatal(err)
				}
				rowCount, colCount := comp.OriginalTableSize()
				if rowCount != tt.rowCount || colCount != tt.colCount {
					t.Fatalf("unexpected table size; want: %vx%v, got: %vx%v", tt.rowCount, tt.colCount, rowCount, colCount)
				}
				for i := 0; i < tt.rowCount; i++ {
					for j := 0; j < tt.colCount; j++ {
						v, err := comp.Lookup(i, j)
						if err != nil {
							t.Fatal(err)
						}
						expected := tt.original[i*tt.colCount+j]
						if v != expected {
							t.Fatalf("unexpected entry (%v, %v); want: %v, got: %v", i, j, expected, v)
						}
					}
				}

				// Calling with out-of-range indexes should be an error.
				if _, err := comp.Lookup(0, -1); err == nil {
					t.Fatalf("expected error didn't occur (0, -1)")
				}
				if _, err := comp.Lookup(-1, 0); err == nil {
					t.Fatalf("expected error didn't occur (-1, 0)")
				}
				if _, err := comp.Lookup(rowCount-1, colCount); err == nil {
					t.Fatalf("expected error didn't occur (%v, %v)", rowCount-1, colCount)
				}
				if _, err := comp.Lookup(rowCount, colCount-1); err == nil {
					t.Fatalf("expected error didn't occur (%v, %v)", rowCount, colCount-1)
				}

				// The compressor must not break the original table.
				for i := 0; i < tt.rowCount; i++ {
					for j := 0; j < tt.colCount; j++ {
						idx := i*tt.colCount + j
						if tt.original[idx] != dup[idx] {
							t.Fatalf("the original table is broken (%v, %v); want: %v, got: %v", i, j, dup[idx], tt.original[idx])
						}
					}
				}
			})
		}
	}
}



func MainTest() {
	tests := []testing.InternalTest{
		{ "TestCompressor_Compress", TestCompressor_Compress },
	}

	deps := testdeps.TestDeps{}
	benchmarks  := []testing.InternalBenchmark {}
	fuzzTargets := []testing.InternalFuzzTarget{}
	examples    := []testing.InternalExample   {}
	m := testing.MainStart(deps, tests, benchmarks, fuzzTargets, examples)
	os.Exit(m.Run())
}