-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
121 lines (110 loc) · 2.67 KB
/
storage_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
package main
import (
"testing"
"time"
)
func TestGenerateID(t *testing.T) {
id1 := generateID()
id2 := generateID()
if id1 == id2 {
t.Errorf("id must be unique")
}
}
func TestConvertStringToUInt64(t *testing.T) {
num := convertStringToUInt64("a")
if num != 0 {
t.Errorf("num must be zero")
}
}
func TestGetLevel(t *testing.T) {
if getLevel("1") != levelEasy {
t.Errorf("invalid level")
}
if getLevel("EaSy") != levelEasy {
t.Errorf("invalid level")
}
if getLevel("2") != levelHard {
t.Errorf("invalid level")
}
if getLevel("hArd") != levelHard {
t.Errorf("invalid level")
}
if getLevel("else") != levelMedium {
t.Errorf("invalid level")
}
}
func TestStorage1(t *testing.T) {
storage := newStorage(true)
if storage.count() != 0 {
t.Errorf("count must be zero")
}
storage.cleanUp()
item := storage.newItem(levelHard, "fa", 3)
mustTrue := storage.validate(item.id, item.value)
if mustTrue != true {
t.Errorf("item validation must be true")
}
}
func TestStorage2(t *testing.T) {
storage := newStorage(true)
item := storage.newItem(levelEasy, "ar", 3)
mustTrue := storage.validate(item.id, item.value)
if mustTrue != true {
t.Errorf("item validation must be true")
}
mustFalse := storage.validate(item.id, item.value)
if mustFalse != false {
t.Errorf("item validation must be false, double checked")
}
}
func TestStorage3(t *testing.T) {
storage := newStorage(true)
item := storage.newItem(levelMedium, "en", 0)
time.Sleep(1 * time.Second)
mustFalse := storage.validate(item.id, item.value)
if mustFalse != false {
t.Errorf("item validation must be false, expired")
}
}
func TestStorage4(t *testing.T) {
storage := newStorage(true)
item1 := storage.newItem(levelMedium, "en", 0)
item2 := storage.newItem(999, "fa", 3)
time.Sleep(1 * time.Second)
mustFalse := storage.validate(item1.id, item1.value)
if mustFalse != false {
t.Errorf("item validation must be false, expired")
}
mustTrue := storage.validate(item2.id, item2.value)
if mustTrue != true {
t.Errorf("item validation must be true")
}
}
func TestStorage5(t *testing.T) {
storage := newStorage(true)
storage.newItem(levelMedium, "en", 0)
storage.newItem(levelMedium, "ar", 0)
storage.cleanUp()
storage.count()
if storage.count() != 0 {
t.Errorf("count must be zero")
}
}
func TestStorage6(t *testing.T) {
storage := newStorage(false)
storage.newItem(levelMedium, "en", 0)
storage.newItem(levelMedium, "ar", 0)
if storage.count() != 0 {
t.Errorf("count must be zero")
}
}
func BenchmarkGenerateID(b *testing.B) {
for i := 0; i < b.N; i++ {
generateID()
}
}
func BenchmarkGenerateProblem(b *testing.B) {
for i := 0; i < b.N; i++ {
generateProblem(levelMedium)
}
}