Debian Codegraph @

redistest/resp/encode_test.go

raw
1 package resp
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 var encodeValidCases = []struct {
9 enc []byte
10 val interface{}
11 err error
12 }{
13 0: {[]byte{'+', '\r', '\n'}, SimpleString(""), nil},
14 1: {[]byte{'+', 'a', '\r', '\n'}, SimpleString("a"), nil},
15 2: {[]byte{'+', 'O', 'K', '\r', '\n'}, SimpleString("OK"), nil},
16 3: {[]byte("+ceci n'est pas un string\r\n"), SimpleString("ceci n'est pas un string"), nil},
17 4: {[]byte{'-', '\r', '\n'}, Error(""), nil},
18 5: {[]byte{'-', 'a', '\r', '\n'}, Error("a"), nil},
19 6: {[]byte{'-', 'K', 'O', '\r', '\n'}, Error("KO"), nil},
20 7: {[]byte("-ceci n'est pas un string\r\n"), Error("ceci n'est pas un string"), nil},
21 8: {[]byte(":1\r\n"), int64(1), nil},
22 9: {[]byte(":123\r\n"), int64(123), nil},
23 10: {[]byte(":-123\r\n"), int64(-123), nil},
24 11: {[]byte("$0\r\n\r\n"), "", nil},
25 12: {[]byte("$24\r\nceci n'est pas un string\r\n"), "ceci n'est pas un string", nil},
26 13: {[]byte("$51\r\nceci n'est pas un string\r\navec\rdes\nsauts\r\nde\x00ligne.\r\n"), "ceci n'est pas un string\r\navec\rdes\nsauts\r\nde\x00ligne.", nil},
27 14: {[]byte("$-1\r\n"), nil, nil},
28 15: {[]byte("*0\r\n"), Array{}, nil},
29 16: {[]byte("*1\r\n:10\r\n"), Array{int64(10)}, nil},
30 17: {[]byte("*-1\r\n"), Array(nil), nil},
31 18: {[]byte("*3\r\n+string\r\n-error\r\n:-2345\r\n"),
32 Array{SimpleString("string"), Error("error"), int64(-2345)}, nil},
33 19: {[]byte("*5\r\n+string\r\n-error\r\n:-2345\r\n$4\r\nallo\r\n*2\r\n$0\r\n\r\n$-1\r\n"),
34 Array{SimpleString("string"), Error("error"), int64(-2345), "allo",
35 Array{"", nil}}, nil},
36 }
37
38 func TestEncode(t *testing.T) {
39 var buf bytes.Buffer
40 var err error
41
42 for i, c := range encodeValidCases {
43 buf.Reset()
44 err = Encode(&buf, c.val)
45 if err != nil {
46 t.Errorf("%d: got error %s", i, err)
47 continue
48 }
49 if !bytes.Equal(buf.Bytes(), c.enc) {
50 t.Errorf("%d: expected %x (%q), got %x (%q)", i, c.enc, string(c.enc), buf.Bytes(), buf.String())
51 }
52 }
53 }
54
55 func BenchmarkEncodeSimpleString(b *testing.B) {
56 var err error
57 var buf bytes.Buffer
58 for i := 0; i < b.N; i++ {
59 err = Encode(&buf, encodeValidCases[3].val)
60 }
61 if err != nil {
62 b.Fatal(err)
63 }
64 }
65
66 func BenchmarkEncodeError(b *testing.B) {
67 var err error
68 var buf bytes.Buffer
69 for i := 0; i < b.N; i++ {
70 err = Encode(&buf, encodeValidCases[7].val)
71 }
72 if err != nil {
73 b.Fatal(err)
74 }
75 }
76
77 func BenchmarkEncodeInteger(b *testing.B) {
78 var err error
79 var buf bytes.Buffer
80 for i := 0; i < b.N; i++ {
81 err = Encode(&buf, encodeValidCases[10].val)
82 }
83 if err != nil {
84 b.Fatal(err)
85 }
86 }
87
88 func BenchmarkEncodeBulkString(b *testing.B) {
89 var err error
90 var buf bytes.Buffer
91 for i := 0; i < b.N; i++ {
92 err = Encode(&buf, encodeValidCases[13].val)
93 }
94 if err != nil {
95 b.Fatal(err)
96 }
97 }
98
99 func BenchmarkEncodeArray(b *testing.B) {
100 var err error
101 var buf bytes.Buffer
102 for i := 0; i < b.N; i++ {
103 err = Encode(&buf, encodeValidCases[19].val)
104 }
105 if err != nil {
106 b.Fatal(err)
107 }
108 }