Debian Codegraph @

version_test.go

raw
1 package version
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func TestNewVersion(t *testing.T) {
9 cases := []struct {
10 version string
11 expected Version
12 }{
13 // Test 0
14 {"0", Version{0, "0", ""}},
15 {"0:0", Version{0, "0", ""}},
16 {"0:0-", Version{0, "0", ""}},
17 {"0:0-0", Version{0, "0", "0"}},
18 {"0:0.0-0.0", Version{0, "0.0", "0.0"}},
19 // Test epoch
20 {"1:0", Version{1, "0", ""}},
21 {"5:1", Version{5, "1", ""}},
22 // Test multiple hyphens
23 {"0:0-0-0", Version{0, "0", "0-0"}},
24 {"0:0-0-0-0", Version{0, "0", "0-0-0"}},
25 // Test multiple colons
26 {"0:0:0-0", Version{0, "0:0", "0"}},
27 {"0:0:0:0-0", Version{0, "0:0:0", "0"}},
28 // Test multiple hyphens and colons
29 {"0:0:0-0-0", Version{0, "0:0", "0-0"}},
30 {"0:0-0:0-0", Version{0, "0", "0:0-0"}},
31 // Test version with leading and trailing spaces
32 {" 0:0-1", Version{0, "0", "1"}},
33 {"0:0-1 ", Version{0, "0", "1 "}},
34 {" 0:0-1 ", Version{0, "0", "1 "}},
35 // Test empty version
36 {"", Version{}},
37 {" ", Version{0, " ", ""}},
38 {"0:", Version{}},
39 // Test version with embedded spaces
40 {"0:0 0-1", Version{0, "0 0", "1"}},
41 // Test version with negative epoch
42 {"-1:0-1", Version{-1, "0", "1"}},
43 // Test invalid characters in epoch
44 {"a:0-0", Version{0, "0", "0"}},
45 {"A:1-2", Version{0, "1", "2"}},
46 // Test version not starting with a digit
47 {"0:abc3-0", Version{0, "abc3", "0"}},
48 // Test actual version
49 {"1.2.3", Version{0, "1.2.3", ""}},
50 {"1:1.2.3", Version{1, "1.2.3", ""}},
51 {"A:1.2.3", Version{0, "1.2.3", ""}},
52 {"-1:1.2.3", Version{-1, "1.2.3", ""}},
53 {"6.0-4.el6.x86_64", Version{0, "6.0", "4.el6.x86_64"}},
54 {"c105b9de-4e0fd3a3", Version{0, "c105b9de", "4e0fd3a3"}},
55 {"4.999.9-0.5.beta.20091007git.el6", Version{0, "4.999.9", "0.5.beta.20091007git.el6"}},
56 }
57
58 for _, tc := range cases {
59 actual := NewVersion(tc.version)
60 if !reflect.DeepEqual(actual, tc.expected) {
61 t.Fatalf(
62 "version: %s\nexpected: %v\nactual: %v",
63 tc.version, tc.expected, actual)
64 }
65 }
66 }
67
68 func TestEqual(t *testing.T) {
69 for _, tc := range cases {
70 expected := (tc.expected == 0)
71
72 // compare 'version'
73 a := Version{0, tc.v1, ""}
74 b := Version{0, tc.v2, ""}
75 if actual := a.Equal(b); actual != expected {
76 t.Errorf("[Version] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
77 tc.v1, tc.v2, expected, actual)
78 }
79
80 // compare 'release'
81 a = Version{0, "", tc.v1}
82 b = Version{0, "", tc.v2}
83 if actual := a.Equal(b); actual != expected {
84 t.Errorf("[Release] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
85 tc.v1, tc.v2, tc.expected, actual)
86 }
87 }
88 }
89
90 func TestGreaterThan(t *testing.T) {
91 for _, tc := range cases {
92 expected := tc.expected > 0
93
94 // compare 'version'
95 a := Version{0, tc.v1, ""}
96 b := Version{0, tc.v2, ""}
97 if actual := a.GreaterThan(b); actual != expected {
98 t.Errorf("[Version] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
99 tc.v1, tc.v2, expected, actual)
100 }
101
102 // compare 'release'
103 a = Version{0, "", tc.v1}
104 b = Version{0, "", tc.v2}
105 if actual := a.GreaterThan(b); actual != expected {
106 t.Errorf("[Release] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
107 tc.v1, tc.v2, tc.expected, actual)
108 }
109 }
110 }
111
112 func TestLessThan(t *testing.T) {
113 for _, tc := range cases {
114 expected := tc.expected < 0
115
116 // compare 'version'
117 a := Version{0, tc.v1, ""}
118 b := Version{0, tc.v2, ""}
119 if actual := a.LessThan(b); actual != expected {
120 t.Errorf("[Version] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
121 tc.v1, tc.v2, expected, actual)
122 }
123
124 // compare 'release'
125 a = Version{0, "", tc.v1}
126 b = Version{0, "", tc.v2}
127 if actual := a.LessThan(b); actual != expected {
128 t.Errorf("[Release] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
129 tc.v1, tc.v2, tc.expected, actual)
130 }
131 }
132 }
133
134 func TestCompare(t *testing.T) {
135 for _, tc := range cases {
136 // compare 'version'
137 a := Version{0, tc.v1, ""}
138 b := Version{0, tc.v2, ""}
139 if actual := a.Compare(b); actual != tc.expected {
140 t.Errorf("[Version] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
141 tc.v1, tc.v2, tc.expected, actual)
142 }
143
144 // compare 'release'
145 a = Version{0, "", tc.v1}
146 b = Version{0, "", tc.v2}
147 if actual := a.Compare(b); actual != tc.expected {
148 t.Errorf("[Release] v1: %s\nv2: %s\nexpected: %v\nactual: %v",
149 tc.v1, tc.v2, tc.expected, actual)
150 }
151 }
152 }
153
154 func TestString(t *testing.T) {
155 cases := []struct {
156 v Version
157 expected string
158 }{
159 {Version{2, "7.4.052", "1.el6"}, "2:7.4.052-1.el6"},
160 {Version{2, "7.4.052", "1"}, "2:7.4.052-1"},
161 {Version{0, "7.4.052", "1"}, "7.4.052-1"},
162 }
163 for _, tc := range cases {
164 actual := tc.v.String()
165 if actual != tc.expected {
166 t.Errorf("v: %v\n\nexpected: %v\nactual: %v",
167 tc.v, tc.expected, actual)
168 }
169 }
170 }
171
172 func TestRpmVerCmp(t *testing.T) {
173 // Import cases from version_testcase.go
174 for _, tc := range cases {
175 actual := rpmvercmp(tc.v1, tc.v2)
176 if tc.expected != actual {
177 t.Fatalf("v1: %s\nv2: %s\nexpected: %v\nactual: %v",
178 tc.v1, tc.v2, tc.expected, actual)
179 }
180 }
181 }
182
183 func TestParseAndCompare(t *testing.T) {
184 cases := []struct {
185 v1 string
186 expected int
187 v2 string
188 }{
189 // Oracle Linux corner cases.
190 {"2.9.1-6.0.1.el7_2.3", GREATER, "2.9.1-6.el7_2.3"},
191 {"3.10.0-327.28.3.el7", GREATER, "3.10.0-327.el7"},
192 {"3.14.3-23.3.el6_8", GREATER, "3.14.3-23.el6_7"},
193 {"2.23.2-22.el7_1", LESS, "2.23.2-22.el7_1.1"},
194
195 // Tests imported from tests/rpmvercmp.at
196 {"1.0", EQUAL, "1.0"},
197 {"1.0", LESS, "2.0"},
198 {"2.0", GREATER, "1.0"},
199 {"2.0.1", EQUAL, "2.0.1"},
200 {"2.0", LESS, "2.0.1"},
201 {"2.0.1", GREATER, "2.0"},
202 {"2.0.1a", EQUAL, "2.0.1a"},
203 {"2.0.1a", GREATER, "2.0.1"},
204 {"2.0.1", LESS, "2.0.1a"},
205 {"5.5p1", EQUAL, "5.5p1"},
206 {"5.5p1", LESS, "5.5p2"},
207 {"5.5p2", GREATER, "5.5p1"},
208 {"5.5p10", EQUAL, "5.5p10"},
209 {"5.5p1", LESS, "5.5p10"},
210 {"5.5p10", GREATER, "5.5p1"},
211 {"10xyz", LESS, "10.1xyz"},
212 {"10.1xyz", GREATER, "10xyz"},
213 {"xyz10", EQUAL, "xyz10"},
214 {"xyz10", LESS, "xyz10.1"},
215 {"xyz10.1", GREATER, "xyz10"},
216 {"xyz.4", EQUAL, "xyz.4"},
217 {"xyz.4", LESS, "8"},
218 {"8", GREATER, "xyz.4"},
219 {"xyz.4", LESS, "2"},
220 {"2", GREATER, "xyz.4"},
221 {"5.5p2", LESS, "5.6p1"},
222 {"5.6p1", GREATER, "5.5p2"},
223 {"5.6p1", LESS, "6.5p1"},
224 {"6.5p1", GREATER, "5.6p1"},
225 {"6.0.rc1", GREATER, "6.0"},
226 {"6.0", LESS, "6.0.rc1"},
227 {"10b2", GREATER, "10a1"},
228 {"10a2", LESS, "10b2"},
229 {"1.0aa", EQUAL, "1.0aa"},
230 {"1.0a", LESS, "1.0aa"},
231 {"1.0aa", GREATER, "1.0a"},
232 {"10.0001", EQUAL, "10.0001"},
233 {"10.0001", EQUAL, "10.1"},
234 {"10.1", EQUAL, "10.0001"},
235 {"10.0001", LESS, "10.0039"},
236 {"10.0039", GREATER, "10.0001"},
237 {"4.999.9", LESS, "5.0"},
238 {"5.0", GREATER, "4.999.9"},
239 {"20101121", EQUAL, "20101121"},
240 {"20101121", LESS, "20101122"},
241 {"20101122", GREATER, "20101121"},
242 {"2_0", EQUAL, "2_0"},
243 {"2.0", EQUAL, "2_0"},
244 {"2_0", EQUAL, "2.0"},
245 {"a", EQUAL, "a"},
246 {"a+", EQUAL, "a+"},
247 {"a+", EQUAL, "a_"},
248 {"a_", EQUAL, "a+"},
249 {"+a", EQUAL, "+a"},
250 {"+a", EQUAL, "_a"},
251 {"_a", EQUAL, "+a"},
252 {"+_", EQUAL, "+_"},
253 {"_+", EQUAL, "+_"},
254 {"_+", EQUAL, "_+"},
255 {"+", EQUAL, "_"},
256 {"_", EQUAL, "+"},
257 {"1.0~rc1", EQUAL, "1.0~rc1"},
258 {"1.0~rc1", LESS, "1.0"},
259 {"1.0", GREATER, "1.0~rc1"},
260 {"1.0~rc1", LESS, "1.0~rc2"},
261 {"1.0~rc2", GREATER, "1.0~rc1"},
262 {"1.0~rc1~git123", EQUAL, "1.0~rc1~git123"},
263 {"1.0~rc1~git123", LESS, "1.0~rc1"},
264 {"1.0~rc1", GREATER, "1.0~rc1~git123"},
265
266 // Test epoch
267 {"1:1.0~rc1", GREATER, "0:1.0~rc1"},
268 {"1.0~rc1", LESS, "2:1.0~rc1"},
269 {"3:1.0~rc1", EQUAL, "3:1.0~rc1"},
270 }
271
272 for _, tc := range cases {
273 v1 := NewVersion(tc.v1)
274 v2 := NewVersion(tc.v2)
275 actual := v1.Compare(v2)
276 if actual != tc.expected {
277 t.Errorf("v1: %s\nv2: %s\nexpected: %v\nactual: %v",
278 tc.v1, tc.v2, tc.expected, actual)
279 }
280 }
281 }