Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test examples and some fixed bugs #16

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions DoublyLinkedList/DoublyLinkedList.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
221 changes: 221 additions & 0 deletions DoublyLinkedList/DoublyLinkedList_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
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)
}
}

if ll.GetSize() != 1000{
t.Fatal()
}
}

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)
}
}

if ll.GetSize() != 1000{
t.Fatal()
}
}

func TestLinkedList_GetSize(t *testing.T) {
ll := LinkedList{}
count := 1000
for i := 1; i <= count; i++ {
ll.InsertFirst(i)
if ll.GetSize() != i {
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()
}
}
}

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()
}
}

if ll.GetSize() > 0 {
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()
}
}

if ll.GetSize() > 0 {
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()
}
}

if ll.GetSize() > 0 {
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()
}
}

if ll.GetSize() > 0 {
t.Fatal()
}
}