-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools_test.go
136 lines (121 loc) · 2.8 KB
/
tools_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
package tools
import (
"runtime"
"testing"
"math/rand"
)
func TestRandomString(t *testing.T) {
r := RandomString(10)
if len(r) != 10 {
t.Error("Should produce string of length 10, produced", r)
}
}
func TestUuid(t *testing.T) {
s := Uuid()
if len(s) != 16 {
t.Error("Uuid() should return 16 runes, but returned ", s)
}
}
func bigIntTest(t *testing.T, i, b int, s string) {
r := NewBigIntInt(i).BaseString(b)
if r != s {
t.Error("Wrong representation of ", i, " in base ",b,": wanted ",s," but got ",r)
}
}
func bigIntTestString(t *testing.T, s string, b int, s2 string, b2 int) {
r := NewBigIntString(s, b).BaseString(b2)
if r != s2 {
t.Error("Wrong representation of ", s, " in base ",b," when converted to base", b2, " : wanted ",s2," but got ",r)
}
}
func TestBigInt(t *testing.T) {
bigIntTestString(t, "45", 10, "50", 9)
bigIntTestString(t, "1712", 8, "68A", 12)
bigIntTestString(t, "45", 14, "2021", 3)
bigIntTest(t, 10, 10, "10")
bigIntTest(t, 11, 10, "11")
bigIntTest(t, 1, 10, "1")
bigIntTest(t, 4, 10, "4")
bigIntTest(t, 14, 10, "14")
bigIntTest(t, 61, 10, "61")
bigIntTest(t, 615, 10, "615")
bigIntTest(t, 11261, 10, "11261")
bigIntTest(t, 10, 8, "12")
bigIntTest(t, 11, 8, "13")
bigIntTest(t, 1, 8, "1")
bigIntTest(t, 4, 8, "4")
bigIntTest(t, 14, 8, "16")
bigIntTest(t, 61, 8, "75")
bigIntTest(t, 615, 8, "1147")
bigIntTest(t, 11261, 8, "25775")
bigIntTest(t, 10, 16, "A")
bigIntTest(t, 11, 16, "B")
bigIntTest(t, 1, 16, "1")
bigIntTest(t, 4, 16, "4")
bigIntTest(t, 14, 16, "E")
bigIntTest(t, 61, 16, "3D")
bigIntTest(t, 615, 16, "267")
bigIntTest(t, 11261, 16, "2BFD")
}
func TestMap(t *testing.T) {
m := NewMap()
if _, ok := m.Get("hej"); ok {
t.Error("should not have content")
}
m.Put("hej", "jaha")
if v, ok := m.Get("hej"); ok {
if v != "jaha" {
t.Error("should have jaha")
}
} else {
t.Error("should have content")
}
}
type maplike interface {
Get(k interface{}) (interface{}, bool)
Put(k, v interface{}) (interface{}, bool)
}
func action(b *testing.B, m maplike, i int, do, done chan bool) {
<- do
for j := 0; j < i; j++ {
m.Put(j, rand.Int())
m.Get(j)
}
done <- true
}
func BenchmarkMyMapConc(b *testing.B) {
b.StopTimer()
runtime.GOMAXPROCS(runtime.NumCPU())
do := make(chan bool)
done := make(chan bool)
m := NewMap()
for i := 0; i < runtime.NumCPU(); i++ {
go action(b, m, b.N, do, done)
}
close(do)
b.StartTimer()
for i := 0; i < runtime.NumCPU(); i++ {
<- done
}
runtime.GOMAXPROCS(1)
}
func BenchmarkMyMap(b *testing.B) {
m := NewMap()
for i := 0; i < b.N; i++ {
m.Put(i, i)
j, _ := m.Get(i)
if j != i {
b.Error("should be same value")
}
}
}
func BenchmarkNativeMap(b *testing.B) {
m := make(map[int]int)
for i := 0; i < b.N; i++ {
m[i] = i
j, _ := m[i]
if j != i {
b.Error("should be same value")
}
}
}