github.com/mymmsc/gox@v1.3.33/util/linkedhashmap/linkedhashmap_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 linkedhashmap
     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{}{5, 6, 7, 3, 4, 1, 2}; !sameElements(actualValue, expectedValue) {
    27  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    28  	}
    29  	if actualValue, expectedValue := m.Values(), []interface{}{"e", "f", "g", "c", "d", "a", "b"}; !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{}{3, 4, 1, 2}; !sameElements(actualValue, expectedValue) {
    72  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    73  	}
    74  
    75  	if actualValue, expectedValue := m.Values(), []interface{}{"c", "d", "a", "b"}; !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 sameElements(a []interface{}, b []interface{}) bool {
   122  	// If one is nil, the other must also be nil.
   123  	if (a == nil) != (b == nil) {
   124  		return false
   125  	}
   126  
   127  	if len(a) != len(b) {
   128  		return false
   129  	}
   130  
   131  	for i := range a {
   132  		if a[i] != b[i] {
   133  			return false
   134  		}
   135  	}
   136  
   137  	return true
   138  }
   139  
   140  func TestMapEach(t *testing.T) {
   141  	m := New()
   142  	m.Put("c", 1)
   143  	m.Put("a", 2)
   144  	m.Put("b", 3)
   145  	count := 0
   146  	m.Each(func(key interface{}, value interface{}) {
   147  		count++
   148  		if actualValue, expectedValue := count, value; actualValue != expectedValue {
   149  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   150  		}
   151  		switch value {
   152  		case 1:
   153  			if actualValue, expectedValue := key, "c"; actualValue != expectedValue {
   154  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   155  			}
   156  		case 2:
   157  			if actualValue, expectedValue := key, "a"; actualValue != expectedValue {
   158  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   159  			}
   160  		case 3:
   161  			if actualValue, expectedValue := key, "b"; actualValue != expectedValue {
   162  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   163  			}
   164  		default:
   165  			t.Errorf("Too many")
   166  		}
   167  	})
   168  }
   169  
   170  func TestMapMap(t *testing.T) {
   171  	m := New()
   172  	m.Put("c", 3)
   173  	m.Put("a", 1)
   174  	m.Put("b", 2)
   175  	mappedMap := m.Map(func(key1 interface{}, value1 interface{}) (key2 interface{}, value2 interface{}) {
   176  		return key1, value1.(int) * value1.(int)
   177  	})
   178  	if actualValue, _ := mappedMap.Get("c"); actualValue != 9 {
   179  		t.Errorf("Got %v expected %v", actualValue, "mapped: c")
   180  	}
   181  	if actualValue, _ := mappedMap.Get("a"); actualValue != 1 {
   182  		t.Errorf("Got %v expected %v", actualValue, "mapped: a")
   183  	}
   184  	if actualValue, _ := mappedMap.Get("b"); actualValue != 4 {
   185  		t.Errorf("Got %v expected %v", actualValue, "mapped: b")
   186  	}
   187  	if mappedMap.Size() != 3 {
   188  		t.Errorf("Got %v expected %v", mappedMap.Size(), 3)
   189  	}
   190  }
   191  
   192  func TestMapSelect(t *testing.T) {
   193  	m := New()
   194  	m.Put("c", 3)
   195  	m.Put("b", 1)
   196  	m.Put("a", 2)
   197  	selectedMap := m.Select(func(key interface{}, value interface{}) bool {
   198  		return key.(string) >= "a" && key.(string) <= "b"
   199  	})
   200  	if actualValue, _ := selectedMap.Get("b"); actualValue != 1 {
   201  		t.Errorf("Got %v expected %v", actualValue, "value: a")
   202  	}
   203  	if actualValue, _ := selectedMap.Get("a"); actualValue != 2 {
   204  		t.Errorf("Got %v expected %v", actualValue, "value: b")
   205  	}
   206  	if selectedMap.Size() != 2 {
   207  		t.Errorf("Got %v expected %v", selectedMap.Size(), 2)
   208  	}
   209  }
   210  
   211  func TestMapAny(t *testing.T) {
   212  	m := New()
   213  	m.Put("c", 3)
   214  	m.Put("a", 1)
   215  	m.Put("b", 2)
   216  	any := m.Any(func(key interface{}, value interface{}) bool {
   217  		return value.(int) == 3
   218  	})
   219  	if any != true {
   220  		t.Errorf("Got %v expected %v", any, true)
   221  	}
   222  	any = m.Any(func(key interface{}, value interface{}) bool {
   223  		return value.(int) == 4
   224  	})
   225  	if any != false {
   226  		t.Errorf("Got %v expected %v", any, false)
   227  	}
   228  }
   229  
   230  func TestMapAll(t *testing.T) {
   231  	m := New()
   232  	m.Put("c", 3)
   233  	m.Put("a", 1)
   234  	m.Put("b", 2)
   235  	all := m.All(func(key interface{}, value interface{}) bool {
   236  		return key.(string) >= "a" && key.(string) <= "c"
   237  	})
   238  	if all != true {
   239  		t.Errorf("Got %v expected %v", all, true)
   240  	}
   241  	all = m.All(func(key interface{}, value interface{}) bool {
   242  		return key.(string) >= "a" && key.(string) <= "b"
   243  	})
   244  	if all != false {
   245  		t.Errorf("Got %v expected %v", all, false)
   246  	}
   247  }
   248  
   249  func TestMapFind(t *testing.T) {
   250  	m := New()
   251  	m.Put("c", 3)
   252  	m.Put("a", 1)
   253  	m.Put("b", 2)
   254  	foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool {
   255  		return key.(string) == "c"
   256  	})
   257  	if foundKey != "c" || foundValue != 3 {
   258  		t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3)
   259  	}
   260  	foundKey, foundValue = m.Find(func(key interface{}, value interface{}) bool {
   261  		return key.(string) == "x"
   262  	})
   263  	if foundKey != nil || foundValue != nil {
   264  		t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, nil, nil)
   265  	}
   266  }
   267  
   268  func TestMapChaining(t *testing.T) {
   269  	m := New()
   270  	m.Put("c", 3)
   271  	m.Put("a", 1)
   272  	m.Put("b", 2)
   273  	chainedMap := m.Select(func(key interface{}, value interface{}) bool {
   274  		return value.(int) > 1
   275  	}).Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
   276  		return key.(string) + key.(string), value.(int) * value.(int)
   277  	})
   278  	if actualValue := chainedMap.Size(); actualValue != 2 {
   279  		t.Errorf("Got %v expected %v", actualValue, 2)
   280  	}
   281  	if actualValue, found := chainedMap.Get("aa"); actualValue != nil || found {
   282  		t.Errorf("Got %v expected %v", actualValue, nil)
   283  	}
   284  	if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found {
   285  		t.Errorf("Got %v expected %v", actualValue, 4)
   286  	}
   287  	if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found {
   288  		t.Errorf("Got %v expected %v", actualValue, 9)
   289  	}
   290  }
   291  
   292  func TestMapIteratorNextOnEmpty(t *testing.T) {
   293  	m := New()
   294  	it := m.Iterator()
   295  	it = m.Iterator()
   296  	for it.Next() {
   297  		t.Errorf("Shouldn't iterate on empty map")
   298  	}
   299  }
   300  
   301  func TestMapIteratorPrevOnEmpty(t *testing.T) {
   302  	m := New()
   303  	it := m.Iterator()
   304  	it = m.Iterator()
   305  	for it.Prev() {
   306  		t.Errorf("Shouldn't iterate on empty map")
   307  	}
   308  }
   309  
   310  func TestMapIteratorNext(t *testing.T) {
   311  	m := New()
   312  	m.Put("c", 1)
   313  	m.Put("a", 2)
   314  	m.Put("b", 3)
   315  
   316  	it := m.Iterator()
   317  	count := 0
   318  	for it.Next() {
   319  		count++
   320  		key := it.Key()
   321  		value := it.Value()
   322  		switch key {
   323  		case "c":
   324  			if actualValue, expectedValue := value, 1; actualValue != expectedValue {
   325  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   326  			}
   327  		case "a":
   328  			if actualValue, expectedValue := value, 2; actualValue != expectedValue {
   329  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   330  			}
   331  		case "b":
   332  			if actualValue, expectedValue := value, 3; actualValue != expectedValue {
   333  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   334  			}
   335  		default:
   336  			t.Errorf("Too many")
   337  		}
   338  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   339  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   340  		}
   341  	}
   342  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   343  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   344  	}
   345  }
   346  
   347  func TestMapIteratorPrev(t *testing.T) {
   348  	m := New()
   349  	m.Put("c", 1)
   350  	m.Put("a", 2)
   351  	m.Put("b", 3)
   352  
   353  	it := m.Iterator()
   354  	for it.Next() {
   355  	}
   356  	countDown := m.Size()
   357  	for it.Prev() {
   358  		key := it.Key()
   359  		value := it.Value()
   360  		switch key {
   361  		case "c":
   362  			if actualValue, expectedValue := value, 1; actualValue != expectedValue {
   363  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   364  			}
   365  		case "a":
   366  			if actualValue, expectedValue := value, 2; actualValue != expectedValue {
   367  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   368  			}
   369  		case "b":
   370  			if actualValue, expectedValue := value, 3; actualValue != expectedValue {
   371  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   372  			}
   373  		default:
   374  			t.Errorf("Too many")
   375  		}
   376  		if actualValue, expectedValue := value, countDown; actualValue != expectedValue {
   377  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   378  		}
   379  		countDown--
   380  	}
   381  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   382  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   383  	}
   384  }
   385  
   386  func TestMapIteratorBegin(t *testing.T) {
   387  	m := New()
   388  	it := m.Iterator()
   389  	it.Begin()
   390  	m.Put(3, "c")
   391  	m.Put(1, "a")
   392  	m.Put(2, "b")
   393  	for it.Next() {
   394  	}
   395  	it.Begin()
   396  	it.Next()
   397  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   398  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   399  	}
   400  }
   401  
   402  func TestMapTreeIteratorEnd(t *testing.T) {
   403  	m := New()
   404  	it := m.Iterator()
   405  	m.Put(3, "c")
   406  	m.Put(1, "a")
   407  	m.Put(2, "b")
   408  	it.End()
   409  	it.Prev()
   410  	if key, value := it.Key(), it.Value(); key != 2 || value != "b" {
   411  		t.Errorf("Got %v,%v expected %v,%v", key, value, 2, "b")
   412  	}
   413  }
   414  
   415  func TestMapIteratorFirst(t *testing.T) {
   416  	m := New()
   417  	m.Put(3, "c")
   418  	m.Put(1, "a")
   419  	m.Put(2, "b")
   420  	it := m.Iterator()
   421  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   422  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   423  	}
   424  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   425  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   426  	}
   427  }
   428  
   429  func TestMapIteratorLast(t *testing.T) {
   430  	m := New()
   431  	m.Put(3, "c")
   432  	m.Put(1, "a")
   433  	m.Put(2, "b")
   434  	it := m.Iterator()
   435  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   436  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   437  	}
   438  	if key, value := it.Key(), it.Value(); key != 2 || value != "b" {
   439  		t.Errorf("Got %v,%v expected %v,%v", key, value, 2, "b")
   440  	}
   441  }
   442  
   443  func TestMapSerialization(t *testing.T) {
   444  	for i := 0; i < 10; i++ {
   445  		original := New()
   446  		original.Put("d", "4")
   447  		original.Put("e", "5")
   448  		original.Put("c", "3")
   449  		original.Put("b", "2")
   450  		original.Put("a", "1")
   451  
   452  		assertSerialization(original, "A", t)
   453  
   454  		serialized, err := original.ToJSON()
   455  		if err != nil {
   456  			t.Errorf("Got error %v", err)
   457  		}
   458  		assertSerialization(original, "B", t)
   459  
   460  		deserialized := New()
   461  		err = deserialized.FromJSON(serialized)
   462  		if err != nil {
   463  			t.Errorf("Got error %v", err)
   464  		}
   465  		assertSerialization(deserialized, "C", t)
   466  	}
   467  }
   468  
   469  //noinspection GoBoolExpressions
   470  func assertSerialization(m *Map, txt string, t *testing.T) {
   471  	if actualValue := m.Keys(); false ||
   472  		actualValue[0].(string) != "d" ||
   473  		actualValue[1].(string) != "e" ||
   474  		actualValue[2].(string) != "c" ||
   475  		actualValue[3].(string) != "b" ||
   476  		actualValue[4].(string) != "a" {
   477  		t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[d,e,c,b,a]")
   478  	}
   479  	if actualValue := m.Values(); false ||
   480  		actualValue[0].(string) != "4" ||
   481  		actualValue[1].(string) != "5" ||
   482  		actualValue[2].(string) != "3" ||
   483  		actualValue[3].(string) != "2" ||
   484  		actualValue[4].(string) != "1" {
   485  		t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[4,5,3,2,1]")
   486  	}
   487  	if actualValue, expectedValue := m.Size(), 5; actualValue != expectedValue {
   488  		t.Errorf("[%s] Got %v expected %v", txt, actualValue, expectedValue)
   489  	}
   490  }
   491  
   492  func benchmarkGet(b *testing.B, m *Map, size int) {
   493  	for i := 0; i < b.N; i++ {
   494  		for n := 0; n < size; n++ {
   495  			m.Get(n)
   496  		}
   497  	}
   498  }
   499  
   500  func benchmarkPut(b *testing.B, m *Map, size int) {
   501  	for i := 0; i < b.N; i++ {
   502  		for n := 0; n < size; n++ {
   503  			m.Put(n, struct{}{})
   504  		}
   505  	}
   506  }
   507  
   508  func benchmarkRemove(b *testing.B, m *Map, size int) {
   509  	for i := 0; i < b.N; i++ {
   510  		for n := 0; n < size; n++ {
   511  			m.Remove(n)
   512  		}
   513  	}
   514  }
   515  
   516  func BenchmarkTreeMapGet100(b *testing.B) {
   517  	b.StopTimer()
   518  	size := 100
   519  	m := New()
   520  	for n := 0; n < size; n++ {
   521  		m.Put(n, struct{}{})
   522  	}
   523  	b.StartTimer()
   524  	benchmarkGet(b, m, size)
   525  }
   526  
   527  func BenchmarkTreeMapGet1000(b *testing.B) {
   528  	b.StopTimer()
   529  	size := 1000
   530  	m := New()
   531  	for n := 0; n < size; n++ {
   532  		m.Put(n, struct{}{})
   533  	}
   534  	b.StartTimer()
   535  	benchmarkGet(b, m, size)
   536  }
   537  
   538  func BenchmarkTreeMapGet10000(b *testing.B) {
   539  	b.StopTimer()
   540  	size := 10000
   541  	m := New()
   542  	for n := 0; n < size; n++ {
   543  		m.Put(n, struct{}{})
   544  	}
   545  	b.StartTimer()
   546  	benchmarkGet(b, m, size)
   547  }
   548  
   549  func BenchmarkTreeMapGet100000(b *testing.B) {
   550  	b.StopTimer()
   551  	size := 100000
   552  	m := New()
   553  	for n := 0; n < size; n++ {
   554  		m.Put(n, struct{}{})
   555  	}
   556  	b.StartTimer()
   557  	benchmarkGet(b, m, size)
   558  }
   559  
   560  func BenchmarkTreeMapPut100(b *testing.B) {
   561  	b.StopTimer()
   562  	size := 100
   563  	m := New()
   564  	b.StartTimer()
   565  	benchmarkPut(b, m, size)
   566  }
   567  
   568  func BenchmarkTreeMapPut1000(b *testing.B) {
   569  	b.StopTimer()
   570  	size := 1000
   571  	m := New()
   572  	for n := 0; n < size; n++ {
   573  		m.Put(n, struct{}{})
   574  	}
   575  	b.StartTimer()
   576  	benchmarkPut(b, m, size)
   577  }
   578  
   579  func BenchmarkTreeMapPut10000(b *testing.B) {
   580  	b.StopTimer()
   581  	size := 10000
   582  	m := New()
   583  	for n := 0; n < size; n++ {
   584  		m.Put(n, struct{}{})
   585  	}
   586  	b.StartTimer()
   587  	benchmarkPut(b, m, size)
   588  }
   589  
   590  func BenchmarkTreeMapPut100000(b *testing.B) {
   591  	b.StopTimer()
   592  	size := 100000
   593  	m := New()
   594  	for n := 0; n < size; n++ {
   595  		m.Put(n, struct{}{})
   596  	}
   597  	b.StartTimer()
   598  	benchmarkPut(b, m, size)
   599  }
   600  
   601  func BenchmarkTreeMapRemove100(b *testing.B) {
   602  	b.StopTimer()
   603  	size := 100
   604  	m := New()
   605  	for n := 0; n < size; n++ {
   606  		m.Put(n, struct{}{})
   607  	}
   608  	b.StartTimer()
   609  	benchmarkRemove(b, m, size)
   610  }
   611  
   612  func BenchmarkTreeMapRemove1000(b *testing.B) {
   613  	b.StopTimer()
   614  	size := 1000
   615  	m := New()
   616  	for n := 0; n < size; n++ {
   617  		m.Put(n, struct{}{})
   618  	}
   619  	b.StartTimer()
   620  	benchmarkRemove(b, m, size)
   621  }
   622  
   623  func BenchmarkTreeMapRemove10000(b *testing.B) {
   624  	b.StopTimer()
   625  	size := 10000
   626  	m := New()
   627  	for n := 0; n < size; n++ {
   628  		m.Put(n, struct{}{})
   629  	}
   630  	b.StartTimer()
   631  	benchmarkRemove(b, m, size)
   632  }
   633  
   634  func BenchmarkTreeMapRemove100000(b *testing.B) {
   635  	b.StopTimer()
   636  	size := 100000
   637  	m := New()
   638  	for n := 0; n < size; n++ {
   639  		m.Put(n, struct{}{})
   640  	}
   641  	b.StartTimer()
   642  	benchmarkRemove(b, m, size)
   643  }