加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
runcontainer_test.go 58.36 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
package roaring
import (
"fmt"
"math/rand"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// trial is used in the randomized testing of runContainers
type trial struct {
n int
percentFill float64
ntrial int
// only in the union test
// only subtract test
percentDelete float64
// only in 067 randomized operations
// we do this + 1 passes
numRandomOpsPass int
// allow sampling range control
// only recent tests respect this.
srang *interval16
}
// canMerge, and mergeInterval16s should do what they say
func TestRleInterval16s(t *testing.T) {
a := newInterval16Range(0, 9)
b := newInterval16Range(0, 1)
report := sliceToString16([]interval16{a, b})
_ = report
c := newInterval16Range(2, 4)
d := newInterval16Range(2, 5)
e := newInterval16Range(0, 4)
f := newInterval16Range(9, 9)
g := newInterval16Range(8, 9)
h := newInterval16Range(5, 6)
i := newInterval16Range(6, 6)
aIb, empty := intersectInterval16s(a, b)
assert.False(t, empty)
assert.EqualValues(t, b, aIb)
assert.True(t, canMerge16(b, c))
assert.True(t, canMerge16(c, b))
assert.True(t, canMerge16(a, h))
assert.True(t, canMerge16(d, e))
assert.True(t, canMerge16(f, g))
assert.True(t, canMerge16(c, h))
assert.False(t, canMerge16(b, h))
assert.False(t, canMerge16(h, b))
assert.False(t, canMerge16(c, i))
assert.EqualValues(t, e, mergeInterval16s(b, c))
assert.EqualValues(t, e, mergeInterval16s(c, b))
assert.EqualValues(t, h, mergeInterval16s(h, i))
assert.EqualValues(t, h, mergeInterval16s(i, h))
////// start
assert.EqualValues(t, newInterval16Range(0, 1), mergeInterval16s(newInterval16Range(0, 0), newInterval16Range(1, 1)))
assert.EqualValues(t, newInterval16Range(0, 1), mergeInterval16s(newInterval16Range(1, 1), newInterval16Range(0, 0)))
assert.EqualValues(t, newInterval16Range(0, 5), mergeInterval16s(newInterval16Range(0, 4), newInterval16Range(3, 5)))
assert.EqualValues(t, newInterval16Range(0, 4), mergeInterval16s(newInterval16Range(0, 4), newInterval16Range(3, 4)))
assert.EqualValues(t, newInterval16Range(0, 8), mergeInterval16s(newInterval16Range(1, 7), newInterval16Range(0, 8)))
assert.EqualValues(t, newInterval16Range(0, 8), mergeInterval16s(newInterval16Range(1, 7), newInterval16Range(0, 8)))
assert.Panics(t, func() { _ = mergeInterval16s(newInterval16Range(0, 0), newInterval16Range(2, 3)) })
}
func TestRunOffset(t *testing.T) {
v := newRunContainer16TakeOwnership([]interval16{newInterval16Range(34, 39)})
offtest := uint16(65500)
w := v.addOffset(offtest)
w0card := w[0].getCardinality()
w1card := w[1].getCardinality()
if w0card+w1card != 6 {
t.Errorf("Bogus cardinality.")
}
expected := []int{65534, 65535, 65536, 65537, 65538, 65539}
wout := make([]int, len(expected))
for i := 0; i < w0card; i++ {
wout[i] = w[0].selectInt(uint16(i))
}
for i := 0; i < w1card; i++ {
wout[i+w0card] = w[1].selectInt(uint16(i)) + 65536
}
for i, x := range wout {
if x != expected[i] {
t.Errorf("found discrepancy %d!=%d", x, expected[i])
}
}
}
func TestRleRunIterator16(t *testing.T) {
t.Run("RunIterator16 unit tests for next, hasNext, and peekNext should pass", func(t *testing.T) {
{
rc := newRunContainer16()
msg := rc.String()
_ = msg
assert.EqualValues(t, 0, rc.cardinality())
it := rc.newRunIterator16()
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.peekNext() })
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 4)})
assert.EqualValues(t, 1, rc.cardinality())
it := rc.newRunIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(4), it.peekNext())
assert.EqualValues(t, uint16(4), it.next())
}
{
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
assert.EqualValues(t, 6, rc.cardinality())
it := rc.newRunIterator16()
assert.True(t, it.hasNext())
for i := 4; i < 10; i++ {
assert.Equal(t, uint16(i), it.next())
}
assert.False(t, it.hasNext())
}
{
// basic nextMany test
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
assert.EqualValues(t, 6, rc.cardinality())
it := rc.newManyRunIterator16()
buf := make([]uint32, 10)
n := it.nextMany(0, buf)
assert.Equal(t, 6, n)
expected := []uint32{4, 5, 6, 7, 8, 9, 0, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
}
{
// nextMany with len(buf) == 0
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
assert.EqualValues(t, 6, rc.cardinality())
it := rc.newManyRunIterator16()
var buf []uint32
n := it.nextMany(0, buf)
assert.Equal(t, 0, n)
}
{
// basic nextMany test across ranges
rc := newRunContainer16CopyIv([]interval16{
newInterval16Range(4, 7),
newInterval16Range(11, 13),
newInterval16Range(18, 21)})
assert.EqualValues(t, 11, rc.cardinality())
it := rc.newManyRunIterator16()
buf := make([]uint32, 15)
n := it.nextMany(0, buf)
assert.Equal(t, 11, n)
expected := []uint32{4, 5, 6, 7, 11, 12, 13, 18, 19, 20, 21, 0, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
}
{
// basic nextMany test across ranges with different buffer sizes
rc := newRunContainer16CopyIv([]interval16{
newInterval16Range(4, 7),
newInterval16Range(11, 13),
newInterval16Range(18, 21)})
expectedCard := 11
expectedVals := []uint32{4, 5, 6, 7, 11, 12, 13, 18, 19, 20, 21}
hs := uint32(1 << 16)
assert.EqualValues(t, expectedCard, rc.cardinality())
for bufSize := 2; bufSize < 15; bufSize++ {
buf := make([]uint32, bufSize)
seen := 0
it := rc.newManyRunIterator16()
for n := it.nextMany(hs, buf); n != 0; n = it.nextMany(hs, buf) {
// catch runaway iteration
assert.LessOrEqual(t, seen+n, expectedCard)
for i, e := range expectedVals[seen : seen+n] {
assert.Equal(t, e+hs, buf[i])
}
seen += n
// if we have more values to return then we shouldn't leave empty slots in the buffer
if seen < expectedCard {
assert.Equal(t, bufSize, n)
}
}
assert.Equal(t, expectedCard, seen)
}
}
{
// basic nextMany interaction with hasNext
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 4)})
assert.EqualValues(t, 1, rc.cardinality())
it := rc.newManyRunIterator16()
assert.True(t, it.hasNext())
buf := make([]uint32, 4)
n := it.nextMany(0, buf)
assert.Equal(t, 1, n)
expected := []uint32{4, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
assert.False(t, it.hasNext())
buf = make([]uint32, 4)
n = it.nextMany(0, buf)
assert.Equal(t, 0, n)
expected = []uint32{0, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
}
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
})
rc1 := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(6, 7),
newInterval16Range(10, 11),
newInterval16Range(MaxUint16, MaxUint16),
})
rc = rc.union(rc1)
assert.EqualValues(t, 8, rc.cardinality())
it := rc.newRunIterator16()
assert.EqualValues(t, 0, it.next())
assert.EqualValues(t, 2, it.next())
assert.EqualValues(t, 4, it.next())
assert.EqualValues(t, 6, it.next())
assert.EqualValues(t, 7, it.next())
assert.EqualValues(t, 10, it.next())
assert.EqualValues(t, 11, it.next())
assert.EqualValues(t, MaxUint16, it.next())
assert.False(t, it.hasNext())
newInterval16Range(0, MaxUint16)
rc2 := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, MaxUint16)})
rc2 = rc2.union(rc)
assert.Equal(t, 1, rc2.numIntervals())
}
})
}
func TestRleRunReverseIterator16(t *testing.T) {
t.Run("RunReverseIterator16 unit tests for next, hasNext, and peekNext should pass", func(t *testing.T) {
{
rc := newRunContainer16()
it := rc.newRunReverseIterator16()
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, 0)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(0), it.next())
assert.Panics(t, func() { it.next() })
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 4)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(4), it.next())
assert.False(t, it.hasNext())
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(MaxUint16, MaxUint16)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(MaxUint16), it.next())
assert.False(t, it.hasNext())
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 9)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
for i := 9; i >= 4; i-- {
assert.Equal(t, uint16(i), it.next())
if i > 4 {
assert.True(t, it.hasNext())
} else if i == 4 {
assert.False(t, it.hasNext())
}
}
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
newInterval16Range(6, 7),
newInterval16Range(10, 12),
newInterval16Range(MaxUint16, MaxUint16),
})
it := rc.newRunReverseIterator16()
assert.Equal(t, uint16(MaxUint16), it.next())
assert.Equal(t, uint16(12), it.next())
assert.Equal(t, uint16(11), it.next())
assert.Equal(t, uint16(10), it.next())
assert.Equal(t, uint16(7), it.next())
assert.Equal(t, uint16(6), it.next())
assert.Equal(t, uint16(4), it.next())
assert.Equal(t, uint16(2), it.next())
assert.Equal(t, uint16(0), it.next())
assert.Equal(t, false, it.hasNext())
assert.Panics(t, func() { it.next() })
}
})
}
func TestRleRunSearch16(t *testing.T) {
t.Run("RunContainer16.search should respect the prior bounds we provide for efficiency of searching through a subset of the intervals", func(t *testing.T) {
{
vals := []uint16{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, MaxUint16 - 3, MaxUint16}
exAt := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} // expected at
absent := []uint16{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, MaxUint16 - 2}
rc := newRunContainer16FromVals(true, vals...)
assert.EqualValues(t, 12, rc.cardinality())
var where int64
var present bool
for i, v := range vals {
where, present, _ = rc.search(int64(v), nil)
assert.True(t, present)
assert.EqualValues(t, exAt[i], where)
}
for i, v := range absent {
where, present, _ = rc.search(int64(v), nil)
assert.False(t, present)
assert.EqualValues(t, i, where)
}
// delete the MaxUint16 so we can test
// the behavior when searching near upper limit.
assert.EqualValues(t, 12, rc.cardinality())
assert.Equal(t, 12, rc.numIntervals())
rc.removeKey(MaxUint16)
assert.EqualValues(t, 11, rc.cardinality())
assert.Equal(t, 11, rc.numIntervals())
where, present, _ = rc.search(MaxUint16, nil)
assert.False(t, present)
assert.EqualValues(t, 10, where)
var numCompares int
where, present, numCompares = rc.search(MaxUint16, nil)
assert.False(t, present)
assert.EqualValues(t, 10, where)
assert.EqualValues(t, 3, numCompares)
opts := &searchOptions{
startIndex: 5,
}
where, present, numCompares = rc.search(MaxUint16, opts)
assert.False(t, present)
assert.EqualValues(t, 10, where)
assert.EqualValues(t, 2, numCompares)
where, present, _ = rc.search(MaxUint16-3, opts)
assert.True(t, present)
assert.EqualValues(t, 10, where)
// with the bound in place, MaxUint16-3 should not be found
opts.endxIndex = 10
where, present, _ = rc.search(MaxUint16-3, opts)
assert.False(t, present)
assert.EqualValues(t, 9, where)
}
})
}
func TestRleIntersection16(t *testing.T) {
t.Run("RunContainer16.intersect of two RunContainer16(s) should return their intersection", func(t *testing.T) {
{
vals := []uint16{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, MaxUint16 - 3, MaxUint16 - 1}
a := newRunContainer16FromVals(true, vals[:5]...)
b := newRunContainer16FromVals(true, vals[2:]...)
assert.True(t, haveOverlap16(newInterval16Range(0, 2), newInterval16Range(2, 2)))
assert.False(t, haveOverlap16(newInterval16Range(0, 2), newInterval16Range(3, 3)))
isect := a.intersect(b)
assert.EqualValues(t, 3, isect.cardinality())
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(6))
assert.True(t, isect.contains(8))
newInterval16Range(0, MaxUint16)
d := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, MaxUint16)})
isect = isect.intersect(d)
assert.EqualValues(t, 3, isect.cardinality())
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(6))
assert.True(t, isect.contains(8))
e := newRunContainer16TakeOwnership(
[]interval16{
newInterval16Range(2, 4),
newInterval16Range(8, 9),
newInterval16Range(14, 16),
newInterval16Range(20, 22)},
)
f := newRunContainer16TakeOwnership(
[]interval16{
newInterval16Range(3, 18),
newInterval16Range(22, 23)},
)
{
isect = e.intersect(f)
assert.EqualValues(t, 8, isect.cardinality())
assert.True(t, isect.contains(3))
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(8))
assert.True(t, isect.contains(9))
assert.True(t, isect.contains(14))
assert.True(t, isect.contains(15))
assert.True(t, isect.contains(16))
assert.True(t, isect.contains(22))
}
{
// check for symmetry
isect = f.intersect(e)
assert.EqualValues(t, 8, isect.cardinality())
assert.True(t, isect.contains(3))
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(8))
assert.True(t, isect.contains(9))
assert.True(t, isect.contains(14))
assert.True(t, isect.contains(15))
assert.True(t, isect.contains(16))
assert.True(t, isect.contains(22))
}
}
})
}
func TestRleRandomIntersection16(t *testing.T) {
t.Run("RunContainer.intersect of two RunContainers should return their intersection, and this should hold over randomized container content when compared to intersection done with hash maps", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .80, ntrial: 10},
{n: 1000, percentFill: .20, ntrial: 20},
{n: 10000, percentFill: .01, ntrial: 10},
{n: 1000, percentFill: .99, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
var first, second int
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
if i == 0 {
first = r0
second = r0 + 1
a = append(a, uint16(second))
ma[second] = true
}
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// print a; very likely it has dups
sort.Sort(uint16Slice(a))
stringA := ""
for i := range a {
stringA += fmt.Sprintf("%v, ", a[i])
}
// hash version of intersect:
hashi := make(map[int]bool)
for k := range ma {
if mb[k] {
hashi[k] = true
}
}
// RunContainer's Intersect
brle := newRunContainer16FromVals(false, b...)
//arle := newRunContainer16FromVals(false, a...)
// instead of the above line, create from array
// get better test coverage:
arr := newArrayContainerRange(int(first), int(second))
arle := newRunContainer16FromArray(arr)
arle.set(false, a...)
isect := arle.intersect(brle)
//showHash("hashi", hashi)
for k := range hashi {
assert.True(t, isect.contains(uint16(k)))
}
assert.EqualValues(t, len(hashi), isect.cardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleRandomUnion16(t *testing.T) {
t.Run("RunContainer.union of two RunContainers should return their union, and this should hold over randomized container content when compared to union done with hash maps", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .80, ntrial: 10},
{n: 1000, percentFill: .20, ntrial: 20},
{n: 10000, percentFill: .01, ntrial: 10},
{n: 1000, percentFill: .99, ntrial: 10, percentDelete: .04},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
numDel := int(float64(n) * tr.percentDelete)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// hash version of union:
hashu := make(map[int]bool)
for k := range ma {
hashu[k] = true
}
for k := range mb {
hashu[k] = true
}
//showHash("hashu", hashu)
// RunContainer's Union
arle := newRunContainer16()
for i := range a {
arle.Add(a[i])
}
brle := newRunContainer16()
brle.set(false, b...)
union := arle.union(brle)
un := union.AsSlice()
sort.Sort(uint16Slice(un))
for kk, v := range un {
_ = kk
assert.True(t, hashu[int(v)])
}
for k := range hashu {
assert.True(t, union.contains(uint16(k)))
}
assert.EqualValues(t, len(hashu), union.cardinality())
// do the deletes, exercising the remove functionality
for i := 0; i < numDel; i++ {
r1 := rand.Intn(len(a))
goner := a[r1]
union.removeKey(goner)
delete(hashu, int(goner))
}
// verify the same as in the hashu
assert.EqualValues(t, len(hashu), union.cardinality())
for k := range hashu {
assert.True(t, union.contains(uint16(k)))
}
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleAndOrXor16(t *testing.T) {
t.Run("RunContainer And, Or, Xor tests", func(t *testing.T) {
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
})
b0 := NewBitmap()
b0.Add(2)
b0.Add(6)
b0.Add(8)
and := rc.And(b0)
or := rc.Or(b0)
xor := rc.Xor(b0)
assert.EqualValues(t, 1, and.GetCardinality())
assert.EqualValues(t, 5, or.GetCardinality())
assert.EqualValues(t, 4, xor.GetCardinality())
// test creating size 0 and 1 from array
arr := newArrayContainerCapacity(0)
empty := newRunContainer16FromArray(arr)
onceler := newArrayContainerCapacity(1)
onceler.content = append(onceler.content, uint16(0))
oneZero := newRunContainer16FromArray(onceler)
assert.EqualValues(t, 0, empty.cardinality())
assert.EqualValues(t, 1, oneZero.cardinality())
assert.EqualValues(t, 0, empty.And(b0).GetCardinality())
assert.EqualValues(t, 3, empty.Or(b0).GetCardinality())
// exercise newRunContainer16FromVals() with 0 and 1 inputs.
empty2 := newRunContainer16FromVals(false, []uint16{}...)
assert.EqualValues(t, 0, empty2.cardinality())
one2 := newRunContainer16FromVals(false, []uint16{1}...)
assert.EqualValues(t, 1, one2.cardinality())
}
})
}
func TestRlePanics16(t *testing.T) {
t.Run("Some RunContainer calls/methods should panic if misused", func(t *testing.T) {
// newRunContainer16FromVals
assert.Panics(t, func() { newRunContainer16FromVals(true, 1, 0) })
arr := newArrayContainerRange(1, 3)
arr.content = []uint16{2, 3, 3, 2, 1}
assert.Panics(t, func() { newRunContainer16FromArray(arr) })
})
}
func TestRleCoverageOddsAndEnds16(t *testing.T) {
t.Run("Some RunContainer code paths that don't otherwise get coverage -- these should be tested to increase percentage of code coverage in testing", func(t *testing.T) {
rc := &runContainer16{}
assert.Equal(t, "runContainer16{}", rc.String())
rc.iv = make([]interval16, 1)
rc.iv[0] = newInterval16Range(3, 4)
assert.Equal(t, "runContainer16{0:[3, 4], }", rc.String())
a := newInterval16Range(5, 9)
b := newInterval16Range(0, 1)
c := newInterval16Range(1, 2)
// intersectInterval16s(a, b interval16)
isect, isEmpty := intersectInterval16s(a, b)
assert.True(t, isEmpty)
// [0,0] can't be trusted: assert.Equal(t, 0, isect.runlen())
isect, isEmpty = intersectInterval16s(b, c)
assert.False(t, isEmpty)
assert.EqualValues(t, 1, isect.runlen())
// runContainer16.union
{
ra := newRunContainer16FromVals(false, 4, 5)
rb := newRunContainer16FromVals(false, 4, 6, 8, 9, 10)
ra.union(rb)
assert.EqualValues(t, 2, rb.indexOfIntervalAtOrAfter(4, 2))
assert.EqualValues(t, 2, rb.indexOfIntervalAtOrAfter(3, 2))
}
// runContainer.intersect
{
ra := newRunContainer16()
rb := newRunContainer16()
assert.EqualValues(t, 0, ra.intersect(rb).cardinality())
}
{
ra := newRunContainer16FromVals(false, 1)
rb := newRunContainer16FromVals(false, 4)
assert.EqualValues(t, 0, ra.intersect(rb).cardinality())
}
// runContainer.Add
{
ra := newRunContainer16FromVals(false, 1)
rb := newRunContainer16FromVals(false, 4)
assert.EqualValues(t, 1, ra.cardinality())
assert.EqualValues(t, 1, rb.cardinality())
ra.Add(5)
assert.EqualValues(t, 2, ra.cardinality())
// newRunIterator16()
empty := newRunContainer16()
it := empty.newRunIterator16()
assert.Panics(t, func() { it.next() })
it2 := ra.newRunIterator16()
it2.curIndex = int64(len(it2.rc.iv))
assert.Panics(t, func() { it2.next() })
// runIterator16.peekNext()
emptyIt := empty.newRunIterator16()
assert.Panics(t, func() { emptyIt.peekNext() })
// newRunContainer16FromArray
arr := newArrayContainerRange(1, 6)
arr.content = []uint16{5, 5, 5, 6, 9}
rc3 := newRunContainer16FromArray(arr)
assert.EqualValues(t, 3, rc3.cardinality())
// runContainer16SerializedSizeInBytes
// runContainer16.SerializedSizeInBytes
_ = runContainer16SerializedSizeInBytes(3)
_ = rc3.serializedSizeInBytes()
// findNextIntervalThatIntersectsStartingFrom
idx, _ := rc3.findNextIntervalThatIntersectsStartingFrom(0, 100)
assert.EqualValues(t, 1, idx)
// deleteAt / remove
rc3.Add(10)
rc3.removeKey(10)
rc3.removeKey(9)
assert.EqualValues(t, 2, rc3.cardinality())
rc3.Add(9)
rc3.Add(10)
rc3.Add(12)
assert.EqualValues(t, 5, rc3.cardinality())
it3 := rc3.newRunIterator16()
it3.next()
it3.next()
it3.next()
it3.next()
assert.EqualValues(t, 12, it3.peekNext())
assert.EqualValues(t, 12, it3.next())
}
// runContainer16.equals
{
rc16 := newRunContainer16()
assert.True(t, rc16.equals16(rc16))
rc16b := newRunContainer16()
assert.True(t, rc16.equals16(rc16b))
rc16.Add(1)
rc16b.Add(2)
assert.False(t, rc16.equals16(rc16b))
}
})
}
func TestRleStoringMax16(t *testing.T) {
t.Run("Storing the MaxUint16 should be possible, because it may be necessary to do so--users will assume that any valid uint16 should be storable. In particular the smaller 16-bit version will definitely expect full access to all bits.", func(t *testing.T) {
rc := newRunContainer16()
rc.Add(MaxUint16)
assert.True(t, rc.contains(MaxUint16))
assert.EqualValues(t, 1, rc.cardinality())
rc.removeKey(MaxUint16)
assert.False(t, rc.contains(MaxUint16))
assert.EqualValues(t, 0, rc.cardinality())
rc.set(false, MaxUint16-1, MaxUint16)
assert.EqualValues(t, 2, rc.cardinality())
assert.True(t, rc.contains(MaxUint16-1))
assert.True(t, rc.contains(MaxUint16))
rc.removeKey(MaxUint16 - 1)
assert.EqualValues(t, 1, rc.cardinality())
rc.removeKey(MaxUint16)
assert.EqualValues(t, 0, rc.cardinality())
rc.set(false, MaxUint16-2, MaxUint16-1, MaxUint16)
assert.EqualValues(t, 3, rc.cardinality())
assert.EqualValues(t, 1, rc.numIntervals())
rc.removeKey(MaxUint16 - 1)
assert.EqualValues(t, 2, rc.numIntervals())
assert.EqualValues(t, 2, rc.cardinality())
})
}
// go test -bench BenchmarkFromBitmap -run -
func BenchmarkFromBitmap16(b *testing.B) {
b.StopTimer()
seed := int64(42)
rand.Seed(seed)
tr := trial{n: 10000, percentFill: .95, ntrial: 1, numRandomOpsPass: 100}
_, _, bc := getRandomSameThreeContainers(tr)
b.StartTimer()
for j := 0; j < b.N; j++ {
newRunContainer16FromBitmapContainer(bc)
}
}
func TestRle16RandomIntersectAgainstOtherContainers010(t *testing.T) {
t.Run("runContainer16 `and` operation against other container types should correctly do the intersection", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of intersect:
hashi := make(map[int]bool)
for k := range ma {
if mb[k] {
hashi[k] = true
}
}
// RunContainer's Intersect
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
rcVsBcIsect := rc.and(bc)
rcVsAcIsect := rc.and(ac)
rcVsRcbIsect := rc.and(rcb)
for k := range hashi {
assert.True(t, rcVsBcIsect.contains(uint16(k)))
assert.True(t, rcVsAcIsect.contains(uint16(k)))
assert.True(t, rcVsRcbIsect.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcIsect.getCardinality())
assert.Equal(t, len(hashi), rcVsAcIsect.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbIsect.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomUnionAgainstOtherContainers011(t *testing.T) {
t.Run("runContainer16 `or` operation against other container types should correctly do the intersection", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of union
hashi := make(map[int]bool)
for k := range ma {
hashi[k] = true
}
for k := range mb {
hashi[k] = true
}
// RunContainer's 'or'
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
rcVsBcUnion := rc.or(bc)
rcVsAcUnion := rc.or(ac)
rcVsRcbUnion := rc.or(rcb)
for k := range hashi {
assert.True(t, rcVsBcUnion.contains(uint16(k)))
assert.True(t, rcVsAcUnion.contains(uint16(k)))
assert.True(t, rcVsRcbUnion.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcUnion.getCardinality())
assert.Equal(t, len(hashi), rcVsAcUnion.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbUnion.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomInplaceUnionAgainstOtherContainers012(t *testing.T) {
t.Run("runContainer16 `ior` inplace union operation against other container types should correctly do the intersection", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 10, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of union
hashi := make(map[int]bool)
for k := range ma {
hashi[k] = true
}
for k := range mb {
hashi[k] = true
}
// RunContainer's 'or'
rc := newRunContainer16FromVals(false, a...)
rcVsBcUnion := rc.Clone()
rcVsAcUnion := rc.Clone()
rcVsRcbUnion := rc.Clone()
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
rcVsBcUnion.ior(bc)
rcVsAcUnion.ior(ac)
rcVsRcbUnion.ior(rcb)
for k := range hashi {
assert.True(t, rcVsBcUnion.contains(uint16(k)))
assert.True(t, rcVsAcUnion.contains(uint16(k)))
assert.True(t, rcVsRcbUnion.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcUnion.getCardinality())
assert.Equal(t, len(hashi), rcVsAcUnion.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbUnion.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomInplaceIntersectAgainstOtherContainers014(t *testing.T) {
t.Run("runContainer16 `iand` inplace-and operation against other container types should correctly do the intersection", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of intersect:
hashi := make(map[int]bool)
for k := range ma {
if mb[k] {
hashi[k] = true
}
}
// RunContainer's Intersect
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
var rcVsBcIsect container = rc.Clone()
var rcVsAcIsect container = rc.Clone()
var rcVsRcbIsect container = rc.Clone()
rcVsBcIsect = rcVsBcIsect.iand(bc)
rcVsAcIsect = rcVsAcIsect.iand(ac)
rcVsRcbIsect = rcVsRcbIsect.iand(rcb)
for k := range hashi {
assert.True(t, rcVsBcIsect.contains(uint16(k)))
assert.True(t, rcVsAcIsect.contains(uint16(k)))
assert.True(t, rcVsRcbIsect.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcIsect.getCardinality())
assert.Equal(t, len(hashi), rcVsAcIsect.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbIsect.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RemoveApi015(t *testing.T) {
t.Run("runContainer16 `remove` (a minus b) should work", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of remove:
hashrm := make(map[int]bool)
for k := range ma {
hashrm[k] = true
}
for k := range mb {
delete(hashrm, k)
}
// RunContainer's remove
rc := newRunContainer16FromVals(false, a...)
for k := range mb {
rc.iremove(uint16(k))
}
for k := range hashrm {
assert.True(t, rc.contains(uint16(k)))
}
assert.Equal(t, len(hashrm), rc.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func showArray16(a []uint16, name string) {
sort.Sort(uint16Slice(a))
stringA := ""
for i := range a {
stringA += fmt.Sprintf("%v, ", a[i])
}
}
func TestRle16RandomAndNot016(t *testing.T) {
t.Run("runContainer16 `andNot` operation against other container types should correctly do the and-not operation", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 1000, percentFill: .95, ntrial: 2},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of and-not
hashi := make(map[int]bool)
for k := range ma {
hashi[k] = true
}
for k := range mb {
delete(hashi, k)
}
// RunContainer's and-not
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
rcVsBcAndnot := rc.andNot(bc)
rcVsAcAndnot := rc.andNot(ac)
rcVsRcbAndnot := rc.andNot(rcb)
for k := range hashi {
assert.True(t, rcVsBcAndnot.contains(uint16(k)))
assert.True(t, rcVsAcAndnot.contains(uint16(k)))
assert.True(t, rcVsRcbAndnot.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcAndnot.getCardinality())
assert.Equal(t, len(hashi), rcVsAcAndnot.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbAndnot.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomInplaceAndNot017(t *testing.T) {
t.Run("runContainer16 `iandNot` operation against other container types should correctly do the inplace-and-not operation", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 1000, percentFill: .95, ntrial: 2},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
//showArray16(a, "a")
//showArray16(b, "b")
// hash version of and-not
hashi := make(map[int]bool)
for k := range ma {
hashi[k] = true
}
for k := range mb {
delete(hashi, k)
}
// RunContainer's and-not
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
rcVsBcIandnot := rc.Clone()
rcVsAcIandnot := rc.Clone()
rcVsRcbIandnot := rc.Clone()
rcVsBcIandnot.iandNot(bc)
rcVsAcIandnot.iandNot(ac)
rcVsRcbIandnot.iandNot(rcb)
for k := range hashi {
assert.True(t, rcVsBcIandnot.contains(uint16(k)))
assert.True(t, rcVsAcIandnot.contains(uint16(k)))
assert.True(t, rcVsRcbIandnot.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcIandnot.getCardinality())
assert.Equal(t, len(hashi), rcVsAcIandnot.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbIandnot.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16InversionOfIntervals018(t *testing.T) {
t.Run("runContainer `invert` operation should do a NOT on the set of intervals, in-place", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 1000, percentFill: .90, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
hashNotA := make(map[int]bool)
n := tr.n
a := []uint16{}
// hashNotA will be NOT ma
//for i := 0; i < n; i++ {
for i := 0; i < MaxUint16+1; i++ {
hashNotA[i] = true
}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
delete(hashNotA, r0)
}
// RunContainer's invert
rc := newRunContainer16FromVals(false, a...)
inv := rc.invert()
assert.Equal(t, 1+MaxUint16-rc.cardinality(), inv.cardinality())
for k := 0; k < n; k++ {
if hashNotA[k] {
assert.True(t, inv.contains(uint16(k)))
}
}
// skip for now, too big to do 2^16-1
assert.Equal(t, len(hashNotA), inv.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16SubtractionOfIntervals019(t *testing.T) {
t.Run("runContainer `subtract` operation removes an interval in-place", func(t *testing.T) {
// basics
i22 := newInterval16Range(2, 2)
left, _ := i22.subtractInterval(i22)
assert.EqualValues(t, 0, len(left))
v := newInterval16Range(1, 6)
left, _ = v.subtractInterval(newInterval16Range(3, 4))
assert.EqualValues(t, 2, len(left))
assert.EqualValues(t, 1, left[0].start)
assert.EqualValues(t, 2, left[0].last())
assert.EqualValues(t, 5, left[1].start)
assert.EqualValues(t, 6, left[1].last())
v = newInterval16Range(1, 6)
left, _ = v.subtractInterval(newInterval16Range(4, 10))
assert.EqualValues(t, 1, len(left))
assert.EqualValues(t, 1, left[0].start)
assert.EqualValues(t, 3, left[0].last())
v = newInterval16Range(5, 10)
left, _ = v.subtractInterval(newInterval16Range(0, 7))
assert.EqualValues(t, 1, len(left))
assert.EqualValues(t, 8, left[0].start)
assert.EqualValues(t, 10, left[0].last())
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 1000, percentFill: .90, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
// hashAminusB will be ma - mb
hashAminusB := make(map[int]bool)
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
hashAminusB[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
for k := range mb {
delete(hashAminusB, k)
}
// RunContainer's subtract A - B
rc := newRunContainer16FromVals(false, a...)
rcb := newRunContainer16FromVals(false, b...)
abkup := rc.Clone()
it := rcb.newRunIterator16()
for it.hasNext() {
nx := it.next()
rc.isubtract(newInterval16Range(nx, nx))
}
// also check full interval subtraction
for _, p := range rcb.iv {
abkup.isubtract(p)
}
for k := range hashAminusB {
assert.True(t, rc.contains(uint16(k)))
assert.True(t, abkup.contains(uint16(k)))
}
assert.EqualValues(t, len(hashAminusB), rc.getCardinality())
assert.EqualValues(t, len(hashAminusB), abkup.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16Rank020(t *testing.T) {
v := container(newRunContainer16())
v = v.iaddReturnMinimized(10)
v = v.iaddReturnMinimized(100)
v = v.iaddReturnMinimized(1000)
if v.getCardinality() != 3 {
t.Errorf("Bogus cardinality.")
}
for i := 0; i <= arrayDefaultMaxSize; i++ {
thisrank := v.rank(uint16(i))
if i < 10 {
if thisrank != 0 {
t.Errorf("At %d should be zero but is %d ", i, thisrank)
}
} else if i < 100 {
if thisrank != 1 {
t.Errorf("At %d should be zero but is %d ", i, thisrank)
}
} else if i < 1000 {
if thisrank != 2 {
t.Errorf("At %d should be zero but is %d ", i, thisrank)
}
} else {
if thisrank != 3 {
t.Errorf("At %d should be zero but is %d ", i, thisrank)
}
}
}
}
func TestRle16NotAlsoKnownAsFlipRange021(t *testing.T) {
t.Run("runContainer `Not` operation should flip the bits of a range on the new returned container", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .8, ntrial: 2},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
// what is the interval we are going to flip?
ma := make(map[int]bool)
flipped := make(map[int]bool)
n := tr.n
a := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
flipped[r0] = true
}
// pick an interval to flip
begin := rand.Intn(n)
last := rand.Intn(n)
if last < begin {
begin, last = last, begin
}
// do the flip on the hash `flipped`
for i := begin; i <= last; i++ {
if flipped[i] {
delete(flipped, i)
} else {
flipped[i] = true
}
}
// RunContainer's Not
rc := newRunContainer16FromVals(false, a...)
flp := rc.Not(begin, last+1)
assert.EqualValues(t, len(flipped), flp.cardinality())
for k := 0; k < n; k++ {
if flipped[k] {
assert.True(t, flp.contains(uint16(k)))
} else {
assert.False(t, flp.contains(uint16(k)))
}
}
assert.EqualValues(t, len(flipped), flp.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleEquals022(t *testing.T) {
t.Run("runContainer `equals` should accurately compare contents against other container types", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .2, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
n := tr.n
a := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
}
rc := newRunContainer16FromVals(false, a...)
// make bitmap and array versions:
bc := newBitmapContainer()
ac := newArrayContainer()
for k := range ma {
ac.iadd(uint16(k))
bc.iadd(uint16(k))
}
// compare equals() across all three
assert.True(t, rc.equals(ac))
assert.True(t, rc.equals(bc))
assert.True(t, ac.equals(rc))
assert.True(t, ac.equals(bc))
assert.True(t, bc.equals(ac))
assert.True(t, bc.equals(rc))
// and for good measure, check against the hash
assert.EqualValues(t, len(ma), rc.getCardinality())
assert.EqualValues(t, len(ma), ac.getCardinality())
assert.EqualValues(t, len(ma), bc.getCardinality())
for k := range ma {
assert.True(t, rc.contains(uint16(k)))
assert.True(t, ac.contains(uint16(k)))
assert.True(t, bc.contains(uint16(k)))
}
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleIntersects023(t *testing.T) {
t.Run("runContainer `intersects` query should work against any mix of container types", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 10, percentFill: .293, ntrial: 1000},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// determine if they intersect from the maps
isect := false
for k := range ma {
if mb[k] {
isect = true
break
}
}
rcA := newRunContainer16FromVals(false, a...)
rcB := newRunContainer16FromVals(false, b...)
// make bitmap and array versions:
bcA := newBitmapContainer()
bcB := newBitmapContainer()
acA := newArrayContainer()
acB := newArrayContainer()
for k := range ma {
acA.iadd(uint16(k))
bcA.iadd(uint16(k))
}
for k := range mb {
acB.iadd(uint16(k))
bcB.iadd(uint16(k))
}
// compare intersects() across all three
// same type
assert.Equal(t, isect, rcA.intersects(rcB))
assert.Equal(t, isect, acA.intersects(acB))
assert.Equal(t, isect, bcA.intersects(bcB))
// across types
assert.Equal(t, isect, rcA.intersects(acB))
assert.Equal(t, isect, rcA.intersects(bcB))
assert.Equal(t, isect, acA.intersects(rcB))
assert.Equal(t, isect, acA.intersects(bcB))
assert.Equal(t, isect, bcA.intersects(acB))
assert.Equal(t, isect, bcA.intersects(rcB))
// and swap the call pattern, so we test B intersects A as well.
// same type
assert.Equal(t, isect, rcB.intersects(rcA))
assert.Equal(t, isect, acB.intersects(acA))
assert.Equal(t, isect, bcB.intersects(bcA))
// across types
assert.Equal(t, isect, rcB.intersects(acA))
assert.Equal(t, isect, rcB.intersects(bcA))
assert.Equal(t, isect, acB.intersects(rcA))
assert.Equal(t, isect, acB.intersects(bcA))
assert.Equal(t, isect, bcB.intersects(acA))
assert.Equal(t, isect, bcB.intersects(rcA))
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleToEfficientContainer027(t *testing.T) {
t.Run("runContainer toEfficientContainer should return equivalent containers", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
// 4096 or fewer integers -> array typically
trials := []trial{
{n: 8000, percentFill: .01, ntrial: 10},
{n: 8000, percentFill: .99, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
n := tr.n
a := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
}
rc := newRunContainer16FromVals(false, a...)
c := rc.toEfficientContainer()
assert.True(t, rc.equals(c))
}
}
for i := range trials {
tester(trials[i])
}
})
t.Run("runContainer toEfficientContainer should return an equivalent bitmap when that is efficient", func(t *testing.T) {
a := []uint16{}
// odd intergers should be smallest as a bitmap
for i := 0; i < MaxUint16; i++ {
if i%2 == 1 {
a = append(a, uint16(i))
}
}
rc := newRunContainer16FromVals(false, a...)
c := rc.toEfficientContainer()
assert.True(t, rc.equals(c))
_, isBitmapContainer := c.(*bitmapContainer)
assert.True(t, isBitmapContainer)
})
}
func TestRle16RandomFillLeastSignificant16bits029(t *testing.T) {
t.Run("runContainer16.fillLeastSignificant16bits() should fill contents as expected, matching the same function on bitmap and array containers", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
n := tr.n
a := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
}
//showArray16(a, "a")
// RunContainer
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, av := range a {
bc.iadd(av)
}
// vs arrayContainer
ac := newArrayContainer()
for _, av := range a {
ac.iadd(av)
}
acOut := make([]uint32, n+10)
bcOut := make([]uint32, n+10)
rcOut := make([]uint32, n+10)
pos2 := 0
// see Bitmap.ToArray() for principal use
hs := uint32(43) << 16
ac.fillLeastSignificant16bits(acOut, pos2, hs)
bc.fillLeastSignificant16bits(bcOut, pos2, hs)
rc.fillLeastSignificant16bits(rcOut, pos2, hs)
assert.EqualValues(t, acOut, rcOut)
assert.EqualValues(t, bcOut, rcOut)
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomGetShortIterator030(t *testing.T) {
t.Run("runContainer16.getShortIterator should traverse the contents expected, matching the traversal of the bitmap and array containers", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
n := tr.n
a := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
}
//showArray16(a, "a")
// RunContainer
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, av := range a {
bc.iadd(av)
}
// vs arrayContainer
ac := newArrayContainer()
for _, av := range a {
ac.iadd(av)
}
rit := rc.getShortIterator()
ait := ac.getShortIterator()
bit := bc.getShortIterator()
for ait.hasNext() {
rn := rit.next()
an := ait.next()
bn := bit.next()
assert.Equal(t, an, rn)
assert.Equal(t, bn, rn)
}
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomIaddRangeIremoveRange031(t *testing.T) {
t.Run("runContainer16.iaddRange and iremoveRange should add/remove contents as expected, matching the same operations on the bitmap and array containers and the hashmap pos control", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 101, percentFill: .9, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
n := tr.n
a := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
}
//showArray16(a, "a")
// RunContainer
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, av := range a {
bc.iadd(av)
}
// vs arrayContainer
ac := newArrayContainer()
for _, av := range a {
ac.iadd(av)
}
// iaddRange and iRemoveRange : pick some distinct random endpoints
a0 := rand.Intn(n)
a1 := a0
for a1 == a0 {
a1 = rand.Intn(n)
}
if a0 > a1 {
a0, a1 = a1, a0
}
r0 := rand.Intn(n)
r1 := r0
for r1 == r0 {
r1 = rand.Intn(n)
}
if r0 > r1 {
r0, r1 = r1, r0
}
// do the add
for i := a0; i <= a1; i++ {
ma[i] = true
}
// then the remove
for i := r0; i <= r1; i++ {
delete(ma, i)
}
rc.iaddRange(a0, a1+1)
rc.iremoveRange(r0, r1+1)
bc.iaddRange(a0, a1+1)
bc.iremoveRange(r0, r1+1)
ac.iaddRange(a0, a1+1)
ac.iremoveRange(r0, r1+1)
assert.EqualValues(t, len(ma), rc.getCardinality())
assert.Equal(t, ac.getCardinality(), rc.getCardinality())
assert.Equal(t, bc.getCardinality(), rc.getCardinality())
rit := rc.getShortIterator()
ait := ac.getShortIterator()
bit := bc.getShortIterator()
for ait.hasNext() {
rn := rit.next()
an := ait.next()
bn := bit.next()
assert.Equal(t, an, rn)
assert.Equal(t, bn, rn)
}
// verify againt the map
for k := range ma {
assert.True(t, rc.contains(uint16(k)))
}
// coverage for run16 method
assert.Equal(t, 2+4*rc.numberOfRuns(), rc.serializedSizeInBytes())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestAllContainerMethodsAllContainerTypes065(t *testing.T) {
t.Run("each of the container methods that takes two containers should handle all 3x3==9 possible ways of being called -- without panic", func(t *testing.T) {
a := newArrayContainer()
r := newRunContainer16()
b := newBitmapContainer()
arr := []container{a, r, b}
for _, i := range arr {
for _, j := range arr {
i.and(j)
i.iand(j)
i.andNot(j)
i.iandNot(j)
i.xor(j)
i.equals(j)
i.or(j)
i.ior(j)
i.intersects(j)
i.lazyOR(j)
i.lazyIOR(j)
}
}
})
}
type twoCall func(r container) container
type twofer struct {
name string
call twoCall
cn container
}
func TestAllContainerMethodsAllContainerTypesWithData067(t *testing.T) {
t.Run("each of the container methods that takes two containers should handle all 3x3==9 possible ways of being called -- and return results that agree with each other", func(t *testing.T) {
//rleVerbose = true
seed := int64(42)
rand.Seed(seed)
srang := newInterval16Range(MaxUint16-100, MaxUint16)
trials := []trial{
{n: 100, percentFill: .7, ntrial: 1, numRandomOpsPass: 100},
{n: 100, percentFill: .7, ntrial: 1, numRandomOpsPass: 100, srang: &srang}}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
a, r, b := getRandomSameThreeContainers(tr)
a2, r2, b2 := getRandomSameThreeContainers(tr)
receiver := []container{a, r, b}
arg := []container{a2, r2, b2}
callme := []twofer{}
nCalls := 0
for k, c := range receiver {
callme = append(callme, twofer{"and", c.and, c})
callme = append(callme, twofer{"iand", c.iand, c})
callme = append(callme, twofer{"ior", c.ior, c})
callme = append(callme, twofer{"lazyOR", c.lazyOR, c})
callme = append(callme, twofer{"lazyIOR", c.lazyIOR, c})
callme = append(callme, twofer{"or", c.or, c})
callme = append(callme, twofer{"xor", c.xor, c})
callme = append(callme, twofer{"andNot", c.andNot, c})
callme = append(callme, twofer{"iandNot", c.iandNot, c})
if k == 0 {
nCalls = len(callme)
}
}
for pass := 0; pass < tr.numRandomOpsPass+1; pass++ {
for k := 0; k < nCalls; k++ {
perm := getRandomPermutation(nCalls)
kk := perm[k]
c1 := callme[kk] // array receiver
c2 := callme[kk+nCalls] // run receiver
c3 := callme[kk+2*nCalls] // bitmap receiver
if c1.name != c2.name {
panic("internal logic error")
}
if c3.name != c2.name {
panic("internal logic error")
}
for k2, a := range arg {
if !c1.cn.equals(c2.cn) {
panic("c1 not equal to c2")
}
if !c1.cn.equals(c3.cn) {
panic("c1 not equal to c3")
}
res1 := c1.call(a) // array
res2 := c2.call(a) // run
res3 := c3.call(a) // bitmap
z := c1.name
// In-place operation are best effort
// User should not assume the receiver is modified, returned container has to be used
if strings.HasPrefix(z, "i") {
c1.cn = res1
c2.cn = res2
c3.cn = res3
}
if strings.HasPrefix(z, "lazy") {
// on purpose, the lazy functions
// do not scan to update their cardinality
if asBc, isBc := res1.(*bitmapContainer); isBc {
asBc.computeCardinality()
}
if asBc, isBc := res2.(*bitmapContainer); isBc {
asBc.computeCardinality()
}
if asBc, isBc := res3.(*bitmapContainer); isBc {
asBc.computeCardinality()
}
}
// check for equality all ways...
// excercising equals() calls too.
if !res1.equals(res2) {
panic(fmt.Sprintf("k:%v, k2:%v, res1 != res2,"+
" call is '%s'", k, k2, c1.name))
}
if !res2.equals(res1) {
panic(fmt.Sprintf("k:%v, k2:%v, res2 != res1,"+
" call is '%s'", k, k2, c1.name))
}
if !res1.equals(res3) {
panic(fmt.Sprintf("k:%v, k2:%v, res1 != res3,"+
" call is '%s'", k, k2, c1.name))
}
if !res3.equals(res1) {
panic(fmt.Sprintf("k:%v, k2:%v, res3 != res1,"+
" call is '%s'", k, k2, c1.name))
}
if !res2.equals(res3) {
panic(fmt.Sprintf("k:%v, k2:%v, res2 != res3,"+
" call is '%s'", k, k2, c1.name))
}
if !res3.equals(res2) {
panic(fmt.Sprintf("k:%v, k2:%v, res3 != res2,"+
" call is '%s'", k, k2, c1.name))
}
}
} // end k
} // end pass
} // end j
} // end tester
for i := range trials {
tester(trials[i])
}
})
}
func TestRuntimeIteratorPeekNext(t *testing.T) {
testContainerIteratorPeekNext(t, newRunContainer16())
}
func TestRuntimeIteratorAdvance(t *testing.T) {
testContainerIteratorAdvance(t, newRunContainer16())
}
// go test -bench BenchmarkShortIteratorAdvance -run -
func BenchmarkShortIteratorAdvanceRuntime(b *testing.B) {
benchmarkContainerIteratorAdvance(b, newRunContainer16())
}
// go test -bench BenchmarkShortIteratorNext -run -
func BenchmarkShortIteratorNextRuntime(b *testing.B) {
benchmarkContainerIteratorNext(b, newRunContainer16())
}
// generate random contents, then return that same
// logical content in three different container types
func getRandomSameThreeContainers(tr trial) (*arrayContainer, *runContainer16, *bitmapContainer) {
ma := make(map[int]bool)
n := tr.n
a := []uint16{}
var samp interval16
if tr.srang != nil {
samp = *tr.srang
} else {
if n-1 > MaxUint16 {
panic(fmt.Errorf("n out of range: %v", n))
}
samp.start = 0
samp.length = uint16(n - 2)
}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := int(samp.start) + rand.Intn(int(samp.runlen()))
a = append(a, uint16(r0))
ma[r0] = true
}
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainerFromRun(rc)
ac := rc.toArrayContainer()
return ac, rc, bc
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化