github.com/mymmsc/gox@v1.3.33/util/hashbidimap/hashbidimap_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 hashbidimap
     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 TestMapGetKey(t *testing.T) {
   122  	m := New()
   123  	m.Put(5, "e")
   124  	m.Put(6, "f")
   125  	m.Put(7, "g")
   126  	m.Put(3, "c")
   127  	m.Put(4, "d")
   128  	m.Put(1, "x")
   129  	m.Put(2, "b")
   130  	m.Put(1, "a") //overwrite
   131  
   132  	// key,expectedValue,expectedFound
   133  	tests1 := [][]interface{}{
   134  		{1, "a", true},
   135  		{2, "b", true},
   136  		{3, "c", true},
   137  		{4, "d", true},
   138  		{5, "e", true},
   139  		{6, "f", true},
   140  		{7, "g", true},
   141  		{nil, "x", false},
   142  	}
   143  
   144  	for _, test := range tests1 {
   145  		// retrievals
   146  		actualValue, actualFound := m.GetKey(test[1])
   147  		if actualValue != test[0] || actualFound != test[2] {
   148  			t.Errorf("Got %v expected %v", actualValue, test[0])
   149  		}
   150  	}
   151  }
   152  
   153  func TestMapSerialization(t *testing.T) {
   154  	m := New()
   155  	m.Put("a", 1.0)
   156  	m.Put("b", 2.0)
   157  	m.Put("c", 3.0)
   158  
   159  	var err error
   160  	assert := func() {
   161  		if actualValue, expectedValue := m.Keys(), []interface{}{"a", "b", "c"}; !sameElements(actualValue, expectedValue) {
   162  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   163  		}
   164  		if actualValue, expectedValue := m.Values(), []interface{}{1.0, 2.0, 3.0}; !sameElements(actualValue, expectedValue) {
   165  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   166  		}
   167  		if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue {
   168  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   169  		}
   170  		if err != nil {
   171  			t.Errorf("Got error %v", err)
   172  		}
   173  	}
   174  
   175  	assert()
   176  
   177  	json, err := m.ToJSON()
   178  	assert()
   179  
   180  	err = m.FromJSON(json)
   181  	assert()
   182  }
   183  
   184  func sameElements(a []interface{}, b []interface{}) bool {
   185  	if len(a) != len(b) {
   186  		return false
   187  	}
   188  	for _, av := range a {
   189  		found := false
   190  		for _, bv := range b {
   191  			if av == bv {
   192  				found = true
   193  				break
   194  			}
   195  		}
   196  		if !found {
   197  			return false
   198  		}
   199  	}
   200  	return true
   201  }
   202  
   203  func benchmarkGet(b *testing.B, m *Map, size int) {
   204  	for i := 0; i < b.N; i++ {
   205  		for n := 0; n < size; n++ {
   206  			m.Get(n)
   207  		}
   208  	}
   209  }
   210  
   211  func benchmarkPut(b *testing.B, m *Map, size int) {
   212  	for i := 0; i < b.N; i++ {
   213  		for n := 0; n < size; n++ {
   214  			m.Put(n, n)
   215  		}
   216  	}
   217  }
   218  
   219  func benchmarkRemove(b *testing.B, m *Map, size int) {
   220  	for i := 0; i < b.N; i++ {
   221  		for n := 0; n < size; n++ {
   222  			m.Remove(n)
   223  		}
   224  	}
   225  }
   226  
   227  func BenchmarkHashBidiMapGet100(b *testing.B) {
   228  	b.StopTimer()
   229  	size := 100
   230  	m := New()
   231  	for n := 0; n < size; n++ {
   232  		m.Put(n, n)
   233  	}
   234  	b.StartTimer()
   235  	benchmarkGet(b, m, size)
   236  }
   237  
   238  func BenchmarkHashBidiMapGet1000(b *testing.B) {
   239  	b.StopTimer()
   240  	size := 1000
   241  	m := New()
   242  	for n := 0; n < size; n++ {
   243  		m.Put(n, n)
   244  	}
   245  	b.StartTimer()
   246  	benchmarkGet(b, m, size)
   247  }
   248  
   249  func BenchmarkHashBidiMapGet10000(b *testing.B) {
   250  	b.StopTimer()
   251  	size := 10000
   252  	m := New()
   253  	for n := 0; n < size; n++ {
   254  		m.Put(n, n)
   255  	}
   256  	b.StartTimer()
   257  	benchmarkGet(b, m, size)
   258  }
   259  
   260  func BenchmarkHashBidiMapGet100000(b *testing.B) {
   261  	b.StopTimer()
   262  	size := 100000
   263  	m := New()
   264  	for n := 0; n < size; n++ {
   265  		m.Put(n, n)
   266  	}
   267  	b.StartTimer()
   268  	benchmarkGet(b, m, size)
   269  }
   270  
   271  func BenchmarkHashBidiMapPut100(b *testing.B) {
   272  	b.StopTimer()
   273  	size := 100
   274  	m := New()
   275  	b.StartTimer()
   276  	benchmarkPut(b, m, size)
   277  }
   278  
   279  func BenchmarkHashBidiMapPut1000(b *testing.B) {
   280  	b.StopTimer()
   281  	size := 1000
   282  	m := New()
   283  	for n := 0; n < size; n++ {
   284  		m.Put(n, n)
   285  	}
   286  	b.StartTimer()
   287  	benchmarkPut(b, m, size)
   288  }
   289  
   290  func BenchmarkHashBidiMapPut10000(b *testing.B) {
   291  	b.StopTimer()
   292  	size := 10000
   293  	m := New()
   294  	for n := 0; n < size; n++ {
   295  		m.Put(n, n)
   296  	}
   297  	b.StartTimer()
   298  	benchmarkPut(b, m, size)
   299  }
   300  
   301  func BenchmarkHashBidiMapPut100000(b *testing.B) {
   302  	b.StopTimer()
   303  	size := 100000
   304  	m := New()
   305  	for n := 0; n < size; n++ {
   306  		m.Put(n, n)
   307  	}
   308  	b.StartTimer()
   309  	benchmarkPut(b, m, size)
   310  }
   311  
   312  func BenchmarkHashBidiMapRemove100(b *testing.B) {
   313  	b.StopTimer()
   314  	size := 100
   315  	m := New()
   316  	for n := 0; n < size; n++ {
   317  		m.Put(n, n)
   318  	}
   319  	b.StartTimer()
   320  	benchmarkRemove(b, m, size)
   321  }
   322  
   323  func BenchmarkHashBidiMapRemove1000(b *testing.B) {
   324  	b.StopTimer()
   325  	size := 1000
   326  	m := New()
   327  	for n := 0; n < size; n++ {
   328  		m.Put(n, n)
   329  	}
   330  	b.StartTimer()
   331  	benchmarkRemove(b, m, size)
   332  }
   333  
   334  func BenchmarkHashBidiMapRemove10000(b *testing.B) {
   335  	b.StopTimer()
   336  	size := 10000
   337  	m := New()
   338  	for n := 0; n < size; n++ {
   339  		m.Put(n, n)
   340  	}
   341  	b.StartTimer()
   342  	benchmarkRemove(b, m, size)
   343  }
   344  
   345  func BenchmarkHashBidiMapRemove100000(b *testing.B) {
   346  	b.StopTimer()
   347  	size := 100000
   348  	m := New()
   349  	for n := 0; n < size; n++ {
   350  		m.Put(n, n)
   351  	}
   352  	b.StartTimer()
   353  	benchmarkRemove(b, m, size)
   354  }