-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid62_test.go
166 lines (142 loc) · 4.12 KB
/
uuid62_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
package uuid62
import "testing"
import (
"fmt"
"github.com/google/uuid"
. "gopkg.in/check.v1"
"math/big"
"math/rand"
"strings"
)
func Test(t *testing.T) { TestingT(t) }
type Uuid62Suite struct{}
var _ = Suite(&Uuid62Suite{})
var seed = int64(62)
type intTestVector struct {
i *big.Int
s string
radix int
}
type uuidTestVector struct {
id string
base62 string
}
// Creates a new big.Int from the string s.
// Returns a pointer to the new big.Int and a bool indicating success
func newBigInt(s string) (*big.Int, bool) {
i := big.Int{}
return i.SetString(s, 10)
}
func mustNewBigInt(s string) *big.Int {
i, _ := newBigInt(s)
return i
}
var intTestVectors = []intTestVector{
{i: mustNewBigInt("13"), s: "1101", radix: 2},
{i: mustNewBigInt("59774123759"), s: "dead0beef", radix: 16},
{i: mustNewBigInt("1"), s: "1", radix: 62},
{i: mustNewBigInt("63"), s: "11", radix: 62},
{i: mustNewBigInt("61"), s: "Z", radix: 62},
}
var uuidTestVectors = []uuidTestVector{
{id: "00000000-0000-0000-0000-000000000000", base62: "0"},
{id: "3078350b-bfd0-41ff-8cc2-3a3a7969ceb9", base62: "1tsz7Nk9Grmziqc5gFI0pX"},
{id: "3078350b-bfd0-41ff-8cc2-3a3a7969ceba", base62: "1tsz7Nk9Grmziqc5gFI0pY"},
}
func (s *Uuid62Suite) TestUuid2Base62String(c *C) {
for _, tv := range uuidTestVectors {
id, _ := uuid.Parse(tv.id)
base62, err := Uuid2Base62String(id, false)
c.Assert(err, IsNil)
c.Assert(base62, Equals, tv.base62)
}
}
func (s *Uuid62Suite) TestPadBase62String(c *C) {
for _, tv := range uuidTestVectors {
id, _ := uuid.Parse(tv.id)
base62, err := Uuid2Base62String(id, true)
expected := fmt.Sprintf("%23s", tv.base62)
expected = strings.Replace(expected, " ", "0", -1)
c.Assert(err, IsNil)
c.Assert(len(base62), Equals, 23)
c.Assert(base62, Equals, expected)
}
}
func (s *Uuid62Suite) TestBase62String2Uuid(c *C) {
for _, tv := range uuidTestVectors {
id, err := Base62String2Uuid(tv.base62)
c.Assert(err, IsNil)
c.Assert(id.String(), Equals, tv.id)
}
}
// Do 1000 runs with random uuids, to test
// that identity holds when composing uuid2str and str2uuid
func (s *Uuid62Suite) TestRandomUuidIdentity(c *C) {
runs := 1000
for i := 0; i < runs; i++ {
id, err := uuid.NewRandom()
c.Assert(err, IsNil)
s, err := Uuid2Base62String(id, true)
c.Assert(err, IsNil)
identity, err := Base62String2Uuid(s)
c.Assert(err, IsNil)
c.Assert(*identity, Equals, id)
}
}
func (s *Uuid62Suite) TestBigInt2String(c *C) {
for _, tv := range intTestVectors {
base62, err := BigInt2String(tv.i, tv.radix)
c.Assert(err, Equals, nil)
c.Assert(base62, Equals, tv.s)
}
}
func (s *Uuid62Suite) TestEdgeCaseBigInt2String(c *C) {
// empty string
result, err := BigInt2String(big.NewInt(int64(0)), 62)
c.Assert(err, IsNil)
c.Assert(result, Equals, "0")
}
func (s *Uuid62Suite) TestString2BigInt(c *C) {
for _, tv := range intTestVectors {
i, err := String2BigInt(tv.s, tv.radix)
c.Assert(err, IsNil)
c.Assert(i.Cmp(tv.i), Equals, 0)
}
}
func (s *Uuid62Suite) TestEdgeCaseString2BigInt(c *C) {
// empty string
result, err := String2BigInt("", 62)
c.Assert(err, IsNil)
c.Assert(result.Int64(), Equals, int64(0))
}
func (s *Uuid62Suite) TestInvalidString2BigInt(c *C) {
// empty string
_, err1 := String2BigInt("a", 63)
c.Assert(err1, ErrorMatches, "Radix must be.*")
_, err2 := String2BigInt("a", 1)
c.Assert(err2, ErrorMatches, "Radix must be.*")
_, err3 := String2BigInt("-", 62)
c.Assert(err3, ErrorMatches, "Digit.*")
}
func (s *Uuid62Suite) TestPaddedString2BigInt(c *C) {
result, err := String2BigInt("00000013", 10)
c.Assert(err, IsNil)
c.Assert(result.Int64(), Equals, int64(13))
}
// Do 1000 runs with random big ints, to test
// that identity holds when composing bi2str and str2bi
func (s *Uuid62Suite) TestRandomIdentity(c *C) {
prng := rand.New(rand.NewSource(seed))
two := big.NewInt(int64(2))
max := two.Exp(two, big.NewInt(int64(129)), nil)
runs := 1000
bi := big.NewInt(int64(0))
for i := 0; i < runs; i++ {
bi.Rand(prng, max)
s, err := BigInt2String(bi, 62)
c.Assert(err, IsNil)
result, err := String2BigInt(s, 62)
c.Assert(err, IsNil)
c.Assert(result.String(), Equals, bi.String())
}
}