-
Notifications
You must be signed in to change notification settings - Fork 5
/
version_test.go
218 lines (193 loc) · 4.31 KB
/
version_test.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package pkgbuild
import "testing"
// Test version comparison
func TestVersionComparison(t *testing.T) {
alphaNumeric := []Version{
"1.0.1",
"1.0.a",
"1.0",
"1.0rc",
"1.0pre",
"1.0p",
"1.0beta",
"1.0b",
"1.0a",
}
numeric := []Version{
"20141130",
"012",
"11",
"3.0.0",
"2.011",
"2.03",
"2.0",
"1.2",
"1.1.1",
"1.1",
"1.0.1",
"1.0.0.0.0.0",
"1.0",
"1",
}
git := []Version{
"r1000.b481c3c",
"r37.e481c3c",
"r36.f481c3c",
}
bigger := func(list []Version) {
for i, v := range list {
for _, v2 := range list[i:] {
if v != v2 && !v.bigger(v2) {
t.Errorf("%s should be bigger than %s", v, v2)
}
}
}
}
smaller := func(list []Version) {
for i := len(list) - 1; i >= 0; i-- {
v := list[i]
for _, v2 := range list[:i] {
if v != v2 && v.bigger(v2) {
t.Errorf("%s should be smaller than %s", v, v2)
}
}
}
}
bigger(alphaNumeric)
smaller(alphaNumeric)
bigger(numeric)
smaller(numeric)
bigger(git)
smaller(git)
}
// Test alphaCompare function
func TestAlphaCompare(t *testing.T) {
if alphaCompare([]rune("test"), []rune("test")) != 0 {
t.Error("should be 0")
}
if alphaCompare([]rune("test"), []rune("test123")) > 0 {
t.Error("should be less than 0")
}
if alphaCompare([]rune("test123"), []rune("test")) < 0 {
t.Error("should be greater than 0")
}
}
// Test CompleteVersion comparisons
func TestCompleteVersionComparison(t *testing.T) {
a := &CompleteVersion{
Version: "2",
Epoch: 1,
Pkgrel: Version("2"),
}
older := []string{
"0:3-4",
"1:2-1",
"1:2-1.5",
"1:1-1",
}
for _, o := range older {
if b, err := NewCompleteVersion(o); err != nil {
t.Errorf("%s fails to parse %v", o, err)
} else if a.Older(b) || !a.Newer(b) {
t.Errorf("%s should be older than %s", o, a.String())
}
}
newer := []string{
"2:1-1",
"1:3-1",
"1:2-3",
"1:2-2.1",
}
for _, n := range newer {
if b, err := NewCompleteVersion(n); err != nil {
t.Errorf("%s fails to parse %v", n, err)
} else if a.Newer(b) || !a.Older(b) {
t.Errorf("%s should be newer than %s", n, a.String())
}
}
equal := []string{
"1:2-2",
"1:2",
}
for _, n := range equal {
if b, err := NewCompleteVersion(n); err != nil {
t.Errorf("%s fails to parse %v", n, err)
} else if a.Newer(b) || a.Older(b) || !a.Equal(b) {
t.Errorf("%s should be equal to %s", n, a.String())
}
}
}
func TestCompleteVersionString(t *testing.T) {
str := "42:3.14-1"
version, _ := NewCompleteVersion(str)
if version.String() != str {
t.Errorf("%v should equal %s", version, str)
}
}
func TestSatisfies(t *testing.T) {
deps, _ := ParseDeps([]string{
"a>1", "a<2",
"b>=1", "b<=2",
"c>=1:1", "c<=1:2",
"d=1.2.3",
"e=1.2.3-1",
})
versionPass := [][]string{
{"1.1", "1.9", "1.99", "1.001", "0:1.1"},
{"1.1", "1.9", "1.99", "1.001", "1", "2"},
{"1:1.1", "1:1.9", "1:1.99", "1:1.001", "1:1", "1:2"},
{"1.2.3", "1.2.3-1", "1.2.3-999", "0:1.2.3", "0:1.2.3-1", "0:1.2.3-999"},
{"1.2.3-1", "0:1.2.3-1"},
}
versionFail := [][]string{
{"0", "0.99", "2.001", "2"},
{"0", "0.99", "2.001"},
{"0:1.1", "2:1.9", "0:1.99", "2:1.001", "0:1", "2:2", "0:1.0", "2:2.0"},
{"1:1.2.3", "1.2.3.0", "1.2.3.1"},
{"1:1.2.3-1", "0:1.2.3-2", "1.2.3-3", "1.2.3-4"},
}
for i, dep := range deps {
versions := versionPass[i]
for _, versionStr := range versions {
version, err := NewCompleteVersion(versionStr)
if err != nil {
t.Errorf("%s fails to parse %v", versionStr, err)
}
if !version.Satisfies(dep) {
t.Errorf("%s should satisfy %+v", version, dep)
}
}
}
for i, dep := range deps {
versions := versionFail[i]
for _, versionStr := range versions {
version, err := NewCompleteVersion(versionStr)
if err != nil {
t.Errorf("%s fails to parse %v", versionStr, err)
}
if version.Satisfies(dep) {
t.Errorf("%s should not satisfy %+v", version, dep)
}
}
}
}
// Benchmark rpmvercmp
func BenchmarkVersionCompare(b *testing.B) {
for i := 0; i < b.N; i++ {
rpmvercmp("1.0", "1.0.0")
}
}
func TestUnicode(t *testing.T) {
str := "13:2.0.0.α.r29.g18fc492-1"
expected := CompleteVersion{
Epoch: 13,
Version: "2.0.0.α.r29.g18fc492",
Pkgrel: "1",
}
version, err := NewCompleteVersion(str)
if err != nil {
t.Error(err)
} else if *version != expected {
t.Errorf("%v should be %v", version, expected)
}
}