@ -6,14 +6,14 @@ package treemap
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/utils"
"strings"
"testing"
"github.com/emirpasic/gods/v2/testutils"
)
func TestMapPut ( t * testing . T ) {
m := NewWith ( utils . IntComparator )
m := New [ int , string ] ( )
m . Put ( 5 , "e" )
m . Put ( 6 , "f" )
m . Put ( 7 , "g" )
@ -26,12 +26,8 @@ func TestMapPut(t *testing.T) {
if actualValue := m . Size ( ) ; actualValue != 7 {
t . Errorf ( "Got %v expected %v" , actualValue , 7 )
}
if actualValue , expectedValue := m . Keys ( ) , [ ] interface { } { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; ! sameElements ( actualValue , expectedValue ) {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
if actualValue , expectedValue := m . Values ( ) , [ ] interface { } { "a" , "b" , "c" , "d" , "e" , "f" , "g" } ; ! sameElements ( actualValue , expectedValue ) {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
testutils . SameElements ( t , m . Keys ( ) , [ ] int { 1 , 2 , 3 , 4 , 5 , 6 , 7 } )
testutils . SameElements ( t , m . Values ( ) , [ ] string { "a" , "b" , "c" , "d" , "e" , "f" , "g" } )
// key,expectedValue,expectedFound
tests1 := [ ] [ ] interface { } {
@ -42,12 +38,12 @@ func TestMapPut(t *testing.T) {
{ 5 , "e" , true } ,
{ 6 , "f" , true } ,
{ 7 , "g" , true } ,
{ 8 , nil , false } ,
{ 8 , "" , false } ,
}
for _ , test := range tests1 {
// retrievals
actualValue , actualFound := m . Get ( test [ 0 ] )
actualValue , actualFound := m . Get ( test [ 0 ] .( int ) )
if actualValue != test [ 1 ] || actualFound != test [ 2 ] {
t . Errorf ( "Got %v expected %v" , actualValue , test [ 1 ] )
}
@ -55,10 +51,10 @@ func TestMapPut(t *testing.T) {
}
func TestMapMin ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
if k , v := m . Min ( ) ; k != nil || v != nil {
t . Errorf ( "Got %v->%v expected %v->%v", k , v , nil , nil )
if k , v , ok := m . Min ( ) ; k != 0 || v != "" || ok {
t . Errorf ( "Got %v->%v ->%v expected %v->%v-%v ", k , v , ok , 0 , "" , false )
}
m . Put ( 5 , "e" )
@ -70,21 +66,24 @@ func TestMapMin(t *testing.T) {
m . Put ( 2 , "b" )
m . Put ( 1 , "a" ) //overwrite
actualKey , actualValue := m . Min ( )
expectedKey , expectedValue := 1 , "a"
actualKey , actualValue , actualOk := m . Min ( )
expectedKey , expectedValue , expectedOk := 1 , "a" , true
if actualKey != expectedKey {
t . Errorf ( "Got %v expected %v" , actualKey , expectedKey )
}
if actualValue != expectedValue {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
if actualOk != expectedOk {
t . Errorf ( "Got %v expected %v" , actualOk , expectedOk )
}
}
func TestMapMax ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
if k , v := m . Max ( ) ; k != nil || v != nil {
t . Errorf ( "Got %v->%v expected %v->%v", k , v , nil , nil )
if k , v , ok := m . Max ( ) ; k != 0 || v != "" || ok {
t . Errorf ( "Got %v->%v ->%v expected %v->%v-%v ", k , v , ok , 0 , "" , false )
}
m . Put ( 5 , "e" )
@ -96,18 +95,21 @@ func TestMapMax(t *testing.T) {
m . Put ( 2 , "b" )
m . Put ( 1 , "a" ) //overwrite
actualKey , actualValue := m . Max ( )
expectedKey , expectedValue := 7 , "g"
actualKey , actualValue , actualOk := m . Max ( )
expectedKey , expectedValue , expectedOk := 7 , "g" , true
if actualKey != expectedKey {
t . Errorf ( "Got %v expected %v" , actualKey , expectedKey )
}
if actualValue != expectedValue {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
if actualOk != expectedOk {
t . Errorf ( "Got %v expected %v" , actualOk , expectedOk )
}
}
func TestMapClear ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 5 , "e" )
m . Put ( 6 , "f" )
m . Put ( 7 , "g" )
@ -122,7 +124,7 @@ func TestMapClear(t *testing.T) {
}
func TestMapRemove ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 5 , "e" )
m . Put ( 6 , "f" )
m . Put ( 7 , "g" )
@ -138,13 +140,9 @@ func TestMapRemove(t *testing.T) {
m . Remove ( 8 )
m . Remove ( 5 )
if actualValue , expectedValue := m . Keys ( ) , [ ] interface { } { 1 , 2 , 3 , 4 } ; ! sameElements ( actualValue , expectedValue ) {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
testutils . SameElements ( t , m . Keys ( ) , [ ] int { 1 , 2 , 3 , 4 } )
testutils . SameElements ( t , m . Values ( ) , [ ] string { "a" , "b" , "c" , "d" } )
if actualValue , expectedValue := m . Values ( ) , [ ] interface { } { "a" , "b" , "c" , "d" } ; ! sameElements ( actualValue , expectedValue ) {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
if actualValue := m . Size ( ) ; actualValue != 4 {
t . Errorf ( "Got %v expected %v" , actualValue , 4 )
}
@ -154,14 +152,14 @@ func TestMapRemove(t *testing.T) {
{ 2 , "b" , true } ,
{ 3 , "c" , true } ,
{ 4 , "d" , true } ,
{ 5 , nil , false } ,
{ 6 , nil , false } ,
{ 7 , nil , false } ,
{ 8 , nil , false } ,
{ 5 , "" , false } ,
{ 6 , "" , false } ,
{ 7 , "" , false } ,
{ 8 , "" , false } ,
}
for _ , test := range tests2 {
actualValue , actualFound := m . Get ( test [ 0 ] )
actualValue , actualFound := m . Get ( test [ 0 ] .( int ) )
if actualValue != test [ 1 ] || actualFound != test [ 2 ] {
t . Errorf ( "Got %v expected %v" , actualValue , test [ 1 ] )
}
@ -174,12 +172,8 @@ func TestMapRemove(t *testing.T) {
m . Remove ( 2 )
m . Remove ( 2 )
if actualValue , expectedValue := fmt . Sprintf ( "%s" , m . Keys ( ) ) , "[]" ; actualValue != expectedValue {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
if actualValue , expectedValue := fmt . Sprintf ( "%s" , m . Values ( ) ) , "[]" ; actualValue != expectedValue {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
}
testutils . SameElements ( t , m . Keys ( ) , nil )
testutils . SameElements ( t , m . Values ( ) , nil )
if actualValue := m . Size ( ) ; actualValue != 0 {
t . Errorf ( "Got %v expected %v" , actualValue , 0 )
}
@ -189,15 +183,15 @@ func TestMapRemove(t *testing.T) {
}
func TestMapFloor ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 7 , "g" )
m . Put ( 3 , "c" )
m . Put ( 1 , "a" )
// key,expectedKey,expectedValue,expectedFound
tests1 := [ ] [ ] interface { } {
{ - 1 , nil , nil , false } ,
{ 0 , nil , nil , false } ,
{ - 1 , 0 , "" , false } ,
{ 0 , 0 , "" , false } ,
{ 1 , 1 , "a" , true } ,
{ 2 , 1 , "a" , true } ,
{ 3 , 3 , "c" , true } ,
@ -208,16 +202,15 @@ func TestMapFloor(t *testing.T) {
for _ , test := range tests1 {
// retrievals
actualKey , actualValue := m . Floor ( test [ 0 ] )
actualFound := actualKey != nil && actualValue != nil
if actualKey != test [ 1 ] || actualValue != test [ 2 ] || actualFound != test [ 3 ] {
t . Errorf ( "Got %v, %v, %v, expected %v, %v, %v" , actualKey , actualValue , actualFound , test [ 1 ] , test [ 2 ] , test [ 3 ] )
actualKey , actualValue , actualOk := m . Floor ( test [ 0 ] . ( int ) )
if actualKey != test [ 1 ] || actualValue != test [ 2 ] || actualOk != test [ 3 ] {
t . Errorf ( "Got %v, %v, %v, expected %v, %v, %v" , actualKey , actualValue , actualOk , test [ 1 ] , test [ 2 ] , test [ 3 ] )
}
}
}
func TestMapCeiling ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 7 , "g" )
m . Put ( 3 , "c" )
m . Put ( 1 , "a" )
@ -231,45 +224,25 @@ func TestMapCeiling(t *testing.T) {
{ 3 , 3 , "c" , true } ,
{ 4 , 7 , "g" , true } ,
{ 7 , 7 , "g" , true } ,
{ 8 , nil , nil , false } ,
{ 8 , 0 , "" , false } ,
}
for _ , test := range tests1 {
// retrievals
actualKey , actualValue := m . Ceiling ( test [ 0 ] )
actualFound := actualKey != nil && actualValue != nil
if actualKey != test [ 1 ] || actualValue != test [ 2 ] || actualFound != test [ 3 ] {
t . Errorf ( "Got %v, %v, %v, expected %v, %v, %v" , actualKey , actualValue , actualFound , test [ 1 ] , test [ 2 ] , test [ 3 ] )
}
}
}
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
actualKey , actualValue , actualOk := m . Ceiling ( test [ 0 ] . ( int ) )
if actualKey != test [ 1 ] || actualValue != test [ 2 ] || actualOk != test [ 3 ] {
t . Errorf ( "Got %v, %v, %v, expected %v, %v, %v" , actualKey , actualValue , actualOk , test [ 1 ] , test [ 2 ] , test [ 3 ] )
}
}
return true
}
func TestMapEach ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
count := 0
m . Each ( func ( key interface { } , value interface { } ) {
m . Each ( func ( key string , value int ) {
count ++
if actualValue , expectedValue := count , value ; actualValue != expectedValue {
t . Errorf ( "Got %v expected %v" , actualValue , expectedValue )
@ -294,12 +267,12 @@ func TestMapEach(t *testing.T) {
}
func TestMapMap ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
mappedMap := m . Map ( func ( key1 interface { } , value1 interface { } ) ( key2 interface { } , value2 interface { } ) {
return key1 , value1 . ( int ) * value1 . ( int )
mappedMap := m . Map ( func ( key1 string , value1 int ) ( key2 string , value2 int ) {
return key1 , value1 * value1
} )
if actualValue , _ := mappedMap . Get ( "a" ) ; actualValue != 1 {
t . Errorf ( "Got %v expected %v" , actualValue , "mapped: a" )
@ -316,12 +289,12 @@ func TestMapMap(t *testing.T) {
}
func TestMapSelect ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
selectedMap := m . Select ( func ( key interface { } , value interface { } ) bool {
return key . ( string ) >= "a" && key . ( string ) <= "b"
selectedMap := m . Select ( func ( key string , value int ) bool {
return key >= "a" && key <= "b"
} )
if actualValue , _ := selectedMap . Get ( "a" ) ; actualValue != 1 {
t . Errorf ( "Got %v expected %v" , actualValue , "value: a" )
@ -335,18 +308,18 @@ func TestMapSelect(t *testing.T) {
}
func TestMapAny ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
any := m . Any ( func ( key interface { } , value interface { } ) bool {
return value . ( int ) == 3
any := m . Any ( func ( key string , value int ) bool {
return value == 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
any = m . Any ( func ( key string , value int ) bool {
return value == 4
} )
if any != false {
t . Errorf ( "Got %v expected %v" , any , false )
@ -354,18 +327,18 @@ func TestMapAny(t *testing.T) {
}
func TestMapAll ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
all := m . All ( func ( key interface { } , value interface { } ) bool {
return key . ( string ) >= "a" && key . ( string ) <= "c"
all := m . All ( func ( key string , value int ) bool {
return key >= "a" && key <= "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"
all = m . All ( func ( key string , value int ) bool {
return key >= "a" && key <= "b"
} )
if all != false {
t . Errorf ( "Got %v expected %v" , all , false )
@ -373,38 +346,38 @@ func TestMapAll(t *testing.T) {
}
func TestMapFind ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
foundKey , foundValue := m . Find ( func ( key interface { } , value interface { } ) bool {
return key . ( string ) == "c"
foundKey , foundValue := m . Find ( func ( key string , value int ) bool {
return key == "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"
foundKey , foundValue = m . Find ( func ( key string , value int ) bool {
return key == "x"
} )
if foundKey != nil || foundValue != nil {
if foundKey != "" || foundValue != 0 {
t . Errorf ( "Got %v at %v expected %v at %v" , foundValue , foundKey , nil , nil )
}
}
func TestMapChaining ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
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 )
chainedMap := m . Select ( func ( key string , value int ) bool {
return value > 1
} ) . Map ( func ( key string , value int ) ( string , int ) {
return key + key , value * value
} )
if actualValue := chainedMap . Size ( ) ; actualValue != 2 {
t . Errorf ( "Got %v expected %v" , actualValue , 2 )
}
if actualValue , found := chainedMap . Get ( "aa" ) ; actualValue != nil || found {
if actualValue , found := chainedMap . Get ( "aa" ) ; actualValue != 0 || found {
t . Errorf ( "Got %v expected %v" , actualValue , nil )
}
if actualValue , found := chainedMap . Get ( "bb" ) ; actualValue != 4 || ! found {
@ -416,7 +389,7 @@ func TestMapChaining(t *testing.T) {
}
func TestMapIteratorNextOnEmpty ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
it := m . Iterator ( )
it = m . Iterator ( )
for it . Next ( ) {
@ -425,7 +398,7 @@ func TestMapIteratorNextOnEmpty(t *testing.T) {
}
func TestMapIteratorPrevOnEmpty ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
it := m . Iterator ( )
it = m . Iterator ( )
for it . Prev ( ) {
@ -434,7 +407,7 @@ func TestMapIteratorPrevOnEmpty(t *testing.T) {
}
func TestMapIteratorNext ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
@ -471,7 +444,7 @@ func TestMapIteratorNext(t *testing.T) {
}
func TestMapIteratorPrev ( t * testing . T ) {
m := NewWithStringComparator ( )
m := New [ string , int ] ( )
m . Put ( "c" , 3 )
m . Put ( "a" , 1 )
m . Put ( "b" , 2 )
@ -510,7 +483,7 @@ func TestMapIteratorPrev(t *testing.T) {
}
func TestMapIteratorBegin ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
it := m . Iterator ( )
it . Begin ( )
m . Put ( 3 , "c" )
@ -526,7 +499,7 @@ func TestMapIteratorBegin(t *testing.T) {
}
func TestMapIteratorEnd ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
it := m . Iterator ( )
m . Put ( 3 , "c" )
m . Put ( 1 , "a" )
@ -539,7 +512,7 @@ func TestMapIteratorEnd(t *testing.T) {
}
func TestMapIteratorFirst ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 3 , "c" )
m . Put ( 1 , "a" )
m . Put ( 2 , "b" )
@ -553,7 +526,7 @@ func TestMapIteratorFirst(t *testing.T) {
}
func TestMapIteratorLast ( t * testing . T ) {
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 3 , "c" )
m . Put ( 1 , "a" )
m . Put ( 2 , "b" )
@ -568,13 +541,13 @@ func TestMapIteratorLast(t *testing.T) {
func TestMapIteratorNextTo ( t * testing . T ) {
// Sample seek function, i.e. string starting with "b"
seek := func ( index interface { } , value interface { } ) bool {
return strings . HasSuffix ( value .( string ) , "b" )
seek := func ( index int , value string ) bool {
return strings . HasSuffix ( value , "b" )
}
// NextTo (empty)
{
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
it := m . Iterator ( )
for it . NextTo ( seek ) {
t . Errorf ( "Shouldn't iterate on empty map" )
@ -583,7 +556,7 @@ func TestMapIteratorNextTo(t *testing.T) {
// NextTo (not found)
{
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 0 , "xx" )
m . Put ( 1 , "yy" )
it := m . Iterator ( )
@ -594,7 +567,7 @@ func TestMapIteratorNextTo(t *testing.T) {
// NextTo (found)
{
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 0 , "aa" )
m . Put ( 1 , "bb" )
m . Put ( 2 , "cc" )
@ -603,13 +576,13 @@ func TestMapIteratorNextTo(t *testing.T) {
if ! it . NextTo ( seek ) {
t . Errorf ( "Shouldn't iterate on empty map" )
}
if index , value := it . Key ( ) , it . Value ( ) ; index != 1 || value . ( string ) != "bb" {
if index , value := it . Key ( ) , it . Value ( ) ; index != 1 || value != "bb" {
t . Errorf ( "Got %v,%v expected %v,%v" , index , value , 1 , "bb" )
}
if ! it . Next ( ) {
t . Errorf ( "Should go to first element" )
}
if index , value := it . Key ( ) , it . Value ( ) ; index != 2 || value . ( string ) != "cc" {
if index , value := it . Key ( ) , it . Value ( ) ; index != 2 || value != "cc" {
t . Errorf ( "Got %v,%v expected %v,%v" , index , value , 2 , "cc" )
}
if it . Next ( ) {
@ -620,13 +593,13 @@ func TestMapIteratorNextTo(t *testing.T) {
func TestMapIteratorPrevTo ( t * testing . T ) {
// Sample seek function, i.e. string starting with "b"
seek := func ( index interface { } , value interface { } ) bool {
return strings . HasSuffix ( value .( string ) , "b" )
seek := func ( index int , value string ) bool {
return strings . HasSuffix ( value , "b" )
}
// PrevTo (empty)
{
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
it := m . Iterator ( )
it . End ( )
for it . PrevTo ( seek ) {
@ -636,7 +609,7 @@ func TestMapIteratorPrevTo(t *testing.T) {
// PrevTo (not found)
{
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 0 , "xx" )
m . Put ( 1 , "yy" )
it := m . Iterator ( )
@ -648,7 +621,7 @@ func TestMapIteratorPrevTo(t *testing.T) {
// PrevTo (found)
{
m := NewWithIntComparator ( )
m := New [ int , string ] ( )
m . Put ( 0 , "aa" )
m . Put ( 1 , "bb" )
m . Put ( 2 , "cc" )
@ -657,13 +630,13 @@ func TestMapIteratorPrevTo(t *testing.T) {
if ! it . PrevTo ( seek ) {
t . Errorf ( "Shouldn't iterate on empty map" )
}
if index , value := it . Key ( ) , it . Value ( ) ; index != 1 || value . ( string ) != "bb" {
if index , value := it . Key ( ) , it . Value ( ) ; index != 1 || value != "bb" {
t . Errorf ( "Got %v,%v expected %v,%v" , index , value , 1 , "bb" )
}
if ! it . Prev ( ) {
t . Errorf ( "Should go to first element" )
}
if index , value := it . Key ( ) , it . Value ( ) ; index != 0 || value . ( string ) != "aa" {
if index , value := it . Key ( ) , it . Value ( ) ; index != 0 || value != "aa" {
t . Errorf ( "Got %v,%v expected %v,%v" , index , value , 0 , "aa" )
}
if it . Prev ( ) {
@ -674,7 +647,7 @@ func TestMapIteratorPrevTo(t *testing.T) {
func TestMapSerialization ( t * testing . T ) {
for i := 0 ; i < 10 ; i ++ {
original := NewWithStringComparator ( )
original := New [ string , string ] ( )
original . Put ( "d" , "4" )
original . Put ( "e" , "5" )
original . Put ( "c" , "3" )
@ -689,7 +662,7 @@ func TestMapSerialization(t *testing.T) {
}
assertSerialization ( original , "B" , t )
deserialized := NewWithStringComparator ( )
deserialized := New [ string , string ] ( )
err = deserialized . FromJSON ( serialized )
if err != nil {
t . Errorf ( "Got error %v" , err )
@ -697,7 +670,7 @@ func TestMapSerialization(t *testing.T) {
assertSerialization ( deserialized , "C" , t )
}
m := NewWithStringComparator ( )
m := New [ string , float64 ] ( )
m . Put ( "a" , 1.0 )
m . Put ( "b" , 2.0 )
m . Put ( "c" , 3.0 )
@ -714,29 +687,29 @@ func TestMapSerialization(t *testing.T) {
}
func TestMapString ( t * testing . T ) {
c := NewWithStringComparator ( )
c := New [ string , int ] ( )
c . Put ( "a" , 1 )
if ! strings . HasPrefix ( c . String ( ) , "TreeMap" ) {
t . Errorf ( "String should start with container name" )
}
}
// noinspection GoBoolExpressions
func assertSerialization ( m * Map , txt string , t * testing . T ) {
// noinspection GoBoolExpressions
func assertSerialization ( m * Map [string , string ] , txt string , t * testing . T ) {
if actualValue := m . Keys ( ) ; false ||
actualValue [ 0 ] . ( string ) != "a" ||
actualValue [ 1 ] . ( string ) != "b" ||
actualValue [ 2 ] . ( string ) != "c" ||
actualValue [ 3 ] . ( string ) != "d" ||
actualValue [ 4 ] . ( string ) != "e" {
actualValue [ 0 ] != "a" ||
actualValue [ 1 ] != "b" ||
actualValue [ 2 ] != "c" ||
actualValue [ 3 ] != "d" ||
actualValue [ 4 ] != "e" {
t . Errorf ( "[%s] Got %v expected %v" , txt , actualValue , "[a,b,c,d,e]" )
}
if actualValue := m . Values ( ) ; false ||
actualValue [ 0 ] . ( string ) != "1" ||
actualValue [ 1 ] . ( string ) != "2" ||
actualValue [ 2 ] . ( string ) != "3" ||
actualValue [ 3 ] . ( string ) != "4" ||
actualValue [ 4 ] . ( string ) != "5" {
actualValue [ 0 ] != "1" ||
actualValue [ 1 ] != "2" ||
actualValue [ 2 ] != "3" ||
actualValue [ 3 ] != "4" ||
actualValue [ 4 ] != "5" {
t . Errorf ( "[%s] Got %v expected %v" , txt , actualValue , "[1,2,3,4,5]" )
}
if actualValue , expectedValue := m . Size ( ) , 5 ; actualValue != expectedValue {
@ -744,7 +717,7 @@ func assertSerialization(m *Map, txt string, t *testing.T) {
}
}
func benchmarkGet ( b * testing . B , m * Map , size int ) {
func benchmarkGet ( b * testing . B , m * Map [int , struct { } ] , size int ) {
for i := 0 ; i < b . N ; i ++ {
for n := 0 ; n < size ; n ++ {
m . Get ( n )
@ -752,7 +725,7 @@ func benchmarkGet(b *testing.B, m *Map, size int) {
}
}
func benchmarkPut ( b * testing . B , m * Map , size int ) {
func benchmarkPut ( b * testing . B , m * Map [int , struct { } ] , size int ) {
for i := 0 ; i < b . N ; i ++ {
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
@ -760,7 +733,7 @@ func benchmarkPut(b *testing.B, m *Map, size int) {
}
}
func benchmarkRemove ( b * testing . B , m * Map , size int ) {
func benchmarkRemove ( b * testing . B , m * Map [int , struct { } ] , size int ) {
for i := 0 ; i < b . N ; i ++ {
for n := 0 ; n < size ; n ++ {
m . Remove ( n )
@ -771,7 +744,7 @@ func benchmarkRemove(b *testing.B, m *Map, size int) {
func BenchmarkTreeMapGet100 ( b * testing . B ) {
b . StopTimer ( )
size := 100
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -782,7 +755,7 @@ func BenchmarkTreeMapGet100(b *testing.B) {
func BenchmarkTreeMapGet1000 ( b * testing . B ) {
b . StopTimer ( )
size := 1000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -793,7 +766,7 @@ func BenchmarkTreeMapGet1000(b *testing.B) {
func BenchmarkTreeMapGet10000 ( b * testing . B ) {
b . StopTimer ( )
size := 10000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -804,7 +777,7 @@ func BenchmarkTreeMapGet10000(b *testing.B) {
func BenchmarkTreeMapGet100000 ( b * testing . B ) {
b . StopTimer ( )
size := 100000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -815,7 +788,7 @@ func BenchmarkTreeMapGet100000(b *testing.B) {
func BenchmarkTreeMapPut100 ( b * testing . B ) {
b . StopTimer ( )
size := 100
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
b . StartTimer ( )
benchmarkPut ( b , m , size )
}
@ -823,7 +796,7 @@ func BenchmarkTreeMapPut100(b *testing.B) {
func BenchmarkTreeMapPut1000 ( b * testing . B ) {
b . StopTimer ( )
size := 1000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -834,7 +807,7 @@ func BenchmarkTreeMapPut1000(b *testing.B) {
func BenchmarkTreeMapPut10000 ( b * testing . B ) {
b . StopTimer ( )
size := 10000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -845,7 +818,7 @@ func BenchmarkTreeMapPut10000(b *testing.B) {
func BenchmarkTreeMapPut100000 ( b * testing . B ) {
b . StopTimer ( )
size := 100000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -856,7 +829,7 @@ func BenchmarkTreeMapPut100000(b *testing.B) {
func BenchmarkTreeMapRemove100 ( b * testing . B ) {
b . StopTimer ( )
size := 100
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -867,7 +840,7 @@ func BenchmarkTreeMapRemove100(b *testing.B) {
func BenchmarkTreeMapRemove1000 ( b * testing . B ) {
b . StopTimer ( )
size := 1000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -878,7 +851,7 @@ func BenchmarkTreeMapRemove1000(b *testing.B) {
func BenchmarkTreeMapRemove10000 ( b * testing . B ) {
b . StopTimer ( )
size := 10000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}
@ -889,7 +862,7 @@ func BenchmarkTreeMapRemove10000(b *testing.B) {
func BenchmarkTreeMapRemove100000 ( b * testing . B ) {
b . StopTimer ( )
size := 100000
m := NewWithIntComparator ( )
m := New [ int , struct { } ] ( )
for n := 0 ; n < size ; n ++ {
m . Put ( n , struct { } { } )
}