| 1 |
package gtrie |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/tinylib/msgp/msgp" |
| 12 |
) |
| 13 |
|
| 14 |
func TestMarshalUnmarshalNode(t *testing.T) { |
| 15 |
v := Node{} |
| 16 |
bts, err := v.MarshalMsg(nil) |
| 17 |
if err != nil { |
| 18 |
t.Fatal(err) |
| 19 |
} |
| 20 |
left, err := v.UnmarshalMsg(bts) |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
if len(left) > 0 { |
| 25 |
t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) |
| 26 |
} |
| 27 |
|
| 28 |
left, err = msgp.Skip(bts) |
| 29 |
if err != nil { |
| 30 |
t.Fatal(err) |
| 31 |
} |
| 32 |
if len(left) > 0 { |
| 33 |
t.Errorf("%d bytes left over after Skip(): %q", len(left), left) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func BenchmarkMarshalMsgNode(b *testing.B) { |
| 38 |
v := Node{} |
| 39 |
b.ReportAllocs() |
| 40 |
b.ResetTimer() |
| 41 |
for i := 0; i < b.N; i++ { |
| 42 |
v.MarshalMsg(nil) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func BenchmarkAppendMsgNode(b *testing.B) { |
| 47 |
v := Node{} |
| 48 |
bts := make([]byte, 0, v.Msgsize()) |
| 49 |
bts, _ = v.MarshalMsg(bts[0:0]) |
| 50 |
b.SetBytes(int64(len(bts))) |
| 51 |
b.ReportAllocs() |
| 52 |
b.ResetTimer() |
| 53 |
for i := 0; i < b.N; i++ { |
| 54 |
bts, _ = v.MarshalMsg(bts[0:0]) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func BenchmarkUnmarshalNode(b *testing.B) { |
| 59 |
v := Node{} |
| 60 |
bts, _ := v.MarshalMsg(nil) |
| 61 |
b.ReportAllocs() |
| 62 |
b.SetBytes(int64(len(bts))) |
| 63 |
b.ResetTimer() |
| 64 |
for i := 0; i < b.N; i++ { |
| 65 |
_, err := v.UnmarshalMsg(bts) |
| 66 |
if err != nil { |
| 67 |
b.Fatal(err) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestEncodeDecodeNode(t *testing.T) { |
| 73 |
v := Node{} |
| 74 |
var buf bytes.Buffer |
| 75 |
msgp.Encode(&buf, &v) |
| 76 |
|
| 77 |
m := v.Msgsize() |
| 78 |
if buf.Len() > m { |
| 79 |
t.Logf("WARNING: Msgsize() for %v is inaccurate", v) |
| 80 |
} |
| 81 |
|
| 82 |
vn := Node{} |
| 83 |
err := msgp.Decode(&buf, &vn) |
| 84 |
if err != nil { |
| 85 |
t.Error(err) |
| 86 |
} |
| 87 |
|
| 88 |
buf.Reset() |
| 89 |
msgp.Encode(&buf, &v) |
| 90 |
err = msgp.NewReader(&buf).Skip() |
| 91 |
if err != nil { |
| 92 |
t.Error(err) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func BenchmarkEncodeNode(b *testing.B) { |
| 97 |
v := Node{} |
| 98 |
var buf bytes.Buffer |
| 99 |
msgp.Encode(&buf, &v) |
| 100 |
b.SetBytes(int64(buf.Len())) |
| 101 |
en := msgp.NewWriter(msgp.Nowhere) |
| 102 |
b.ReportAllocs() |
| 103 |
b.ResetTimer() |
| 104 |
for i := 0; i < b.N; i++ { |
| 105 |
v.EncodeMsg(en) |
| 106 |
} |
| 107 |
en.Flush() |
| 108 |
} |
| 109 |
|
| 110 |
func BenchmarkDecodeNode(b *testing.B) { |
| 111 |
v := Node{} |
| 112 |
var buf bytes.Buffer |
| 113 |
msgp.Encode(&buf, &v) |
| 114 |
b.SetBytes(int64(buf.Len())) |
| 115 |
rd := msgp.NewEndlessReader(buf.Bytes(), b) |
| 116 |
dc := msgp.NewReader(rd) |
| 117 |
b.ReportAllocs() |
| 118 |
b.ResetTimer() |
| 119 |
for i := 0; i < b.N; i++ { |
| 120 |
err := v.DecodeMsg(dc) |
| 121 |
if err != nil { |
| 122 |
b.Fatal(err) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestMarshalUnmarshalTransition(t *testing.T) { |
| 128 |
v := Transition{} |
| 129 |
bts, err := v.MarshalMsg(nil) |
| 130 |
if err != nil { |
| 131 |
t.Fatal(err) |
| 132 |
} |
| 133 |
left, err := v.UnmarshalMsg(bts) |
| 134 |
if err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
if len(left) > 0 { |
| 138 |
t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) |
| 139 |
} |
| 140 |
|
| 141 |
left, err = msgp.Skip(bts) |
| 142 |
if err != nil { |
| 143 |
t.Fatal(err) |
| 144 |
} |
| 145 |
if len(left) > 0 { |
| 146 |
t.Errorf("%d bytes left over after Skip(): %q", len(left), left) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func BenchmarkMarshalMsgTransition(b *testing.B) { |
| 151 |
v := Transition{} |
| 152 |
b.ReportAllocs() |
| 153 |
b.ResetTimer() |
| 154 |
for i := 0; i < b.N; i++ { |
| 155 |
v.MarshalMsg(nil) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func BenchmarkAppendMsgTransition(b *testing.B) { |
| 160 |
v := Transition{} |
| 161 |
bts := make([]byte, 0, v.Msgsize()) |
| 162 |
bts, _ = v.MarshalMsg(bts[0:0]) |
| 163 |
b.SetBytes(int64(len(bts))) |
| 164 |
b.ReportAllocs() |
| 165 |
b.ResetTimer() |
| 166 |
for i := 0; i < b.N; i++ { |
| 167 |
bts, _ = v.MarshalMsg(bts[0:0]) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func BenchmarkUnmarshalTransition(b *testing.B) { |
| 172 |
v := Transition{} |
| 173 |
bts, _ := v.MarshalMsg(nil) |
| 174 |
b.ReportAllocs() |
| 175 |
b.SetBytes(int64(len(bts))) |
| 176 |
b.ResetTimer() |
| 177 |
for i := 0; i < b.N; i++ { |
| 178 |
_, err := v.UnmarshalMsg(bts) |
| 179 |
if err != nil { |
| 180 |
b.Fatal(err) |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestEncodeDecodeTransition(t *testing.T) { |
| 186 |
v := Transition{} |
| 187 |
var buf bytes.Buffer |
| 188 |
msgp.Encode(&buf, &v) |
| 189 |
|
| 190 |
m := v.Msgsize() |
| 191 |
if buf.Len() > m { |
| 192 |
t.Logf("WARNING: Msgsize() for %v is inaccurate", v) |
| 193 |
} |
| 194 |
|
| 195 |
vn := Transition{} |
| 196 |
err := msgp.Decode(&buf, &vn) |
| 197 |
if err != nil { |
| 198 |
t.Error(err) |
| 199 |
} |
| 200 |
|
| 201 |
buf.Reset() |
| 202 |
msgp.Encode(&buf, &v) |
| 203 |
err = msgp.NewReader(&buf).Skip() |
| 204 |
if err != nil { |
| 205 |
t.Error(err) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func BenchmarkEncodeTransition(b *testing.B) { |
| 210 |
v := Transition{} |
| 211 |
var buf bytes.Buffer |
| 212 |
msgp.Encode(&buf, &v) |
| 213 |
b.SetBytes(int64(buf.Len())) |
| 214 |
en := msgp.NewWriter(msgp.Nowhere) |
| 215 |
b.ReportAllocs() |
| 216 |
b.ResetTimer() |
| 217 |
for i := 0; i < b.N; i++ { |
| 218 |
v.EncodeMsg(en) |
| 219 |
} |
| 220 |
en.Flush() |
| 221 |
} |
| 222 |
|
| 223 |
func BenchmarkDecodeTransition(b *testing.B) { |
| 224 |
v := Transition{} |
| 225 |
var buf bytes.Buffer |
| 226 |
msgp.Encode(&buf, &v) |
| 227 |
b.SetBytes(int64(buf.Len())) |
| 228 |
rd := msgp.NewEndlessReader(buf.Bytes(), b) |
| 229 |
dc := msgp.NewReader(rd) |
| 230 |
b.ReportAllocs() |
| 231 |
b.ResetTimer() |
| 232 |
for i := 0; i < b.N; i++ { |
| 233 |
err := v.DecodeMsg(dc) |
| 234 |
if err != nil { |
| 235 |
b.Fatal(err) |
| 236 |
} |
| 237 |
} |
| 238 |
} |