@@ -27,18 +27,54 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727package dtrie
2828
2929import (
30+ "fmt"
3031 "testing"
3132
3233 "github.com/stretchr/testify/assert"
3334)
3435
36+ func TestPopCount (t * testing.T ) {
37+ b := []uint32 {
38+ uint32 (0x55555555 ), // 0x55555555 = 01010101 01010101 01010101 01010101
39+ uint32 (0x33333333 ), // 0x33333333 = 00110011 00110011 00110011 00110011
40+ uint32 (0x0F0F0F0F ), // 0x0F0F0F0F = 00001111 00001111 00001111 00001111
41+ uint32 (0x00FF00FF ), // 0x00FF00FF = 00000000 11111111 00000000 11111111
42+ uint32 (0x0000FFFF ), // 0x0000FFFF = 00000000 00000000 11111111 11111111
43+ }
44+ for _ , x := range b {
45+ assert .Equal (t , 16 , popCount (x ))
46+ }
47+ }
48+
3549func TestDefaultHasher (t * testing.T ) {
3650 assert .Equal (t ,
3751 defaultHasher (map [int ]string {11234 : "foo" }),
3852 defaultHasher (map [int ]string {11234 : "foo" }))
3953 assert .NotEqual (t , defaultHasher ("foo" ), defaultHasher ("bar" ))
4054}
4155
56+ type testEntry struct {
57+ hash uint32
58+ key int
59+ value int
60+ }
61+
62+ func (e * testEntry ) KeyHash () uint32 {
63+ return e .hash
64+ }
65+
66+ func (e * testEntry ) Key () interface {} {
67+ return e .key
68+ }
69+
70+ func (e * testEntry ) Value () interface {} {
71+ return e .value
72+ }
73+
74+ func (e * testEntry ) String () string {
75+ return fmt .Sprint (e .value )
76+ }
77+
4278func collisionHash (key interface {}) uint32 {
4379 return uint32 (0xffffffff ) // for testing collisions
4480}
@@ -51,7 +87,7 @@ func TestInsert(t *testing.T) {
5187func insertTest (t * testing.T , hashfunc func (interface {}) uint32 , count int ) * node {
5288 n := emptyNode (0 , 32 )
5389 for i := 0 ; i < count ; i ++ {
54- n = insert (n , & entry {hashfunc (i ), i , i })
90+ n = insert (n , & testEntry {hashfunc (i ), i , i })
5591 }
5692 return n
5793}
@@ -94,7 +130,7 @@ func TestUpdate(t *testing.T) {
94130func updateTest (t * testing.T , hashfunc func (interface {}) uint32 , count int ) {
95131 n := insertTest (t , hashfunc , count )
96132 for i := 0 ; i < count ; i ++ {
97- n = insert (n , & entry {hashfunc (i ), i , - i })
133+ n = insert (n , & testEntry {hashfunc (i ), i , - i })
98134 }
99135}
100136
@@ -138,7 +174,7 @@ func BenchmarkInsert(b *testing.B) {
138174 n := emptyNode (0 , 32 )
139175 b .ResetTimer ()
140176 for i := b .N ; i > 0 ; i -- {
141- n = insert (n , & entry {defaultHasher (i ), i , i })
177+ n = insert (n , & testEntry {defaultHasher (i ), i , i })
142178 }
143179}
144180
@@ -165,6 +201,6 @@ func BenchmarkUpdate(b *testing.B) {
165201 n := insertTest (nil , defaultHasher , b .N )
166202 b .ResetTimer ()
167203 for i := b .N ; i > 0 ; i -- {
168- n = insert (n , & entry {defaultHasher (i ), i , - i })
204+ n = insert (n , & testEntry {defaultHasher (i ), i , - i })
169205 }
170206}
0 commit comments