- bulk initialization for lists

pull/88/head
emirpasic 7 years ago
parent cbce439b4e
commit c6630349c4

@ -32,15 +32,12 @@ 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...)
// Of instantiates a new list of the given values }
func Of(values ...interface{}) *List {
list := New()
list.Add(values)
return list return 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,15 +34,12 @@ 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...)
// Of instantiates a new list of the given values }
func Of(values ...interface{}) *List {
list := New()
list.Add(values)
return list return list
} }

@ -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,15 +33,12 @@ 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...)
// Of instantiates a new list of the given values }
func Of(values ...interface{}) *List {
list := New()
list.Add(values)
return list return list
} }

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