github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/test/singly_list_ginko_test.go (about) 1 package test 2 3 import ( 4 "github.com/benz9527/toy-box/algo/list" 5 "github.com/onsi/ginkgo/v2" 6 "github.com/stretchr/testify/assert" 7 "sort" 8 "sync" 9 ) 10 11 var _ = ginkgo.Describe("Singly Linked BasicLinkedList Unit Tests", ginkgo.Ordered, ginkgo.Serial, func() { 12 ginkgo.It("singly linked list generation", 13 ginkgo.Label("singlyLinkedList"), 14 func() { 15 slist := list.NewSinglyLinkedList[int]() 16 assert.NotNil(ginkgo.GinkgoT(), slist) 17 slist.AppendValue(1, 2, 3) 18 assert.Equal(ginkgo.GinkgoT(), int64(3), slist.Len()) 19 slist.ForEach(func(idx int64, e list.NodeElement[int]) { 20 ginkgo.GinkgoWriter.Printf("node element: %v\n", e.GetValue()) 21 }) 22 }, 23 ) 24 ginkgo.It("singly linked list generation with insert before/after", 25 ginkgo.Label("singlyLinkedList"), 26 func() { 27 slist := list.NewSinglyLinkedList[int]() 28 assert.NotNil(ginkgo.GinkgoT(), slist) 29 _1n := slist.AppendValue(1) 30 _2n := slist.InsertBefore(2, _1n[0]) 31 slist.InsertAfter(3, _2n) 32 assert.Equal(ginkgo.GinkgoT(), int64(3), slist.Len()) 33 expected := []int{2, 3, 1} 34 actual := make([]int, 0, 3) 35 slist.ForEach(func(idx int64, e list.NodeElement[int]) { 36 actual = append(actual, e.GetValue()) 37 ginkgo.GinkgoWriter.Printf("node element: %v\n", e.GetValue()) 38 }) 39 assert.Equal(ginkgo.GinkgoT(), expected, actual) 40 }, 41 ) 42 ginkgo.It("singly linked list generation then find target node", 43 ginkgo.Label("singlyLinkedList"), 44 func() { 45 slist := list.NewSinglyLinkedList[int]() 46 assert.NotNil(ginkgo.GinkgoT(), slist) 47 _1n := slist.AppendValue(1) 48 _2n := slist.InsertBefore(2, _1n[0]) 49 slist.InsertAfter(3, _2n) 50 assert.Equal(ginkgo.GinkgoT(), int64(3), slist.Len()) 51 _3n, ok := slist.FindFirst(3) 52 assert.True(ginkgo.GinkgoT(), ok) 53 assert.Equal(ginkgo.GinkgoT(), 3, _3n.GetValue()) 54 }, 55 ) 56 ginkgo.It("singly linked list generation then remove target node", 57 ginkgo.Label("singlyLinkedList"), 58 func() { 59 slist := list.NewSinglyLinkedList[int]() 60 assert.NotNil(ginkgo.GinkgoT(), slist) 61 _1n := slist.AppendValue(1) 62 _2n := slist.InsertBefore(2, _1n[0]) 63 _3n := slist.InsertAfter(3, _2n) 64 slist.Append(list.NewNodeElement(4)) 65 _3n_1 := slist.Remove(_3n) 66 assert.Equal(ginkgo.GinkgoT(), _3n.GetValue(), _3n_1.GetValue()) 67 _3n_1 = slist.Remove(list.NewNodeElement(3)) 68 assert.Nil(ginkgo.GinkgoT(), _3n_1) 69 assert.Equal(ginkgo.GinkgoT(), int64(3), slist.Len()) 70 expected := []int{2, 1, 4} 71 actual := make([]int, 0, 3) 72 slist.ForEach(func(idx int64, e list.NodeElement[int]) { 73 actual = append(actual, e.GetValue()) 74 ginkgo.GinkgoWriter.Printf("node element: %v\n", e.GetValue()) 75 }) 76 assert.Equal(ginkgo.GinkgoT(), expected, actual) 77 }, 78 ) 79 ginkgo.It("singly linked list generation, remove target node and insert nil node", 80 ginkgo.Label("singlyLinkedList"), 81 func() { 82 slist := list.NewSinglyLinkedList[int]() 83 assert.NotNil(ginkgo.GinkgoT(), slist) 84 _1n := slist.AppendValue(1) 85 _2n := slist.InsertBefore(2, _1n[0]) 86 _3n := slist.InsertAfter(3, _2n) 87 slist.Append(list.NewNodeElement(4)) 88 slist.Remove(_3n) 89 slist.Append(nil) 90 slist.InsertBefore(5, nil) 91 slist.InsertAfter(6, nil) 92 e := slist.Remove(nil) 93 assert.Nil(ginkgo.GinkgoT(), e) 94 assert.Equal(ginkgo.GinkgoT(), int64(3), slist.Len()) 95 expected := []int{2, 1, 4} 96 actual := make([]int, 0, 3) 97 slist.ForEach(func(idx int64, e list.NodeElement[int]) { 98 actual = append(actual, e.GetValue()) 99 ginkgo.GinkgoWriter.Printf("node element: %v\n", e.GetValue()) 100 }) 101 assert.Equal(ginkgo.GinkgoT(), expected, actual) 102 }, 103 ) 104 }) 105 106 var _ = ginkgo.Describe("Concurrent Singly Linked BasicLinkedList Unit Tests", func() { 107 ginkgo.It("singly linked list generation, remove target node and run in parallel", 108 ginkgo.Label("ConcurrentSinglyLinkedList Parallel"), 109 func() { 110 slist := list.NewConcurrentSinglyLinkedList[int]() 111 assert.NotNil(ginkgo.GinkgoT(), slist) 112 wg := sync.WaitGroup{} 113 wg.Add(5) 114 go func() { 115 slist.AppendValue(1) 116 wg.Done() 117 }() 118 go func() { 119 slist.AppendValue(2) 120 wg.Done() 121 }() 122 go func() { 123 slist.AppendValue(3) 124 wg.Done() 125 }() 126 go func() { 127 _3n, ok := slist.FindFirst(3) 128 if ok && _3n != nil { 129 slist.Remove(_3n) 130 } 131 wg.Done() 132 }() 133 go func() { 134 slist.AppendValue(4) 135 wg.Done() 136 }() 137 wg.Wait() 138 expected1 := []int{1, 2, 4} 139 expected2 := []int{1, 2, 3, 4} 140 actual := make([]int, 0, 3) 141 slist.ForEach(func(idx int64, e list.NodeElement[int]) { 142 actual = append(actual, e.GetValue()) 143 ginkgo.GinkgoWriter.Printf("node element: %v\n", e.GetValue()) 144 }) 145 sort.Ints(actual) 146 if slist.Len() == 3 { 147 assert.Equal(ginkgo.GinkgoT(), expected1, actual) 148 } else if slist.Len() == 4 { 149 assert.Equal(ginkgo.GinkgoT(), expected2, actual) 150 } else { 151 assert.Fail(ginkgo.GinkgoT(), "unexpected singly linked list length") 152 } 153 }, 154 ) 155 })