-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgotree_test.go
More file actions
489 lines (450 loc) · 9.34 KB
/
gotree_test.go
File metadata and controls
489 lines (450 loc) · 9.34 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package gotree
import (
"fmt"
"testing"
)
func ExampleTree() {
artist := New("Pantera")
album := artist.Add("Far Beyond Driven\nsee https://en.wikipedia.org/wiki/Pantera\n(1994)")
five := album.Add("5 minutes Alone")
five.Add("song by American\ngroove metal")
album.Add("I’m Broken")
album.Add("Good Friends and a Bottle of Pills")
artist.Add("Power Metal\n(1988)")
artist.Add("Cowboys from Hell\n(1990)")
fmt.Println(artist.Print())
// Output:
// Pantera
// ├── Far Beyond Driven
// │ see https://en.wikipedia.org/wiki/Pantera
// │ (1994)
// │ ├── 5 minutes Alone
// │ │ └── song by American
// │ │ groove metal
// │ ├── I’m Broken
// │ └── Good Friends and a Bottle of Pills
// ├── Power Metal
// │ (1988)
// └── Cowboys from Hell
// (1990)
}
func TestNew(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
args args
want Tree
}{
{
name: "Create new Tree",
args: args{
text: "new tree",
},
want: &tree{
text: "new tree",
items: []Tree{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := New(tt.args.text)
if got.Text() != tt.want.Text() {
t.Errorf("New() text = %v, want %v", got.Text(), tt.want.Text())
}
if len(got.Items()) != len(tt.want.Items()) {
t.Errorf("New() items length = %v, want %v", len(got.Items()), len(tt.want.Items()))
}
})
}
}
func Test_tree_Add(t *testing.T) {
type fields struct {
text string
items []Tree
}
type args struct {
text string
}
tests := []struct {
name string
fields fields
args args
want Tree
parentCount int
}{
{
name: "Adding a new item into an empty tree",
args: args{
text: "child item",
},
fields: fields{
items: []Tree{},
},
want: &tree{
text: "child item",
items: []Tree{},
},
parentCount: 1,
},
{
name: "Adding a new item into a full tree",
args: args{
text: "fourth item",
},
fields: fields{
items: []Tree{
New("test"),
New("test2"),
New("test3"),
},
},
want: &tree{
text: "fourth item",
items: []Tree{},
},
parentCount: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
got := tree.Add(tt.args.text)
if got.Text() != tt.want.Text() {
t.Errorf("tree.Add() text = %v, want %v", got.Text(), tt.want.Text())
}
if len(got.Items()) != len(tt.want.Items()) {
t.Errorf("tree.Add() items length = %v, want %v", len(got.Items()), len(tt.want.Items()))
}
if tt.parentCount != len(tree.Items()) {
t.Errorf("tree total items = %v, want %v", len(tree.Items()), tt.parentCount)
}
})
}
}
func Test_tree_AddTree(t *testing.T) {
type fields struct {
text string
items []Tree
}
type args struct {
tree Tree
}
tests := []struct {
name string
fields fields
args args
itemCount int
}{
{
name: "Adding a new item into an empty tree",
args: args{
tree: New("child item"),
},
fields: fields{
items: []Tree{},
},
itemCount: 1,
},
{
name: "Adding a new item into a full tree",
args: args{
tree: New("fourth item"),
},
fields: fields{
items: []Tree{
New("test"),
New("test2"),
New("test3"),
},
},
itemCount: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
tree.AddTree(tt.args.tree)
})
}
}
func Test_tree_Text(t *testing.T) {
type fields struct {
text string
items []Tree
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Return the correct value",
fields: fields{
text: "item",
},
want: "item",
},
{
name: "Return the correct value while empty",
fields: fields{
text: "",
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
if got := tree.Text(); got != tt.want {
t.Errorf("tree.Text() = %v, want %v", got, tt.want)
}
})
}
}
func Test_tree_Items(t *testing.T) {
type fields struct {
text string
items []Tree
}
tests := []struct {
name string
fields fields
want []Tree
}{
{
name: "Return empty if there is no items under the tree",
fields: fields{
text: "top level item",
items: []Tree{},
},
want: []Tree{},
},
{
name: "Return all items under the tree",
fields: fields{
text: "top level item",
items: []Tree{
New("first child"),
New("second child"),
},
},
want: []Tree{
New("first child"),
New("second child"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
got := tree.Items()
if len(got) != len(tt.want) {
t.Fatalf("Items() length = %v, want %v", len(got), len(tt.want))
}
for i := range got {
if got[i].Text() != tt.want[i].Text() {
t.Errorf("Items()[%d].Text() = %v, want %v", i, got[i].Text(), tt.want[i].Text())
}
}
})
}
}
func Test_tree_Print(t *testing.T) {
threeLevelTree := New("First Level")
threeLevelTree.Add("Second level").Add("Third Level")
complexTree := New("Daft Punk")
ram := complexTree.Add("Random Access Memories")
complexTree.Add("Humam After All")
alive := complexTree.Add("Alive 2007")
ram.Add("Give Life Back to Music")
ram.Add("Giorgio by Moroder")
ram.Add("Within")
alive.Add("Touch It/Technologic")
alive.Add("Face to Face/Too Long")
type fields struct {
tree Tree
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Print a single item tree",
fields: fields{
tree: New("single item"),
},
want: `single item
`,
},
{
name: "Print a three level tree",
fields: fields{
tree: threeLevelTree,
},
want: `First Level
└── Second level
└── Third Level
`,
},
{
name: "Print a three level tree",
fields: fields{
tree: complexTree,
},
want: `Daft Punk
├── Random Access Memories
│ ├── Give Life Back to Music
│ ├── Giorgio by Moroder
│ └── Within
├── Humam After All
└── Alive 2007
├── Touch It/Technologic
└── Face to Face/Too Long
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.tree.Print(); got != tt.want {
t.Errorf("tree.Print() = %#v, want %#v", got, tt.want)
}
})
}
}
// Benchmark tests
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
New("test node")
}
}
func BenchmarkAdd(b *testing.B) {
tree := New("root")
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.Add("child")
}
}
func BenchmarkPrintSmallTree(b *testing.B) {
tree := New("Root")
tree.Add("Child 1")
tree.Add("Child 2")
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.Print()
}
}
func BenchmarkPrintDeepTree(b *testing.B) {
tree := New("Root")
current := tree
for i := 0; i < 10; i++ {
current = current.Add("Level " + string(rune(i+'0')))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.Print()
}
}
func BenchmarkPrintWideTree(b *testing.B) {
tree := New("Root")
for i := 0; i < 100; i++ {
tree.Add("Child " + string(rune(i+'0')))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.Print()
}
}
func BenchmarkPrintComplexTree(b *testing.B) {
tree := New("Root")
for i := 0; i < 10; i++ {
branch := tree.Add("Branch " + string(rune(i+'0')))
for j := 0; j < 10; j++ {
leaf := branch.Add("Leaf " + string(rune(i+'0')) + "-" + string(rune(j+'0')))
if j%2 == 0 {
leaf.Add("Sub-leaf")
}
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.Print()
}
}
func BenchmarkPrintMultilineText(b *testing.B) {
tree := New("Root")
multilineText := "Line 1\nLine 2\nLine 3\nLine 4\nLine 5"
for i := 0; i < 5; i++ {
tree.Add(multilineText)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tree.Print()
}
}
// Edge case tests
func TestAddTree_Nil(t *testing.T) {
tree := New("root")
tree.AddTree(nil) // Should not panic
if len(tree.Items()) != 0 {
t.Errorf("AddTree(nil) should not add item, got %d items", len(tree.Items()))
}
}
func TestPrint_EmptyTree(t *testing.T) {
tree := New("")
got := tree.Print()
want := "\n"
if got != want {
t.Errorf("Print() = %#v, want %#v", got, want)
}
}
func TestPrint_VeryDeepTree(t *testing.T) {
tree := New("Root")
current := tree
// Create a tree 100 levels deep
for i := 0; i < 100; i++ {
current = current.Add(fmt.Sprintf("Level %d", i))
}
// Should not panic or cause stack overflow
output := tree.Print()
// Verify it contains expected depth
if len(output) == 0 {
t.Error("Deep tree should produce output")
}
}
func TestText_PreservesInput(t *testing.T) {
tests := []struct {
name string
text string
}{
{"empty", ""},
{"simple", "simple"},
{"with newlines", "with\nnewlines"},
{"with tabs", "with\ttabs"},
{"with unicode", "with unicode: 🌲🌳🌴"},
{"with special chars", "with special chars: !@#$%^&*()"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := New(tt.text)
if tree.Text() != tt.text {
t.Errorf("Text() = %v, want %v", tree.Text(), tt.text)
}
})
}
}