aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/compressor.go
blob: 621b7311cfd5e3f8c1f4e8ba9a97384a10bbd170 (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
package compressor

import (
	"fmt"
	"testing"
)

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])
						}
					}
				}
			})
		}
	}
}