Skip to content

Commit

Permalink
update List tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hsldymq committed Mar 31, 2024
1 parent 330c576 commit 1f91847
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,78 @@ import (
"testing"
)

func TestNewList(t *testing.T) {
list := NewList[int]()
if len(list.elems) != 0 {
t.Fatalf("test NewList, create an empty list, len should be 0")
}

list = NewList(1, 2, 3)
actual := []int{}
for v := range list.Iter() {
actual = append(actual, v)
}
expect := []int{1, 2, 3}
if !slices.Equal(expect, actual) {
t.Fatalf("test NewList expect: %v, actual: %v", expect, actual)
}
}

func TestList_Merge(t *testing.T) {
list := NewList[int]()
l2 := NewListWithElems(1, 2, 3)
l3 := NewListWithElems(4, 5, 6)
l4 := NewListWithElems(7, 8, 9)
l2 := NewList(1, 2, 3)
l3 := NewList(4, 5, 6)
l4 := NewList(7, 8, 9)
list.Merge(l2, l3, l4)

actual := list.elems
actual := []int{}
for v := range list.Iter() {
actual = append(actual, v)
}
expect := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
if !slices.Equal(expect, actual) {
t.Fatalf("test List.Merge expect: %v, actual: %v", expect, actual)
}

list = NewList(1, 2, 3)
l2 = NewList[int]()
list.Merge(l2)
actual = []int{}
for v := range list.Iter() {
actual = append(actual, v)
}
expect = []int{1, 2, 3}
if !slices.Equal(expect, actual) {
t.Fatalf("test List.Merge expect: %v, actual: %v", expect, actual)
}
}

func TestList_MergeSlices(t *testing.T) {
list := NewList[int]()
list.MergeSlices([]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9})

actual := list.elems
actual := []int{}
for v := range list.Iter() {
actual = append(actual, v)
}
expect := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
if !slices.Equal(expect, actual) {
t.Fatalf("test List.MergeSlices expect: %v, actual: %v", expect, actual)
}

list = NewList[int]()
list.MergeSlices([]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9})
actual = []int{}
for v := range list.Iter() {
actual = append(actual, v)
}
expect = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
if !slices.Equal(expect, actual) {
t.Fatalf("test List.MergeSlices expect: %v, actual: %v", expect, actual)
}
}

func TestList_Remove(t *testing.T) {
list := NewList[int]()
list.Merge(NewListWithElems(1, 2, 3, 4, 5))
list.Merge(NewList(1, 2, 3, 4, 5))
list.Remove(3)
actual := []int{}
for v := range list.Iter() {
Expand All @@ -51,7 +95,7 @@ func TestList_Remove(t *testing.T) {
alice := &person{"Alice", 20}
bob := &person{"Bob", 30}
eve := &person{"Eve", 40}
list2 := NewListWithElems[*person](alice, bob, eve)
list2 := NewList[*person](alice, bob, eve)
list2.Remove(bob)
actual2 := []person{}
for v := range list2.Iter() {
Expand All @@ -66,7 +110,7 @@ func TestList_Remove(t *testing.T) {
}

f1 := func() {}
list3 := NewListWithElems[func()](f1, func() {}, func() {})
list3 := NewList[func()](f1, func() {}, func() {})
list3.Remove(f1)
if list3.Count() != 3 {
t.Fatalf("test List.Remove, func is not comparable, Remove should has no effect")
Expand Down Expand Up @@ -151,7 +195,7 @@ func TestList_Shift(t *testing.T) {
}

func TestList_Take(t *testing.T) {
list := NewListWithElems(1, 2, 3, 4, 5, 6, 7, 8)
list := NewList(1, 2, 3, 4, 5, 6, 7, 8)

actual := []int{}
for v := range list.Take(5).Iter() {
Expand Down Expand Up @@ -182,7 +226,7 @@ func TestList_Take(t *testing.T) {
}

func TestList_TakeLast(t *testing.T) {
list := NewListWithElems(1, 2, 3, 4, 5, 6, 7, 8)
list := NewList(1, 2, 3, 4, 5, 6, 7, 8)

actual := []int{}
for v := range list.TakeLast(5).Iter() {
Expand Down Expand Up @@ -213,7 +257,7 @@ func TestList_TakeLast(t *testing.T) {
}

func TestList_Skip(t *testing.T) {
list := NewListWithElems(1, 2, 3, 4, 5, 6, 7, 8)
list := NewList(1, 2, 3, 4, 5, 6, 7, 8)

actual := []int{}
for v := range list.Skip(5).Iter() {
Expand Down Expand Up @@ -244,7 +288,7 @@ func TestList_Skip(t *testing.T) {
}

func TestList_SkipLast(t *testing.T) {
list := NewListWithElems(1, 2, 3, 4, 5, 6, 7, 8)
list := NewList(1, 2, 3, 4, 5, 6, 7, 8)

actual := []int{}
for v := range list.SkipLast(5).Iter() {
Expand Down

0 comments on commit 1f91847

Please sign in to comment.