-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbag_test.go
126 lines (118 loc) · 2.65 KB
/
bag_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
// Copyright 2023 Sneller, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ion
import (
"testing"
)
func TestBag(t *testing.T) {
items := []Datum{
Null,
String("foo"),
Int(-1),
Uint(1000),
Bool(true),
Bool(false),
NewStruct(nil,
[]Field{
{"foo", String("foo"), 0},
{"bar", Null, 0},
{"inner", NewList(nil, []Datum{
Int(-1), Uint(0), Uint(1),
}).Datum(), 0},
{"name", String("should-come-first"), 0},
},
).Datum(),
}
var bag Bag
for i := range items {
bag.AddDatum(items[i])
}
if bag.Len() != len(items) {
t.Fatalf("bag.Len=%d; expected %d", bag.Len(), len(items))
}
i := 0
bag.Each(func(d Datum) bool {
if !d.Equal(items[i]) {
t.Errorf("item %d is %v", i, d)
}
i++
return true
})
// transcode to a second symbol table
var st Symtab
for _, x := range []string{"baz", "bar", "foo", "quux"} {
st.Intern(x)
}
var buf Buffer
var bag2 Bag
bag.Encode(&buf, &st)
bag2.Add(&st, buf.Bytes())
if !bag.Equals(&bag2) {
t.Fatal("!bag.Equal(bag2)")
}
bag.Append(&bag2)
if bag.Len() != len(items)*2 {
t.Fatalf("bag.Len=%d, want %d", bag.Len(), len(items)*2)
}
i = 0
n := 0
bag.Each(func(d Datum) bool {
if !d.Equal(items[i]) {
t.Errorf("item %d is %v", i, d)
}
i++
n++
if i == len(items) {
i = 0
}
return true
})
if n != bag.Len() {
t.Fatalf("Each iterated %d times, but bag.Len()=%d", n, bag.Len())
}
bag.Reset()
if bag.Len() != 0 {
t.Fatalf("bag.Len = %d after reset?", bag.Len())
}
i = 0
bag.Each(func(d Datum) bool {
i++
return true
})
if i > 0 {
t.Fatalf("bag has contents (%d items) after reset?", i)
}
bag = bag2.Clone()
if !bag.Equals(&bag2) {
t.Errorf("cloned Bag not equal to itself")
}
var bag3 Bag
buf.Reset()
st.Reset()
w := bag3.Writer()
bag2.Encode(&buf, &st)
stpos := buf.Size()
st.Marshal(&buf, true)
data := append(buf.Bytes()[stpos:], buf.Bytes()[:stpos]...)
n, err := w.Write(data)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Fatalf("ion.Bag.Writer().Write wrote %d instead of %d bytes", n, len(data))
}
if !bag3.Equals(&bag2) {
t.Fatal("using bagWriter.Write did not produce an equivalent Bag")
}
}