-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_test.go
155 lines (132 loc) · 3.48 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
package pkgcraft_test
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/pkgcraft/pkgcraft-go"
. "github.com/pkgcraft/pkgcraft-go/internal"
)
func TestVersion(t *testing.T) {
var rev *Revision
// non-revision
ver, err := NewVersion("1")
assert.Nil(t, err)
assert.Equal(t, ver.Revision(), &Revision{})
assert.Equal(t, ver.String(), "1")
// revisioned
ver, err = NewVersion("1-r1")
assert.Nil(t, err)
rev, _ = NewRevision("1")
assert.Equal(t, ver.Revision(), rev)
assert.Equal(t, ver.String(), "1-r1")
// explicit '0' revision
ver, err = NewVersion("1-r0")
assert.Nil(t, err)
rev, _ = NewRevision("0")
assert.Equal(t, ver.Revision(), rev)
assert.Equal(t, ver.String(), "1-r0")
// with operator
ver, err = NewVersion(">1-r2")
assert.Nil(t, err)
rev, _ = NewRevision("2")
assert.Equal(t, ver.Revision(), rev)
assert.Equal(t, ver.String(), ">1-r2")
// invalid
ver, err = NewVersion("-1")
assert.Nil(t, ver)
assert.NotNil(t, err)
}
func TestVersionCmp(t *testing.T) {
for _, s := range VERSION_TOML.Compares {
vals := strings.Fields(s)
v1, _ := NewVersion(vals[0])
op := vals[1]
v2, _ := NewVersion(vals[2])
switch op {
case "<":
assert.Equal(t, v1.Cmp(v2), -1)
case "==":
assert.Equal(t, v1.Cmp(v2), 0)
case "!=":
assert.NotEqual(t, v1.Cmp(v2), 0)
case ">":
assert.Equal(t, v1.Cmp(v2), 1)
default:
panic(fmt.Sprintf("invalid operator: %s", op))
}
}
}
func TestVersionHash(t *testing.T) {
for _, data := range VERSION_TOML.Hashing {
m := make(map[uint64]bool)
for _, s := range data.Versions {
ver, _ := NewVersion(s)
m[ver.Hash()] = true
}
if data.Equal {
assert.Equal(t, len(m), 1)
} else {
assert.Equal(t, len(m), len(data.Versions))
}
}
}
// TODO: use shared intersects test data
func TestVersionIntersects(t *testing.T) {
var v1, v2 *Version
// equal, non-op versions
v1, _ = NewVersion("1.0.2")
v2, _ = NewVersion("1.0.2-r0")
assert.True(t, v1.Intersects(v2))
// unequal, non-op versions
v1, _ = NewVersion("0")
v2, _ = NewVersion("1")
assert.False(t, v1.Intersects(v2))
// non-op and op versions
v1, _ = NewVersion("<0")
v2, _ = NewVersion("0")
assert.False(t, v1.Intersects(v2))
v1, _ = NewVersion("0")
v2, _ = NewVersion("=0*")
assert.True(t, v1.Intersects(v2))
}
func TestVersionSort(t *testing.T) {
for _, data := range VERSION_TOML.Sorting {
var expected []*Version
for _, s := range data.Sorted {
ver, _ := NewVersion(s)
expected = append(expected, ver)
}
sorted := make([]*Version, len(expected))
copy(sorted, expected)
ReverseSlice(sorted)
sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].Cmp(sorted[j]) == -1 })
// equal versions aren't sorted so reversing should restore the original order
if data.Equal {
ReverseSlice(sorted)
}
assert.Equal(t, len(sorted), len(expected))
for i := range sorted {
assert.True(t, sorted[i].Cmp(expected[i]) == 0, "%s != %s", sorted, expected)
}
}
}
func BenchmarkNewVersion(b *testing.B) {
for i := 0; i < b.N; i++ {
ver, _ := NewVersion("1.2.3_alpha4-r5")
assert.NotNil(b, ver)
}
}
func BenchmarkVersionSort(b *testing.B) {
var versions []*Version
for i := 100; i > 0; i-- {
v, _ := NewVersion(fmt.Sprintf("%d", i))
versions = append(versions, v)
}
assert.Equal(b, versions[0].String(), "100")
for i := 0; i < b.N; i++ {
sort.SliceStable(versions, func(i, j int) bool { return versions[i].Cmp(versions[j]) == -1 })
}
assert.Equal(b, versions[0].String(), "1")
}