featurebase/roaring/containers_test.go
2019-05-31 17:16:54 +03:00

144 lines
3.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 (
"reflect"
"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 TestContainers(t *testing.T) {
cs := NewFileBitmap().Containers
first := NewContainerArray([]uint16{1, 2, 3})
last := NewContainerArray([]uint16{1, 2, 3, 4, 5, 6})
cs.Put(3, first)
cs.Put(6, last)
key, container := cs.First()
if key != 3 {
t.Fatalf("cs.First key 3 != %d", key)
}
if !reflect.DeepEqual(first, container) {
t.Fatalf("cs.First container %v != %v", first, container)
}
key, container = cs.Last()
if key != 6 {
t.Fatalf("cs.First key 6 != %d", key)
}
if !reflect.DeepEqual(last, container) {
t.Fatalf("cs.First container %v != %v", last, container)
}
cs = NewFileBitmap().Containers
key, container = cs.First()
if key != 0 {
t.Fatalf("cs.First key 0 != %d", key)
}
if nil != container {
t.Fatalf("cs.First container nil != %v", container)
}
key, container = cs.Last()
if key != 0 {
t.Fatalf("cs.First key 0 != %d", key)
}
if nil != container {
t.Fatalf("cs.First container nil != %v", container)
}
}