-
Notifications
You must be signed in to change notification settings - Fork 104
/
in_memory_database_test.go
44 lines (38 loc) · 1.37 KB
/
in_memory_database_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
package strings
import (
"strings"
"testing"
)
/*
TestInMemoryDictionary tests solution(s) with the following signature and problem description:
func RunDBCommand(cmd string) string
Write an in memory database that stores string key-value pairs and supports SET, GET, EXISTS,
and UNSET commands. It should also allow transactions with BEGIN, COMMIT and ROLLBACK commands.
*/
func TestInMemoryDictionary(t *testing.T) {
tests := []struct {
input, allOutputs string
}{
{"EXISTS A\nSET A 1\nGET A", "false 1"},
{"EXISTS A\nSET A 1\nGET A\nEXISTS A\nUNSET A\nGET A\nEXISTS A", "false 1 true nil false"},
{"GET A\nBEGIN\nSET A 1\nGET A\nROLLBACK\nGET A", "nil 1 nil"},
{"GET A\nBEGIN\nSET A 1\nGET A\nCOMMIT\nGET A", "nil 1 1"},
{"SET A 1\nGET A\nBEGIN\nSET A 2\nGET A\nBEGIN\nUNSET A\nGET A\nCOMMIT\nROLLBACK\nGET A", "1 2 nil 1"},
{"SET A 2\nGET A\nBEGIN\nSET A 1\nGET A\nCOMMIT\nGET A\nBEGIN\nSET A 2\nGET A\nROLLBACK\nGET A", "2 1 1 2 1"},
}
for i, test := range tests {
allOutputs := ""
dbs = make([]map[string]string, 0)
dbs = append(dbs, make(map[string]string))
for _, cmd := range strings.Split(test.input, "\n") {
output := RunDBCommand(cmd)
if output != "" {
allOutputs += " " + output
}
}
allOutputs = allOutputs[1:]
if allOutputs != test.allOutputs {
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.allOutputs, allOutputs)
}
}
}