From 363df0e21f5de1efcb84b9836b6fab859c25f154 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Wed, 13 Apr 2022 14:44:56 +0200 Subject: [PATCH 1/3] Comparator tests --- utils/comparator_test.go | 195 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) diff --git a/utils/comparator_test.go b/utils/comparator_test.go index 40efbd3..356c5e2 100644 --- a/utils/comparator_test.go +++ b/utils/comparator_test.go @@ -110,3 +110,198 @@ func TestCustomComparator(t *testing.T) { } } } + +func TestInt8ComparatorComparator(t *testing.T) { + tests := [][]interface{}{ + {int8(1), int8(1), 0}, + {int8(0), int8(1), -1}, + {int8(1), int8(0), 1}, + } + for _, test := range tests { + actual := Int8Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestInt16Comparator(t *testing.T) { + tests := [][]interface{}{ + {int16(1), int16(1), 0}, + {int16(0), int16(1), -1}, + {int16(1), int16(0), 1}, + } + for _, test := range tests { + actual := Int16Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestInt32Comparator(t *testing.T) { + tests := [][]interface{}{ + {int32(1), int32(1), 0}, + {int32(0), int32(1), -1}, + {int32(1), int32(0), 1}, + } + for _, test := range tests { + actual := Int32Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestInt64Comparator(t *testing.T) { + tests := [][]interface{}{ + {int64(1), int64(1), 0}, + {int64(0), int64(1), -1}, + {int64(1), int64(0), 1}, + } + for _, test := range tests { + actual := Int64Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestUIntComparator(t *testing.T) { + tests := [][]interface{}{ + {uint(1), uint(1), 0}, + {uint(0), uint(1), -1}, + {uint(1), uint(0), 1}, + } + for _, test := range tests { + actual := UIntComparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestUInt8Comparator(t *testing.T) { + tests := [][]interface{}{ + {uint8(1), uint8(1), 0}, + {uint8(0), uint8(1), -1}, + {uint8(1), uint8(0), 1}, + } + for _, test := range tests { + actual := UInt8Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestUInt16Comparator(t *testing.T) { + tests := [][]interface{}{ + {uint16(1), uint16(1), 0}, + {uint16(0), uint16(1), -1}, + {uint16(1), uint16(0), 1}, + } + for _, test := range tests { + actual := UInt16Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestUInt32Comparator(t *testing.T) { + tests := [][]interface{}{ + {uint32(1), uint32(1), 0}, + {uint32(0), uint32(1), -1}, + {uint32(1), uint32(0), 1}, + } + for _, test := range tests { + actual := UInt32Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestUInt64Comparator(t *testing.T) { + tests := [][]interface{}{ + {uint64(1), uint64(1), 0}, + {uint64(0), uint64(1), -1}, + {uint64(1), uint64(0), 1}, + } + for _, test := range tests { + actual := UInt64Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestFloat32Comparator(t *testing.T) { + tests := [][]interface{}{ + {float32(1.1), float32(1.1), 0}, + {float32(0.1), float32(1.1), -1}, + {float32(1.1), float32(0.1), 1}, + } + for _, test := range tests { + actual := Float32Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestFloat64Comparator(t *testing.T) { + tests := [][]interface{}{ + {float64(1.1), float64(1.1), 0}, + {float64(0.1), float64(1.1), -1}, + {float64(1.1), float64(0.1), 1}, + } + for _, test := range tests { + actual := Float64Comparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestByteComparator(t *testing.T) { + tests := [][]interface{}{ + {byte(1), byte(1), 0}, + {byte(0), byte(1), -1}, + {byte(1), byte(0), 1}, + } + for _, test := range tests { + actual := ByteComparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} + +func TestRuneComparator(t *testing.T) { + tests := [][]interface{}{ + {rune(1), rune(1), 0}, + {rune(0), rune(1), -1}, + {rune(1), rune(0), 1}, + } + for _, test := range tests { + actual := RuneComparator(test[0], test[1]) + expected := test[2] + if actual != expected { + t.Errorf("Got %v expected %v", actual, expected) + } + } +} From e2b92bbc7ab90ec26e625d7c81e736f166800678 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Wed, 13 Apr 2022 15:04:39 +0200 Subject: [PATCH 2/3] Interface implementation assertions moved outside the functions --- lists/arraylist/arraylist.go | 5 ++--- lists/arraylist/enumerable.go | 5 ++--- lists/arraylist/iterator.go | 5 ++--- lists/arraylist/serialization.go | 7 +++---- lists/doublylinkedlist/doublylinkedlist.go | 5 ++--- lists/doublylinkedlist/enumerable.go | 5 ++--- lists/doublylinkedlist/iterator.go | 5 ++--- lists/doublylinkedlist/serialization.go | 7 +++---- lists/singlylinkedlist/enumerable.go | 5 ++--- lists/singlylinkedlist/iterator.go | 5 ++--- lists/singlylinkedlist/serialization.go | 7 +++---- lists/singlylinkedlist/singlylinkedlist.go | 5 ++--- maps/hashbidimap/hashbidimap.go | 5 ++--- maps/hashbidimap/serialization.go | 7 +++---- maps/hashmap/hashmap.go | 5 ++--- maps/hashmap/serialization.go | 7 +++---- maps/linkedhashmap/enumerable.go | 5 ++--- maps/linkedhashmap/iterator.go | 5 ++--- maps/linkedhashmap/linkedhashmap.go | 5 ++--- maps/linkedhashmap/serialization.go | 7 +++---- maps/treebidimap/enumerable.go | 5 ++--- maps/treebidimap/iterator.go | 5 ++--- maps/treebidimap/serialization.go | 7 +++---- maps/treebidimap/treebidimap.go | 5 ++--- maps/treemap/enumerable.go | 5 ++--- maps/treemap/iterator.go | 5 ++--- maps/treemap/serialization.go | 7 +++---- maps/treemap/treemap.go | 5 ++--- sets/hashset/hashset.go | 5 ++--- sets/hashset/serialization.go | 7 +++---- sets/linkedhashset/enumerable.go | 5 ++--- sets/linkedhashset/iterator.go | 5 ++--- sets/linkedhashset/linkedhashset.go | 5 ++--- sets/linkedhashset/serialization.go | 7 +++---- sets/treeset/enumerable.go | 5 ++--- sets/treeset/iterator.go | 5 ++--- sets/treeset/serialization.go | 7 +++---- sets/treeset/treeset.go | 5 ++--- stacks/arraystack/arraystack.go | 5 ++--- stacks/arraystack/iterator.go | 5 ++--- stacks/arraystack/serialization.go | 7 +++---- stacks/linkedliststack/iterator.go | 5 ++--- stacks/linkedliststack/linkedliststack.go | 5 ++--- stacks/linkedliststack/serialization.go | 7 +++---- trees/avltree/avltree.go | 5 ++--- trees/avltree/iterator.go | 5 ++--- trees/avltree/serialization.go | 7 +++---- trees/binaryheap/binaryheap.go | 5 ++--- trees/binaryheap/iterator.go | 5 ++--- trees/binaryheap/serialization.go | 7 +++---- trees/btree/btree.go | 5 ++--- trees/btree/iterator.go | 5 ++--- trees/btree/serialization.go | 7 +++---- trees/redblacktree/iterator.go | 5 ++--- trees/redblacktree/redblacktree.go | 5 ++--- trees/redblacktree/serialization.go | 7 +++---- 56 files changed, 129 insertions(+), 185 deletions(-) diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index bfedac9..41327bb 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -17,9 +17,8 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertListImplementation() { - var _ lists.List = (*List)(nil) -} +// Assert List implementation +var _ lists.List = (*List)(nil) // List holds the elements in a slice type List struct { diff --git a/lists/arraylist/enumerable.go b/lists/arraylist/enumerable.go index b3a8738..8bd60b0 100644 --- a/lists/arraylist/enumerable.go +++ b/lists/arraylist/enumerable.go @@ -6,9 +6,8 @@ package arraylist import "github.com/emirpasic/gods/containers" -func assertEnumerableImplementation() { - var _ containers.EnumerableWithIndex = (*List)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithIndex = (*List)(nil) // Each calls the given function once for each element, passing that element's index and value. func (list *List) Each(f func(index int, value interface{})) { diff --git a/lists/arraylist/iterator.go b/lists/arraylist/iterator.go index 2fefbb6..f9efe20 100644 --- a/lists/arraylist/iterator.go +++ b/lists/arraylist/iterator.go @@ -6,9 +6,8 @@ package arraylist import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/lists/arraylist/serialization.go b/lists/arraylist/serialization.go index 402298f..5e86fe9 100644 --- a/lists/arraylist/serialization.go +++ b/lists/arraylist/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*List)(nil) - var _ containers.JSONDeserializer = (*List)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*List)(nil) +var _ containers.JSONDeserializer = (*List)(nil) // ToJSON outputs the JSON representation of list's elements. func (list *List) ToJSON() ([]byte, error) { diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index 9035e38..ab63de4 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -17,9 +17,8 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertListImplementation() { - var _ lists.List = (*List)(nil) -} +// Assert List implementation +var _ lists.List = (*List)(nil) // List holds the elements, where each element points to the next and previous element type List struct { diff --git a/lists/doublylinkedlist/enumerable.go b/lists/doublylinkedlist/enumerable.go index e6cf60f..4b14a47 100644 --- a/lists/doublylinkedlist/enumerable.go +++ b/lists/doublylinkedlist/enumerable.go @@ -6,9 +6,8 @@ package doublylinkedlist import "github.com/emirpasic/gods/containers" -func assertEnumerableImplementation() { - var _ containers.EnumerableWithIndex = (*List)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithIndex = (*List)(nil) // Each calls the given function once for each element, passing that element's index and value. func (list *List) Each(f func(index int, value interface{})) { diff --git a/lists/doublylinkedlist/iterator.go b/lists/doublylinkedlist/iterator.go index 55fb0bc..27b34c2 100644 --- a/lists/doublylinkedlist/iterator.go +++ b/lists/doublylinkedlist/iterator.go @@ -6,9 +6,8 @@ package doublylinkedlist import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/lists/doublylinkedlist/serialization.go b/lists/doublylinkedlist/serialization.go index ce0fb01..f210f9a 100644 --- a/lists/doublylinkedlist/serialization.go +++ b/lists/doublylinkedlist/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*List)(nil) - var _ containers.JSONDeserializer = (*List)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*List)(nil) +var _ containers.JSONDeserializer = (*List)(nil) // ToJSON outputs the JSON representation of list's elements. func (list *List) ToJSON() ([]byte, error) { diff --git a/lists/singlylinkedlist/enumerable.go b/lists/singlylinkedlist/enumerable.go index 61ac289..6fdbcb8 100644 --- a/lists/singlylinkedlist/enumerable.go +++ b/lists/singlylinkedlist/enumerable.go @@ -6,9 +6,8 @@ package singlylinkedlist import "github.com/emirpasic/gods/containers" -func assertEnumerableImplementation() { - var _ containers.EnumerableWithIndex = (*List)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithIndex = (*List)(nil) // Each calls the given function once for each element, passing that element's index and value. func (list *List) Each(f func(index int, value interface{})) { diff --git a/lists/singlylinkedlist/iterator.go b/lists/singlylinkedlist/iterator.go index 5f25ff1..4e7f177 100644 --- a/lists/singlylinkedlist/iterator.go +++ b/lists/singlylinkedlist/iterator.go @@ -6,9 +6,8 @@ package singlylinkedlist import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.IteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.IteratorWithIndex = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/lists/singlylinkedlist/serialization.go b/lists/singlylinkedlist/serialization.go index adcc575..588a316 100644 --- a/lists/singlylinkedlist/serialization.go +++ b/lists/singlylinkedlist/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*List)(nil) - var _ containers.JSONDeserializer = (*List)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*List)(nil) +var _ containers.JSONDeserializer = (*List)(nil) // ToJSON outputs the JSON representation of list's elements. func (list *List) ToJSON() ([]byte, error) { diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index b61df78..c3e2c67 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -17,9 +17,8 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertListImplementation() { - var _ lists.List = (*List)(nil) -} +// Assert List implementation +var _ lists.List = (*List)(nil) // List holds the elements, where each element points to the next element type List struct { diff --git a/maps/hashbidimap/hashbidimap.go b/maps/hashbidimap/hashbidimap.go index 32985ab..5a386ec 100644 --- a/maps/hashbidimap/hashbidimap.go +++ b/maps/hashbidimap/hashbidimap.go @@ -21,9 +21,8 @@ import ( "github.com/emirpasic/gods/maps/hashmap" ) -func assertMapImplementation() { - var _ maps.BidiMap = (*Map)(nil) -} +// Assert Map implementation +var _ maps.BidiMap = (*Map)(nil) // Map holds the elements in two hashmaps. type Map struct { diff --git a/maps/hashbidimap/serialization.go b/maps/hashbidimap/serialization.go index dff7801..dfae043 100644 --- a/maps/hashbidimap/serialization.go +++ b/maps/hashbidimap/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Map)(nil) - var _ containers.JSONDeserializer = (*Map)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Map)(nil) +var _ containers.JSONDeserializer = (*Map)(nil) // ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { diff --git a/maps/hashmap/hashmap.go b/maps/hashmap/hashmap.go index 3f42ffc..e945c39 100644 --- a/maps/hashmap/hashmap.go +++ b/maps/hashmap/hashmap.go @@ -16,9 +16,8 @@ import ( "github.com/emirpasic/gods/maps" ) -func assertMapImplementation() { - var _ maps.Map = (*Map)(nil) -} +// Assert Map implementation +var _ maps.Map = (*Map)(nil) // Map holds the elements in go's native map type Map struct { diff --git a/maps/hashmap/serialization.go b/maps/hashmap/serialization.go index dc2e259..a86fd86 100644 --- a/maps/hashmap/serialization.go +++ b/maps/hashmap/serialization.go @@ -10,10 +10,9 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Map)(nil) - var _ containers.JSONDeserializer = (*Map)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Map)(nil) +var _ containers.JSONDeserializer = (*Map)(nil) // ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { diff --git a/maps/linkedhashmap/enumerable.go b/maps/linkedhashmap/enumerable.go index 644b00f..eafcaa5 100644 --- a/maps/linkedhashmap/enumerable.go +++ b/maps/linkedhashmap/enumerable.go @@ -6,9 +6,8 @@ package linkedhashmap import "github.com/emirpasic/gods/containers" -func assertEnumerableImplementation() { - var _ containers.EnumerableWithKey = (*Map)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithKey = (*Map)(nil) // Each calls the given function once for each element, passing that element's key and value. func (m *Map) Each(f func(key interface{}, value interface{})) { diff --git a/maps/linkedhashmap/iterator.go b/maps/linkedhashmap/iterator.go index 17f3bb3..4c14178 100644 --- a/maps/linkedhashmap/iterator.go +++ b/maps/linkedhashmap/iterator.go @@ -9,9 +9,8 @@ import ( "github.com/emirpasic/gods/lists/doublylinkedlist" ) -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/maps/linkedhashmap/linkedhashmap.go b/maps/linkedhashmap/linkedhashmap.go index 45c4e35..d625b3d 100644 --- a/maps/linkedhashmap/linkedhashmap.go +++ b/maps/linkedhashmap/linkedhashmap.go @@ -18,9 +18,8 @@ import ( "strings" ) -func assertMapImplementation() { - var _ maps.Map = (*Map)(nil) -} +// Assert Map implementation +var _ maps.Map = (*Map)(nil) // Map holds the elements in a regular hash table, and uses doubly-linked list to store key ordering. type Map struct { diff --git a/maps/linkedhashmap/serialization.go b/maps/linkedhashmap/serialization.go index 805421d..9265f1d 100644 --- a/maps/linkedhashmap/serialization.go +++ b/maps/linkedhashmap/serialization.go @@ -11,10 +11,9 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Map)(nil) - var _ containers.JSONDeserializer = (*Map)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Map)(nil) +var _ containers.JSONDeserializer = (*Map)(nil) // ToJSON outputs the JSON representation of map. func (m *Map) ToJSON() ([]byte, error) { diff --git a/maps/treebidimap/enumerable.go b/maps/treebidimap/enumerable.go index d5d829a..8daef72 100644 --- a/maps/treebidimap/enumerable.go +++ b/maps/treebidimap/enumerable.go @@ -6,9 +6,8 @@ package treebidimap import "github.com/emirpasic/gods/containers" -func assertEnumerableImplementation() { - var _ containers.EnumerableWithKey = (*Map)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithKey = (*Map)(nil) // Each calls the given function once for each element, passing that element's key and value. func (m *Map) Each(f func(key interface{}, value interface{})) { diff --git a/maps/treebidimap/iterator.go b/maps/treebidimap/iterator.go index 72e9983..9961a11 100644 --- a/maps/treebidimap/iterator.go +++ b/maps/treebidimap/iterator.go @@ -9,9 +9,8 @@ import ( rbt "github.com/emirpasic/gods/trees/redblacktree" ) -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/maps/treebidimap/serialization.go b/maps/treebidimap/serialization.go index e7ee0c2..2cccce6 100644 --- a/maps/treebidimap/serialization.go +++ b/maps/treebidimap/serialization.go @@ -10,10 +10,9 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Map)(nil) - var _ containers.JSONDeserializer = (*Map)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Map)(nil) +var _ containers.JSONDeserializer = (*Map)(nil) // ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { diff --git a/maps/treebidimap/treebidimap.go b/maps/treebidimap/treebidimap.go index 87eff9f..37af07e 100644 --- a/maps/treebidimap/treebidimap.go +++ b/maps/treebidimap/treebidimap.go @@ -25,9 +25,8 @@ import ( "strings" ) -func assertMapImplementation() { - var _ maps.BidiMap = (*Map)(nil) -} +// Assert Map implementation +var _ maps.BidiMap = (*Map)(nil) // Map holds the elements in two red-black trees. type Map struct { diff --git a/maps/treemap/enumerable.go b/maps/treemap/enumerable.go index 8cea6d0..34b3704 100644 --- a/maps/treemap/enumerable.go +++ b/maps/treemap/enumerable.go @@ -9,9 +9,8 @@ import ( rbt "github.com/emirpasic/gods/trees/redblacktree" ) -func assertEnumerableImplementation() { - var _ containers.EnumerableWithKey = (*Map)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithKey = (*Map)(nil) // Each calls the given function once for each element, passing that element's key and value. func (m *Map) Each(f func(key interface{}, value interface{})) { diff --git a/maps/treemap/iterator.go b/maps/treemap/iterator.go index e7950a1..becb56d 100644 --- a/maps/treemap/iterator.go +++ b/maps/treemap/iterator.go @@ -9,9 +9,8 @@ import ( rbt "github.com/emirpasic/gods/trees/redblacktree" ) -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/maps/treemap/serialization.go b/maps/treemap/serialization.go index 619a6a7..415a77d 100644 --- a/maps/treemap/serialization.go +++ b/maps/treemap/serialization.go @@ -8,10 +8,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Map)(nil) - var _ containers.JSONDeserializer = (*Map)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Map)(nil) +var _ containers.JSONDeserializer = (*Map)(nil) // ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index a8e1dc5..a77d16d 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -19,9 +19,8 @@ import ( "strings" ) -func assertMapImplementation() { - var _ maps.Map = (*Map)(nil) -} +// Assert Map implementation +var _ maps.Map = (*Map)(nil) // Map holds the elements in a red-black tree type Map struct { diff --git a/sets/hashset/hashset.go b/sets/hashset/hashset.go index 558e628..9439928 100644 --- a/sets/hashset/hashset.go +++ b/sets/hashset/hashset.go @@ -15,9 +15,8 @@ import ( "strings" ) -func assertSetImplementation() { - var _ sets.Set = (*Set)(nil) -} +// Assert Set implementation +var _ sets.Set = (*Set)(nil) // Set holds elements in go's native map type Set struct { diff --git a/sets/hashset/serialization.go b/sets/hashset/serialization.go index b0353e8..583d129 100644 --- a/sets/hashset/serialization.go +++ b/sets/hashset/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Set)(nil) - var _ containers.JSONDeserializer = (*Set)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Set)(nil) +var _ containers.JSONDeserializer = (*Set)(nil) // ToJSON outputs the JSON representation of the set. func (set *Set) ToJSON() ([]byte, error) { diff --git a/sets/linkedhashset/enumerable.go b/sets/linkedhashset/enumerable.go index ad6ac96..fc85572 100644 --- a/sets/linkedhashset/enumerable.go +++ b/sets/linkedhashset/enumerable.go @@ -6,9 +6,8 @@ package linkedhashset import "github.com/emirpasic/gods/containers" -func assertEnumerableImplementation() { - var _ containers.EnumerableWithIndex = (*Set)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithIndex = (*Set)(nil) // Each calls the given function once for each element, passing that element's index and value. func (set *Set) Each(f func(index int, value interface{})) { diff --git a/sets/linkedhashset/iterator.go b/sets/linkedhashset/iterator.go index 5aa176c..aa79384 100644 --- a/sets/linkedhashset/iterator.go +++ b/sets/linkedhashset/iterator.go @@ -9,9 +9,8 @@ import ( "github.com/emirpasic/gods/lists/doublylinkedlist" ) -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/sets/linkedhashset/linkedhashset.go b/sets/linkedhashset/linkedhashset.go index e028591..3bf4e5f 100644 --- a/sets/linkedhashset/linkedhashset.go +++ b/sets/linkedhashset/linkedhashset.go @@ -20,9 +20,8 @@ import ( "strings" ) -func assertSetImplementation() { - var _ sets.Set = (*Set)(nil) -} +// Assert Set implementation +var _ sets.Set = (*Set)(nil) // Set holds elements in go's native map type Set struct { diff --git a/sets/linkedhashset/serialization.go b/sets/linkedhashset/serialization.go index 33b3fef..ab2f3b4 100644 --- a/sets/linkedhashset/serialization.go +++ b/sets/linkedhashset/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Set)(nil) - var _ containers.JSONDeserializer = (*Set)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Set)(nil) +var _ containers.JSONDeserializer = (*Set)(nil) // ToJSON outputs the JSON representation of the set. func (set *Set) ToJSON() ([]byte, error) { diff --git a/sets/treeset/enumerable.go b/sets/treeset/enumerable.go index 59a0913..c774834 100644 --- a/sets/treeset/enumerable.go +++ b/sets/treeset/enumerable.go @@ -9,9 +9,8 @@ import ( rbt "github.com/emirpasic/gods/trees/redblacktree" ) -func assertEnumerableImplementation() { - var _ containers.EnumerableWithIndex = (*Set)(nil) -} +// Assert Enumerable implementation +var _ containers.EnumerableWithIndex = (*Set)(nil) // Each calls the given function once for each element, passing that element's index and value. func (set *Set) Each(f func(index int, value interface{})) { diff --git a/sets/treeset/iterator.go b/sets/treeset/iterator.go index 377d8b6..88a0bea 100644 --- a/sets/treeset/iterator.go +++ b/sets/treeset/iterator.go @@ -9,9 +9,8 @@ import ( rbt "github.com/emirpasic/gods/trees/redblacktree" ) -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) // Iterator returns a stateful iterator whose values can be fetched by an index. type Iterator struct { diff --git a/sets/treeset/serialization.go b/sets/treeset/serialization.go index a17eb78..76d049d 100644 --- a/sets/treeset/serialization.go +++ b/sets/treeset/serialization.go @@ -9,10 +9,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Set)(nil) - var _ containers.JSONDeserializer = (*Set)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Set)(nil) +var _ containers.JSONDeserializer = (*Set)(nil) // ToJSON outputs the JSON representation of the set. func (set *Set) ToJSON() ([]byte, error) { diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index 7e7d1d6..3507cc9 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -18,9 +18,8 @@ import ( "strings" ) -func assertSetImplementation() { - var _ sets.Set = (*Set)(nil) -} +// Assert Set implementation +var _ sets.Set = (*Set)(nil) // Set holds elements in a red-black tree type Set struct { diff --git a/stacks/arraystack/arraystack.go b/stacks/arraystack/arraystack.go index 9a971e9..78c3dda 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -16,9 +16,8 @@ import ( "strings" ) -func assertStackImplementation() { - var _ stacks.Stack = (*Stack)(nil) -} +// Assert Stack implementation +var _ stacks.Stack = (*Stack)(nil) // Stack holds elements in an array-list type Stack struct { diff --git a/stacks/arraystack/iterator.go b/stacks/arraystack/iterator.go index df592f0..c01d7c8 100644 --- a/stacks/arraystack/iterator.go +++ b/stacks/arraystack/iterator.go @@ -6,9 +6,8 @@ package arraystack import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) // Iterator returns a stateful iterator whose values can be fetched by an index. type Iterator struct { diff --git a/stacks/arraystack/serialization.go b/stacks/arraystack/serialization.go index 1456c9e..e65889f 100644 --- a/stacks/arraystack/serialization.go +++ b/stacks/arraystack/serialization.go @@ -8,10 +8,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Stack)(nil) - var _ containers.JSONDeserializer = (*Stack)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Stack)(nil) +var _ containers.JSONDeserializer = (*Stack)(nil) // ToJSON outputs the JSON representation of the stack. func (stack *Stack) ToJSON() ([]byte, error) { diff --git a/stacks/linkedliststack/iterator.go b/stacks/linkedliststack/iterator.go index 875f922..dea086b 100644 --- a/stacks/linkedliststack/iterator.go +++ b/stacks/linkedliststack/iterator.go @@ -6,9 +6,8 @@ package linkedliststack import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.IteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.IteratorWithIndex = (*Iterator)(nil) // Iterator returns a stateful iterator whose values can be fetched by an index. type Iterator struct { diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index da5bf2f..ce69b21 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -16,9 +16,8 @@ import ( "strings" ) -func assertStackImplementation() { - var _ stacks.Stack = (*Stack)(nil) -} +// Assert Stack implementation +var _ stacks.Stack = (*Stack)(nil) // Stack holds elements in a singly-linked-list type Stack struct { diff --git a/stacks/linkedliststack/serialization.go b/stacks/linkedliststack/serialization.go index 3beb7a8..a82b768 100644 --- a/stacks/linkedliststack/serialization.go +++ b/stacks/linkedliststack/serialization.go @@ -8,10 +8,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Stack)(nil) - var _ containers.JSONDeserializer = (*Stack)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Stack)(nil) +var _ containers.JSONDeserializer = (*Stack)(nil) // ToJSON outputs the JSON representation of the stack. func (stack *Stack) ToJSON() ([]byte, error) { diff --git a/trees/avltree/avltree.go b/trees/avltree/avltree.go index fd1a405..128d313 100644 --- a/trees/avltree/avltree.go +++ b/trees/avltree/avltree.go @@ -15,9 +15,8 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertTreeImplementation() { - var _ trees.Tree = new(Tree) -} +// Assert Tree implementation +var _ trees.Tree = new(Tree) // Tree holds elements of the AVL tree. type Tree struct { diff --git a/trees/avltree/iterator.go b/trees/avltree/iterator.go index c4d69f4..0186e40 100644 --- a/trees/avltree/iterator.go +++ b/trees/avltree/iterator.go @@ -6,9 +6,8 @@ package avltree import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/trees/avltree/serialization.go b/trees/avltree/serialization.go index 1ccd4a8..257c404 100644 --- a/trees/avltree/serialization.go +++ b/trees/avltree/serialization.go @@ -10,10 +10,9 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Tree)(nil) - var _ containers.JSONDeserializer = (*Tree)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Tree)(nil) +var _ containers.JSONDeserializer = (*Tree)(nil) // ToJSON outputs the JSON representation of the tree. func (tree *Tree) ToJSON() ([]byte, error) { diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index 70b28cf..b3412c5 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -19,9 +19,8 @@ import ( "strings" ) -func assertTreeImplementation() { - var _ trees.Tree = (*Heap)(nil) -} +// Assert Tree implementation +var _ trees.Tree = (*Heap)(nil) // Heap holds elements in an array-list type Heap struct { diff --git a/trees/binaryheap/iterator.go b/trees/binaryheap/iterator.go index 343fa52..8a01b05 100644 --- a/trees/binaryheap/iterator.go +++ b/trees/binaryheap/iterator.go @@ -6,9 +6,8 @@ package binaryheap import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) // Iterator returns a stateful iterator whose values can be fetched by an index. type Iterator struct { diff --git a/trees/binaryheap/serialization.go b/trees/binaryheap/serialization.go index 30ccb25..c1fce0a 100644 --- a/trees/binaryheap/serialization.go +++ b/trees/binaryheap/serialization.go @@ -8,10 +8,9 @@ import ( "github.com/emirpasic/gods/containers" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Heap)(nil) - var _ containers.JSONDeserializer = (*Heap)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Heap)(nil) +var _ containers.JSONDeserializer = (*Heap)(nil) // ToJSON outputs the JSON representation of the heap. func (heap *Heap) ToJSON() ([]byte, error) { diff --git a/trees/btree/btree.go b/trees/btree/btree.go index 523358a..bb36f93 100644 --- a/trees/btree/btree.go +++ b/trees/btree/btree.go @@ -24,9 +24,8 @@ import ( "strings" ) -func assertTreeImplementation() { - var _ trees.Tree = (*Tree)(nil) -} +// Assert Tree implementation +var _ trees.Tree = (*Tree)(nil) // Tree holds elements of the B-tree type Tree struct { diff --git a/trees/btree/iterator.go b/trees/btree/iterator.go index c74b646..fb20955 100644 --- a/trees/btree/iterator.go +++ b/trees/btree/iterator.go @@ -6,9 +6,8 @@ package btree import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/trees/btree/serialization.go b/trees/btree/serialization.go index 5ff7175..460f6e0 100644 --- a/trees/btree/serialization.go +++ b/trees/btree/serialization.go @@ -10,10 +10,9 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Tree)(nil) - var _ containers.JSONDeserializer = (*Tree)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Tree)(nil) +var _ containers.JSONDeserializer = (*Tree)(nil) // ToJSON outputs the JSON representation of the tree. func (tree *Tree) ToJSON() ([]byte, error) { diff --git a/trees/redblacktree/iterator.go b/trees/redblacktree/iterator.go index ce9bcaf..e39da7d 100644 --- a/trees/redblacktree/iterator.go +++ b/trees/redblacktree/iterator.go @@ -6,9 +6,8 @@ package redblacktree import "github.com/emirpasic/gods/containers" -func assertIteratorImplementation() { - var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) -} +// Assert Iterator implementation +var _ containers.ReverseIteratorWithKey = (*Iterator)(nil) // Iterator holding the iterator's state type Iterator struct { diff --git a/trees/redblacktree/redblacktree.go b/trees/redblacktree/redblacktree.go index cce9fe0..b335e3d 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -17,9 +17,8 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertTreeImplementation() { - var _ trees.Tree = (*Tree)(nil) -} +// Assert Tree implementation +var _ trees.Tree = (*Tree)(nil) type color bool diff --git a/trees/redblacktree/serialization.go b/trees/redblacktree/serialization.go index da13777..9f2a23c 100644 --- a/trees/redblacktree/serialization.go +++ b/trees/redblacktree/serialization.go @@ -10,10 +10,9 @@ import ( "github.com/emirpasic/gods/utils" ) -func assertSerializationImplementation() { - var _ containers.JSONSerializer = (*Tree)(nil) - var _ containers.JSONDeserializer = (*Tree)(nil) -} +// Assert Serialization implementation +var _ containers.JSONSerializer = (*Tree)(nil) +var _ containers.JSONDeserializer = (*Tree)(nil) // ToJSON outputs the JSON representation of the tree. func (tree *Tree) ToJSON() ([]byte, error) { From 41012c6c58efc30f16d70f087c04c10255cbf8cf Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Wed, 13 Apr 2022 16:52:21 +0200 Subject: [PATCH 3/3] Improve code coverage --- containers/containers_test.go | 2 + lists/arraylist/arraylist_test.go | 13 +++ .../doublylinkedlist/doublylinkedlist_test.go | 35 ++++++++ .../singlylinkedlist/singlylinkedlist_test.go | 35 ++++++++ maps/hashbidimap/hashbidimap_test.go | 14 ++++ maps/hashmap/hashmap_test.go | 14 ++++ maps/linkedhashmap/linkedhashmap_test.go | 13 +++ maps/treebidimap/treebidimap_test.go | 13 +++ maps/treemap/treemap_test.go | 83 ++++++++++++++++++- sets/hashset/hashset_test.go | 14 ++++ sets/linkedhashset/linkedhashset_test.go | 13 +++ sets/treeset/treeset_test.go | 13 +++ stacks/arraystack/arraystack_test.go | 19 +++++ .../linkedliststack/linkedliststack_test.go | 13 +++ trees/avltree/avltree_test.go | 26 +++++- trees/binaryheap/binaryheap_test.go | 13 +++ trees/btree/btree_test.go | 13 +++ trees/redblacktree/redblacktree_test.go | 16 +++- 18 files changed, 359 insertions(+), 3 deletions(-) diff --git a/containers/containers_test.go b/containers/containers_test.go index 60e0332..e92d123 100644 --- a/containers/containers_test.go +++ b/containers/containers_test.go @@ -46,6 +46,7 @@ func (container ContainerTest) String() string { func TestGetSortedValuesInts(t *testing.T) { container := ContainerTest{} + GetSortedValues(container, utils.IntComparator) container.values = []interface{}{5, 1, 3, 2, 4} values := GetSortedValues(container, utils.IntComparator) for i := 1; i < container.Size(); i++ { @@ -57,6 +58,7 @@ func TestGetSortedValuesInts(t *testing.T) { func TestGetSortedValuesStrings(t *testing.T) { container := ContainerTest{} + GetSortedValues(container, utils.StringComparator) container.values = []interface{}{"g", "a", "d", "e", "f", "c", "b"} values := GetSortedValues(container, utils.StringComparator) for i := 1; i < container.Size(); i++ { diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index 2a704d1..d490ba7 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -631,6 +631,19 @@ func TestListSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &list) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestListString(t *testing.T) { + c := New() + c.Add(1) + if !strings.HasPrefix(c.String(), "ArrayList") { + t.Errorf("String should start with container name") + } } func benchmarkGet(b *testing.B, list *List, size int) { diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index 911da77..a69c699 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -54,6 +54,28 @@ func TestListAdd(t *testing.T) { } } +func TestListAppendAndPrepend(t *testing.T) { + list := New() + list.Add("b") + list.Prepend("a") + list.Append("c") + if actualValue := list.Empty(); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) + } + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + if actualValue, ok := list.Get(0); actualValue != "a" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } + if actualValue, ok := list.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } + if actualValue, ok := list.Get(2); actualValue != "c" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} + func TestListRemove(t *testing.T) { list := New() list.Add("a") @@ -637,6 +659,19 @@ func TestListSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &list) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestListString(t *testing.T) { + c := New() + c.Add(1) + if !strings.HasPrefix(c.String(), "DoublyLinkedList") { + t.Errorf("String should start with container name") + } } func benchmarkGet(b *testing.B, list *List, size int) { diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 97eb78b..4d58b7d 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -54,6 +54,28 @@ func TestListAdd(t *testing.T) { } } +func TestListAppendAndPrepend(t *testing.T) { + list := New() + list.Add("b") + list.Prepend("a") + list.Append("c") + if actualValue := list.Empty(); actualValue != false { + t.Errorf("Got %v expected %v", actualValue, false) + } + if actualValue := list.Size(); actualValue != 3 { + t.Errorf("Got %v expected %v", actualValue, 3) + } + if actualValue, ok := list.Get(0); actualValue != "a" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } + if actualValue, ok := list.Get(1); actualValue != "b" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } + if actualValue, ok := list.Get(2); actualValue != "c" || !ok { + t.Errorf("Got %v expected %v", actualValue, "c") + } +} + func TestListRemove(t *testing.T) { list := New() list.Add("a") @@ -500,6 +522,19 @@ func TestListSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &list) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestListString(t *testing.T) { + c := New() + c.Add(1) + if !strings.HasPrefix(c.String(), "SinglyLinkedList") { + t.Errorf("String should start with container name") + } } func benchmarkGet(b *testing.B, list *List, size int) { diff --git a/maps/hashbidimap/hashbidimap_test.go b/maps/hashbidimap/hashbidimap_test.go index 8b607f4..dd91116 100644 --- a/maps/hashbidimap/hashbidimap_test.go +++ b/maps/hashbidimap/hashbidimap_test.go @@ -7,6 +7,7 @@ package hashbidimap import ( "encoding/json" "fmt" + "strings" "testing" ) @@ -185,6 +186,19 @@ func TestMapSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestMapString(t *testing.T) { + c := New() + c.Put("a", 1) + if !strings.HasPrefix(c.String(), "HashBidiMap") { + t.Errorf("String should start with container name") + } } func sameElements(a []interface{}, b []interface{}) bool { diff --git a/maps/hashmap/hashmap_test.go b/maps/hashmap/hashmap_test.go index 1496680..91acca8 100644 --- a/maps/hashmap/hashmap_test.go +++ b/maps/hashmap/hashmap_test.go @@ -7,6 +7,7 @@ package hashmap import ( "encoding/json" "fmt" + "strings" "testing" ) @@ -153,6 +154,19 @@ func TestMapSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestMapString(t *testing.T) { + c := New() + c.Put("a", 1) + if !strings.HasPrefix(c.String(), "HashMap") { + t.Errorf("String should start with container name") + } } func sameElements(a []interface{}, b []interface{}) bool { diff --git a/maps/linkedhashmap/linkedhashmap_test.go b/maps/linkedhashmap/linkedhashmap_test.go index ac7b994..ca44792 100644 --- a/maps/linkedhashmap/linkedhashmap_test.go +++ b/maps/linkedhashmap/linkedhashmap_test.go @@ -582,6 +582,19 @@ func TestMapSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestMapString(t *testing.T) { + c := New() + c.Put("a", 1) + if !strings.HasPrefix(c.String(), "LinkedHashMap") { + t.Errorf("String should start with container name") + } } //noinspection GoBoolExpressions diff --git a/maps/treebidimap/treebidimap_test.go b/maps/treebidimap/treebidimap_test.go index acd3270..75296e8 100644 --- a/maps/treebidimap/treebidimap_test.go +++ b/maps/treebidimap/treebidimap_test.go @@ -615,6 +615,19 @@ func TestMapSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestMapString(t *testing.T) { + c := NewWithStringComparators() + c.Put("a", "a") + if !strings.HasPrefix(c.String(), "TreeBidiMap") { + t.Errorf("String should start with container name") + } } //noinspection GoBoolExpressions diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index 92e9612..f0c96ba 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -7,12 +7,13 @@ package treemap import ( "encoding/json" "fmt" + "github.com/emirpasic/gods/utils" "strings" "testing" ) func TestMapPut(t *testing.T) { - m := NewWithIntComparator() + m := NewWith(utils.IntComparator) m.Put(5, "e") m.Put(6, "f") m.Put(7, "g") @@ -53,6 +54,73 @@ func TestMapPut(t *testing.T) { } } +func TestMapMin(t *testing.T) { + m := NewWithIntComparator() + + if k, v := m.Min(); k != nil || v != nil { + t.Errorf("Got %v->%v expected %v->%v", k, v, nil, nil) + } + + 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 + + actualKey, actualValue := m.Min() + expectedKey, expectedValue := 1, "a" + if actualKey != expectedKey { + t.Errorf("Got %v expected %v", actualKey, expectedKey) + } + if actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } +} + +func TestMapMax(t *testing.T) { + m := NewWithIntComparator() + + if k, v := m.Max(); k != nil || v != nil { + t.Errorf("Got %v->%v expected %v->%v", k, v, nil, nil) + } + + 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 + + actualKey, actualValue := m.Max() + expectedKey, expectedValue := 7, "g" + if actualKey != expectedKey { + t.Errorf("Got %v expected %v", actualKey, expectedKey) + } + if actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } +} + +func TestMapClear(t *testing.T) { + m := NewWithIntComparator() + m.Put(5, "e") + m.Put(6, "f") + m.Put(7, "g") + m.Put(3, "c") + if actualValue, expectedValue := m.Size(), 4; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + m.Clear() + if actualValue, expectedValue := m.Size(), 0; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } +} + func TestMapRemove(t *testing.T) { m := NewWithIntComparator() m.Put(5, "e") @@ -638,6 +706,19 @@ func TestMapSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestMapString(t *testing.T) { + c := NewWithStringComparator() + c.Put("a", 1) + if !strings.HasPrefix(c.String(), "TreeMap") { + t.Errorf("String should start with container name") + } } //noinspection GoBoolExpressions diff --git a/sets/hashset/hashset_test.go b/sets/hashset/hashset_test.go index ccb50b3..fe516b7 100644 --- a/sets/hashset/hashset_test.go +++ b/sets/hashset/hashset_test.go @@ -6,6 +6,7 @@ package hashset import ( "encoding/json" + "strings" "testing" ) @@ -109,6 +110,19 @@ func TestSetSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &set) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestSetString(t *testing.T) { + c := New() + c.Add(1) + if !strings.HasPrefix(c.String(), "HashSet") { + t.Errorf("String should start with container name") + } } func TestSetIntersection(t *testing.T) { diff --git a/sets/linkedhashset/linkedhashset_test.go b/sets/linkedhashset/linkedhashset_test.go index 7e3c236..3f857ea 100644 --- a/sets/linkedhashset/linkedhashset_test.go +++ b/sets/linkedhashset/linkedhashset_test.go @@ -463,6 +463,19 @@ func TestSetSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &set) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestSetString(t *testing.T) { + c := New() + c.Add(1) + if !strings.HasPrefix(c.String(), "LinkedHashSet") { + t.Errorf("String should start with container name") + } } func TestSetIntersection(t *testing.T) { diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index 2839d4b..9e7c670 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -472,6 +472,19 @@ func TestSetSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`["1","2","3"]`), &set) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestSetString(t *testing.T) { + c := NewWithIntComparator() + c.Add(1) + if !strings.HasPrefix(c.String(), "TreeSet") { + t.Errorf("String should start with container name") + } } func TestSetIntersection(t *testing.T) { diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index d15e7eb..eba4287 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -116,6 +116,12 @@ func TestStackIteratorNext(t *testing.T) { if actualValue, expectedValue := count, 3; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } + + stack.Clear() + it = stack.Iterator() + for it.Next() { + t.Errorf("Shouldn't iterate on empty stack") + } } func TestStackIteratorPrev(t *testing.T) { @@ -371,6 +377,19 @@ func TestStackSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &stack) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestStackString(t *testing.T) { + c := New() + c.Push(1) + if !strings.HasPrefix(c.String(), "ArrayStack") { + t.Errorf("String should start with container name") + } } func benchmarkPush(b *testing.B, stack *Stack, size int) { diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index 6b34e39..f491fd3 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -233,6 +233,19 @@ func TestStackSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &stack) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestStackString(t *testing.T) { + c := New() + c.Push(1) + if !strings.HasPrefix(c.String(), "LinkedListStack") { + t.Errorf("String should start with container name") + } } func benchmarkPush(b *testing.B, stack *Stack, size int) { diff --git a/trees/avltree/avltree_test.go b/trees/avltree/avltree_test.go index 37bdd2f..ce27d6a 100644 --- a/trees/avltree/avltree_test.go +++ b/trees/avltree/avltree_test.go @@ -6,6 +6,7 @@ package avltree import ( "encoding/json" "fmt" + "github.com/emirpasic/gods/utils" "strings" "testing" ) @@ -263,6 +264,7 @@ func TestAVLTreeIterator1Next(t *testing.T) { // └── 2 // └── 1 it := tree.Iterator() + count := 0 for it.Next() { count++ @@ -709,7 +711,8 @@ func TestAVLTreeIteratorPrevTo(t *testing.T) { } func TestAVLTreeSerialization(t *testing.T) { - tree := NewWithStringComparator() + tree := NewWith(utils.StringComparator) + tree = NewWithStringComparator() tree.Put("c", "3") tree.Put("b", "2") tree.Put("a", "1") @@ -742,6 +745,27 @@ func TestAVLTreeSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &tree) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestAVLTreeString(t *testing.T) { + c := NewWithIntComparator() + c.Put(1, 1) + c.Put(2, 1) + c.Put(3, 1) + c.Put(4, 1) + c.Put(5, 1) + c.Put(6, 1) + c.Put(7, 1) + c.Put(8, 1) + + if !strings.HasPrefix(c.String(), "AVLTree") { + t.Errorf("String should start with container name") + } } func benchmarkGet(b *testing.B, tree *Tree, size int) { diff --git a/trees/binaryheap/binaryheap_test.go b/trees/binaryheap/binaryheap_test.go index e14794f..26d99ea 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -402,6 +402,19 @@ func TestBinaryHeapSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`[1,2,3]`), &heap) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestBTreeString(t *testing.T) { + c := NewWithIntComparator() + c.Push(1) + if !strings.HasPrefix(c.String(), "BinaryHeap") { + t.Errorf("String should start with container name") + } } func benchmarkPush(b *testing.B, heap *Heap, size int) { diff --git a/trees/btree/btree_test.go b/trees/btree/btree_test.go index 49c6ab5..f8b6e61 100644 --- a/trees/btree/btree_test.go +++ b/trees/btree/btree_test.go @@ -1262,6 +1262,19 @@ func TestBTreeSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &tree) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestBTreeString(t *testing.T) { + c := NewWithStringComparator(3) + c.Put("a", 1) + if !strings.HasPrefix(c.String(), "BTree") { + t.Errorf("String should start with container name") + } } func benchmarkGet(b *testing.B, tree *Tree, size int) { diff --git a/trees/redblacktree/redblacktree_test.go b/trees/redblacktree/redblacktree_test.go index c717648..5de39b6 100644 --- a/trees/redblacktree/redblacktree_test.go +++ b/trees/redblacktree/redblacktree_test.go @@ -7,6 +7,7 @@ package redblacktree import ( "encoding/json" "fmt" + "github.com/emirpasic/gods/utils" "strings" "testing" ) @@ -200,7 +201,7 @@ func TestRedBlackTreeLeftAndRight(t *testing.T) { } func TestRedBlackTreeCeilingAndFloor(t *testing.T) { - tree := NewWithIntComparator() + tree := NewWith(utils.IntComparator) if node, found := tree.Floor(0); node != nil || found { t.Errorf("Got %v expected %v", node, "") @@ -745,6 +746,19 @@ func TestRedBlackTreeSerialization(t *testing.T) { if err != nil { t.Errorf("Got error %v", err) } + + err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &tree) + if err != nil { + t.Errorf("Got error %v", err) + } +} + +func TestRedBlackTreeString(t *testing.T) { + c := NewWithStringComparator() + c.Put("a", 1) + if !strings.HasPrefix(c.String(), "RedBlackTree") { + t.Errorf("String should start with container name") + } } func benchmarkGet(b *testing.B, tree *Tree, size int) {