-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
intern_test.go
146 lines (116 loc) · 2.71 KB
/
intern_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
137
138
139
140
141
142
143
144
145
146
package msgpack_test
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/require"
"github.com/vmihailenco/msgpack/v5"
)
type NoIntern struct {
A string
B string
C interface{}
}
type Intern struct {
A string `msgpack:",intern"`
B string `msgpack:",intern"`
C interface{} `msgpack:",intern"`
}
func TestInternedString(t *testing.T) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
enc.UseInternedStrings(true)
dec := msgpack.NewDecoder(&buf)
dec.UseInternedStrings(true)
for i := 0; i < 3; i++ {
err := enc.EncodeString("hello")
require.Nil(t, err)
}
for i := 0; i < 3; i++ {
s, err := dec.DecodeString()
require.Nil(t, err)
require.Equal(t, "hello", s)
}
err := enc.Encode("hello")
require.Nil(t, err)
v, err := dec.DecodeInterface()
require.Nil(t, err)
require.Equal(t, "hello", v)
_, err = dec.DecodeInterface()
require.Equal(t, io.EOF, err)
}
func TestInternedStringTag(t *testing.T) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
dec := msgpack.NewDecoder(&buf)
in := []Intern{
{"f", "f", "f"},
{"fo", "fo", "fo"},
{"foo", "foo", "foo"},
{"f", "fo", "foo"},
}
err := enc.Encode(in)
require.Nil(t, err)
var out []Intern
err = dec.Decode(&out)
require.Nil(t, err)
require.Equal(t, in, out)
}
func TestResetDict(t *testing.T) {
dict := []string{"hello world", "foo bar"}
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
dec := msgpack.NewDecoder(&buf)
{
enc.ResetDict(&buf, dictMap(dict))
err := enc.EncodeString("hello world")
require.Nil(t, err)
require.Equal(t, 3, buf.Len())
dec.ResetDict(&buf, dict)
s, err := dec.DecodeString()
require.Nil(t, err)
require.Equal(t, "hello world", s)
}
{
enc.ResetDict(&buf, dictMap(dict))
err := enc.Encode("foo bar")
require.Nil(t, err)
require.Equal(t, 3, buf.Len())
dec.ResetDict(&buf, dict)
s, err := dec.DecodeInterface()
require.Nil(t, err)
require.Equal(t, "foo bar", s)
}
dec.ResetDict(&buf, dict)
_ = enc.EncodeString("xxxx")
require.Equal(t, 5, buf.Len())
_ = enc.Encode("xxxx")
require.Equal(t, 10, buf.Len())
}
func TestMapWithInternedString(t *testing.T) {
type M map[string]interface{}
dict := []string{"hello world", "foo bar"}
var buf bytes.Buffer
enc := msgpack.NewEncoder(nil)
enc.ResetDict(&buf, dictMap(dict))
dec := msgpack.NewDecoder(nil)
dec.ResetDict(&buf, dict)
for i := 0; i < 100; i++ {
in := M{
"foo bar": "hello world",
"hello world": "foo bar",
"foo": "bar",
}
err := enc.Encode(in)
require.Nil(t, err)
_, err = dec.DecodeInterface()
require.Nil(t, err)
}
}
func dictMap(dict []string) map[string]int {
m := make(map[string]int, len(dict))
for i, s := range dict {
m[s] = i
}
return m
}