From 867426032e136fe2e14015fe21c544f323d64278 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Thu, 18 Jun 2020 14:55:27 +0800 Subject: [PATCH 1/3] add test examples and some fixed bugs --- DoublyLinkedList/DoublyLinkedList.go | 19 ++- DoublyLinkedList/DoublyLinkedList_test.go | 187 ++++++++++++++++++++++ 2 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 DoublyLinkedList/DoublyLinkedList_test.go diff --git a/DoublyLinkedList/DoublyLinkedList.go b/DoublyLinkedList/DoublyLinkedList.go index e6e39f9..5310ae2 100644 --- a/DoublyLinkedList/DoublyLinkedList.go +++ b/DoublyLinkedList/DoublyLinkedList.go @@ -18,20 +18,23 @@ func (list *LinkedList) InsertFirst(i int) { data.next = list.head } list.head = data + + if list.tail == nil{ + list.tail = data + } } func (list *LinkedList) InsertLast(i int) { data := &Node{data: i} - if list.head == nil { - list.head = data - list.tail = data - return - } if list.tail != nil { list.tail.next = data data.prev = list.tail } list.tail = data + + if list.head == nil { + list.head = data + } } func (list *LinkedList) RemoveByValue(i int) bool { @@ -40,7 +43,11 @@ func (list *LinkedList) RemoveByValue(i int) bool { } if list.head.data == i { list.head = list.head.next - list.head.prev = nil + if list.head != nil { + list.head.prev = nil + } else { + list.tail = nil + } return true } if list.tail.data == i { diff --git a/DoublyLinkedList/DoublyLinkedList_test.go b/DoublyLinkedList/DoublyLinkedList_test.go new file mode 100644 index 0000000..ad4d6b6 --- /dev/null +++ b/DoublyLinkedList/DoublyLinkedList_test.go @@ -0,0 +1,187 @@ +package DoublyLinkedList + +import "testing" + +func TestLinkedList_InsertFirst(t *testing.T) { + ll := LinkedList{} + + var f, l int + + for i := 1; i <= 1000; i++ { + ll.InsertFirst(i) + if f, _ = ll.GetFirst(); f != i { + t.Fatalf("GetFirst Error want:%d but return:%d", i, f) + } + if l, _ = ll.GetLast(); l != 1 { + t.Fatalf("GetFirst Error want:%d but return:%d", 1, l) + } + } +} + +func TestLinkedList_InsertLast(t *testing.T) { + ll := LinkedList{} + + var f, l int + + for i := 1; i <= 1000; i++ { + ll.InsertLast(i) + if f, _ = ll.GetLast(); f != i { + t.Fatalf("GetFirst Error want:%d but return:%d", i, f) + } + if l, _ = ll.GetFirst(); l != 1 { + t.Fatalf("GetFirst Error want:%d but return:%d", 1, l) + } + } +} + +func TestLinkedList_GetSize(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + if ll.GetSize() != count { + t.Fatal() + } +} + +func TestLinkedList_GetFirst(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + if v, _ := ll.GetFirst(); v != count { + t.Fatal() + } +} + +func TestLinkedList_GetLast(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + } + + if v, _ := ll.GetLast(); v != count { + t.Fatal() + } +} + +func TestLinkedList_GetItemsFromStart(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + //from 1000 to 1 + for _, v := range ll.GetItemsFromStart() { + if v != count { + t.Fatal() + } + count-- + } +} + +func TestLinkedList_GetItemsFromEnd(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + //from 1 to 1000 + index := 1 + for _, v := range ll.GetItemsFromEnd() { + if v != index { + t.Fatal() + } + index++ + } +} + +func TestLinkedList_SearchValue(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + for i := 1; i <= count; i++ { + if !ll.SearchValue(i) { + t.Fatal() + } + } + + if ll.SearchValue(1001) { + t.Fatal() + } + if ll.SearchValue(0) { + t.Fatal() + } +} + +func TestLinkedList_RemoveByValue(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + for i := 1; i <= count; i++ { + ll.RemoveByValue(i) + if ll.SearchValue(i) { + t.Fatal() + } + } +} + +func TestLinkedList_RemoveByValue1(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + } + + for i := 1; i <= count; i++ { + ll.RemoveByValue(i) + if ll.SearchValue(i) { + t.Fatal() + } + } +} + +func TestLinkedList_RemoveByIndex(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + } + + //from 1 to 1000 + for i := 1; i <= count; i++ { + ll.RemoveByIndex(0) + if ll.SearchValue(i) { + t.Fatal() + } + } +} + +func TestLinkedList_RemoveByIndex2(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertFirst(i) + } + + //from 1 to 1000 + for i := 1; i <= count; i++ { + ll.RemoveByIndex(ll.GetSize() - 1) + if ll.SearchValue(i) { + t.Fatal() + } + } +} From 51af795a633e03d4ddba7a948d703fdb487020a2 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Thu, 18 Jun 2020 14:58:10 +0800 Subject: [PATCH 2/3] add test example --- DoublyLinkedList/DoublyLinkedList_test.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/DoublyLinkedList/DoublyLinkedList_test.go b/DoublyLinkedList/DoublyLinkedList_test.go index ad4d6b6..9abc2f7 100644 --- a/DoublyLinkedList/DoublyLinkedList_test.go +++ b/DoublyLinkedList/DoublyLinkedList_test.go @@ -39,10 +39,20 @@ func TestLinkedList_GetSize(t *testing.T) { count := 1000 for i := 1; i <= count; i++ { ll.InsertFirst(i) + if ll.GetSize() != i { + t.Fatal() + } } +} - if ll.GetSize() != count { - t.Fatal() +func TestLinkedList_GetSize2(t *testing.T) { + ll := LinkedList{} + count := 1000 + for i := 1; i <= count; i++ { + ll.InsertLast(i) + if ll.GetSize() != i { + t.Fatal() + } } } From 9ad203d54c96e0e4e8f7c13d207c780303ce5470 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Thu, 18 Jun 2020 17:02:05 +0800 Subject: [PATCH 3/3] complete test examples --- DoublyLinkedList/DoublyLinkedList_test.go | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/DoublyLinkedList/DoublyLinkedList_test.go b/DoublyLinkedList/DoublyLinkedList_test.go index 9abc2f7..24bcc38 100644 --- a/DoublyLinkedList/DoublyLinkedList_test.go +++ b/DoublyLinkedList/DoublyLinkedList_test.go @@ -16,6 +16,10 @@ func TestLinkedList_InsertFirst(t *testing.T) { t.Fatalf("GetFirst Error want:%d but return:%d", 1, l) } } + + if ll.GetSize() != 1000{ + t.Fatal() + } } func TestLinkedList_InsertLast(t *testing.T) { @@ -32,6 +36,10 @@ func TestLinkedList_InsertLast(t *testing.T) { t.Fatalf("GetFirst Error want:%d but return:%d", 1, l) } } + + if ll.GetSize() != 1000{ + t.Fatal() + } } func TestLinkedList_GetSize(t *testing.T) { @@ -147,6 +155,10 @@ func TestLinkedList_RemoveByValue(t *testing.T) { t.Fatal() } } + + if ll.GetSize() > 0 { + t.Fatal() + } } func TestLinkedList_RemoveByValue1(t *testing.T) { @@ -162,6 +174,10 @@ func TestLinkedList_RemoveByValue1(t *testing.T) { t.Fatal() } } + + if ll.GetSize() > 0 { + t.Fatal() + } } func TestLinkedList_RemoveByIndex(t *testing.T) { @@ -178,6 +194,10 @@ func TestLinkedList_RemoveByIndex(t *testing.T) { t.Fatal() } } + + if ll.GetSize() > 0 { + t.Fatal() + } } func TestLinkedList_RemoveByIndex2(t *testing.T) { @@ -194,4 +214,8 @@ func TestLinkedList_RemoveByIndex2(t *testing.T) { t.Fatal() } } + + if ll.GetSize() > 0 { + t.Fatal() + } }