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