github.com/andy2046/gopie@v0.7.0/pkg/dll/dll_test.go (about) 1 package dll 2 3 import ( 4 "fmt" 5 "sync" 6 "testing" 7 ) 8 9 var ( 10 m = make(map[int]bool) 11 lock sync.Mutex 12 ) 13 14 func TestDListEmpty(t *testing.T) { 15 l := New() 16 if !l.Empty() { 17 t.Error("new list is empty") 18 } 19 20 var elm *Element 21 22 elm = l.PushLeft(1) 23 if l.Empty() { 24 t.Error("list is not empty") 25 } 26 l.Remove(elm) 27 if !l.Empty() { 28 t.Error("list is empty") 29 } 30 31 elm = l.PushRight(2) 32 if l.Empty() { 33 t.Error("list is not empty") 34 } 35 l.Remove(elm) 36 if !l.Empty() { 37 t.Error("list is empty") 38 } 39 40 elm = l.PushLeft(3) 41 if l.Empty() { 42 t.Error("list is not empty") 43 } 44 l.PopRight() 45 if !l.Empty() { 46 t.Error("list is empty") 47 } 48 49 elm = l.PushRight(4) 50 if l.Empty() { 51 t.Error("list is not empty") 52 } 53 l.PopLeft() 54 if !l.Empty() { 55 t.Error("list is empty") 56 } 57 } 58 59 func TestDListNext(t *testing.T) { 60 l := New() 61 start := make(chan struct{}) 62 n := 100 63 64 var init *Element 65 var wg sync.WaitGroup 66 wg.Add(1) 67 68 go func() { 69 <-start 70 removeCnt := 0 71 for i := 0; i < n; i++ { 72 e := l.PushRight(i + n) 73 fmt.Printf("pushRight %v\n", i+n) 74 75 if i == 0 { 76 init = e 77 continue 78 } 79 80 if removeCnt < n/4 { 81 r := l.Remove(e) 82 removeCnt++ 83 if r != nil { 84 fmt.Printf("remove %v\n", r) 85 } 86 } 87 } 88 wg.Done() 89 fmt.Println("pushRight done") 90 }() 91 92 close(start) 93 wg.Wait() 94 95 nxt := l.Next(init) 96 fmt.Printf("Next of %v is %v\n", init.Value, nxt.Value) 97 98 for { 99 v := l.PopLeft() 100 if v == nil { 101 fmt.Println() 102 break 103 } 104 fmt.Printf("%v.", v) 105 } 106 107 if !l.Empty() { 108 t.Error("list is empty") 109 } 110 } 111 112 func TestDListPrev(t *testing.T) { 113 l := New() 114 start := make(chan struct{}) 115 n := 100 116 117 var init *Element 118 var wg sync.WaitGroup 119 wg.Add(1) 120 121 go func() { 122 <-start 123 removeCnt := 0 124 for i := 0; i < n; i++ { 125 e := l.PushLeft(i + n) 126 fmt.Printf("pushRight %v\n", i+n) 127 128 if i == 0 { 129 init = e 130 continue 131 } 132 133 if removeCnt < n/4 { 134 r := l.Remove(e) 135 removeCnt++ 136 if r != nil { 137 fmt.Printf("remove %v\n", r) 138 } 139 } 140 } 141 wg.Done() 142 fmt.Println("pushRight done") 143 }() 144 145 close(start) 146 wg.Wait() 147 148 nxt := l.Prev(init) 149 fmt.Printf("Prev of %v is %v\n", init.Value, nxt.Value) 150 151 for { 152 v := l.PopLeft() 153 if v == nil { 154 fmt.Println() 155 break 156 } 157 fmt.Printf("%v.", v) 158 } 159 160 if !l.Empty() { 161 t.Error("list is empty") 162 } 163 } 164 165 func TestDListPL(t *testing.T) { 166 l := New() 167 start := make(chan struct{}) 168 n := 100 169 170 var wg sync.WaitGroup 171 wg.Add(2) 172 173 go func() { 174 <-start 175 removeCnt := 0 176 for i := 0; i < n; i++ { 177 e := l.PushRight(i + n) 178 fmt.Printf("pushRight %v\n", i+n) 179 if removeCnt < n/4 { 180 r := l.Remove(e) 181 removeCnt++ 182 if r != nil { 183 // vv := r.(int) 184 // record(t, vv) 185 fmt.Printf("remove %v\n", r) 186 } 187 } 188 } 189 wg.Done() 190 fmt.Println("pushRight done") 191 }() 192 193 go func() { 194 <-start 195 for i := 0; i < n/2; i++ { 196 v := l.PopLeft() 197 // if v != nil { 198 // vv := v.(int) 199 // record(t, vv) 200 // } 201 fmt.Printf("popLeft %v\n", v) 202 } 203 wg.Done() 204 fmt.Println("popLeft done") 205 }() 206 207 close(start) 208 wg.Wait() 209 for { 210 v := l.PopLeft() 211 if v == nil { 212 fmt.Println() 213 break 214 } 215 fmt.Printf("%v.", v) 216 } 217 218 if !l.Empty() { 219 t.Error("list is empty") 220 } 221 } 222 223 func TestDListPR(t *testing.T) { 224 l := New() 225 start := make(chan struct{}) 226 n := 100 227 228 var wg sync.WaitGroup 229 wg.Add(2) 230 231 go func() { 232 <-start 233 removeCnt := 0 234 for i := 0; i < n; i++ { 235 e := l.PushLeft(i) 236 fmt.Printf("pushLeft %v\n", i) 237 if removeCnt < n/4 { 238 r := l.Remove(e) 239 removeCnt++ 240 if r != nil { 241 // vv := r.(int) 242 // record(t, vv) 243 fmt.Printf("remove %v\n", r) 244 } 245 } 246 } 247 wg.Done() 248 fmt.Println("pushLeft done") 249 }() 250 251 go func() { 252 <-start 253 for i := 0; i < n/2; i++ { 254 v := l.PopRight() 255 // if v != nil { 256 // vv := v.(int) 257 // record(t, vv) 258 // } 259 fmt.Printf("popRight %v\n", v) 260 } 261 wg.Done() 262 fmt.Println("popRight done") 263 }() 264 265 close(start) 266 wg.Wait() 267 for { 268 v := l.PopLeft() 269 if v == nil { 270 fmt.Println() 271 break 272 } 273 fmt.Printf("%v.", v) 274 } 275 276 if !l.Empty() { 277 t.Error("list is empty") 278 } 279 } 280 281 func record(t *testing.T, v int) { 282 lock.Lock() 283 defer lock.Unlock() 284 285 if _, existed := m[v]; existed { 286 t.Fatalf("duplicated %v", v) 287 } 288 289 m[v] = true 290 }