github.com/better-concurrent/guc@v0.0.0-20190520022744-eb29266403a1/priorityqueue_test.go (about) 1 package guc 2 3 import ( 4 "container/list" 5 "testing" 6 ) 7 8 func TestNewPriority(t *testing.T) { 9 p := NewPriority() 10 if p != p.data.queue { 11 t.Fatal("underlying queue reference should be equals to queue itself") 12 } 13 } 14 15 type comparatorSample struct { 16 } 17 18 func (comparatorSample) Compare(o1, o2 interface{}) int { 19 panic("implement me") 20 } 21 22 func TestNewPriorityWithComparator(t *testing.T) { 23 p := NewPriorityWithComparator(comparatorSample{}) 24 if p.data.comparator == nil { 25 t.Fatal("underlying comparator should not be nil") 26 } 27 } 28 29 type sampleItem struct { 30 Value int 31 } 32 33 func (this *sampleItem) Equals(i interface{}) bool { 34 dst, ok := i.(*sampleItem) 35 if !ok { 36 return false 37 } 38 return this.Value == dst.Value 39 } 40 41 func (this *sampleItem) CompareTo(i interface{}) int { 42 return this.Value - i.(*sampleItem).Value 43 } 44 45 func newSampleItem(value int) *sampleItem { 46 return &sampleItem{ 47 Value: value, 48 } 49 } 50 51 func newPreparedPriorityQueue() *PriorityQueue { 52 p := NewPriority() 53 p.Add(newSampleItem(6)) 54 p.Add(newSampleItem(8)) 55 p.Add(newSampleItem(3)) 56 p.Add(newSampleItem(6)) 57 p.Add(newSampleItem(33)) 58 p.Add(newSampleItem(7)) 59 p.Add(newSampleItem(2)) 60 return p 61 } 62 63 func TestPriorityQueue_Add(t *testing.T) { 64 p := NewPriority() 65 if !p.IsEmpty() { 66 t.Fatal("queue should be empty") 67 } 68 b := p.Add(newSampleItem(10)) 69 if !b { 70 t.Fatal("add result not true") 71 } 72 if len(p.data.data) != 1 { 73 t.Fatal("data len should be 1") 74 } 75 if p.data.data[0].(*sampleItem).Value != 10 { 76 t.Fatal("item value is not the origin value") 77 } 78 if p.IsEmpty() { 79 t.Fatal("queue should not be empty") 80 } 81 if p.Size() != 1 { 82 t.Fatal("queue size should be 1") 83 } 84 } 85 86 func TestPriorityQueue_Remove(t *testing.T) { 87 p := NewPriority() 88 p.Add(newSampleItem(10)) 89 b := p.Remove(newSampleItem(10)) 90 if !b { 91 t.Fatal("remove result should be true") 92 } 93 if !p.IsEmpty() { 94 t.Fatal("queue should be empty") 95 } 96 if p.Size() != 0 { 97 t.Fatal("queue size should be zero") 98 } 99 100 p.Add(newSampleItem(10)) 101 if p.Remove(newSampleItem(20)) { 102 t.Fatal("remove result should be false") 103 } 104 if p.Size() != 1 { 105 t.Fatal("queue size should be 1") 106 } 107 } 108 109 func TestPriorityQueue_Add_Poll_Multi(t *testing.T) { 110 p := newPreparedPriorityQueue() 111 if p.Size() != 7 { 112 t.Fatal("queue size not match") 113 } 114 115 prev := -1 116 for i := 0; i < 7; i++ { 117 s := p.Poll().(*sampleItem) 118 if prev == -1 { 119 prev = s.Value 120 continue 121 } 122 if prev > s.Value { 123 t.Fatal("prev value:", prev, "should be <= current value:", s.Value) 124 } 125 prev = s.Value 126 } 127 } 128 129 func TestPriorityQueue_ForEach(t *testing.T) { 130 p := newPreparedPriorityQueue() 131 if p.Size() != 7 { 132 t.Fatal("queue size not match") 133 } 134 135 l := list.New() 136 p.ForEach(func(i interface{}) { 137 l.PushBack(i) 138 }) 139 if l.Len() != 7 { 140 t.Fatal("total number of foreach items are not 7") 141 } 142 } 143 144 func TestPriorityQueue_Contains(t *testing.T) { 145 p := newPreparedPriorityQueue() 146 if p.Size() != 7 { 147 t.Fatal("queue size not match") 148 } 149 150 if !p.Contains(newSampleItem(6)) { 151 t.Fatal("should have value 6 item") 152 } 153 if p.Contains(newSampleItem(100)) { 154 t.Fatal("should not have value 100 item") 155 } 156 } 157 158 func TestPriorityQueue_ToArray(t *testing.T) { 159 p := newPreparedPriorityQueue() 160 if p.Size() != 7 { 161 t.Fatal("queue size not match") 162 } 163 164 arr := p.ToArray() 165 if len(arr) != 7 { 166 t.Fatal("array size must be 7") 167 } 168 matched := false 169 for _, v := range arr { 170 if v.(*sampleItem).Value == 6 { 171 matched = true 172 break 173 } 174 } 175 if !matched { 176 t.Fatal("must have value 6 in array") 177 } 178 } 179 180 func TestPriorityQueue_FillArray(t *testing.T) { 181 p := newPreparedPriorityQueue() 182 if p.Size() != 7 { 183 t.Fatal("queue size not match") 184 } 185 186 arr := p.FillArray(make([]interface{}, 0)) 187 if len(arr) != 7 { 188 t.Fatal("array size must be 7") 189 } 190 matched := false 191 for _, v := range arr { 192 if v.(*sampleItem).Value == 6 { 193 matched = true 194 break 195 } 196 } 197 if !matched { 198 t.Fatal("must have value 6 in array") 199 } 200 201 newArr := make([]interface{}, 7) 202 p.FillArray(newArr) 203 matched = false 204 for _, v := range newArr { 205 if v.(*sampleItem).Value == 6 { 206 matched = true 207 break 208 } 209 } 210 if !matched { 211 t.Fatal("must have value 6 in array") 212 } 213 } 214 215 func TestPriorityQueue_RemoveIf(t *testing.T) { 216 p := newPreparedPriorityQueue() 217 if p.Size() != 7 { 218 t.Fatal("queue size not match") 219 } 220 221 b := p.RemoveIf(func(i interface{}) bool { 222 return i.(*sampleItem).Value == 6 223 }) 224 if !b { 225 t.Fatal("remove result should be true") 226 } 227 if p.Size() != 6 { 228 t.Fatal("queue size should be 6") 229 } 230 231 b = p.RemoveIf(func(i interface{}) bool { 232 return i.(*sampleItem).Value == 100 233 }) 234 if b { 235 t.Fatal("remove result should be false") 236 } 237 if p.Size() != 6 { 238 t.Fatal("queue size should be 6") 239 } 240 } 241 242 func TestPriorityQueue_Clear(t *testing.T) { 243 p := newPreparedPriorityQueue() 244 if p.Size() != 7 { 245 t.Fatal("queue size not match") 246 } 247 p.Clear() 248 if p.Size() != 0 { 249 t.Fatal("queue size should be 0") 250 } 251 if !p.IsEmpty() { 252 t.Fatal("queue should be empty") 253 } 254 } 255 256 func TestPriorityQueue_Equals(t *testing.T) { 257 p := newPreparedPriorityQueue() 258 if !p.Equals(p) { 259 t.Fatal("queue should be equals to itself") 260 } 261 if p.Equals(newPreparedPriorityQueue()) { 262 t.Fatal("queue should not be equals to a new one") 263 } 264 if p.Equals(struct{}{}) { 265 t.Fatal("queue should not be equals to another object of other type") 266 } 267 } 268 269 func TestPriorityQueue_HashCode(t *testing.T) { 270 p := newPreparedPriorityQueue() 271 if p.HashCode() != p.HashCode() { 272 t.Fatal("hashcode must same") 273 } 274 } 275 276 func TestPriorityQueue_Offer(t *testing.T) { 277 p := newPreparedPriorityQueue() 278 p.Offer(newSampleItem(100)) 279 if p.Size() != 8 { 280 t.Fatal("queue size must be 8") 281 } 282 if !p.Contains(newSampleItem(100)) { 283 t.Fatal("queue must contains value 100") 284 } 285 } 286 287 func TestPriorityQueue_RemoveHead(t *testing.T) { 288 p := newPreparedPriorityQueue() 289 h := p.RemoveHead().(*sampleItem) 290 if h.Value != 2 { 291 t.Fatal("remove head must value 2") 292 } 293 if p.Size() != 6 { 294 t.Fatal("queue size must be 6") 295 } 296 297 emptyQueue := NewPriority() 298 r := func(p *PriorityQueue) (result bool) { 299 result = false 300 defer func() { 301 err := recover() 302 if err != nil { 303 result = true 304 } 305 }() 306 p.RemoveHead() 307 return 308 }(emptyQueue) 309 if !r { 310 t.Fatal("should panic when RemoveHead of an empty queue") 311 } 312 } 313 314 func TestPriorityQueue_Element(t *testing.T) { 315 p := newPreparedPriorityQueue() 316 h := p.Element().(*sampleItem) 317 if h.Value != 2 { 318 t.Fatal("element() must value 2") 319 } 320 321 emptyQueue := NewPriority() 322 r := func(p *PriorityQueue) (result bool) { 323 result = false 324 defer func() { 325 err := recover() 326 if err != nil { 327 result = true 328 } 329 }() 330 p.Element() 331 return 332 }(emptyQueue) 333 if !r { 334 t.Fatal("should panic when element() of an empty queue") 335 } 336 } 337 338 func TestPriorityQueue_Peek(t *testing.T) { 339 p := newPreparedPriorityQueue() 340 h := p.Peek().(*sampleItem) 341 if h.Value != 2 { 342 t.Fatal("remove head must value 2") 343 } 344 345 emptyQueue := NewPriority() 346 r := emptyQueue.Peek() 347 if r != nil { 348 t.Fatal("peek of empty queue should be nil") 349 } 350 } 351 352 func TestPriorityQueue_Iterator(t *testing.T) { 353 p := newPreparedPriorityQueue() 354 cnt := 0 355 matched := false 356 iter := p.Iterator() 357 if iter == nil { 358 t.Fatal("iter should not be nil") 359 } 360 for iter.HasNext() { 361 cnt++ 362 if iter.Next().(*sampleItem).Value == 6 { 363 matched = true 364 } 365 } 366 if cnt != 7 { 367 t.Fatal("iter count should be 7") 368 } 369 if !matched { 370 t.Fatal("should contains value 6") 371 } 372 } 373 374 func TestPriorityQueueIter_Remove(t *testing.T) { 375 p := newPreparedPriorityQueue() 376 iter := p.Iterator() 377 iter.HasNext() 378 iter.Remove() 379 if p.Size() != 6 { 380 t.Fatal("queue size should be 6") 381 } 382 } 383 384 func TestPriorityQueueIter_ForEachRemaining(t *testing.T) { 385 p := newPreparedPriorityQueue() 386 iter := p.Iterator() 387 388 l := list.New() 389 iter.ForEachRemaining(func(i interface{}) { 390 l.PushBack(i) 391 }) 392 if l.Len() != 7 { 393 t.Fatal("total number of foreach items are not 7") 394 } 395 } 396 397 func TestPriorityQueue_ContainsAll(t *testing.T) { 398 p := newPreparedPriorityQueue() 399 400 c := NewPriority() 401 c.Add(newSampleItem(6)) 402 c.Add(newSampleItem(2)) 403 if !p.ContainsAll(c) { 404 t.Fatal("should contains all") 405 } 406 407 c = NewPriority() 408 c.Add(newSampleItem(6)) 409 c.Add(newSampleItem(100)) 410 if p.ContainsAll(c) { 411 t.Fatal("should not contains all - 1") 412 } 413 414 c = NewPriority() 415 c.Add(newSampleItem(200)) 416 c.Add(newSampleItem(100)) 417 if p.ContainsAll(c) { 418 t.Fatal("should not contains all - 2") 419 } 420 } 421 422 func TestPriorityQueue_AddAll(t *testing.T) { 423 p := newPreparedPriorityQueue() 424 if p.Size() != 7 { 425 t.Fatal("queue size must be 7") 426 } 427 428 c := NewPriority() 429 c.Add(newSampleItem(200)) 430 c.Add(newSampleItem(100)) 431 if !p.AddAll(c) { 432 t.Fatal("add result should be true") 433 } 434 435 if !p.Contains(newSampleItem(100)) { 436 t.Fatal("queue should contains value 100") 437 } 438 if !p.Contains(newSampleItem(200)) { 439 t.Fatal("queue should contains value 200") 440 } 441 if p.Size() != 9 { 442 t.Fatal("queue size should be 9") 443 } 444 } 445 446 func TestPriorityQueue_RemoveAll(t *testing.T) { 447 p := newPreparedPriorityQueue() 448 if p.Size() != 7 { 449 t.Fatal("queue size must be 7") 450 } 451 452 c := NewPriority() 453 c.Add(newSampleItem(200)) 454 c.Add(newSampleItem(100)) 455 if p.RemoveAll(c) { 456 t.Fatal("remove all should return false") 457 } 458 if p.Size() != 7 { 459 t.Fatal("queue size should be 7") 460 } 461 462 c = NewPriority() 463 c.Add(newSampleItem(3)) 464 c.Add(newSampleItem(100)) 465 if !p.RemoveAll(c) { 466 t.Fatal("remove all should return true") 467 } 468 if p.Size() != 6 { 469 t.Fatal("queue size should be 6") 470 } 471 472 c = NewPriority() 473 c.Add(newSampleItem(8)) 474 c.Add(newSampleItem(2)) 475 if !p.RemoveAll(c) { 476 t.Fatal("remove all should return true") 477 } 478 if p.Size() != 4 { 479 t.Fatal("queue size should be 4") 480 } 481 } 482 483 func TestPriorityQueue_RetainAll(t *testing.T) { 484 { 485 p := newPreparedPriorityQueue() 486 c := NewPriority() 487 c.Add(newSampleItem(200)) 488 c.Add(newSampleItem(100)) 489 if !p.RetainAll(c) { 490 t.Fatal("remove all should return true") 491 } 492 if p.Size() != 0 { 493 t.Fatal("queue size should be 0") 494 } 495 } 496 { 497 p := newPreparedPriorityQueue() 498 c := NewPriority() 499 c.Add(newSampleItem(3)) 500 c.Add(newSampleItem(100)) 501 if !p.RetainAll(c) { 502 t.Fatal("remove all should return true") 503 } 504 if p.Size() != 1 { 505 t.Fatal("queue size should be 1") 506 } 507 } 508 { 509 p := newPreparedPriorityQueue() 510 c := NewPriority() 511 c.Add(newSampleItem(8)) 512 c.Add(newSampleItem(2)) 513 if !p.RetainAll(c) { 514 t.Fatal("remove all should return true") 515 } 516 if p.Size() != 2 { 517 t.Fatal("queue size should be 2") 518 } 519 } 520 }