github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/refertable/query_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package refertable 7 8 import ( 9 "fmt" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestBinSearch(t *testing.T) { 16 cases := []struct { 17 li []int 18 v int 19 ok bool 20 idx int 21 }{ 22 { 23 []int{1, 2, 5, 6}, 24 2, 25 true, 26 1, 27 }, 28 { 29 []int{1, 2, 5, 6}, 30 5, 31 true, 32 2, 33 }, 34 { 35 []int{1, 2, 5, 6}, 36 1, 37 true, 38 0, 39 }, 40 { 41 []int{1, 2, 5, 6}, 42 6, 43 true, 44 3, 45 }, 46 { 47 []int{1, 2, 5, 6, 11}, 48 2, 49 true, 50 1, 51 }, 52 { 53 []int{1, 2, 5, 6, 11}, 54 11, 55 true, 56 4, 57 }, 58 { 59 []int{1}, 60 6, 61 false, 62 0, 63 }, 64 { 65 []int{1}, 66 1, 67 true, 68 0, 69 }, 70 } 71 72 for _, v := range cases { 73 if idx, ok := binSearch(v.li, v.v); ok { 74 assert.Equal(t, v.ok, ok) 75 assert.Equal(t, v.idx, idx) 76 } else { 77 assert.Equal(t, v.ok, ok) 78 } 79 } 80 } 81 82 func TestQuery(t *testing.T) { 83 cases := []struct { 84 index map[string]map[any][]int 85 keys []string 86 values []any 87 count int 88 ok bool 89 ret []int 90 }{ 91 { 92 index: map[string]map[any][]int{ 93 "key1": { 94 "1": {1, 3, 5, 11, 18}, 95 "3": {2, 4, 6, 8, 12}, 96 }, 97 "key2": { 98 1: {1, 2, 5, 8, 12, 17}, 99 2: {3, 4, 6, 11}, 100 }, 101 }, 102 keys: []string{"key2", "key1"}, 103 values: []any{1, "1"}, 104 count: 0, 105 ok: true, 106 ret: []int{1, 5}, 107 }, 108 { 109 index: map[string]map[any][]int{ 110 "key1": { 111 "1": {3, 5, 11, 18}, 112 "3": {2, 4, 6, 8, 12}, 113 }, 114 "key2": { 115 1: {1, 2, 3, 5, 8, 12, 17}, 116 2: {4, 6, 11}, 117 }, 118 "key3": { 119 1.1: {2, 3, 5}, 120 2.2: {1, 4, 6, 7}, 121 }, 122 }, 123 keys: []string{"key3", "key2", "key1"}, 124 values: []any{1.1, 1, "1"}, 125 count: 1, 126 ok: true, 127 ret: []int{3}, 128 }, 129 { 130 index: map[string]map[any][]int{}, 131 keys: []string{"key5", "key2", "key1"}, 132 values: []any{1.1, 1, "1"}, 133 count: 1, 134 ok: false, 135 ret: []int{3}, 136 }, 137 { 138 index: map[string]map[any][]int{ 139 "key1": {}, 140 "key3": { 141 1.1: {2, 3, 5}, 142 2.2: {1, 4, 6, 7}, 143 }, 144 }, 145 keys: []string{"key3", "key1"}, 146 values: []any{1.1, "1"}, 147 count: 1, 148 ok: false, 149 ret: []int{3}, 150 }, 151 { 152 index: map[string]map[any][]int{ 153 "key1": { 154 "1": {1, 3, 5, 11, 18}, 155 "3": {2, 4, 6, 8, 12}, 156 }, 157 "key2": { 158 1: {2, 8, 12, 17}, 159 2: {3, 4, 6, 11}, 160 }, 161 }, 162 keys: []string{"key2", "key1"}, 163 values: []any{1, "1"}, 164 count: 0, 165 ok: false, 166 ret: []int{1, 5}, 167 }, 168 { 169 index: map[string]map[any][]int{ 170 "key1": { 171 "1": {1, 3, 5, 11, 18}, 172 "3": {2, 4, 6, 8, 12}, 173 }, 174 "key2": { 175 1: {2, 8, 12, 17}, 176 2: {3, 4, 6, 11}, 177 }, 178 }, 179 keys: []string{"key2", "key1"}, 180 values: []any{1}, 181 count: 0, 182 ok: false, 183 ret: []int{1, 5}, 184 }, 185 } 186 for idx, v := range cases { 187 t.Run(fmt.Sprint("index_", idx), func(t *testing.T) { 188 if ret, ok := query(v.index, v.keys, v.values, v.count); ok { 189 assert.Equal(t, v.ok, ok) 190 assert.Equal(t, v.ret, ret) 191 } else { 192 assert.Equal(t, v.ok, ok) 193 } 194 }) 195 } 196 }