File: jetset_test.go

package info (click to toggle)
golang-github-pointlander-jetset 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 64 kB
  • sloc: makefile: 2
file content (166 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2015 The jetset Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package jetset

import (
	"testing"
)

func TestCompress(t *testing.T) {
	items, comp := []uint64{1, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 22, 24, 26, 28}, newCompressor()
	for _, item := range items {
		comp.compress(item)
	}
	set := comp.close()
	decomp := newDecompressor(set)
	for _, item := range items {
		d, status := decomp.decompress()
		if !status || d != item {
			t.Errorf("compression/decompression error")
			break
		}
	}
}

func makeTestSetA() Set {
	set := Set{}
	set = set.Add(10)
	set = set.Add(11)
	set = set.Add(11)
	set = set.Add(12)
	set = set.Add(14)
	set = set.Add(15)
	set = set.Add(16)
	set = set.Add(17)
	set = set.Add(20)
	set = set.Add(1)
	set = set.Add(1)
	set = set.Add(3)
	set = set.Add(4)
	set = set.Add(5)
	set = set.Add(7)
	set = set.Add(8)
	set = set.Add(9)
	set = set.Add(22)
	set = set.Add(21)
	return set
}

func makeTestSetB() Set {
	set := Set{}
	set = set.Add(10)
	set = set.Add(12)
	set = set.Add(15)
	set = set.Add(17)
	set = set.Add(1)
	set = set.Add(1)
	set = set.Add(4)
	set = set.Add(7)
	set = set.Add(9)
	set = set.Add(21)
	set = set.Add(23)
	set = set.Add(24)
	set = set.Add(28)
	return set
}

func TestAdd(t *testing.T) {
	items, set := []uint64{1, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22}, makeTestSetA()
	decomp := newDecompressor(set)
	for _, item := range items {
		d, status := decomp.decompress()
		if !status || d != item {
			t.Errorf("add error")
			break
		}
	}
}

func TestAddRange(t *testing.T) {
	items, set := []uint64{1, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22, 23, 24, 25, 26}, makeTestSetA().AddRange(21, 26)
	decomp := newDecompressor(set)
	for _, item := range items {
		d, status := decomp.decompress()
		if !status || d != item {
			t.Errorf("add range error")
			break
		}
	}
}

func TestHas(t *testing.T) {
	set := makeTestSetA()
	if !set.Has(11) {
		t.Errorf("set should include 11")
	}
	if set.Has(13) {
		t.Errorf("set shouldn't have 13")
	}
	if !set.Has(16) {
		t.Errorf("set should have 16")
	}
}

func TestComplement(t *testing.T) {
	set := makeTestSetA()
	set = set.Complement(0x110000)
	if set.Has(11) {
		t.Errorf("set shouldn't have 11")
	}
	if !set.Has(13) {
		t.Errorf("set should have 13")
	}
	if set.Has(16) {
		t.Errorf("set shouldn't have 16")
	}
}

func TestUnion(t *testing.T) {
	a, b := makeTestSetA(), makeTestSetB()
	set := a.Union(b)
	if !set.Has(10) {
		t.Errorf("set should have 10")
	}
	if !set.Has(28) {
		t.Errorf("set should have 28")
	}
	if !set.Has(9) {
		t.Errorf("set should have 9")
	}
}

func TestIntersection(t *testing.T) {
	a, b := makeTestSetA(), makeTestSetB()
	set := a.Intersection(b)
	if !set.Has(10) {
		t.Errorf("set should have 10")
	}
	if set.Has(28) {
		t.Errorf("set shouldn't have 28")
	}
	if !set.Has(9) {
		t.Errorf("set should have 9")
	}
	if set.Has(11) {
		t.Errorf("set should have 11")
	}
}

func TestIntersects(t *testing.T) {
	a, b := makeTestSetA(), makeTestSetB()
	if !a.Intersects(b) {
		t.Errorf("a should intersect b")
	}
}

func TestLen(t *testing.T) {
	a, b := makeTestSetA(), makeTestSetB()
	if a.Len() != 17 {
		t.Errorf("length of set should be 17")
	}
	if b.Len() != 12 {
		t.Errorf("length of set should be 12")
	}
}