github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/ds/art/art_test.go (about) 1 package art 2 3 import ( 4 "bytes" 5 "github.com/stretchr/testify/assert" 6 "reflect" 7 "sort" 8 "testing" 9 ) 10 11 func TestAdaptiveRadixTree_PrefixScan(t *testing.T) { 12 art := NewART() 13 art.Put([]byte("acse"), 123) 14 art.Put([]byte("cced"), 123) 15 art.Put([]byte("acde"), 123) 16 art.Put([]byte("bbfe"), 123) 17 art.Put([]byte("bbfc"), 123) 18 art.Put([]byte("eefs"), 123) 19 20 keys1 := art.PrefixScan([]byte("bbf"), -1) 21 assert.Equal(t, 0, len(keys1)) 22 23 keys2 := art.PrefixScan(nil, 0) 24 assert.Equal(t, 0, len(keys2)) 25 26 keys3 := art.PrefixScan([]byte("b"), 1) 27 assert.Equal(t, 1, len(keys3)) 28 29 keys4 := art.PrefixScan(nil, 6) 30 assert.Equal(t, 6, len(keys4)) 31 } 32 33 func TestAdaptiveRadixTree_Put(t *testing.T) { 34 tree := NewART() 35 type args struct { 36 key []byte 37 value interface{} 38 } 39 tests := []struct { 40 name string 41 art *AdaptiveRadixTree 42 args args 43 wantOldVal interface{} 44 wantUpdated bool 45 }{ 46 { 47 "nil", tree, args{key: nil, value: nil}, nil, false, 48 }, 49 { 50 "normal-1", tree, args{key: []byte("1"), value: 11}, nil, false, 51 }, 52 { 53 // must run after previous one. 54 "normal-2", tree, args{key: []byte("1"), value: 22}, 11, true, 55 }, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 gotOldVal, gotUpdated := tt.art.Put(tt.args.key, tt.args.value) 60 if !reflect.DeepEqual(gotOldVal, tt.wantOldVal) { 61 t.Errorf("Put() gotOldVal = %v, want %v", gotOldVal, tt.wantOldVal) 62 } 63 if gotUpdated != tt.wantUpdated { 64 t.Errorf("Put() gotUpdated = %v, want %v", gotUpdated, tt.wantUpdated) 65 } 66 }) 67 } 68 } 69 70 func TestAdaptiveRadixTree_Get(t *testing.T) { 71 tree := NewART() 72 tree.Put(nil, nil) 73 tree.Put([]byte("0"), 0) 74 tree.Put([]byte("11"), 11) 75 tree.Put([]byte("11"), "rewrite-data") 76 77 type args struct { 78 key []byte 79 } 80 tests := []struct { 81 name string 82 tree *AdaptiveRadixTree 83 args args 84 want interface{} 85 }{ 86 { 87 88 "nil", tree, args{key: nil}, nil, 89 }, 90 { 91 "zero", tree, args{key: []byte("0")}, 0, 92 }, 93 { 94 "rewrite-data", tree, args{key: []byte("11")}, "rewrite-data", 95 }, 96 } 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 if got := tt.tree.Get(tt.args.key); !reflect.DeepEqual(got, tt.want) { 100 t.Errorf("Get() = %v, want %v", got, tt.want) 101 } 102 }) 103 } 104 } 105 106 func TestAdaptiveRadixTree_Delete(t *testing.T) { 107 tree := NewART() 108 tree.Put(nil, nil) 109 tree.Put([]byte("0"), 0) 110 tree.Put([]byte("11"), 11) 111 tree.Put([]byte("11"), "rewrite-data") 112 113 type args struct { 114 key []byte 115 } 116 tests := []struct { 117 name string 118 tree *AdaptiveRadixTree 119 args args 120 wantVal interface{} 121 wantUpdated bool 122 }{ 123 { 124 "nil", tree, args{key: nil}, nil, false, 125 }, 126 { 127 "zero", tree, args{key: []byte("0")}, 0, true, 128 }, 129 { 130 "rewrite-data", tree, args{key: []byte("11")}, "rewrite-data", true, 131 }, 132 { 133 "not-exist", tree, args{key: []byte("not-exist")}, nil, false, 134 }, 135 } 136 for _, tt := range tests { 137 t.Run(tt.name, func(t *testing.T) { 138 gotVal, gotUpdated := tt.tree.Delete(tt.args.key) 139 if !reflect.DeepEqual(gotVal, tt.wantVal) { 140 t.Errorf("Delete() gotVal = %v, want %v", gotVal, tt.wantVal) 141 } 142 if gotUpdated != tt.wantUpdated { 143 t.Errorf("Delete() gotUpdated = %v, want %v", gotUpdated, tt.wantUpdated) 144 } 145 }) 146 } 147 } 148 149 func TestAdaptiveRadixTree_Iterator(t *testing.T) { 150 art := NewART() 151 iter1 := art.Iterator() 152 assert.False(t, iter1.HasNext()) 153 154 var keys = [][]byte{[]byte("acse"), []byte("cced"), []byte("acde"), []byte("bbfe")} 155 for i, key := range keys { 156 art.Put(key, i) 157 } 158 159 sort.Slice(keys, func(i, j int) bool { 160 return bytes.Compare(keys[i], keys[j]) < 0 161 }) 162 var targes [][]byte 163 iter2 := art.Iterator() 164 for iter2.HasNext() { 165 node, err := iter2.Next() 166 assert.Nil(t, err) 167 targes = append(targes, node.Key()) 168 } 169 assert.Equal(t, keys, targes) 170 }