2019-05-09 10:29:09 +00:00
|
|
|
package arraylist
|
2019-05-08 02:42:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
2019-07-24 18:30:55 +00:00
|
|
|
func TestIterator(t *testing.T) {
|
|
|
|
l := New()
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
l.Push(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
iter := l.Iterator()
|
|
|
|
|
|
|
|
var result []int
|
|
|
|
for iter.Next() {
|
|
|
|
result = append(result, iter.Value().(int))
|
|
|
|
}
|
|
|
|
|
|
|
|
if spew.Sprint(result) != "[0 1 2 3 4]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = l.Iterator()
|
|
|
|
result = nil
|
|
|
|
for iter.Prev() {
|
|
|
|
result = append(result, iter.Value().(int))
|
|
|
|
}
|
|
|
|
|
|
|
|
if spew.Sprint(result) != "[4 3 2 1 0]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
citer := l.CircularIterator()
|
|
|
|
result = nil
|
|
|
|
for i := 0; i < 11; i++ {
|
|
|
|
if citer.Next() {
|
|
|
|
result = append(result, citer.Value().(int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 11 {
|
|
|
|
t.Error("len(result) != 11, is ", len(result))
|
|
|
|
}
|
|
|
|
|
|
|
|
if spew.Sprint(result) != "[0 1 2 3 4 0 1 2 3 4 0]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
citer = l.CircularIterator()
|
|
|
|
result = nil
|
|
|
|
for i := 0; i < 11; i++ {
|
|
|
|
if citer.Prev() {
|
|
|
|
result = append(result, citer.Value().(int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != 11 {
|
|
|
|
t.Error("len(result) != 11, is ", len(result))
|
|
|
|
}
|
|
|
|
|
|
|
|
if spew.Sprint(result) != "[4 3 2 1 0 4 3 2 1 0 4]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 02:42:51 +00:00
|
|
|
func TestPush(t *testing.T) {
|
|
|
|
l := New()
|
2019-07-04 17:10:23 +00:00
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
l.PushFront(1)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
var result string
|
|
|
|
result = spew.Sprint(l.Values())
|
2019-05-22 09:52:50 +00:00
|
|
|
if result != "[1 1]" {
|
2019-05-08 02:42:51 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
l.PushBack(2)
|
|
|
|
}
|
2019-05-08 02:42:51 +00:00
|
|
|
result = spew.Sprint(l.Values())
|
2019-05-22 09:52:50 +00:00
|
|
|
if result != "[1 1 2 2]" {
|
2019-05-08 02:42:51 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
2019-07-04 17:10:23 +00:00
|
|
|
|
2019-07-24 18:30:55 +00:00
|
|
|
l.Push(3)
|
|
|
|
result = spew.Sprint(l.Values())
|
|
|
|
if result != "[1 1 2 2 3]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func TestGrowth(t *testing.T) {
|
2019-05-08 02:42:51 +00:00
|
|
|
l := New()
|
|
|
|
for i := 0; i < 5; i++ {
|
2019-05-22 09:52:50 +00:00
|
|
|
l.PushFront(1)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result string
|
|
|
|
result = spew.Sprint(l.Values())
|
2019-06-04 10:27:43 +00:00
|
|
|
if result != "[1 1 1 1 1]" {
|
2019-05-08 02:42:51 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
l = New()
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
l.PushBack(1)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = spew.Sprint(l.Values())
|
2019-06-04 10:27:43 +00:00
|
|
|
if result != "[1 1 1 1 1 1 1]" {
|
2019-05-08 02:42:51 +00:00
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
// for i := 0; i < 2; i++ {
|
|
|
|
// l.PushBack(2)
|
|
|
|
// }
|
|
|
|
// result = spew.Sprint(l.Values())
|
|
|
|
// if result != "[1 1 2 2]" {
|
|
|
|
// t.Error(result)
|
|
|
|
// }
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
func TestPop(t *testing.T) {
|
2019-05-08 02:42:51 +00:00
|
|
|
l := New()
|
|
|
|
for i := 0; i < 5; i++ {
|
2019-05-22 09:52:50 +00:00
|
|
|
l.PushFront(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 4; i >= 0; i-- {
|
|
|
|
if v, ok := l.PopFront(); ok {
|
|
|
|
if v != i {
|
|
|
|
t.Error("should be ", v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("should be ok, value is", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := l.PopFront(); ok {
|
|
|
|
t.Error("should not be ok, v = ", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
l.PushFront(i)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
2019-05-22 09:52:50 +00:00
|
|
|
if v, ok := l.PopBack(); ok {
|
|
|
|
if v != i {
|
|
|
|
t.Error("should be ", v)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-22 09:52:50 +00:00
|
|
|
t.Error("should be ok, value is", v)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:52:50 +00:00
|
|
|
if v, ok := l.PopBack(); ok {
|
|
|
|
t.Error("should not be ok, v = ", v)
|
2019-05-08 02:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-05 18:17:35 +00:00
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
l := New()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
l.PushFront(uint(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
var result string
|
|
|
|
|
|
|
|
for _, selval := range []uint{4, 3} {
|
2019-07-25 09:05:29 +00:00
|
|
|
last, _ := l.Index((int)(selval))
|
|
|
|
if v, isfound := l.Remove((int)(selval)); isfound {
|
2019-06-05 18:17:35 +00:00
|
|
|
if v != last {
|
|
|
|
t.Error(v, " != ", last)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("should be found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = spew.Sprint(l.Values())
|
|
|
|
if result != "[4 3 2]" {
|
|
|
|
t.Error("should be [4 3 2], value =", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := l.Remove(1)
|
|
|
|
if v != uint(3) {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ = l.Remove(1)
|
|
|
|
if v != uint(2) {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ = l.Remove(1)
|
|
|
|
if v != nil && l.Size() != 1 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ = l.Remove(0)
|
|
|
|
if v != uint(4) && l.Size() != 0 {
|
|
|
|
t.Error(v, "size = ", l.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-04 17:10:23 +00:00
|
|
|
|
2019-07-25 03:40:50 +00:00
|
|
|
func TestTraversal(t *testing.T) {
|
|
|
|
l := New()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
l.PushFront(uint(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []interface{}
|
|
|
|
|
|
|
|
l.Traversal(func(v interface{}) bool {
|
|
|
|
result = append(result, v)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if spew.Sprint(result) != "[4 3 2 1 0]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.PushBack(7, 8)
|
|
|
|
result = nil
|
|
|
|
l.Traversal(func(v interface{}) bool {
|
|
|
|
result = append(result, v)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if spew.Sprint(result) != "[4 3 2 1 0 7 8]" {
|
|
|
|
t.Error(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
func TestRemain(t *testing.T) {
|
|
|
|
l := New()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
l.Push(i)
|
|
|
|
if !l.Contains(i) {
|
|
|
|
t.Error("Contains", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.String() != "[0 1 2 3 4 5 6 7 8 9]" {
|
|
|
|
t.Error(l.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 10; i < 100; i++ {
|
|
|
|
l.Push(i)
|
2019-07-04 17:10:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
for !l.Empty() {
|
|
|
|
l.PopBack()
|
|
|
|
}
|
2019-07-04 17:10:23 +00:00
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
for i := 10; i < 100; i++ {
|
|
|
|
l.Push(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Clear()
|
2019-07-04 17:10:23 +00:00
|
|
|
|
2019-07-25 09:05:29 +00:00
|
|
|
if l.Size() != 0 {
|
|
|
|
t.Error("Size != 0")
|
2019-07-04 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-25 09:05:29 +00:00
|
|
|
|
|
|
|
// func loadTestData() []int {
|
|
|
|
// data, err := ioutil.ReadFile("../../l.log")
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println(err)
|
|
|
|
// }
|
|
|
|
// var l []int
|
|
|
|
// decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
|
|
// decoder.Decode(&l)
|
|
|
|
// return l
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func BenchmarkPush(b *testing.B) {
|
|
|
|
// l := loadTestData()
|
|
|
|
// b.N = len(l)
|
|
|
|
|
|
|
|
// arr := New()
|
|
|
|
|
|
|
|
// for i := 0; i < b.N; i++ {
|
|
|
|
// arr.PushBack(l[i])
|
|
|
|
// }
|
|
|
|
// }
|