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

     1  package chained
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/scottcagno/storage/pkg/util"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestNewHashMap(t *testing.T) {
    12  	hm := NewHashMap(128)
    13  	util.AssertExpected(t, 0, hm.Len())
    14  	hm.Put("0", nil)
    15  	util.AssertExpected(t, 1, hm.Len())
    16  	for i := 1; i < 5; i++ {
    17  		hm.Put(strconv.Itoa(i), nil)
    18  	}
    19  	util.AssertExpected(t, 5, hm.Len())
    20  	hm.Close()
    21  }
    22  
    23  func Test_alignBucketCount(t *testing.T) {
    24  	var count uint64
    25  	util.AssertExpected(t, uint64(0), count)
    26  	count = alignBucketCount(31)
    27  	util.AssertExpected(t, uint64(32), count)
    28  	count = alignBucketCount(12)
    29  	util.AssertExpected(t, uint64(16), count)
    30  }
    31  
    32  func Test_bucket_delete(t *testing.T) {
    33  	b := &bucket{
    34  		hashkey: 1234567890,
    35  		head:    nil,
    36  	}
    37  
    38  	b.insert("1", []byte("1"))
    39  	b.insert("2", []byte("2"))
    40  	b.insert("3", []byte("3"))
    41  	b.insert("4", []byte("4"))
    42  	b.insert("5", []byte("5"))
    43  
    44  	var count int
    45  	b.scan(func(key keyType, val valType) bool {
    46  		if key != keyZeroType {
    47  			count++
    48  			return true
    49  		}
    50  		return false
    51  	})
    52  	util.AssertExpected(t, 5, count)
    53  
    54  	val, ok := b.delete("1")
    55  	util.AssertExpected(t, true, ok)
    56  	util.AssertExpected(t, []byte("1"), val)
    57  
    58  	val, ok = b.delete("2")
    59  	util.AssertExpected(t, true, ok)
    60  	util.AssertExpected(t, []byte("2"), val)
    61  
    62  	val, ok = b.delete("3")
    63  	util.AssertExpected(t, true, ok)
    64  	util.AssertExpected(t, []byte("3"), val)
    65  
    66  	count = 0
    67  	b.scan(func(key keyType, val valType) bool {
    68  		if key != keyZeroType {
    69  			count++
    70  			return true
    71  		}
    72  		return false
    73  	})
    74  	util.AssertExpected(t, 2, count)
    75  
    76  	b = nil
    77  }
    78  
    79  func Test_bucket_insert(t *testing.T) {
    80  	b := &bucket{
    81  		hashkey: 1234567890,
    82  		head:    nil,
    83  	}
    84  	val, ok := b.insert("1", []byte("1"))
    85  	util.AssertExpected(t, false, ok)
    86  	util.AssertExpected(t, []byte("1"), val)
    87  
    88  	val, ok = b.insert("2", []byte("2"))
    89  	util.AssertExpected(t, false, ok)
    90  	util.AssertExpected(t, []byte("2"), val)
    91  
    92  	val, ok = b.insert("3", []byte("3"))
    93  	util.AssertExpected(t, false, ok)
    94  	util.AssertExpected(t, []byte("3"), val)
    95  
    96  	var count int
    97  	b.scan(func(key keyType, val valType) bool {
    98  		if key != keyZeroType {
    99  			count++
   100  			return true
   101  		}
   102  		return false
   103  	})
   104  	util.AssertExpected(t, 3, count)
   105  	b = nil
   106  }
   107  
   108  func Test_bucket_scan(t *testing.T) {
   109  	b := &bucket{
   110  		hashkey: 1234567890,
   111  		head:    nil,
   112  	}
   113  
   114  	b.insert("1", []byte("1"))
   115  	b.insert("2", []byte("2"))
   116  	b.insert("3", []byte("3"))
   117  	b.insert("4", []byte("4"))
   118  	b.insert("5", []byte("5"))
   119  
   120  	var count int
   121  	b.scan(func(key keyType, val valType) bool {
   122  		if key != keyZeroType {
   123  			count++
   124  			return true
   125  		}
   126  		return false
   127  	})
   128  	util.AssertExpected(t, 5, count)
   129  	b = nil
   130  }
   131  
   132  func Test_bucket_search(t *testing.T) {
   133  	b := &bucket{
   134  		hashkey: 1234567890,
   135  		head:    nil,
   136  	}
   137  
   138  	b.insert("1", []byte("1"))
   139  	b.insert("2", []byte("2"))
   140  	b.insert("3", []byte("3"))
   141  	b.insert("4", []byte("4"))
   142  	b.insert("5", []byte("5"))
   143  
   144  	val, ok := b.search("3")
   145  	util.AssertExpected(t, true, ok)
   146  	util.AssertExpected(t, []byte("3"), val)
   147  
   148  	val, ok = b.search("1")
   149  	util.AssertExpected(t, true, ok)
   150  	util.AssertExpected(t, []byte("1"), val)
   151  
   152  	val, ok = b.search("5")
   153  	util.AssertExpected(t, true, ok)
   154  	util.AssertExpected(t, []byte("5"), val)
   155  
   156  	val, ok = b.search("2")
   157  	util.AssertExpected(t, true, ok)
   158  	util.AssertExpected(t, []byte("2"), val)
   159  
   160  	val, ok = b.search("4")
   161  	util.AssertExpected(t, true, ok)
   162  	util.AssertExpected(t, []byte("4"), val)
   163  
   164  	b = nil
   165  }
   166  
   167  // 25 words
   168  var words = []string{
   169  	"reproducibility",
   170  	"eruct",
   171  	"acids",
   172  	"flyspecks",
   173  	"driveshafts",
   174  	"volcanically",
   175  	"discouraging",
   176  	"acapnia",
   177  	"phenazines",
   178  	"hoarser",
   179  	"abusing",
   180  	"samara",
   181  	"thromboses",
   182  	"impolite",
   183  	"drivennesses",
   184  	"tenancy",
   185  	"counterreaction",
   186  	"kilted",
   187  	"linty",
   188  	"kistful",
   189  	"biomarkers",
   190  	"infusiblenesses",
   191  	"capsulate",
   192  	"reflowering",
   193  	"heterophyllies",
   194  }
   195  
   196  func Test_defaultHashFunc(t *testing.T) {
   197  	set := make(map[uint64]string, len(words))
   198  	var hash uint64
   199  	var coll int
   200  	for _, word := range words {
   201  		hash = defaultHashFunc(word)
   202  		if old, ok := set[hash]; !ok {
   203  			set[hash] = word
   204  		} else {
   205  			coll++
   206  			fmt.Printf(
   207  				"collision: current word: %s, old word: %s, hash: %d\n", word, old, hash)
   208  		}
   209  	}
   210  	fmt.Printf("encountered %d collisions comparing %d words\n", coll, len(words))
   211  }
   212  
   213  func Test_HashMap_Del(t *testing.T) {
   214  	hm := NewHashMap(128)
   215  	for i := 0; i < len(words); i++ {
   216  		hm.Put(words[i], []byte{0x69})
   217  	}
   218  	util.AssertExpected(t, 25, hm.Len())
   219  	count := hm.Len()
   220  	var stop = hm.Len()
   221  	for i := 0; i < stop; i++ {
   222  		ret, ok := hm.Del(words[i])
   223  		util.AssertExpected(t, true, ok)
   224  		util.AssertExpected(t, []byte{0x69}, ret)
   225  		count--
   226  	}
   227  	util.AssertExpected(t, 0, count)
   228  	hm.Close()
   229  }
   230  
   231  func Test_HashMap_Get(t *testing.T) {
   232  	hm := NewHashMap(128)
   233  	for i := 0; i < len(words); i++ {
   234  		hm.Put(words[i], []byte{0x69})
   235  	}
   236  	util.AssertExpected(t, 25, hm.Len())
   237  	var count int
   238  	for i := 0; i < hm.Len(); i++ {
   239  		ret, ok := hm.Get(words[i])
   240  		util.AssertExpected(t, true, ok)
   241  		util.AssertExpected(t, []byte{0x69}, ret)
   242  		count++
   243  	}
   244  	util.AssertExpected(t, 25, count)
   245  	hm.Close()
   246  }
   247  
   248  func Test_HashMap_Len(t *testing.T) {
   249  	hm := NewHashMap(128)
   250  	for i := 0; i < len(words); i++ {
   251  		hm.Put(words[i], []byte{0x69})
   252  	}
   253  	util.AssertExpected(t, 25, hm.Len())
   254  	hm.Close()
   255  }
   256  
   257  func Test_HashMap_PercentFull(t *testing.T) {
   258  	hm := NewHashMap(0)
   259  	for i := 0; i < len(words); i++ {
   260  		hm.Put(words[i], []byte{0x69})
   261  	}
   262  	percent := fmt.Sprintf("%.2f", hm.PercentFull())
   263  	util.AssertExpected(t, "0.78", percent)
   264  	hm.Close()
   265  }
   266  
   267  func Test_HashMap_Put(t *testing.T) {
   268  	hm := NewHashMap(128)
   269  	for i := 0; i < len(words); i++ {
   270  		hm.Put(words[i], []byte{0x69})
   271  	}
   272  	util.AssertExpected(t, 25, hm.Len())
   273  	hm.Close()
   274  }
   275  
   276  func Test_HashMap_Range(t *testing.T) {
   277  	hm := NewHashMap(128)
   278  	for i := 0; i < len(words); i++ {
   279  		hm.Put(words[i], []byte{0x69})
   280  	}
   281  	util.AssertExpected(t, 25, hm.Len())
   282  	var counted int
   283  	hm.Range(func(key keyType, value valType) bool {
   284  		if key != "" && bytes.Equal(value, []byte{0x69}) {
   285  			counted++
   286  			return true
   287  		}
   288  		return false
   289  	})
   290  	util.AssertExpected(t, 25, counted)
   291  	hm.Close()
   292  }