github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/inverted/searcher_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package inverted
    13  
    14  import (
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/weaviate/sroar"
    19  )
    20  
    21  func TestDocBitmap(t *testing.T) {
    22  	t.Run("empty doc bitmap", func(t *testing.T) {
    23  		dbm := newDocBitmap()
    24  
    25  		assert.Equal(t, 0, dbm.count())
    26  		assert.Empty(t, dbm.IDs())
    27  	})
    28  
    29  	t.Run("filled doc bitmap", func(t *testing.T) {
    30  		ids := []uint64{1, 2, 3, 4, 5}
    31  
    32  		dbm := newDocBitmap()
    33  		dbm.docIDs.SetMany(ids)
    34  
    35  		assert.Equal(t, 5, dbm.count())
    36  		assert.ElementsMatch(t, ids, dbm.IDs())
    37  	})
    38  }
    39  
    40  func TestDocBitmap_IDsWithLimit(t *testing.T) {
    41  	type test struct {
    42  		name           string
    43  		limit          int
    44  		input          []uint64
    45  		expectedOutput []uint64
    46  	}
    47  
    48  	tests := []test{
    49  		{
    50  			name:           "empty bitmap, positive limit",
    51  			input:          []uint64{},
    52  			limit:          7,
    53  			expectedOutput: []uint64{},
    54  		},
    55  		{
    56  			name:           "limit matches bitmap cardinality",
    57  			input:          []uint64{2, 4, 6, 8, 10},
    58  			limit:          5,
    59  			expectedOutput: []uint64{2, 4, 6, 8, 10},
    60  		},
    61  		{
    62  			name:           "limit less than cardinality",
    63  			input:          []uint64{2, 4, 6, 8, 10},
    64  			limit:          3,
    65  			expectedOutput: []uint64{2, 4, 6},
    66  		},
    67  		{
    68  			name:           "limit higher than cardinality",
    69  			input:          []uint64{2, 4, 6, 8, 10},
    70  			limit:          10,
    71  			expectedOutput: []uint64{2, 4, 6, 8, 10},
    72  		},
    73  	}
    74  
    75  	for _, test := range tests {
    76  		t.Run(test.name, func(t *testing.T) {
    77  			dbm := docBitmap{
    78  				docIDs: sroar.NewBitmap(),
    79  			}
    80  
    81  			dbm.docIDs.SetMany(test.input)
    82  
    83  			res := dbm.IDsWithLimit(test.limit)
    84  			assert.Equal(t, test.expectedOutput, res)
    85  		})
    86  	}
    87  }
    88  
    89  func TestDocIDsIterator_Slice(t *testing.T) {
    90  	t.Run("iterator empty slice", func(t *testing.T) {
    91  		it := newSliceDocIDsIterator([]uint64{})
    92  
    93  		id1, ok1 := it.Next()
    94  
    95  		assert.Equal(t, 0, it.Len())
    96  		assert.False(t, ok1)
    97  		assert.Equal(t, uint64(0), id1)
    98  	})
    99  
   100  	t.Run("iterator step by step", func(t *testing.T) {
   101  		it := newSliceDocIDsIterator([]uint64{3, 1, 0, 2})
   102  
   103  		id1, ok1 := it.Next()
   104  		id2, ok2 := it.Next()
   105  		id3, ok3 := it.Next()
   106  		id4, ok4 := it.Next()
   107  		id5, ok5 := it.Next()
   108  
   109  		assert.Equal(t, 4, it.Len())
   110  		assert.True(t, ok1)
   111  		assert.Equal(t, uint64(3), id1)
   112  		assert.True(t, ok2)
   113  		assert.Equal(t, uint64(1), id2)
   114  		assert.True(t, ok3)
   115  		assert.Equal(t, uint64(0), id3)
   116  		assert.True(t, ok4)
   117  		assert.Equal(t, uint64(2), id4)
   118  		assert.False(t, ok5)
   119  		assert.Equal(t, uint64(0), id5)
   120  	})
   121  
   122  	t.Run("iterator in loop", func(t *testing.T) {
   123  		it := newSliceDocIDsIterator([]uint64{3, 1, 0, 2})
   124  		ids := []uint64{}
   125  
   126  		for id, ok := it.Next(); ok; id, ok = it.Next() {
   127  			ids = append(ids, id)
   128  		}
   129  
   130  		assert.Equal(t, 4, it.Len())
   131  		assert.Equal(t, []uint64{3, 1, 0, 2}, ids)
   132  	})
   133  }