-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapstorage_test.go
122 lines (78 loc) · 2.97 KB
/
mapstorage_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
package main
import (
"testing"
)
func TestSetAndGet(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("babab"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 0, "invalid flag")
assertEquals(t, int(bytes), 5, "invalid byte lenght")
assertEquals(t, string(content), "babab", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func TestSetShouldUpdateCas(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
_, _, cas_before, _, _ := storage.Get("foo")
storage.Set("foo", 0, 60, 5, []byte("bbbbb"))
_, _, cas_after, _, _ := storage.Get("foo")
assertNotEquals(t, cas_before, cas_after, "Invalid cas update")
}
func TestAddShouldFailIfKeyAlreadyExists(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Add("foo", 1, 30, 4, []byte("bbbb"))
assertNotEquals(t, err, nil, "failed to add")
}
func TestAddShouldAddIfNotExists(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Add("bar", 1, 30, 4, []byte("bbbb"))
assertEquals(t, err, nil, "failed to add")
}
func TestShouldReplaceIfExists(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Replace("foo", 1, 120, 4, []byte("bbbb"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 1, "invalid flag")
assertEquals(t, int(bytes), 4, "invalid byte lenght")
assertEquals(t, string(content), "bbbb", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func TestReplaceShouldFailIfKeyNotExists(t *testing.T) {
storage := newMapStorage()
err := storage.Replace("foo", 0, 60, 4, []byte("aaaa"))
assertNotEquals(t, err, nil, "invalid error")
}
func TestShouldAppendContentForKey(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Append("foo", 4, []byte("bbbb"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 0, "invalid flag")
assertEquals(t, int(bytes), 9, "invalid byte lenght")
assertEquals(t, string(content), "aaaaabbbb", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func TestShouldPrependContentForKey(t *testing.T) {
storage := newMapStorage()
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Prepend("foo", 4, []byte("bbbb"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 0, "invalid flag")
assertEquals(t, int(bytes), 9, "invalid byte lenght")
assertEquals(t, string(content), "bbbbaaaaa", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func assertEquals(t *testing.T, a interface{}, b interface{}, cause string) {
if a != b {
t.Error(cause);
}
}
func assertNotEquals(t *testing.T, a interface{}, b interface{}, cause string) {
if a == b {
t.Error(cause);
}
}