github.com/XiaoMi/Gaea@v1.2.5/proxy/plan/util_test.go (about) 1 // Copyright 2016 The kingshard Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 // Copyright 2019 The Gaea Authors. All Rights Reserved. 16 // 17 // Licensed under the Apache License, Version 2.0 (the "License"); 18 // you may not use this file except in compliance with the License. 19 // You may obtain a copy of the License at 20 // 21 // http://www.apache.org/licenses/LICENSE-2.0 22 // 23 // Unless required by applicable law or agreed to in writing, software 24 // distributed under the License is distributed on an "AS IS" BASIS, 25 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 26 // See the License for the specific language governing permissions and 27 // limitations under the License. 28 29 package plan 30 31 import ( 32 "sort" 33 "testing" 34 ) 35 36 func testCheckList(t *testing.T, l []int, checkList ...int) { 37 if len(l) != len(checkList) { 38 t.Fatal("invalid list len", len(l), len(checkList)) 39 } 40 41 for i := 0; i < len(l); i++ { 42 if l[i] != checkList[i] { 43 t.Fatal("invalid list item", l[i], i) 44 } 45 } 46 } 47 48 func TestListSet(t *testing.T) { 49 var l1 []int 50 var l2 []int 51 var l3 []int 52 53 l1 = []int{1, 2, 3} 54 l2 = []int{2} 55 56 l3 = interList(l1, l2) 57 testCheckList(t, l3, 2) 58 59 l1 = []int{1, 2, 3} 60 l2 = []int{2, 3} 61 62 l3 = interList(l1, l2) 63 testCheckList(t, l3, 2, 3) 64 65 l1 = []int{1, 2, 4} 66 l2 = []int{2, 3} 67 68 l3 = interList(l1, l2) 69 testCheckList(t, l3, 2) 70 71 l1 = []int{1, 2, 4} 72 l2 = []int{} 73 74 l3 = interList(l1, l2) 75 testCheckList(t, l3) 76 77 l1 = []int{1, 2, 3} 78 l2 = []int{2} 79 80 l3 = unionList(l1, l2) 81 testCheckList(t, l3, 1, 2, 3) 82 83 l1 = []int{1, 2, 4} 84 l2 = []int{3} 85 86 l3 = unionList(l1, l2) 87 testCheckList(t, l3, 1, 2, 3, 4) 88 89 l1 = []int{1, 2, 3} 90 l2 = []int{2, 3, 4} 91 92 l3 = unionList(l1, l2) 93 testCheckList(t, l3, 1, 2, 3, 4) 94 95 l1 = []int{1, 2, 3} 96 l2 = []int{} 97 98 l3 = unionList(l1, l2) 99 testCheckList(t, l3, 1, 2, 3) 100 101 l1 = []int{1, 2, 3, 4} 102 l2 = []int{2} 103 104 l3 = differentList(l1, l2) 105 testCheckList(t, l3, 1, 3, 4) 106 107 l1 = []int{1, 2, 3, 4} 108 l2 = []int{} 109 110 l3 = differentList(l1, l2) 111 testCheckList(t, l3, 1, 2, 3, 4) 112 113 l1 = []int{1, 2, 3, 4} 114 l2 = []int{1, 3, 5} 115 116 l3 = differentList(l1, l2) 117 testCheckList(t, l3, 2, 4) 118 119 l1 = []int{1, 2, 3} 120 l2 = []int{1, 3, 5, 6} 121 122 l3 = differentList(l1, l2) 123 testCheckList(t, l3, 2) 124 125 l1 = []int{1, 2, 3, 4} 126 l2 = []int{2, 3} 127 128 l3 = differentList(l1, l2) 129 testCheckList(t, l3, 1, 4) 130 131 l1 = []int{1, 2, 2, 1, 5, 3, 5, 2} 132 l2 = cleanList(l1) 133 sort.Sort(sort.IntSlice(l2)) 134 testCheckList(t, l2, 1, 2, 3, 5) 135 } 136 137 func TestMakeLeList(t *testing.T) { 138 l1 := []int{20150802, 20150812, 20150822, 20150823, 20150825, 20150828} 139 l2 := makeLeList(20150822, l1) 140 testCheckList(t, l2, 20150802, 20150812, 20150822) 141 l3 := makeLeList(20150824, l1) 142 testCheckList(t, l3, []int{}...) 143 } 144 145 func TestMakeLtList(t *testing.T) { 146 l1 := []int{20150802, 20150812, 20150822, 20150823, 20150825, 20150828} 147 l2 := makeLtList(20150822, l1) 148 testCheckList(t, l2, 20150802, 20150812) 149 l3 := makeLtList(20150824, l1) 150 testCheckList(t, l3, []int{}...) 151 l4 := makeLtList(20150802, l1) 152 testCheckList(t, l4, []int{}...) 153 } 154 155 func TestMakeGeList(t *testing.T) { 156 l1 := []int{20150802, 20150812, 20150822, 20150823, 20150825, 20150828} 157 l2 := makeGeList(20150822, l1) 158 testCheckList(t, l2, 20150822, 20150823, 20150825, 20150828) 159 l3 := makeGeList(20150828, l1) 160 testCheckList(t, l3, 20150828) 161 } 162 163 func TestMakeGtList(t *testing.T) { 164 l1 := []int{20150802, 20150812, 20150822, 20150823, 20150825, 20150828} 165 l2 := makeGtList(20150822, l1) 166 testCheckList(t, l2, 20150823, 20150825, 20150828) 167 l3 := makeGtList(20150824, l1) 168 testCheckList(t, l3, []int{}...) 169 l4 := makeGtList(20150828, l1) 170 testCheckList(t, l4, []int{}...) 171 }