gods/maps/treemap/treemap_test.go

360 lines
9.8 KiB
Go
Raw Normal View History

2015-03-05 17:11:14 +00:00
/*
Copyright (c) 2015, Emir Pasic
All rights reserved.
2015-03-05 17:11:14 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
2015-03-05 17:11:14 +00:00
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2015-03-05 17:11:14 +00:00
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2015-03-05 17:11:14 +00:00
*/
package treemap
import (
"fmt"
"testing"
)
2016-06-24 17:38:16 +00:00
func TestMapPut(t *testing.T) {
2015-03-05 17:11:14 +00:00
m := NewWithIntComparator()
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-24 17:38:16 +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:11:14 +00:00
}
2016-06-24 17:38:16 +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:11:14 +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 := NewWithIntComparator()
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:11:14 +00:00
m.Remove(5)
m.Remove(6)
m.Remove(7)
m.Remove(8)
m.Remove(5)
2016-06-24 17:38:16 +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:11:14 +00:00
}
2016-06-24 17:38:16 +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:11:14 +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:11:14 +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)
if actualValue, expectedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
2015-03-05 17:11:14 +00:00
}
if actualValue, expectedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
2015-03-05 17:11:14 +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)
}
2016-06-24 17:38:16 +00:00
}
2015-03-05 17:11:14 +00:00
2016-06-24 17:38:16 +00:00
func sameElements(a []interface{}, b []interface{}) bool {
if len(a) != len(b) {
return false
}
2016-06-24 17:38:16 +00:00
for _, av := range a {
found := false
for _, bv := range b {
if av == bv {
found = true
break
}
}
if !found {
return false
}
}
2016-06-24 17:38:16 +00:00
return true
2015-03-05 17:11:14 +00:00
}
2016-06-24 17:38:16 +00:00
func TestMapEach(t *testing.T) {
2016-06-22 18:56:18 +00:00
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
count := 0
m.Each(func(key interface{}, value interface{}) {
2016-06-24 19:52:16 +00:00
count++
2016-06-22 19:30:00 +00:00
if actualValue, expectedValue := count, value; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
switch value {
case 1:
if actualValue, expectedValue := key, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := key, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 3:
if actualValue, expectedValue := key, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
})
2016-06-24 17:38:16 +00:00
}
2016-06-22 19:30:00 +00:00
2016-06-24 17:38:16 +00:00
func TestMapMap(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
mappedMap := m.Map(func(key1 interface{}, value1 interface{}) (key2 interface{}, value2 interface{}) {
return key1, "mapped: " + key1.(string)
})
2016-06-22 19:30:00 +00:00
if actualValue, _ := mappedMap.Get("a"); actualValue != "mapped: a" {
t.Errorf("Got %v expected %v", actualValue, "mapped: a")
}
if actualValue, _ := mappedMap.Get("b"); actualValue != "mapped: b" {
t.Errorf("Got %v expected %v", actualValue, "mapped: b")
}
if actualValue, _ := mappedMap.Get("c"); actualValue != "mapped: c" {
t.Errorf("Got %v expected %v", actualValue, "mapped: c")
}
if mappedMap.Size() != 3 {
t.Errorf("Got %v expected %v", mappedMap.Size(), 3)
}
2016-06-24 17:38:16 +00:00
}
2016-06-22 19:30:00 +00:00
2016-06-24 17:38:16 +00:00
func TestMapSelect(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
selectedMap := m.Select(func(key interface{}, value interface{}) bool {
return key.(string) >= "a" && key.(string) <= "b"
})
2016-06-22 19:30:00 +00:00
if actualValue, _ := selectedMap.Get("a"); actualValue != 1 {
t.Errorf("Got %v expected %v", actualValue, "value: a")
}
if actualValue, _ := selectedMap.Get("b"); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, "value: b")
}
if selectedMap.Size() != 2 {
2016-06-24 17:38:16 +00:00
t.Errorf("Got %v expected %v", selectedMap.Size(), 2)
2016-06-22 19:30:00 +00:00
}
2016-06-24 17:38:16 +00:00
}
2016-06-22 19:30:00 +00:00
2016-06-24 17:38:16 +00:00
func TestMapAny(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
any := m.Any(func(key interface{}, value interface{}) bool {
return value.(int) == 3
})
if any != true {
t.Errorf("Got %v expected %v", any, true)
}
any = m.Any(func(key interface{}, value interface{}) bool {
return value.(int) == 4
})
if any != false {
t.Errorf("Got %v expected %v", any, false)
}
2016-06-24 17:38:16 +00:00
}
2016-06-22 19:30:00 +00:00
2016-06-24 17:38:16 +00:00
func TestMapAll(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
all := m.All(func(key interface{}, value interface{}) bool {
return key.(string) >= "a" && key.(string) <= "c"
})
if all != true {
t.Errorf("Got %v expected %v", all, true)
}
all = m.All(func(key interface{}, value interface{}) bool {
return key.(string) >= "a" && key.(string) <= "b"
})
if all != false {
t.Errorf("Got %v expected %v", all, false)
}
2016-06-24 17:38:16 +00:00
}
2016-06-22 19:30:00 +00:00
2016-06-24 17:38:16 +00:00
func TestMapFind(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool {
return key.(string) == "c"
})
if foundKey != "c" || foundValue != 3 {
t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3)
}
foundKey, foundValue = m.Find(func(key interface{}, value interface{}) bool {
return key.(string) == "x"
})
if foundKey != nil || foundValue != nil {
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, nil, nil)
}
2016-06-24 17:38:16 +00:00
}
func TestMapChaining(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
chainedMap := m.Select(func(key interface{}, value interface{}) bool {
return value.(int) > 1
}).Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
return key.(string) + key.(string), value.(int) * value.(int)
})
if actualValue := chainedMap.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, found := chainedMap.Get("aa"); actualValue != nil || found {
t.Errorf("Got %v expected %v", actualValue, nil)
}
if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found {
t.Errorf("Got %v expected %v", actualValue, 4)
}
if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found {
t.Errorf("Got %v expected %v", actualValue, 9)
}
}
func TestMapIterator(t *testing.T) {
m := NewWithStringComparator()
m.Put("c", 3)
m.Put("a", 1)
m.Put("b", 2)
2016-06-22 19:30:00 +00:00
2016-06-22 18:56:18 +00:00
it := m.Iterator()
for it.Next() {
2016-06-22 19:30:00 +00:00
key := it.Key()
2016-06-22 18:56:18 +00:00
value := it.Value()
2016-06-22 19:30:00 +00:00
switch key {
case "a":
if actualValue, expectedValue := value, 1; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case "b":
if actualValue, expectedValue := value, 2; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case "c":
if actualValue, expectedValue := value, 3; actualValue != expectedValue {
2016-06-22 18:56:18 +00:00
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
}
2016-06-24 17:38:16 +00:00
2016-06-22 18:56:18 +00:00
m.Clear()
it = m.Iterator()
for it.Next() {
t.Errorf("Shouldn't iterate on empty map")
}
}
2016-06-24 17:38:16 +00:00
func BenchmarkMap(b *testing.B) {
for i := 0; i < b.N; i++ {
m := NewWithIntComparator()
for n := 0; n < 1000; n++ {
m.Put(n, n)
}
for n := 0; n < 1000; n++ {
m.Remove(n)
}
}
}