focus/set/treeset/treeset_test.go

195 lines
3.9 KiB
Go
Raw Permalink Normal View History

new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
package treeset
import (
"strings"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree/avldup"
)
func TestTreeSet_Add(t *testing.T) {
type fields struct {
tree *avldup.Tree
}
type args struct {
items []interface{}
}
tests := []struct {
name string
result string
fields fields
args args
}{
{name: "add int", result: "(1, 3, 5)", args: args{items: []interface{}{1, 5, 3, 3, 5}}},
{name: "add -int", result: "(-5, 1, 5, 3132)", args: args{items: []interface{}{-5, -5, 3132, 3132, 5, 1, 1, 1}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
set := New(compare.Int)
set.Add(tt.args.items...)
if set.String() != tt.result {
t.Error(set.String(), " != ", tt.result)
}
})
}
tests2 := []struct {
name string
result string
fields fields
args args
}{
{name: "add String 1", result: "(1, 3, 5)", args: args{items: []interface{}{"1", "5", "3", "3", "5"}}},
{name: "add String 2", result: "(-5, 1, 3132, 5)", args: args{items: []interface{}{"-5", "-5", "3132", "3132", "5", "1", "1", "1"}}},
{name: "add String 3", result: "(a, aa, b, bc)", args: args{items: []interface{}{"a", "b", "aa", "aa", "bc"}}},
{name: "add String 4", result: "(他, 你, 我, 我我)", args: args{items: []interface{}{"我", "你", "他", "我", "我我"}}},
}
for _, tt := range tests2 {
t.Run(tt.name, func(t *testing.T) {
set := New(compare.String)
set.Add(tt.args.items...)
if set.String() != tt.result {
t.Error(set.String(), " != ", tt.result)
}
vstr := spew.Sprint(set.Values())
if vstr[1:len(vstr)-1] != strings.ReplaceAll(tt.result[1:len(tt.result)-1], ",", "") {
t.Error(vstr[1:len(vstr)-1], tt.result[1:len(tt.result)-1])
}
})
}
}
func TestTreeSet_Remove(t *testing.T) {
type fields struct {
tree *avldup.Tree
}
type args struct {
addItems []interface{}
removeItems []interface{}
}
tests := []struct {
name string
result string
fields fields
args args
}{
// TODO: Add test cases.
{name: "remove 1", result: "()",
args: args{
addItems: []interface{}{5, 7, 5, 3, 2},
removeItems: []interface{}{5, 7, 3, 2}},
},
{name: "remove 2", result: "(5)",
args: args{
addItems: []interface{}{5, 7, 5, 3, 2},
removeItems: []interface{}{7, 3, 2}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
set := New(compare.Int)
set.Add(tt.args.addItems...)
set.Remove(tt.args.removeItems...)
if set.String() != tt.result {
t.Error(set.String(), " != ", tt.result)
}
})
}
}
func TestTreeSet_Base(t *testing.T) {
set := New(compare.Int)
set.Add(5, 4, 3, 5)
if !set.Contains(5) {
t.Error("Contains 5 is true")
}
set.Clear()
if set.Contains(5) {
t.Error("Contains 5 is false")
}
if !set.Empty() {
t.Error("should be Empty")
}
set.Add(1, 1, 1, 2, 2, 3, 7, 9, 10)
var results []interface{}
results = set.GetRange(4, 10)
if spew.Sprint(results) != "[7 9 10]" {
t.Error(results)
}
results = set.GetRange(1, 10)
if spew.Sprint(results) != "[1 2 3 7 9 10]" {
t.Error(results)
}
results3 := set.GetAround(3)
if spew.Sprint(results3) != "[2 3 7]" {
t.Error(results3)
}
results3 = set.GetAround(1)
if spew.Sprint(results3) != "[<nil> 1 2]" {
t.Error(results3)
}
}
func TestTreeSet_Iterator(t *testing.T) {
set := New(compare.Int)
set.Add(5, 4, 3, 5)
iter := set.Iterator()
iter.ToHead()
// if not call Next Prev will error
// 5 4 3
// if iter.Value() != nil {
// t.Error(iter.Value())
// }
iter.Next()
if iter.Value() != 3 {
t.Error(iter.Value())
}
iter.Next()
if iter.Value() != 4 {
t.Error(iter.Value())
}
iter.ToTail()
iter.Prev()
if iter.Value() != 5 {
t.Error(iter.Value())
}
iter.Prev()
if iter.Value() != 4 {
t.Error(iter.Value())
}
iter.ToHead()
iter.Prev()
if iter.Value() != 3 {
t.Error(iter.Value())
}
iter.ToTail()
iter.Next()
if iter.Value() != 5 {
t.Error(iter.Value())
}
}