-
Notifications
You must be signed in to change notification settings - Fork 11
/
xml_tree_test.go
200 lines (177 loc) · 3.65 KB
/
xml_tree_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package golibxml
import "testing"
import "syscall"
type ElementTypeTestCase struct {
got ElementType
expected string
}
var element_type_tests[] ElementTypeTestCase = []ElementTypeTestCase{
{ XML_ELEMENT_NODE, "Node" },
{ XML_ATTRIBUTE_NODE, "Attribute" },
{ XML_TEXT_NODE, "Text" },
}
func getRSS() uint64 {
rusage := &syscall.Rusage{}
ret := syscall.Getrusage(0, rusage)
if ret == nil && rusage.Maxrss > 0 {
return uint64(rusage.Maxrss)
}
return 0
}
//
// Buffer tests
func testNewBuffer(t *testing.T) (buffer *Buffer) {
buffer = NewBuffer()
if buffer.Ptr == nil {
t.Fail()
}
return
}
func testBufferFree(t *testing.T, buffer *Buffer) {
buffer.Free()
if buffer.Ptr != nil {
t.Fail()
}
}
func TestNewBuffer(t *testing.T) {
testNewBuffer(t)
}
func TestNewBufferLeak(t *testing.T) {
var buffer *Buffer
for i := 0; i < 1000000; i++ {
buffer = testNewBuffer(t)
buffer.Free()
}
if getRSS() > 5000 {
t.Fatal("Memory leak")
}
}
func TestNewBufferSize(t *testing.T) {
buffer := NewBufferSize(10)
if buffer.Ptr == nil {
t.Fail()
}
return
}
func TestNewBufferSizeLeak(t *testing.T) {
var buffer *Buffer
for i := 0; i < 1000000; i++ {
buffer = NewBufferSize(1024)
buffer.Free()
}
if getRSS() > 5000 {
t.Fatal("Memory leak")
}
}
func TestBufferFree(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
}
func TestBufferWriteChar(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
buffer.WriteChar("test")
}
func TestBufferEmpty(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
buffer.WriteChar("test")
buffer.Empty()
if buffer.Content() != "" {
t.Fail()
}
}
func TestBufferResize(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
if buffer.Resize(10) != 1 {
t.Fail()
}
}
func TestBufferLength(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
if buffer.Length() != 0 {
t.Fail()
}
buffer.WriteChar("test")
if buffer.Length() != 4 {
t.Fail()
}
}
/*
func TestBufferGrow(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
buffer.Grow(128)
if buffer.Length() != 128 {
t.Fail()
}
}
func TestBufferShrink(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
buffer.Shrink(128)
if buffer.Length() != 128 {
t.Fail()
}
}
*/
func TestBufferCat(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
buffer.Cat("test")
}
func TestBufferContent(t *testing.T) {
buffer := testNewBuffer(t)
defer testBufferFree(t, buffer)
buffer.WriteChar("test")
if buffer.Content() != "test" {
t.Fail()
}
}
func TestNewDoc(t *testing.T) {
doc := NewDoc("1.0")
defer doc.Free()
result := doc.String()
if result != "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" {
t.Fail()
}
}
func TestNewNode(t *testing.T) {
doc := NewDoc("1.0")
defer doc.Free()
node := NewNode(nil, "div")
doc.AddChild(node)
result := node.String()
if result != "<div/>" {
t.Fail()
}
}
func TestNewNodePath(t *testing.T) {
doc := NewDoc("1.0")
defer doc.Free()
node := NewNode(nil, "div")
doc.AddChild(node)
result := node.Path()
if result != "/div" {
t.Fatal("Expected: /div Got:", node.Path())
}
}
func TestNewComment(t *testing.T) {
doc := NewDoc("1.0")
defer doc.Free()
comment := doc.NewComment("this is a comment")
doc.AddChild(comment)
result := comment.String()
if result != "<!--this is a comment-->" {
t.Fail()
}
}
func TestElementTypeString(t *testing.T) {
for _, test := range element_type_tests {
if test.got.String() != test.expected {
t.Fatal("Testing node type:", test.got, "got:", test.got.String(), "expected:", test.expected)
}
}
}