github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/results/blockresults/sortblockresult_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package blockresults 18 19 import ( 20 "math/rand" 21 "testing" 22 23 "github.com/siglens/siglens/pkg/segment/structs" 24 "github.com/siglens/siglens/pkg/segment/utils" 25 log "github.com/sirupsen/logrus" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func Test_simpleRecordSortAscendingInvalid(t *testing.T) { 30 31 count := uint64(10) 32 ascending := true 33 sortReq := &structs.SortRequest{ 34 ColName: "name", 35 Ascending: ascending, 36 } 37 sort, _ := InitializeSort(count, sortReq) 38 39 for i := count; i > 0; i-- { 40 currItem := &utils.RecordResultContainer{ 41 SortColumnValue: float64(i), 42 } 43 sort.Add(currItem) 44 } 45 46 currItem := &utils.RecordResultContainer{ 47 SortColumnValue: float64(count + 1), 48 } 49 sort.Add(currItem) 50 allVals := sort.GetSortedResults() 51 minVal := allVals[0].SortColumnValue 52 maxVal := allVals[len(allVals)-1].SortColumnValue 53 assert.Equal(t, minVal, float64(1)) 54 assert.Equal(t, maxVal, float64(count)) 55 56 var prevVal float64 57 firstVal := true 58 for i := uint64(0); i < count; i++ { 59 item := allVals[i] 60 if firstVal { 61 prevVal = item.SortColumnValue 62 firstVal = false 63 } else { 64 assert.True(t, item.SortColumnValue > prevVal) 65 prevVal = item.SortColumnValue 66 } 67 log.Infof("%+v: %+v ", i, item.SortColumnValue) 68 } 69 assert.Len(t, allVals, 10, "dont add invalid column/more than count") 70 } 71 72 func Test_RecordSortAscendingReplace(t *testing.T) { 73 74 count := uint64(10) 75 ascending := true 76 sortReq := &structs.SortRequest{ 77 ColName: "name", 78 Ascending: ascending, 79 } 80 sort, _ := InitializeSort(count, sortReq) 81 82 for i := count; i > 0; i-- { 83 currItem := &utils.RecordResultContainer{ 84 SortColumnValue: float64(i), 85 } 86 sort.Add(currItem) 87 } 88 currItem := &utils.RecordResultContainer{ 89 SortColumnValue: float64(1.1), 90 } 91 sort.Add(currItem) 92 93 currItem = &utils.RecordResultContainer{ 94 SortColumnValue: float64(count + 1), 95 } 96 sort.Add(currItem) 97 allVals := sort.GetSortedResults() 98 minVal := allVals[0].SortColumnValue 99 maxVal := allVals[len(allVals)-1].SortColumnValue 100 assert.Equal(t, minVal, float64(1)) 101 assert.Equal(t, allVals[1].SortColumnValue, float64(1.1)) 102 assert.Equal(t, maxVal, float64(count-1)) 103 104 var prevVal float64 105 firstVal := true 106 for i := uint64(0); i < count; i++ { 107 item := allVals[i] 108 if firstVal { 109 prevVal = item.SortColumnValue 110 firstVal = false 111 } else { 112 assert.True(t, item.SortColumnValue > prevVal) 113 prevVal = item.SortColumnValue 114 } 115 log.Infof("%+v: %+v ", i, item.SortColumnValue) 116 } 117 assert.Len(t, allVals, 10, "dont add invalid column/more than count") 118 } 119 120 func Test_RecordSortDescendingInvalid(t *testing.T) { 121 122 count := uint64(10) 123 ascending := false 124 sortReq := &structs.SortRequest{ 125 ColName: "name", 126 Ascending: ascending, 127 } 128 sort, _ := InitializeSort(count, sortReq) 129 130 currItem := &utils.RecordResultContainer{ 131 SortColumnValue: float64(1.1), 132 } 133 sort.Add(currItem) 134 135 for i := count; i > 0; i-- { 136 currItem := &utils.RecordResultContainer{ 137 SortColumnValue: rand.Float64(), 138 } 139 sort.Add(currItem) 140 } 141 142 currItem = &utils.RecordResultContainer{ 143 SortColumnValue: float64(count + 1), 144 } 145 sort.Add(currItem) 146 allVals := sort.GetSortedResults() 147 148 // we know first two values should be (count + 1) and 1.1 as rand.Float64() gives a float (0, 1) 149 assert.Equal(t, allVals[0].SortColumnValue, float64(11)) 150 assert.Equal(t, allVals[1].SortColumnValue, float64(1.1)) 151 var prevVal float64 152 firstVal := true 153 for i := uint64(0); i < count; i++ { 154 item := allVals[i] 155 if firstVal { 156 prevVal = item.SortColumnValue 157 firstVal = false 158 } else { 159 assert.True(t, item.SortColumnValue < prevVal) 160 prevVal = item.SortColumnValue 161 } 162 log.Infof("%+v: %+v ", i, item.SortColumnValue) 163 } 164 assert.Len(t, allVals, 10, "dont add invalid column/more than count") 165 } 166 167 func Test_RecordSortDescendingReplace(t *testing.T) { 168 169 count := uint64(10) 170 ascending := false 171 sortReq := &structs.SortRequest{ 172 ColName: "name", 173 Ascending: ascending, 174 } 175 sort, _ := InitializeSort(count, sortReq) 176 177 currItem := &utils.RecordResultContainer{ 178 SortColumnValue: float64(1.1), 179 } 180 sort.Add(currItem) 181 182 for i := count; i > 0; i-- { 183 currItem := &utils.RecordResultContainer{ 184 SortColumnValue: rand.Float64(), 185 } 186 sort.Add(currItem) 187 } 188 189 currItem = &utils.RecordResultContainer{ 190 SortColumnValue: float64(count + 1), 191 } 192 sort.Add(currItem) 193 allVals := sort.GetSortedResults() 194 195 var prevVal float64 196 firstVal := true 197 for i := uint64(0); i < count; i++ { 198 item := allVals[i] 199 if firstVal { 200 prevVal = item.SortColumnValue 201 firstVal = false 202 } else { 203 assert.True(t, item.SortColumnValue < prevVal) 204 prevVal = item.SortColumnValue 205 } 206 log.Infof("%+v: %+v ", i, item.SortColumnValue) 207 } 208 assert.Len(t, allVals, 10, "dont add invalid column/more than count") 209 }