Merge pull request #88 from emirpasic/development

List bulk initialization
pull/90/head
Emir Pasic 7 years ago committed by GitHub
commit 5ef8ba6800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,9 +32,13 @@ const (
shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink) shrinkFactor = float32(0.25) // shrink when size is 25% of capacity (0 means never shrink)
) )
// New instantiates a new empty list // New instantiates a new list and adds the passed values, if any, to the list
func New() *List { func New(values ...interface{}) *List {
return &List{} list := &List{}
if len(values) > 0 {
list.Add(values...)
}
return list
} }
// Add appends a value at the end of the list // Add appends a value at the end of the list

@ -6,11 +6,36 @@ package arraylist
import ( import (
"fmt" "fmt"
"testing"
"github.com/emirpasic/gods/utils" "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) { func TestListAdd(t *testing.T) {
list := New() list := New()
list.Add("a") list.Add("a")

@ -34,9 +34,13 @@ type element struct {
next *element next *element
} }
// New instantiates a new empty list // New instantiates a new list and adds the passed values, if any, to the list
func New() *List { func New(values ...interface{}) *List {
return &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()) // Add appends a value (one or more) at the end of the list (same as Append())

@ -11,6 +11,32 @@ import (
"github.com/emirpasic/gods/utils" "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) { func TestListAdd(t *testing.T) {
list := New() list := New()
list.Add("a") list.Add("a")

@ -33,9 +33,13 @@ type element struct {
next *element next *element
} }
// New instantiates a new empty list // New instantiates a new list and adds the passed values, if any, to the list
func New() *List { func New(values ...interface{}) *List {
return &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()) // Add appends a value (one or more) at the end of the list (same as Append())

@ -11,6 +11,32 @@ import (
"github.com/emirpasic/gods/utils" "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) { func TestListAdd(t *testing.T) {
list := New() list := New()
list.Add("a") list.Add("a")

Loading…
Cancel
Save