Skip to content

Commit

Permalink
Merge pull request #88 from emirpasic/development
Browse files Browse the repository at this point in the history
List bulk initialization
  • Loading branch information
emirpasic authored Sep 20, 2018
2 parents 370f7ab + c663034 commit 5ef8ba6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lists/arraylist/arraylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ const (
shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
)

// New instantiates a new empty list
func New() *List {
return &List{}
// New instantiates a new list and adds the passed values, if any, to the list
func New(values ...interface{}) *List {
list := &List{}
if len(values) > 0 {
list.Add(values...)
}
return list
}

// Add appends a value at the end of the list
Expand Down
29 changes: 27 additions & 2 deletions lists/arraylist/arraylist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,36 @@ package arraylist

import (
"fmt"
"testing"

"github.com/emirpasic/gods/utils"
"testing"
)

func TestListNew(t *testing.T) {
list1 := New()

if actualValue := list1.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}

list2 := New(1, "b")

if actualValue := list2.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}

if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}

if actualValue, ok := list2.Get(1); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}

if actualValue, ok := list2.Get(2); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, nil)
}
}

func TestListAdd(t *testing.T) {
list := New()
list.Add("a")
Expand Down
10 changes: 7 additions & 3 deletions lists/doublylinkedlist/doublylinkedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ type element struct {
next *element
}

// New instantiates a new empty list
func New() *List {
return &List{}
// New instantiates a new list and adds the passed values, if any, to the list
func New(values ...interface{}) *List {
list := &List{}
if len(values) > 0 {
list.Add(values...)
}
return list
}

// Add appends a value (one or more) at the end of the list (same as Append())
Expand Down
26 changes: 26 additions & 0 deletions lists/doublylinkedlist/doublylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ import (
"github.com/emirpasic/gods/utils"
)

func TestListNew(t *testing.T) {
list1 := New()

if actualValue := list1.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}

list2 := New(1, "b")

if actualValue := list2.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}

if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}

if actualValue, ok := list2.Get(1); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}

if actualValue, ok := list2.Get(2); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, nil)
}
}

func TestListAdd(t *testing.T) {
list := New()
list.Add("a")
Expand Down
10 changes: 7 additions & 3 deletions lists/singlylinkedlist/singlylinkedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ type element struct {
next *element
}

// New instantiates a new empty list
func New() *List {
return &List{}
// New instantiates a new list and adds the passed values, if any, to the list
func New(values ...interface{}) *List {
list := &List{}
if len(values) > 0 {
list.Add(values...)
}
return list
}

// Add appends a value (one or more) at the end of the list (same as Append())
Expand Down
26 changes: 26 additions & 0 deletions lists/singlylinkedlist/singlylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ import (
"github.com/emirpasic/gods/utils"
)

func TestListNew(t *testing.T) {
list1 := New()

if actualValue := list1.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}

list2 := New(1, "b")

if actualValue := list2.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}

if actualValue, ok := list2.Get(0); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}

if actualValue, ok := list2.Get(1); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}

if actualValue, ok := list2.Get(2); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, nil)
}
}

func TestListAdd(t *testing.T) {
list := New()
list.Add("a")
Expand Down

0 comments on commit 5ef8ba6

Please sign in to comment.