github.com/scottcagno/storage@v1.8.0/pkg/hashmap/chained/sharded_test.go (about)

     1  package chained
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/scottcagno/storage/pkg/util"
     6  	"strconv"
     7  	"testing"
     8  )
     9  
    10  func TestShardedHashMap(t *testing.T) {
    11  	count := 1000000
    12  	hm := NewShardedHashMap(128)
    13  	for i := 0; i < count; i++ {
    14  		hm.Put(strconv.Itoa(i), nil)
    15  	}
    16  	if hm.Len() != count {
    17  		t.Errorf("error: incorrect count of entries\n")
    18  	}
    19  	fmt.Printf("hashmap containing %d entries is taking %d bytes (%.2f kb, %.2f mb)\n",
    20  		count, util.Sizeof(hm), float64(util.Sizeof(hm)/1024), float64(util.Sizeof(hm)/1024/1024))
    21  	for i := 0; i < count; i++ {
    22  		_, ok := hm.Get(strconv.Itoa(i))
    23  		if !ok {
    24  			t.Errorf("error: could not located value for key: %q\n", strconv.Itoa(i))
    25  		}
    26  	}
    27  	for i := 0; i < count; i++ {
    28  		_, ok := hm.Del(strconv.Itoa(i))
    29  		if !ok {
    30  			t.Errorf("error: could not remove value for key: %q\n", strconv.Itoa(i))
    31  		}
    32  	}
    33  	if hm.Len() != count-count {
    34  		t.Errorf("error: incorrect count of entries\n")
    35  	}
    36  	hm.Close()
    37  }