Improve code coverage

pull/197/head
Emir Pasic 2 years ago
parent e2b92bbc7a
commit 41012c6c58

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save