github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/results/blockresults/segheap_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  	"container/heap"
    21  	"math/rand"
    22  	"testing"
    23  
    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_recordHeapAscending(t *testing.T) {
    30  
    31  	count := 10
    32  	ascending := true
    33  	pq := make(SortedResultRecords, 0)
    34  	heap.Init(&pq)
    35  
    36  	for i := count; i > 0; i-- {
    37  		currRRC := &utils.RecordResultContainer{
    38  			SortColumnValue: float64(i),
    39  		}
    40  		currItem := &ResultRecordSort{
    41  			Ascending: ascending,
    42  			Rrc:       currRRC,
    43  		}
    44  		heap.Push(&pq, currItem)
    45  	}
    46  	currRRC := &utils.RecordResultContainer{
    47  		SortColumnValue: float64(1.1),
    48  	}
    49  	currItem := &ResultRecordSort{
    50  		Ascending: ascending,
    51  		Rrc:       currRRC,
    52  	}
    53  	heap.Push(&pq, currItem)
    54  
    55  	var prevVal float64
    56  	firstVal := true
    57  	for i := 0; i < count; i++ {
    58  		item := heap.Pop(&pq).(*ResultRecordSort)
    59  		if firstVal {
    60  			prevVal = item.Rrc.SortColumnValue
    61  			firstVal = false
    62  		} else {
    63  			assert.True(t, item.Rrc.SortColumnValue > prevVal)
    64  			prevVal = item.Rrc.SortColumnValue
    65  		}
    66  		log.Infof("%+v: %+v ", item.Index, item.Rrc.SortColumnValue)
    67  	}
    68  	_ = heap.Pop(&pq).(*ResultRecordSort)
    69  	assert.Equal(t, pq.Len(), 0)
    70  }
    71  
    72  func Test_recordHeapDescending(t *testing.T) {
    73  
    74  	count := 10
    75  	ascending := false
    76  	pq := make(SortedResultRecords, 0)
    77  	heap.Init(&pq)
    78  
    79  	currRRC := &utils.RecordResultContainer{
    80  		SortColumnValue: float64(1.1),
    81  	}
    82  	currItem := &ResultRecordSort{
    83  		Ascending: ascending,
    84  		Rrc:       currRRC,
    85  	}
    86  	heap.Push(&pq, currItem)
    87  
    88  	for i := count; i > 0; i-- {
    89  		currRRC := &utils.RecordResultContainer{
    90  			SortColumnValue: rand.Float64(),
    91  		}
    92  		currItem := &ResultRecordSort{
    93  			Ascending: ascending,
    94  			Rrc:       currRRC,
    95  		}
    96  		heap.Push(&pq, currItem)
    97  	}
    98  
    99  	var prevVal float64
   100  	firstVal := true
   101  	for i := 0; i < count; i++ {
   102  		item := heap.Pop(&pq).(*ResultRecordSort)
   103  		if firstVal {
   104  			prevVal = item.Rrc.SortColumnValue
   105  			firstVal = false
   106  		} else {
   107  			assert.True(t, item.Rrc.SortColumnValue < prevVal)
   108  			prevVal = item.Rrc.SortColumnValue
   109  		}
   110  		log.Infof("%+v: %+v ", item.Index, item.Rrc.SortColumnValue)
   111  	}
   112  	_ = heap.Pop(&pq).(*ResultRecordSort)
   113  	assert.Equal(t, pq.Len(), 0)
   114  }