Merge pull request #197 from emirpasic/development

Improve code coverage
pull/199/head
Emir Pasic 2 years ago committed by GitHub
commit d953513772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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++ {

@ -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 {

@ -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) {

@ -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{})) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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{})) {

@ -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 {

@ -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) {

@ -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{})) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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 {

@ -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) {

@ -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 {

@ -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 {

@ -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) {

@ -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{})) {

@ -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 {

@ -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 {

@ -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

@ -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) {

@ -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{})) {

@ -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 {

@ -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) {

@ -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 {

@ -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

@ -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{})) {

@ -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 {

@ -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) {

@ -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 {

@ -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

@ -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 {

@ -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) {

@ -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) {

@ -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{})) {

@ -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 {

@ -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 {

@ -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) {

@ -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) {

@ -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{})) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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 {

@ -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) {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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) {

@ -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 {

@ -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

@ -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, "<nil>")
@ -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) {

@ -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) {

@ -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)
}
}
}

Loading…
Cancel
Save