github.com/hdt3213/godis@v1.2.9/datastruct/dict/simple_test.go (about) 1 package dict 2 3 import ( 4 "github.com/hdt3213/godis/lib/utils" 5 "sort" 6 "testing" 7 ) 8 9 func TestSimpleDict_Keys(t *testing.T) { 10 d := MakeSimple() 11 size := 10 12 var expectKeys []string 13 for i := 0; i < size; i++ { 14 str := utils.RandString(5) 15 d.Put(str, str) 16 expectKeys = append(expectKeys, str) 17 } 18 sort.Slice(expectKeys, func(i, j int) bool { 19 return expectKeys[i] > expectKeys[j] 20 }) 21 keys := d.Keys() 22 if len(keys) != size { 23 t.Errorf("expect %d keys, actual: %d", size, len(d.Keys())) 24 } 25 sort.Slice(keys, func(i, j int) bool { 26 return keys[i] > keys[j] 27 }) 28 for i, k := range keys { 29 if k != expectKeys[i] { 30 t.Errorf("expect %s actual %s", expectKeys[i], k) 31 } 32 } 33 } 34 35 func TestSimpleDict_PutIfExists(t *testing.T) { 36 d := MakeSimple() 37 key := utils.RandString(5) 38 val := key + "1" 39 ret := d.PutIfExists(key, val) 40 if ret != 0 { 41 t.Error("expect 0") 42 return 43 } 44 d.Put(key, val) 45 val = key + "2" 46 ret = d.PutIfExists(key, val) 47 if ret != 1 { 48 t.Error("expect 1") 49 return 50 } 51 if v, _ := d.Get(key); v != val { 52 t.Error("wrong value") 53 return 54 } 55 }