Search Apps Documentation Source Content File Folder Download Copy

tree_test.gno

3.50 Kb ยท 161 lines
  1package avl
  2
  3import "testing"
  4
  5func TestNewTree(t *testing.T) {
  6	tree := NewTree()
  7	if tree.node != nil {
  8		t.Error("Expected tree.node to be nil")
  9	}
 10}
 11
 12func TestTreeSize(t *testing.T) {
 13	tree := NewTree()
 14	if tree.Size() != 0 {
 15		t.Error("Expected empty tree size to be 0")
 16	}
 17
 18	tree.Set("key1", "value1")
 19	tree.Set("key2", "value2")
 20	if tree.Size() != 2 {
 21		t.Error("Expected tree size to be 2")
 22	}
 23}
 24
 25func TestTreeHas(t *testing.T) {
 26	tree := NewTree()
 27	tree.Set("key1", "value1")
 28
 29	if !tree.Has("key1") {
 30		t.Error("Expected tree to have key1")
 31	}
 32
 33	if tree.Has("key2") {
 34		t.Error("Expected tree to not have key2")
 35	}
 36}
 37
 38func TestTreeGet(t *testing.T) {
 39	tree := NewTree()
 40	tree.Set("key1", "value1")
 41
 42	value, exists := tree.Get("key1")
 43	if !exists || value != "value1" {
 44		t.Error("Expected Get to return value1 and true")
 45	}
 46
 47	_, exists = tree.Get("key2")
 48	if exists {
 49		t.Error("Expected Get to return false for non-existent key")
 50	}
 51}
 52
 53func TestTreeGetByIndex(t *testing.T) {
 54	tree := NewTree()
 55	tree.Set("key1", "value1")
 56	tree.Set("key2", "value2")
 57
 58	key, value := tree.GetByIndex(0)
 59	if key != "key1" || value != "value1" {
 60		t.Error("Expected GetByIndex(0) to return key1 and value1")
 61	}
 62
 63	key, value = tree.GetByIndex(1)
 64	if key != "key2" || value != "value2" {
 65		t.Error("Expected GetByIndex(1) to return key2 and value2")
 66	}
 67
 68	defer func() {
 69		if r := recover(); r == nil {
 70			t.Error("Expected GetByIndex to panic for out-of-range index")
 71		}
 72	}()
 73	tree.GetByIndex(2)
 74}
 75
 76func TestTreeRemove(t *testing.T) {
 77	tree := NewTree()
 78	tree.Set("key1", "value1")
 79
 80	value, removed := tree.Remove("key1")
 81	if !removed || value != "value1" || tree.Size() != 0 {
 82		t.Error("Expected Remove to remove key-value pair")
 83	}
 84
 85	_, removed = tree.Remove("key2")
 86	if removed {
 87		t.Error("Expected Remove to return false for non-existent key")
 88	}
 89}
 90
 91func TestTreeIterate(t *testing.T) {
 92	tree := NewTree()
 93	tree.Set("key1", "value1")
 94	tree.Set("key2", "value2")
 95	tree.Set("key3", "value3")
 96
 97	var keys []string
 98	tree.Iterate("", "", func(key string, value interface{}) bool {
 99		keys = append(keys, key)
100		return false
101	})
102
103	expectedKeys := []string{"key1", "key2", "key3"}
104	if !slicesEqual(keys, expectedKeys) {
105		t.Errorf("Expected keys %v, got %v", expectedKeys, keys)
106	}
107}
108
109func TestTreeReverseIterate(t *testing.T) {
110	tree := NewTree()
111	tree.Set("key1", "value1")
112	tree.Set("key2", "value2")
113	tree.Set("key3", "value3")
114
115	var keys []string
116	tree.ReverseIterate("", "", func(key string, value interface{}) bool {
117		keys = append(keys, key)
118		return false
119	})
120
121	expectedKeys := []string{"key3", "key2", "key1"}
122	if !slicesEqual(keys, expectedKeys) {
123		t.Errorf("Expected keys %v, got %v", expectedKeys, keys)
124	}
125}
126
127func TestTreeIterateByOffset(t *testing.T) {
128	tree := NewTree()
129	tree.Set("key1", "value1")
130	tree.Set("key2", "value2")
131	tree.Set("key3", "value3")
132
133	var keys []string
134	tree.IterateByOffset(1, 2, func(key string, value interface{}) bool {
135		keys = append(keys, key)
136		return false
137	})
138
139	expectedKeys := []string{"key2", "key3"}
140	if !slicesEqual(keys, expectedKeys) {
141		t.Errorf("Expected keys %v, got %v", expectedKeys, keys)
142	}
143}
144
145func TestTreeReverseIterateByOffset(t *testing.T) {
146	tree := NewTree()
147	tree.Set("key1", "value1")
148	tree.Set("key2", "value2")
149	tree.Set("key3", "value3")
150
151	var keys []string
152	tree.ReverseIterateByOffset(1, 2, func(key string, value interface{}) bool {
153		keys = append(keys, key)
154		return false
155	})
156
157	expectedKeys := []string{"key2", "key1"}
158	if !slicesEqual(keys, expectedKeys) {
159		t.Errorf("Expected keys %v, got %v", expectedKeys, keys)
160	}
161}