github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/pqmr/pqmatchresults_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 pqmr
    18  
    19  import (
    20  	"os"
    21  
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestPqmrEncodeDecode(t *testing.T) {
    28  
    29  	fname := "pqmr_res.bin"
    30  	os.Remove(fname)
    31  
    32  	numBlocks := uint16(10)
    33  	numRecs := uint(20_000)
    34  
    35  	for i := uint16(0); i < numBlocks; i++ {
    36  		pqmr := CreatePQMatchResults(10)
    37  
    38  		for recNum := uint(0); recNum < numRecs; recNum++ {
    39  			if recNum%3 == 0 {
    40  				pqmr.AddMatchedRecord(recNum)
    41  			}
    42  		}
    43  		//		t.Logf("blknum=%v, numRecs=%v, bitset size=%v", i, numRecs, pqmr.b.BinaryStorageSize())
    44  		err := pqmr.FlushPqmr(&fname, i)
    45  		assert.Nil(t, err)
    46  	}
    47  
    48  	res, err := ReadPqmr(&fname)
    49  	assert.Nil(t, err)
    50  
    51  	assert.Equal(t, numBlocks, res.GetNumBlocks(), "expected numBlocks=%v, actual=%v", numBlocks, res.GetNumBlocks())
    52  
    53  	for i := uint16(0); i < numBlocks; i++ {
    54  		pqmr, exists := res.GetBlockResults(i)
    55  		assert.True(t, exists, "bitset for blkNum=%v does not exist", i)
    56  		for recNum := uint(0); recNum < numRecs; recNum++ {
    57  			isMatched := pqmr.DoesRecordMatch(recNum)
    58  			if recNum%3 == 0 {
    59  				assert.Equal(t, true, isMatched, "blkNum=%v, recNum=%v, did not match", i, recNum)
    60  			} else {
    61  				assert.Equal(t, false, isMatched, "blkNum=%v, recNum=%v, did not match", i, recNum)
    62  			}
    63  		}
    64  	}
    65  
    66  	//cleanup
    67  	os.Remove(fname)
    68  }