-
Notifications
You must be signed in to change notification settings - Fork 112
/
gob_test.go
64 lines (58 loc) · 1.32 KB
/
gob_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
package mxj
import (
"bytes"
"encoding/gob"
"fmt"
"testing"
)
var gobData = map[string]interface{}{
"one": 1,
"two": 2.0001,
"three": "tres",
"four": []int{1, 2, 3, 4},
"five": map[string]interface{}{"hi": "there"}}
func TestGobHeader(t *testing.T) {
fmt.Println("\n---------------- gob_test.go ...")
}
func TestNewMapGob(t *testing.T) {
var buf bytes.Buffer
gob.Register(gobData)
enc := gob.NewEncoder(&buf)
if err := enc.Encode(gobData); err != nil {
t.Fatal("enc.Encode err:", err.Error())
}
// decode 'buf' into a Map - map[string]interface{}
m, err := NewMapGob(buf.Bytes())
if err != nil {
t.Fatal("NewMapGob err:", err.Error())
}
fmt.Printf("m: %v\n", m)
}
func TestMapGob(t *testing.T) {
mv := Map(gobData)
g, err := mv.Gob()
if err != nil {
t.Fatal("m.Gob err:", err.Error())
}
// decode 'g' into a map[string]interface{}
m := make(map[string]interface{})
r := bytes.NewReader(g)
dec := gob.NewDecoder(r)
if err := dec.Decode(&m); err != nil {
t.Fatal("dec.Decode err:", err.Error())
}
fmt.Printf("m: %v\n", m)
}
func TestGobSymmetric(t *testing.T) {
mv := Map(gobData)
fmt.Printf("mv: %v\n", mv)
g, err := mv.Gob()
if err != nil {
t.Fatal("m.Gob err:", err.Error())
}
m, err := NewMapGob(g)
if err != nil {
t.Fatal("NewMapGob err:", err.Error())
}
fmt.Printf("m : %v\n", m)
}