gitee.com/quant1x/gox@v1.21.2/util/hashmap/hashmap_test.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package hashmap
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  func TestMapPut(t *testing.T) {
    13  	m := New()
    14  	m.Put(5, "e")
    15  	m.Put(6, "f")
    16  	m.Put(7, "g")
    17  	m.Put(3, "c")
    18  	m.Put(4, "d")
    19  	m.Put(1, "x")
    20  	m.Put(2, "b")
    21  	m.Put(1, "a") //overwrite
    22  
    23  	if actualValue := m.Size(); actualValue != 7 {
    24  		t.Errorf("Got %v expected %v", actualValue, 7)
    25  	}
    26  	if actualValue, expectedValue := m.Keys(), []interface{}{1, 2, 3, 4, 5, 6, 7}; !sameElements(actualValue, expectedValue) {
    27  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    28  	}
    29  	if actualValue, expectedValue := m.Values(), []interface{}{"a", "b", "c", "d", "e", "f", "g"}; !sameElements(actualValue, expectedValue) {
    30  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    31  	}
    32  
    33  	// key,expectedValue,expectedFound
    34  	tests1 := [][]interface{}{
    35  		{1, "a", true},
    36  		{2, "b", true},
    37  		{3, "c", true},
    38  		{4, "d", true},
    39  		{5, "e", true},
    40  		{6, "f", true},
    41  		{7, "g", true},
    42  		{8, nil, false},
    43  	}
    44  
    45  	for _, test := range tests1 {
    46  		// retrievals
    47  		actualValue, actualFound := m.Get(test[0])
    48  		if actualValue != test[1] || actualFound != test[2] {
    49  			t.Errorf("Got %v expected %v", actualValue, test[1])
    50  		}
    51  	}
    52  }
    53  
    54  func TestMapRemove(t *testing.T) {
    55  	m := New()
    56  	m.Put(5, "e")
    57  	m.Put(6, "f")
    58  	m.Put(7, "g")
    59  	m.Put(3, "c")
    60  	m.Put(4, "d")
    61  	m.Put(1, "x")
    62  	m.Put(2, "b")
    63  	m.Put(1, "a") //overwrite
    64  
    65  	m.Remove(5)
    66  	m.Remove(6)
    67  	m.Remove(7)
    68  	m.Remove(8)
    69  	m.Remove(5)
    70  
    71  	if actualValue, expectedValue := m.Keys(), []interface{}{1, 2, 3, 4}; !sameElements(actualValue, expectedValue) {
    72  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    73  	}
    74  
    75  	if actualValue, expectedValue := m.Values(), []interface{}{"a", "b", "c", "d"}; !sameElements(actualValue, expectedValue) {
    76  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    77  	}
    78  	if actualValue := m.Size(); actualValue != 4 {
    79  		t.Errorf("Got %v expected %v", actualValue, 4)
    80  	}
    81  
    82  	tests2 := [][]interface{}{
    83  		{1, "a", true},
    84  		{2, "b", true},
    85  		{3, "c", true},
    86  		{4, "d", true},
    87  		{5, nil, false},
    88  		{6, nil, false},
    89  		{7, nil, false},
    90  		{8, nil, false},
    91  	}
    92  
    93  	for _, test := range tests2 {
    94  		actualValue, actualFound := m.Get(test[0])
    95  		if actualValue != test[1] || actualFound != test[2] {
    96  			t.Errorf("Got %v expected %v", actualValue, test[1])
    97  		}
    98  	}
    99  
   100  	m.Remove(1)
   101  	m.Remove(4)
   102  	m.Remove(2)
   103  	m.Remove(3)
   104  	m.Remove(2)
   105  	m.Remove(2)
   106  
   107  	if actualValue, expectedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expectedValue {
   108  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   109  	}
   110  	if actualValue, expectedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expectedValue {
   111  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   112  	}
   113  	if actualValue := m.Size(); actualValue != 0 {
   114  		t.Errorf("Got %v expected %v", actualValue, 0)
   115  	}
   116  	if actualValue := m.Empty(); actualValue != true {
   117  		t.Errorf("Got %v expected %v", actualValue, true)
   118  	}
   119  }
   120  
   121  func TestMapSerialization(t *testing.T) {
   122  	m := New()
   123  	m.Put("a", 1.0)
   124  	m.Put("b", 2.0)
   125  	m.Put("c", 3.0)
   126  
   127  	var err error
   128  	assert := func() {
   129  		if actualValue, expectedValue := m.Keys(), []interface{}{"a", "b", "c"}; !sameElements(actualValue, expectedValue) {
   130  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   131  		}
   132  		if actualValue, expectedValue := m.Values(), []interface{}{1.0, 2.0, 3.0}; !sameElements(actualValue, expectedValue) {
   133  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   134  		}
   135  		if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue {
   136  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   137  		}
   138  		if err != nil {
   139  			t.Errorf("Got error %v", err)
   140  		}
   141  	}
   142  
   143  	assert()
   144  
   145  	json, err := m.ToJSON()
   146  	assert()
   147  
   148  	err = m.FromJSON(json)
   149  	assert()
   150  }
   151  
   152  func sameElements(a []interface{}, b []interface{}) bool {
   153  	if len(a) != len(b) {
   154  		return false
   155  	}
   156  	for _, av := range a {
   157  		found := false
   158  		for _, bv := range b {
   159  			if av == bv {
   160  				found = true
   161  				break
   162  			}
   163  		}
   164  		if !found {
   165  			return false
   166  		}
   167  	}
   168  	return true
   169  }
   170  
   171  func benchmarkGet(b *testing.B, m *Map, size int) {
   172  	for i := 0; i < b.N; i++ {
   173  		for n := 0; n < size; n++ {
   174  			m.Get(n)
   175  		}
   176  	}
   177  }
   178  
   179  func benchmarkPut(b *testing.B, m *Map, size int) {
   180  	for i := 0; i < b.N; i++ {
   181  		for n := 0; n < size; n++ {
   182  			m.Put(n, struct{}{})
   183  		}
   184  	}
   185  }
   186  
   187  func benchmarkRemove(b *testing.B, m *Map, size int) {
   188  	for i := 0; i < b.N; i++ {
   189  		for n := 0; n < size; n++ {
   190  			m.Remove(n)
   191  		}
   192  	}
   193  }
   194  
   195  func BenchmarkHashMapGet100(b *testing.B) {
   196  	b.StopTimer()
   197  	size := 100
   198  	m := New()
   199  	for n := 0; n < size; n++ {
   200  		m.Put(n, struct{}{})
   201  	}
   202  	b.StartTimer()
   203  	benchmarkGet(b, m, size)
   204  }
   205  
   206  func BenchmarkHashMapGet1000(b *testing.B) {
   207  	b.StopTimer()
   208  	size := 1000
   209  	m := New()
   210  	for n := 0; n < size; n++ {
   211  		m.Put(n, struct{}{})
   212  	}
   213  	b.StartTimer()
   214  	benchmarkGet(b, m, size)
   215  }
   216  
   217  func BenchmarkHashMapGet10000(b *testing.B) {
   218  	b.StopTimer()
   219  	size := 10000
   220  	m := New()
   221  	for n := 0; n < size; n++ {
   222  		m.Put(n, struct{}{})
   223  	}
   224  	b.StartTimer()
   225  	benchmarkGet(b, m, size)
   226  }
   227  
   228  func BenchmarkHashMapGet100000(b *testing.B) {
   229  	b.StopTimer()
   230  	size := 100000
   231  	m := New()
   232  	for n := 0; n < size; n++ {
   233  		m.Put(n, struct{}{})
   234  	}
   235  	b.StartTimer()
   236  	benchmarkGet(b, m, size)
   237  }
   238  
   239  func BenchmarkHashMapPut100(b *testing.B) {
   240  	b.StopTimer()
   241  	size := 100
   242  	m := New()
   243  	b.StartTimer()
   244  	benchmarkPut(b, m, size)
   245  }
   246  
   247  func BenchmarkHashMapPut1000(b *testing.B) {
   248  	b.StopTimer()
   249  	size := 1000
   250  	m := New()
   251  	for n := 0; n < size; n++ {
   252  		m.Put(n, struct{}{})
   253  	}
   254  	b.StartTimer()
   255  	benchmarkPut(b, m, size)
   256  }
   257  
   258  func BenchmarkHashMapPut10000(b *testing.B) {
   259  	b.StopTimer()
   260  	size := 10000
   261  	m := New()
   262  	for n := 0; n < size; n++ {
   263  		m.Put(n, struct{}{})
   264  	}
   265  	b.StartTimer()
   266  	benchmarkPut(b, m, size)
   267  }
   268  
   269  func BenchmarkHashMapPut100000(b *testing.B) {
   270  	b.StopTimer()
   271  	size := 100000
   272  	m := New()
   273  	for n := 0; n < size; n++ {
   274  		m.Put(n, struct{}{})
   275  	}
   276  	b.StartTimer()
   277  	benchmarkPut(b, m, size)
   278  }
   279  
   280  func BenchmarkHashMapRemove100(b *testing.B) {
   281  	b.StopTimer()
   282  	size := 100
   283  	m := New()
   284  	for n := 0; n < size; n++ {
   285  		m.Put(n, struct{}{})
   286  	}
   287  	b.StartTimer()
   288  	benchmarkRemove(b, m, size)
   289  }
   290  
   291  func BenchmarkHashMapRemove1000(b *testing.B) {
   292  	b.StopTimer()
   293  	size := 1000
   294  	m := New()
   295  	for n := 0; n < size; n++ {
   296  		m.Put(n, struct{}{})
   297  	}
   298  	b.StartTimer()
   299  	benchmarkRemove(b, m, size)
   300  }
   301  
   302  func BenchmarkHashMapRemove10000(b *testing.B) {
   303  	b.StopTimer()
   304  	size := 10000
   305  	m := New()
   306  	for n := 0; n < size; n++ {
   307  		m.Put(n, struct{}{})
   308  	}
   309  	b.StartTimer()
   310  	benchmarkRemove(b, m, size)
   311  }
   312  
   313  func BenchmarkHashMapRemove100000(b *testing.B) {
   314  	b.StopTimer()
   315  	size := 100000
   316  	m := New()
   317  	for n := 0; n < size; n++ {
   318  		m.Put(n, struct{}{})
   319  	}
   320  	b.StartTimer()
   321  	benchmarkRemove(b, m, size)
   322  }