github.com/scottcagno/storage@v1.8.0/pkg/hashmap/openaddr/rhhmap_gp_test.go (about)

     1  package openaddr
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/scottcagno/storage/pkg/bits"
     6  	"github.com/scottcagno/storage/pkg/util"
     7  	"math/rand"
     8  	"testing"
     9  )
    10  
    11  func Test_defaultHashFuncGP(t *testing.T) {
    12  	set := make(map[uint64]string, len(words))
    13  	var hash uint64
    14  	var coll int
    15  	for _, word := range words {
    16  		hash = defaultHashFuncGP(word)
    17  		if old, ok := set[hash]; !ok {
    18  			set[hash] = word
    19  		} else {
    20  			coll++
    21  			fmt.Printf(
    22  				"collision: current word: %s, old word: %s, hash: %d\n", word, old, hash)
    23  		}
    24  	}
    25  	fmt.Printf("encountered %d collisions comparing %d words\n", coll, len(words))
    26  }
    27  
    28  func Test_HashMapGP_Del(t *testing.T) {
    29  	hm := NewHashMapGP(128)
    30  	for i := 0; i < len(words); i++ {
    31  		hm.Set(words[i], 0x69)
    32  	}
    33  	util.AssertExpected(t, 25, hm.Len())
    34  	count := hm.Len()
    35  	var stop = hm.Len()
    36  	for i := 0; i < stop; i++ {
    37  		ret, ok := hm.Del(words[i])
    38  		util.AssertExpected(t, true, ok)
    39  		util.AssertExpected(t, 0x69, ret)
    40  		count--
    41  	}
    42  	util.AssertExpected(t, 0, count)
    43  	hm.Close()
    44  }
    45  
    46  func Test_HashMapGP_Get(t *testing.T) {
    47  	hm := NewHashMapGP(128)
    48  	for i := 0; i < len(words); i++ {
    49  		hm.Set(words[i], []byte{0x69})
    50  	}
    51  	util.AssertExpected(t, 25, hm.Len())
    52  	var count int
    53  	for i := 0; i < hm.Len(); i++ {
    54  		ret, ok := hm.Get(words[i])
    55  		util.AssertExpected(t, true, ok)
    56  		util.AssertExpected(t, []byte{0x69}, ret)
    57  		count++
    58  	}
    59  	util.AssertExpected(t, 25, count)
    60  	hm.Close()
    61  }
    62  
    63  func Test_HashMapGP_Len(t *testing.T) {
    64  	hm := NewHashMapGP(128)
    65  	for i := 0; i < len(words); i++ {
    66  		hm.Set(words[i], []byte{0x69})
    67  	}
    68  	util.AssertExpected(t, 25, hm.Len())
    69  	hm.Close()
    70  }
    71  
    72  func Test_HashMapGP_PercentFull(t *testing.T) {
    73  	hm := NewHashMapGP(0)
    74  	for i := 0; i < len(words); i++ {
    75  		hm.Set(words[i], []byte{0x69})
    76  	}
    77  	percent := fmt.Sprintf("%.2f", hm.PercentFull())
    78  	util.AssertExpected(t, "0.78", percent)
    79  	hm.Close()
    80  }
    81  
    82  func Test_HashMapGP_Set(t *testing.T) {
    83  	hm := NewHashMapGP(128)
    84  	for i := 0; i < len(words); i++ {
    85  		hm.Set(words[i], []byte{0x69})
    86  	}
    87  	util.AssertExpected(t, 25, hm.Len())
    88  	hm.Close()
    89  }
    90  
    91  func Test_HashMapGP_Range(t *testing.T) {
    92  	hm := NewHashMapGP(128)
    93  	for i := 0; i < len(words); i++ {
    94  		hm.Set(words[i], 0x69)
    95  	}
    96  	util.AssertExpected(t, 25, hm.Len())
    97  	var counted int
    98  	hm.Range(func(key string, value interface{}) bool {
    99  		if key != "" && value == 0x69 {
   100  			counted++
   101  			return true
   102  		}
   103  		return false
   104  	})
   105  	util.AssertExpected(t, 25, counted)
   106  	hm.Close()
   107  }
   108  
   109  var resultGP interface{}
   110  
   111  func BenchmarkHashMapGP_Set1(b *testing.B) {
   112  	hm := NewHashMapGP(128)
   113  
   114  	b.ResetTimer()
   115  	b.ReportAllocs()
   116  
   117  	var val []byte
   118  	for n := 0; n < b.N; n++ {
   119  		// try to get key/value "foo"
   120  		v, ok := hm.Get("foo")
   121  		val = v.([]byte)
   122  		if !ok {
   123  			// if it doesn't exist, then initialize it
   124  			hm.Set("foo", make([]byte, 32))
   125  		} else {
   126  			// if it does exist, then pick a random number between
   127  			// 0 and 256--this will be our bit we try and set
   128  			ri := uint(rand.Intn(128))
   129  			if ok := bits.RawBytesHasBit(&val, ri); !ok {
   130  				// we check the bit to see if it's already set, and
   131  				// then we go ahead and set it if it is not set
   132  				bits.RawBytesSetBit(&val, ri)
   133  			}
   134  			// after this, we make sure to save the bitset back to the hashmap
   135  			if n < 64 {
   136  				fmt.Printf("addr: %p, %+v\n", val, val)
   137  				//PrintBits(v)
   138  			}
   139  			hm.Set("foo", val)
   140  		}
   141  	}
   142  	result = val
   143  }
   144  
   145  func BenchmarkHashMapGP_Set2(b *testing.B) {
   146  	hm := NewHashMapGP(128)
   147  
   148  	b.ResetTimer()
   149  	b.ReportAllocs()
   150  
   151  	var val []byte
   152  	for n := 0; n < b.N; n++ {
   153  		// try to get key/value "foo"
   154  		v, ok := hm.Get("foo")
   155  		val = v.([]byte)
   156  		if !ok {
   157  			// if it doesn't exist, then initialize it
   158  			hm.Set("foo", make([]byte, 32))
   159  		} else {
   160  			val = append(val, []byte{byte(n >> 8)}...)
   161  			// after this, we make sure to save the bitset back to the hashmap
   162  			if n < 64 {
   163  				fmt.Printf("addr: %p, %+v\n", val, val)
   164  				//PrintBits(v)
   165  			}
   166  			hm.Set("foo", val)
   167  		}
   168  	}
   169  	result = val
   170  }