-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathexample_serialize_test.go
52 lines (46 loc) · 1.11 KB
/
example_serialize_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
package set_test
import (
"log"
)
import (
"github.com/timtadh/data-structures/list"
"github.com/timtadh/data-structures/set"
"github.com/timtadh/data-structures/types"
)
func makeSet() *set.SortedSet {
return set.FromSlice([]types.Hashable{types.Int(1), types.Int(-1), types.Int(3)})
}
func serialize(s *set.SortedSet) ([]byte, error) {
marshal, unmarshal := types.IntMarshals()
m := set.NewMSortedSet(s, marshal, unmarshal)
return m.MarshalBinary()
}
func deserialize(bytes []byte) (*set.SortedSet, error) {
marshal, unmarshal := types.IntMarshals()
m := &set.MSortedSet{MSorted: list.MSorted{MList: list.MList{MarshalItem: marshal, UnmarshalItem: unmarshal}}}
err := m.UnmarshalBinary(bytes)
if err != nil {
return nil, err
}
return m.SortedSet(), nil
}
func Example_serialize() {
a := makeSet()
b := makeSet()
if !a.Equals(b) {
log.Panic("a was not equal to b")
}
bytes, err := serialize(a)
if err != nil {
log.Panic(err)
}
log.Println(bytes)
c, err := deserialize(bytes)
if err != nil {
log.Panic(err)
}
if !c.Equals(b) {
log.Panic("c was not equal to b")
}
log.Println("success")
}