github.com/chain5j/chain5j-pkg@v1.0.7/collection/maps/treemap/v2/random_test.go (about) 1 package treemap 2 3 import ( 4 "math/rand" 5 "reflect" 6 "sort" 7 "strconv" 8 "testing" 9 ) 10 11 const NumIterations = 10000 12 const RandMax = 40 13 14 func TestRandom(t *testing.T) { 15 tr := New[int, string]() 16 mp := make(map[int]string) 17 kvs := testRandomData() 18 for i, kv := range kvs { 19 k, v := kv.k, kv.v 20 exp, expOK := mp[k] 21 if actual, actualOK := tr.Get(k); actual != exp || actualOK != expOK { 22 t.Errorf("wrong returned value, expected %s, actual %s", exp, actual) 23 } 24 25 if i%3 == 0 && (i/200)%2 == 0 { 26 tr.Set(k, v) 27 mp[k] = v 28 } else { 29 delete(mp, k) 30 tr.Del(k) 31 } 32 33 if len(mp) != tr.Len() { 34 t.Errorf("wrong count, expected %d, actual %d", len(mp), tr.Len()) 35 return 36 } 37 38 testKeys(t, mp, tr) 39 testMinMax(t, mp, tr) 40 testReverse(t, mp, tr) 41 42 if !treeInvariant(tr.endNode.left) { 43 t.Errorf("invariant error") 44 } 45 } 46 } 47 48 func min(kv map[int]string) *int { 49 var key *int 50 for k := range kv { 51 if key == nil || k < *key { 52 temp := k 53 key = &temp 54 } 55 } 56 return key 57 } 58 59 func max(kv map[int]string) *int { 60 var key *int 61 for k := range kv { 62 if key == nil || k > *key { 63 temp := k 64 key = &temp 65 } 66 } 67 return key 68 } 69 70 type pair struct { 71 k int 72 v string 73 } 74 75 func testRandomData() []pair { 76 var kv []pair 77 for i := 0; i < NumIterations; i++ { 78 k := int(rand.Int63n(RandMax)) 79 v := strconv.Itoa(int(rand.Int63n(RandMax))) 80 kv = append(kv, pair{k, v}) 81 } 82 return kv 83 } 84 85 func testKeys(t *testing.T, mp map[int]string, tr *TreeMap[int, string]) { 86 var actualKeys []int 87 for it := tr.Iterator(); it.Valid(); it.Next() { 88 actualKeys = append(actualKeys, it.Key()) 89 } 90 91 var expKeys []int 92 for k := range mp { 93 expKeys = append(expKeys, k) 94 } 95 sort.Ints(expKeys) 96 97 if !reflect.DeepEqual(actualKeys, expKeys) { 98 t.Errorf("wrong keys, expected %v, actual %v", expKeys, actualKeys) 99 } 100 } 101 102 func testMinMax(t *testing.T, mp map[int]string, tr *TreeMap[int, string]) { 103 exp := min(mp) 104 var actual *int 105 if it := tr.Iterator(); it.Valid() { 106 temp := it.Key() 107 actual = &temp 108 } 109 if (exp == nil) != (actual == nil) { 110 t.Errorf("wrong min") 111 } else if exp != nil && actual != nil && *exp != *actual { 112 t.Errorf("wrong min, expected %d, actual %d", *exp, *actual) 113 } 114 115 exp = max(mp) 116 actual = nil 117 if it := tr.Reverse(); it.Valid() { 118 temp := it.Key() 119 actual = &temp 120 } 121 if (exp == nil) != (actual == nil) { 122 t.Errorf("wrong max") 123 } else if exp != nil && actual != nil && *exp != *actual { 124 t.Errorf("wrong max, expected %d, actual %d", *exp, *actual) 125 } 126 } 127 128 func testReverse(t *testing.T, mp map[int]string, tr *TreeMap[int, string]) { 129 for it := tr.Reverse(); it.Valid(); it.Next() { 130 if mp[it.Key()] != it.Value() { 131 t.Errorf("wrong value, expected %s, actual %s", mp[it.Key()], it.Value()) 132 } 133 } 134 }