2022-04-08 11:36:53 +00:00
|
|
|
package treeiter_test
|
2022-02-19 23:23:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-17 15:28:33 +00:00
|
|
|
|
2022-04-08 11:36:53 +00:00
|
|
|
"gitlab.com/slon/shad-go/treeiter"
|
2022-02-19 23:23:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ValuesNode[T any] struct {
|
|
|
|
value T
|
|
|
|
left, right *ValuesNode[T]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t ValuesNode[T]) Left() *ValuesNode[T] {
|
|
|
|
return t.left
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t ValuesNode[T]) Right() *ValuesNode[T] {
|
|
|
|
return t.right
|
|
|
|
}
|
|
|
|
|
|
|
|
type Collector[T any] struct {
|
|
|
|
nodes []*ValuesNode[T]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector[T]) Collect(tree *ValuesNode[T]) {
|
|
|
|
c.nodes = append(c.nodes, tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInOrderNil(t *testing.T) {
|
|
|
|
var collector Collector[any]
|
2022-04-08 11:36:53 +00:00
|
|
|
treeiter.DoInOrder(nil, collector.Collect)
|
2022-02-19 23:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInOrderIntTree(t *testing.T) {
|
|
|
|
root := &ValuesNode[int]{value: 1}
|
|
|
|
collector := Collector[int]{}
|
|
|
|
|
2022-04-08 11:36:53 +00:00
|
|
|
treeiter.DoInOrder(root, collector.Collect)
|
2022-02-19 23:23:36 +00:00
|
|
|
|
|
|
|
assert.Equal(t, []*ValuesNode[int]{{value: 1}}, collector.nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInOrderStringTree(t *testing.T) {
|
|
|
|
rightLeftRight := &ValuesNode[string]{
|
|
|
|
value: "right left right",
|
|
|
|
}
|
|
|
|
rightLeft := &ValuesNode[string]{
|
|
|
|
value: "right left",
|
|
|
|
right: rightLeftRight,
|
|
|
|
}
|
|
|
|
right := &ValuesNode[string]{
|
|
|
|
value: "right",
|
|
|
|
left: rightLeft,
|
|
|
|
}
|
|
|
|
left := &ValuesNode[string]{
|
|
|
|
value: "left",
|
|
|
|
}
|
|
|
|
|
|
|
|
root := &ValuesNode[string]{
|
|
|
|
value: "root",
|
|
|
|
left: left,
|
|
|
|
right: right,
|
|
|
|
}
|
|
|
|
var collector Collector[string]
|
|
|
|
|
2022-04-08 11:36:53 +00:00
|
|
|
treeiter.DoInOrder(root, collector.Collect)
|
2022-02-19 23:23:36 +00:00
|
|
|
|
|
|
|
assert.Equal(t, []*ValuesNode[string]{left, root, rightLeft, rightLeftRight, right}, collector.nodes)
|
|
|
|
}
|