featurebase/roaring/containers_test.go
2020-05-06 23:59:07 +02:00

187 lines
4.6 KiB
Go

// Copyright 2017 Pilosa Corp.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package roaring
import (
"testing"
)
func TestContainersIterator(t *testing.T) {
slc := NewFileBitmap().Containers
testContainersIterator(slc, t)
}
func testContainersIterator(cs Containers, t *testing.T) {
itr, found := cs.Iterator(0)
if found {
t.Fatalf("shouldn't have found 0 in empty btc")
}
if itr.Next() {
t.Fatal("Next() should be false for empty btc")
}
cs.Put(1, NewContainerArray([]uint16{1}))
cs.Put(2, NewContainerArray([]uint16{1, 2}))
itr, found = cs.Iterator(0)
if found {
t.Fatalf("shouldn't have found 0")
}
if !itr.Next() {
t.Fatalf("one should be next, but got false")
}
if key, val := itr.Value(); key != 1 || val.N() != 1 {
t.Fatalf("Wrong k/v, exp: 1,1 got: %v,%v", key, val.N())
}
if !itr.Next() {
t.Fatalf("two should be next, but got false")
}
if key, val := itr.Value(); key != 2 || val.N() != 2 {
t.Fatalf("Wrong k/v, exp: 2,2 got: %v,%v", key, val.N())
}
if itr.Next() {
t.Fatalf("itr should be done, but got true")
}
cs.Put(3, NewContainerArray([]uint16{1, 2, 3}))
cs.Put(5, NewContainerArray([]uint16{1, 2, 3, 4, 5}))
cs.Put(6, NewContainerArray([]uint16{1, 2, 3, 4, 5, 6}))
itr, found = cs.Iterator(3)
if !itr.Next() {
t.Fatalf("3 should be next, but got false")
}
if !found {
t.Fatalf("should have found 3")
}
if key, val := itr.Value(); key != 3 || val.N() != 3 {
t.Fatalf("Wrong k/v, exp: 3,3 got: %v,%v", key, val.N())
}
if !itr.Next() {
t.Fatalf("5 should be next, but got false")
}
if key, val := itr.Value(); key != 5 || val.N() != 5 {
t.Fatalf("Wrong k/v, exp: 5,5 got: %v,%v", key, val.N())
}
itr, found = cs.Iterator(4)
if found {
t.Fatalf("shouldn't have found 4")
}
if !itr.Next() {
t.Fatalf("5 should be next, but got false")
}
if key, val := itr.Value(); key != 5 || val.N() != 5 {
t.Fatalf("Wrong k/v, exp: 5,5 got: %v,%v", key, val.N())
}
if !itr.Next() {
t.Fatalf("6 should be next, but got false")
}
if key, val := itr.Value(); key != 6 || val.N() != 6 {
t.Fatalf("Wrong k/v, exp: 6,6 got: %v,%v", key, val.N())
}
if itr.Next() {
t.Fatalf("itr should be done, but got true")
}
}
func TestSliceContainers(t *testing.T) {
const size = 10
n := size
sc := newSliceContainers()
// Add n keys
for i := 0; i < n; i++ {
key, set := uint64(i), []uint16{uint16(i)}
sc.Put(key, NewContainerArray(set))
}
t.Run("Get n keys", func(t *testing.T) {
for i := 0; i < n; i++ {
key, set := uint64(i), []uint16{uint16(i)}
c := sc.Get(key)
if c == nil {
t.Fatalf("Get(%d) returned nil container", key)
}
if c.data[0] != set[0] {
t.Fatalf("Get(%d): expected: %v, got: %v", key, set[0], c.data[0])
}
}
})
t.Run("Last key/container", func(t *testing.T) {
key, c := sc.Last()
if key != uint64(n-1) || c.data[0] != uint16(n-1) {
t.Fatalf("Last: expected: %v, got: %d, %v", n-1, key, c.data)
}
})
// Remove odd keys
for i := 1; i < size; i += 2 {
key := uint64(i)
sc.Remove(key)
n--
}
t.Run("Try to Get removed containers", func(t *testing.T) {
for i := 1; i < size; i += 2 {
key := uint64(i)
c := sc.Get(key)
if c != nil {
t.Fatalf("Get(for non existing key %d): found container: %v", key, c.data)
}
}
// Test - Last key/container
key, c := sc.Last()
if key != uint64(size-2) || c.data[0] != uint16(size-2) {
t.Fatalf("Last: expected: %v, got: %d, %v", size-2, key, c.data)
}
if sc.Size() != n {
t.Fatalf("Size: expected: %d, got: %d", n, sc.Size())
}
})
t.Run("Nil containers and repair them", func(t *testing.T) {
// Remove half of even containers
for i := range sc.containers {
if i%2 == 0 {
sc.containers[i] = nil
n--
}
}
sc.Repair()
if sc.Size() != n {
t.Fatalf("Size: expected: %d, got: %d", n, sc.Size())
}
for i, key := range sc.keys {
if sc.containers[i] == nil {
t.Fatalf("Found nil container for key: %d at index: %d", key, i)
} else {
if sc.containers[i].data[0] != uint16(key) {
t.Fatalf("Invalid container data for key: %d at index: %d - expected: %d, got: %d",
key, i, uint16(key), sc.containers[i].data[0],
)
}
}
}
})
}