@@ -4,6 +4,7 @@ package roaring
44
55import (
66 "github.com/stretchr/testify/assert"
7+ "math/rand"
78 "testing"
89)
910
@@ -92,16 +93,31 @@ func TestSetUtilIntersection(t *testing.T) {
9293 assert .Equal (t , expectedresult , result )
9394}
9495
95- func TestSetUtilIntersection2 (t * testing.T ) {
96- data1 := []uint16 {0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 }
97- data2 := []uint16 {0 , 3 , 6 , 9 , 12 , 15 , 18 }
98- result := make ([]uint16 , 0 , len (data1 )+ len (data2 ))
99- expectedresult := []uint16 {0 , 6 , 12 , 18 }
100- nl := intersection2by2 (data1 , data2 , result )
101- result = result [:nl ]
102- result = result [:len (expectedresult )]
96+ func TestSetUtilIntersectionCases (t * testing.T ) {
97+ cases := []struct {
98+ name string
99+ algo func (a , b , buf []uint16 ) int
100+ }{
101+ {
102+ name : "onesidedgallopingintersect2by2" ,
103+ algo : onesidedgallopingintersect2by2 ,
104+ },
105+ {
106+ name : "shotgun4Intersect" ,
107+ algo : shotgun4Intersect ,
108+ },
109+ }
103110
104- assert .Equal (t , expectedresult , result )
111+ data1 := []uint16 {0 , 3 , 6 , 9 , 12 , 15 , 18 }
112+ data2 := []uint16 {0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 }
113+ expected := []uint16 {0 , 6 , 12 , 18 }
114+
115+ for _ , c := range cases {
116+ result := make ([]uint16 , 0 , len (data1 )+ len (data2 ))
117+ n := c .algo (data1 , data2 , result )
118+
119+ assert .Equalf (t , expected , result [:n ], "failed algorithm: %s" , c .name )
120+ }
105121}
106122
107123func TestSetUtilBinarySearch (t * testing.T ) {
@@ -119,3 +135,48 @@ func TestSetUtilBinarySearch(t *testing.T) {
119135 }
120136 }
121137}
138+
139+ func BenchmarkIntersectAlgorithms (b * testing.B ) {
140+ sz1 := 1000
141+ s1 := make ([]uint16 , sz1 )
142+
143+ sz2 := MaxUint16
144+ s2 := make ([]uint16 , sz2 )
145+
146+ for i := 0 ; i < sz2 ; i ++ {
147+ s2 [i ] = uint16 (i )
148+ }
149+
150+ r := rand .New (rand .NewSource (0 ))
151+ k := 0
152+
153+ for i := 0 ; i < sz1 && k < sz2 ; i ++ {
154+ n := r .Intn (100 )
155+ k += n
156+
157+ // prevent adding duplicates
158+ if n == 0 && i > 0 {
159+ k ++
160+ }
161+
162+ s1 [i ] = uint16 (s2 [k ])
163+ }
164+
165+ buf := make ([]uint16 , sz1 + sz2 )
166+
167+ b .Run ("onesidedgallopingintersect2by2" , func (b * testing.B ) {
168+ b .ResetTimer ()
169+
170+ for i := 0 ; i < b .N ; i ++ {
171+ onesidedgallopingintersect2by2 (s1 , s2 , buf )
172+ }
173+ })
174+
175+ b .Run ("shotgun4" , func (b * testing.B ) {
176+ b .ResetTimer ()
177+
178+ for i := 0 ; i < b .N ; i ++ {
179+ shotgun4Intersect (s1 , s2 , buf )
180+ }
181+ })
182+ }
0 commit comments