2016-06-27 02:21:09 +00:00
|
|
|
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2015-03-05 17:46:42 +00:00
|
|
|
|
|
|
|
package hashmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-06-24 17:38:16 +00:00
|
|
|
func TestMapPut(t *testing.T) {
|
2015-03-05 17:46:42 +00:00
|
|
|
m := New()
|
|
|
|
m.Put(5, "e")
|
|
|
|
m.Put(6, "f")
|
|
|
|
m.Put(7, "g")
|
|
|
|
m.Put(3, "c")
|
|
|
|
m.Put(4, "d")
|
|
|
|
m.Put(1, "x")
|
|
|
|
m.Put(2, "b")
|
|
|
|
m.Put(1, "a") //overwrite
|
|
|
|
|
|
|
|
if actualValue := m.Size(); actualValue != 7 {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, 7)
|
|
|
|
}
|
2016-06-12 13:04:33 +00:00
|
|
|
if actualValue, expectedValue := m.Keys(), []interface{}{1, 2, 3, 4, 5, 6, 7}; !sameElements(actualValue, expectedValue) {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
2016-06-12 13:04:33 +00:00
|
|
|
if actualValue, expectedValue := m.Values(), []interface{}{"a", "b", "c", "d", "e", "f", "g"}; !sameElements(actualValue, expectedValue) {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// key,expectedValue,expectedFound
|
|
|
|
tests1 := [][]interface{}{
|
|
|
|
{1, "a", true},
|
|
|
|
{2, "b", true},
|
|
|
|
{3, "c", true},
|
|
|
|
{4, "d", true},
|
|
|
|
{5, "e", true},
|
|
|
|
{6, "f", true},
|
|
|
|
{7, "g", true},
|
|
|
|
{8, nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests1 {
|
|
|
|
// retrievals
|
|
|
|
actualValue, actualFound := m.Get(test[0])
|
|
|
|
if actualValue != test[1] || actualFound != test[2] {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, test[1])
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 17:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapRemove(t *testing.T) {
|
|
|
|
m := New()
|
|
|
|
m.Put(5, "e")
|
|
|
|
m.Put(6, "f")
|
|
|
|
m.Put(7, "g")
|
|
|
|
m.Put(3, "c")
|
|
|
|
m.Put(4, "d")
|
|
|
|
m.Put(1, "x")
|
|
|
|
m.Put(2, "b")
|
|
|
|
m.Put(1, "a") //overwrite
|
2015-03-05 17:46:42 +00:00
|
|
|
|
|
|
|
m.Remove(5)
|
|
|
|
m.Remove(6)
|
|
|
|
m.Remove(7)
|
|
|
|
m.Remove(8)
|
|
|
|
m.Remove(5)
|
|
|
|
|
2016-06-12 13:04:33 +00:00
|
|
|
if actualValue, expectedValue := m.Keys(), []interface{}{1, 2, 3, 4}; !sameElements(actualValue, expectedValue) {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 13:04:33 +00:00
|
|
|
if actualValue, expectedValue := m.Values(), []interface{}{"a", "b", "c", "d"}; !sameElements(actualValue, expectedValue) {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
|
|
|
if actualValue := m.Size(); actualValue != 4 {
|
2016-06-24 17:38:16 +00:00
|
|
|
t.Errorf("Got %v expected %v", actualValue, 4)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests2 := [][]interface{}{
|
|
|
|
{1, "a", true},
|
|
|
|
{2, "b", true},
|
|
|
|
{3, "c", true},
|
|
|
|
{4, "d", true},
|
|
|
|
{5, nil, false},
|
|
|
|
{6, nil, false},
|
|
|
|
{7, nil, false},
|
|
|
|
{8, nil, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests2 {
|
|
|
|
actualValue, actualFound := m.Get(test[0])
|
|
|
|
if actualValue != test[1] || actualFound != test[2] {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, test[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Remove(1)
|
|
|
|
m.Remove(4)
|
|
|
|
m.Remove(2)
|
|
|
|
m.Remove(3)
|
|
|
|
m.Remove(2)
|
|
|
|
m.Remove(2)
|
|
|
|
|
2016-06-12 13:04:33 +00:00
|
|
|
if actualValue, expectedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
2016-06-12 13:04:33 +00:00
|
|
|
if actualValue, expectedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expectedValue {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
2015-03-05 17:46:42 +00:00
|
|
|
}
|
|
|
|
if actualValue := m.Size(); actualValue != 0 {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, 0)
|
|
|
|
}
|
|
|
|
if actualValue := m.Empty(); actualValue != true {
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameElements(a []interface{}, b []interface{}) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, av := range a {
|
|
|
|
found := false
|
|
|
|
for _, bv := range b {
|
|
|
|
if av == bv {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2015-03-07 16:09:47 +00:00
|
|
|
|
2016-07-16 02:40:16 +00:00
|
|
|
func benchmarkGet(b *testing.B, m *Map, size int) {
|
2015-03-07 16:09:47 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-07-16 02:40:16 +00:00
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Get(n)
|
2015-03-07 16:09:47 +00:00
|
|
|
}
|
2016-07-16 02:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkPut(b *testing.B, m *Map, size int) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkRemove(b *testing.B, m *Map, size int) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for n := 0; n < size; n++ {
|
2015-03-07 16:09:47 +00:00
|
|
|
m.Remove(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 02:40:16 +00:00
|
|
|
|
|
|
|
func BenchmarkHashMapGet100(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 100
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkGet(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapGet1000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 1000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkGet(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapGet10000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 10000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkGet(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapGet100000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 100000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkGet(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapPut100(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 100
|
|
|
|
m := New()
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkPut(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapPut1000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 1000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkPut(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapPut10000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 10000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkPut(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapPut100000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 100000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkPut(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapRemove100(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 100
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkRemove(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapRemove1000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 1000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkRemove(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapRemove10000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 10000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkRemove(b, m, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHashMapRemove100000(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
size := 100000
|
|
|
|
m := New()
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
m.Put(n, struct{}{})
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
benchmarkRemove(b, m, size)
|
|
|
|
}
|