File size: 3,160 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package inverted

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/weaviate/sroar"
)

func TestDocBitmap(t *testing.T) {
	t.Run("empty doc bitmap", func(t *testing.T) {
		dbm := newDocBitmap()

		assert.Equal(t, 0, dbm.count())
		assert.Empty(t, dbm.IDs())
	})

	t.Run("filled doc bitmap", func(t *testing.T) {
		ids := []uint64{1, 2, 3, 4, 5}

		dbm := newDocBitmap()
		dbm.docIDs.SetMany(ids)

		assert.Equal(t, 5, dbm.count())
		assert.ElementsMatch(t, ids, dbm.IDs())
	})
}

func TestDocBitmap_IDsWithLimit(t *testing.T) {
	type test struct {
		name           string
		limit          int
		input          []uint64
		expectedOutput []uint64
	}

	tests := []test{
		{
			name:           "empty bitmap, positive limit",
			input:          []uint64{},
			limit:          7,
			expectedOutput: []uint64{},
		},
		{
			name:           "limit matches bitmap cardinality",
			input:          []uint64{2, 4, 6, 8, 10},
			limit:          5,
			expectedOutput: []uint64{2, 4, 6, 8, 10},
		},
		{
			name:           "limit less than cardinality",
			input:          []uint64{2, 4, 6, 8, 10},
			limit:          3,
			expectedOutput: []uint64{2, 4, 6},
		},
		{
			name:           "limit higher than cardinality",
			input:          []uint64{2, 4, 6, 8, 10},
			limit:          10,
			expectedOutput: []uint64{2, 4, 6, 8, 10},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			dbm := docBitmap{
				docIDs: sroar.NewBitmap(),
			}

			dbm.docIDs.SetMany(test.input)

			res := dbm.IDsWithLimit(test.limit)
			assert.Equal(t, test.expectedOutput, res)
		})
	}
}

func TestDocIDsIterator_Slice(t *testing.T) {
	t.Run("iterator empty slice", func(t *testing.T) {
		it := newSliceDocIDsIterator([]uint64{})

		id1, ok1 := it.Next()

		assert.Equal(t, 0, it.Len())
		assert.False(t, ok1)
		assert.Equal(t, uint64(0), id1)
	})

	t.Run("iterator step by step", func(t *testing.T) {
		it := newSliceDocIDsIterator([]uint64{3, 1, 0, 2})

		id1, ok1 := it.Next()
		id2, ok2 := it.Next()
		id3, ok3 := it.Next()
		id4, ok4 := it.Next()
		id5, ok5 := it.Next()

		assert.Equal(t, 4, it.Len())
		assert.True(t, ok1)
		assert.Equal(t, uint64(3), id1)
		assert.True(t, ok2)
		assert.Equal(t, uint64(1), id2)
		assert.True(t, ok3)
		assert.Equal(t, uint64(0), id3)
		assert.True(t, ok4)
		assert.Equal(t, uint64(2), id4)
		assert.False(t, ok5)
		assert.Equal(t, uint64(0), id5)
	})

	t.Run("iterator in loop", func(t *testing.T) {
		it := newSliceDocIDsIterator([]uint64{3, 1, 0, 2})
		ids := []uint64{}

		for id, ok := it.Next(); ok; id, ok = it.Next() {
			ids = append(ids, id)
		}

		assert.Equal(t, 4, it.Len())
		assert.Equal(t, []uint64{3, 1, 0, 2}, ids)
	})
}