Debian Codegraph @

skiplist/skiplist_test.go

raw symbols SCIP
1 // Copyright 2012 Google Inc. All rights reserved.
2 // Author: Ric Szopa (Ryszard) <[email protected]>
3
4 // Package skiplist implements skip list based maps and sets.
5 //
6 // Skip lists are a data structure that can be used in place of
7 // balanced trees. Skip lists use probabilistic balancing rather than
8 // strictly enforced balancing and as a result the algorithms for
9 // insertion and deletion in skip lists are much simpler and
10 // significantly faster than equivalent algorithms for balanced trees.
11 //
12 // Skip lists were first described in Pugh, William (June 1990). "Skip
13 // lists: a probabilistic alternative to balanced
14 // trees". Communications of the ACM 33 (6): 668–676
15 package skiplist
16
17 import (
18 "fmt"
19 "math/rand"
20 "sort"
21 "testing"
22 )
23
24 func (s *SkipList) printRepr() {
25
26 fmt.Printf("header:\n")
27 for i, link := range s.header.forward {
28 if link != nil {
29 fmt.Printf("\t%d: -> %v\n", i, link.key)
30 } else {
31 fmt.Printf("\t%d: -> END\n", i)
32 }
33 }
34
35 for node := s.header.next(); node != nil; node = node.next() {
36 fmt.Printf("%v: %v (level %d)\n", node.key, node.value, len(node.forward))
37 for i, link := range node.forward {
38 if link != nil {
39 fmt.Printf("\t%d: -> %v\n", i, link.key)
40 } else {
41 fmt.Printf("\t%d: -> END\n", i)
42 }
43 }
44 }
45 fmt.Println()
46 }
47
48 func TestInitialization(t *testing.T) {
49 s := NewCustomMap(func(l, r interface{}) bool {
50 return l.(int) < r.(int)
51 })
52 if !s.lessThan(1, 2) {
53 t.Errorf("Less than doesn't work correctly.")
54 }
55 }
56
57 func TestEmptyNodeNext(t *testing.T) {
58 n := new(node)
59 if next := n.next(); next != nil {
60 t.Errorf("Next() should be nil for an empty node.")
61 }
62
63 if n.hasNext() {
64 t.Errorf("hasNext() should be false for an empty node.")
65 }
66 }
67
68 func TestEmptyNodePrev(t *testing.T) {
69 n := new(node)
70 if previous := n.previous(); previous != nil {
71 t.Errorf("Previous() should be nil for an empty node.")
72 }
73
74 if n.hasPrevious() {
75 t.Errorf("hasPrevious() should be false for an empty node.")
76 }
77 }
78
79 func TestNodeHasNext(t *testing.T) {
80 s := NewIntMap()
81 s.Set(0, 0)
82 node := s.header.next()
83 if node.key != 0 {
84 t.Fatalf("We got the wrong node: %v.", node)
85 }
86
87 if node.hasNext() {
88 t.Errorf("%v should be the last node.", node)
89 }
90 }
91
92 func TestNodeHasPrev(t *testing.T) {
93 s := NewIntMap()
94 s.Set(0, 0)
95 node := s.header.previous()
96 if node != nil {
97 t.Fatalf("Expected no previous entry, got %v.", node)
98 }
99 }
100
101 func (s *SkipList) check(t *testing.T, key, wanted int) {
102 if got, _ := s.Get(key); got != wanted {
103 t.Errorf("For key %v wanted value %v, got %v.", key, wanted, got)
104 }
105 }
106
107 func TestGet(t *testing.T) {
108 s := NewIntMap()
109 s.Set(0, 0)
110
111 if value, present := s.Get(0); !(value == 0 && present) {
112 t.Errorf("%v, %v instead of %v, %v", value, present, 0, true)
113 }
114
115 if value, present := s.Get(100); value != nil || present {
116 t.Errorf("%v, %v instead of %v, %v", value, present, nil, false)
117 }
118 }
119
120 func TestGetGreaterOrEqual(t *testing.T) {
121 s := NewIntMap()
122
123 if _, value, present := s.GetGreaterOrEqual(5); !(value == nil && !present) {
124 t.Errorf("s.GetGreaterOrEqual(5) should have returned nil and false for an empty map, not %v and %v.", value, present)
125 }
126
127 s.Set(0, 0)
128
129 if _, value, present := s.GetGreaterOrEqual(5); !(value == nil && !present) {
130 t.Errorf("s.GetGreaterOrEqual(5) should have returned nil and false for an empty map, not %v and %v.", value, present)
131 }
132
133 s.Set(10, 10)
134
135 if key, value, present := s.GetGreaterOrEqual(5); !(value == 10 && key == 10 && present) {
136 t.Errorf("s.GetGreaterOrEqual(5) should have returned 10 and true, not %v and %v.", value, present)
137 }
138 }
139
140 func TestSet(t *testing.T) {
141 s := NewIntMap()
142 if l := s.Len(); l != 0 {
143 t.Errorf("Len is not 0, it is %v", l)
144 }
145
146 s.Set(0, 0)
147 s.Set(1, 1)
148 if l := s.Len(); l != 2 {
149 t.Errorf("Len is not 2, it is %v", l)
150 }
151 s.check(t, 0, 0)
152 if t.Failed() {
153 t.Errorf("header.Next() after s.Set(0, 0) and s.Set(1, 1): %v.", s.header.next())
154 }
155 s.check(t, 1, 1)
156
157 }
158
159 func TestChange(t *testing.T) {
160 s := NewIntMap()
161 s.Set(0, 0)
162 s.Set(1, 1)
163 s.Set(2, 2)
164
165 s.Set(0, 7)
166 if value, _ := s.Get(0); value != 7 {
167 t.Errorf("Value should be 7, not %d", value)
168 }
169 s.Set(1, 8)
170 if value, _ := s.Get(1); value != 8 {
171 t.Errorf("Value should be 8, not %d", value)
172 }
173
174 }
175
176 func TestDelete(t *testing.T) {
177 s := NewIntMap()
178 for i := 0; i < 10; i++ {
179 s.Set(i, i)
180 }
181 for i := 0; i < 10; i += 2 {
182 s.Delete(i)
183 }
184
185 for i := 0; i < 10; i += 2 {
186 if _, present := s.Get(i); present {
187 t.Errorf("%d should not be present in s", i)
188 }
189 }
190
191 if v, present := s.Delete(10000); v != nil || present {
192 t.Errorf("Deleting a non-existent key should return nil, false, and not %v, %v.", v, present)
193 }
194
195 if t.Failed() {
196 s.printRepr()
197 }
198
199 }
200
201 func TestLen(t *testing.T) {
202 s := NewIntMap()
203 for i := 0; i < 10; i++ {
204 s.Set(i, i)
205 }
206 if length := s.Len(); length != 10 {
207 t.Errorf("Length should be equal to 10, not %v.", length)
208 s.printRepr()
209 }
210 for i := 0; i < 5; i++ {
211 s.Delete(i)
212 }
213 if length := s.Len(); length != 5 {
214 t.Errorf("Length should be equal to 5, not %v.", length)
215 }
216
217 s.Delete(10000)
218
219 if length := s.Len(); length != 5 {
220 t.Errorf("Length should be equal to 5, not %v.", length)
221 }
222
223 }
224
225 func TestIteration(t *testing.T) {
226 s := NewIntMap()
227 for i := 0; i < 20; i++ {
228 s.Set(i, i)
229 }
230
231 seen := 0
232 var lastKey int
233
234 i := s.Iterator()
235 defer i.Close()
236
237 for i.Next() {
238 seen++
239 lastKey = i.Key().(int)
240 if i.Key() != i.Value() {
241 t.Errorf("Wrong value for key %v: %v.", i.Key(), i.Value())
242 }
243 }
244
245 if seen != s.Len() {
246 t.Errorf("Not all the items in s where iterated through (seen %d, should have seen %d). Last one seen was %d.", seen, s.Len(), lastKey)
247 }
248
249 for i.Previous() {
250 if i.Key() != i.Value() {
251 t.Errorf("Wrong value for key %v: %v.", i.Key(), i.Value())
252 }
253
254 if i.Key().(int) >= lastKey {
255 t.Errorf("Expected key to descend but ascended from %v to %v.", lastKey, i.Key())
256 }
257
258 lastKey = i.Key().(int)
259 }
260
261 if lastKey != 0 {
262 t.Errorf("Expected to count back to zero, but stopped at key %v.", lastKey)
263 }
264 }
265
266 func TestRangeIteration(t *testing.T) {
267 s := NewIntMap()
268 for i := 0; i < 20; i++ {
269 s.Set(i, i)
270 }
271
272 max, min := 0, 100000
273 var lastKey, seen int
274
275 i := s.Range(5, 10)
276 defer i.Close()
277
278 for i.Next() {
279 seen++
280 lastKey = i.Key().(int)
281 if lastKey > max {
282 max = lastKey
283 }
284 if lastKey < min {
285 min = lastKey
286 }
287 if i.Key() != i.Value() {
288 t.Errorf("Wrong value for key %v: %v.", i.Key(), i.Value())
289 }
290 }
291
292 if seen != 5 {
293 t.Errorf("The number of items yielded is incorrect (should be 5, was %v)", seen)
294 }
295 if min != 5 {
296 t.Errorf("The smallest element should have been 5, not %v", min)
297 }
298
299 if max != 9 {
300 t.Errorf("The largest element should have been 9, not %v", max)
301 }
302
303 if i.Seek(4) {
304 t.Error("Allowed to seek to invalid range.")
305 }
306
307 if !i.Seek(5) {
308 t.Error("Could not seek to an allowed range.")
309 }
310 if i.Key().(int) != 5 || i.Value().(int) != 5 {
311 t.Errorf("Expected 5 for key and 5 for value, got %d and %d", i.Key(), i.Value())
312 }
313
314 if !i.Seek(7) {
315 t.Error("Could not seek to an allowed range.")
316 }
317 if i.Key().(int) != 7 || i.Value().(int) != 7 {
318 t.Errorf("Expected 7 for key and 7 for value, got %d and %d", i.Key(), i.Value())
319 }
320
321 if i.Seek(10) {
322 t.Error("Allowed to seek to invalid range.")
323 }
324
325 i.Seek(9)
326
327 seen = 0
328 min = 100000
329 max = -1
330
331 for i.Previous() {
332 seen++
333 lastKey = i.Key().(int)
334 if lastKey > max {
335 max = lastKey
336 }
337 if lastKey < min {
338 min = lastKey
339 }
340 if i.Key() != i.Value() {
341 t.Errorf("Wrong value for key %v: %v.", i.Key(), i.Value())
342 }
343 }
344
345 if seen != 4 {
346 t.Errorf("The number of items yielded is incorrect (should be 5, was %v)", seen)
347 }
348 if min != 5 {
349 t.Errorf("The smallest element should have been 5, not %v", min)
350 }
351
352 if max != 8 {
353 t.Errorf("The largest element should have been 9, not %v", max)
354 }
355 }
356
357 func TestSomeMore(t *testing.T) {
358 s := NewIntMap()
359 insertions := [...]int{4, 1, 2, 9, 10, 7, 3}
360 for _, i := range insertions {
361 s.Set(i, i)
362 }
363 for _, i := range insertions {
364 s.check(t, i, i)
365 }
366
367 }
368
369 func makeRandomList(n int) *SkipList {
370 s := NewIntMap()
371 for i := 0; i < n; i++ {
372 insert := rand.Int()
373 s.Set(insert, insert)
374 }
375 return s
376 }
377
378 func LookupBenchmark(b *testing.B, n int) {
379 b.StopTimer()
380 s := makeRandomList(n)
381 b.StartTimer()
382 for i := 0; i < b.N; i++ {
383 s.Get(rand.Int())
384 }
385 }
386
387 func SetBenchmark(b *testing.B, n int) {
388 b.StopTimer()
389 values := []int{}
390 for i := 0; i < b.N; i++ {
391 values = append(values, rand.Int())
392 }
393 s := NewIntMap()
394 b.StartTimer()
395 for i := 0; i < b.N; i++ {
396 s.Set(values[i], values[i])
397 }
398 }
399
400 // Make sure that all the keys are unique and are returned in order.
401 func TestSanity(t *testing.T) {
402 s := NewIntMap()
403 for i := 0; i < 10000; i++ {
404 insert := rand.Int()
405 s.Set(insert, insert)
406 }
407 var last int = 0
408
409 i := s.Iterator()
410 defer i.Close()
411
412 for i.Next() {
413 if last != 0 && i.Key().(int) <= last {
414 t.Errorf("Not in order!")
415 }
416 last = i.Key().(int)
417 }
418
419 for i.Previous() {
420 if last != 0 && i.Key().(int) > last {
421 t.Errorf("Not in order!")
422 }
423 last = i.Key().(int)
424 }
425 }
426
427 type MyOrdered struct {
428 value int
429 }
430
431 func (me MyOrdered) LessThan(other Ordered) bool {
432 return me.value < other.(MyOrdered).value
433 }
434
435 func TestOrdered(t *testing.T) {
436 s := New()
437 s.Set(MyOrdered{0}, 0)
438 s.Set(MyOrdered{1}, 1)
439
440 if val, _ := s.Get(MyOrdered{0}); val != 0 {
441 t.Errorf("Wrong value for MyOrdered{0}. Should have been %d.", val)
442 }
443 }
444
445 func TestNewStringMap(t *testing.T) {
446 s := NewStringMap()
447 s.Set("a", 1)
448 s.Set("b", 2)
449 if value, _ := s.Get("a"); value != 1 {
450 t.Errorf("Expected 1, got %v.", value)
451 }
452 }
453
454 func TestGetNilKey(t *testing.T) {
455 s := NewStringMap()
456 if v, present := s.Get(nil); v != nil || present {
457 t.Errorf("s.Get(nil) should return nil, false (not %v, %v).", v, present)
458 }
459
460 }
461
462 func TestSetNilKey(t *testing.T) {
463 s := NewStringMap()
464
465 defer func() {
466 if err := recover(); err == nil {
467 t.Errorf("s.Set(nil, 0) should have panicked.")
468 }
469 }()
470
471 s.Set(nil, 0)
472
473 }
474
475 func TestSetMaxLevelInFlight(t *testing.T) {
476 s := NewIntMap()
477 s.MaxLevel = 2
478 for i := 0; i < 64; i++ {
479 insert := 2 * rand.Int()
480 s.Set(insert, insert)
481 }
482
483 s.MaxLevel = 64
484 for i := 0; i < 65536; i++ {
485 insert := 2*rand.Int() + 1
486 s.Set(insert, insert)
487 }
488
489 i := s.Iterator()
490 defer i.Close()
491
492 for i.Next() {
493 if v, _ := s.Get(i.Key()); v != i.Key() {
494 t.Errorf("Bad values in the skip list (%v). Inserted before the call to s.SetMax(): %t.", v, i.Key().(int)%2 == 0)
495 }
496 }
497 }
498
499 func TestDeletingHighestLevelNodeDoesntBreakSkiplist(t *testing.T) {
500 s := NewIntMap()
501 elements := []int{1, 3, 5, 7, 0, 4, 5, 10, 11}
502
503 for _, i := range elements {
504 s.Set(i, i)
505 }
506
507 highestLevelNode := s.header.forward[len(s.header.forward)-1]
508
509 s.Delete(highestLevelNode.key)
510
511 seen := 0
512 i := s.Iterator()
513 defer i.Close()
514
515 for i.Next() {
516 seen++
517 }
518 if seen == 0 {
519 t.Errorf("Iteration is broken (no elements seen).")
520 }
521 }
522
523 func TestNewSet(t *testing.T) {
524 set := NewIntSet()
525 elements := []int{1, 3, 5, 7, 0, 4, 5}
526
527 for _, i := range elements {
528 set.Add(i)
529 }
530
531 if length := set.Len(); length != 6 {
532 t.Errorf("set.Len() should be equal to 6, not %v.", length)
533 }
534
535 if !set.Contains(3) {
536 t.Errorf("set should contain 3.")
537 }
538
539 if set.Contains(1000) {
540 t.Errorf("set should not contain 1000.")
541 }
542
543 removed := set.Remove(1)
544
545 if !removed {
546 t.Errorf("Remove returned false for element that was present in set.")
547 }
548
549 seen := 0
550 i := set.Iterator()
551 defer i.Close()
552
553 for i.Next() {
554 seen++
555 }
556
557 if seen != 5 {
558 t.Errorf("Iterator() iterated through %v elements. Should have been 5.", seen)
559 }
560
561 if set.Contains(1) {
562 t.Errorf("1 was removed, set should not contain 1.")
563 }
564
565 if length := set.Len(); length != 5 {
566 t.Errorf("After removing one element, set.Len() should be equal to 5, not %v.", length)
567 }
568
569 set.SetMaxLevel(10)
570 if ml := set.GetMaxLevel(); ml != 10 {
571 t.Errorf("MaxLevel for set should be 10, not %v", ml)
572 }
573
574 }
575
576 func TestSetRangeIterator(t *testing.T) {
577 set := NewIntSet()
578 elements := []int{0, 1, 3, 5}
579
580 for _, i := range elements {
581 set.Add(i)
582 }
583
584 seen := 0
585 for i := set.Range(2, 1000); i.Next(); {
586 seen++
587 }
588 if seen != 2 {
589 t.Errorf("There should have been 2 elements in Range(2, 1000), not %v.", seen)
590 }
591
592 }
593
594 func TestNewStringSet(t *testing.T) {
595 set := NewStringSet()
596 strings := []string{"ala", "ma", "kota"}
597 for _, v := range strings {
598 set.Add(v)
599 }
600
601 if !set.Contains("ala") {
602 t.Errorf("set should contain \"ala\".")
603 }
604 }
605
606 func TestIteratorPrevHoles(t *testing.T) {
607 m := NewIntMap()
608
609 i := m.Iterator()
610 defer i.Close()
611
612 m.Set(0, 0)
613 m.Set(1, 1)
614 m.Set(2, 2)
615
616 if !i.Next() {
617 t.Errorf("Expected iterator to move successfully to the next.")
618 }
619
620 if !i.Next() {
621 t.Errorf("Expected iterator to move successfully to the next.")
622 }
623
624 if !i.Next() {
625 t.Errorf("Expected iterator to move successfully to the next.")
626 }
627
628 if i.Key().(int) != 2 || i.Value().(int) != 2 {
629 t.Errorf("Expected iterator to reach key 2 and value 2, got %v and %v.", i.Key(), i.Value())
630 }
631
632 if !i.Previous() {
633 t.Errorf("Expected iterator to move successfully to the previous.")
634 }
635
636 if i.Key().(int) != 1 || i.Value().(int) != 1 {
637 t.Errorf("Expected iterator to reach key 1 and value 1, got %v and %v.", i.Key(), i.Value())
638 }
639
640 if !i.Next() {
641 t.Errorf("Expected iterator to move successfully to the next.")
642 }
643
644 m.Delete(1)
645
646 if !i.Previous() {
647 t.Errorf("Expected iterator to move successfully to the previous.")
648 }
649
650 if i.Key().(int) != 0 || i.Value().(int) != 0 {
651 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
652 }
653 }
654
655 func TestIteratorSeek(t *testing.T) {
656 m := NewIntMap()
657
658 i := m.Seek(0)
659
660 if i != nil {
661 t.Errorf("Expected nil iterator, but got %v.", i)
662 }
663
664 i = m.SeekToFirst()
665
666 if i != nil {
667 t.Errorf("Expected nil iterator, but got %v.", i)
668 }
669
670 i = m.SeekToLast()
671
672 if i != nil {
673 t.Errorf("Expected nil iterator, but got %v.", i)
674 }
675
676 m.Set(0, 0)
677
678 i = m.SeekToFirst()
679 defer i.Close()
680
681 if i.Key().(int) != 0 || i.Value().(int) != 0 {
682 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
683 }
684
685 i = m.SeekToLast()
686 defer i.Close()
687
688 if i.Key().(int) != 0 || i.Value().(int) != 0 {
689 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
690 }
691
692 m.Set(1, 1)
693
694 i = m.SeekToFirst()
695 defer i.Close()
696
697 if i.Key().(int) != 0 || i.Value().(int) != 0 {
698 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
699 }
700
701 i = m.SeekToLast()
702 defer i.Close()
703
704 if i.Key().(int) != 1 || i.Value().(int) != 1 {
705 t.Errorf("Expected iterator to reach key 1 and value 1, got %v and %v.", i.Key(), i.Value())
706 }
707
708 m.Set(2, 2)
709
710 i = m.SeekToFirst()
711 defer i.Close()
712
713 if i.Key().(int) != 0 || i.Value().(int) != 0 {
714 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
715 }
716
717 i = m.SeekToLast()
718 defer i.Close()
719
720 if i.Key().(int) != 2 || i.Value().(int) != 2 {
721 t.Errorf("Expected iterator to reach key 2 and value 2, got %v and %v.", i.Key(), i.Value())
722 }
723
724 i = m.Seek(0)
725 defer i.Close()
726
727 if i.Key().(int) != 0 || i.Value().(int) != 0 {
728 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
729 }
730
731 i = m.Seek(2)
732 defer i.Close()
733
734 if i.Key().(int) != 2 || i.Value().(int) != 2 {
735 t.Errorf("Expected iterator to reach key 2 and value 2, got %v and %v.", i.Key(), i.Value())
736 }
737
738 i = m.Seek(1)
739 defer i.Close()
740
741 if i.Key().(int) != 1 || i.Value().(int) != 1 {
742 t.Errorf("Expected iterator to reach key 1 and value 1, got %v and %v.", i.Key(), i.Value())
743 }
744
745 i = m.Seek(3)
746
747 if i != nil {
748 t.Errorf("Expected to receive nil iterator, got %v.", i)
749 }
750
751 m.Set(4, 4)
752
753 i = m.Seek(4)
754 defer i.Close()
755
756 if i.Key().(int) != 4 || i.Value().(int) != 4 {
757 t.Errorf("Expected iterator to reach key 4 and value 4, got %v and %v.", i.Key(), i.Value())
758 }
759
760 i = m.Seek(3)
761 defer i.Close()
762
763 if i.Key().(int) != 4 || i.Value().(int) != 4 {
764 t.Errorf("Expected iterator to reach key 4 and value 4, got %v and %v.", i.Key(), i.Value())
765 }
766
767 m.Delete(4)
768
769 i = m.SeekToFirst()
770 defer i.Close()
771
772 if i.Key().(int) != 0 || i.Value().(int) != 0 {
773 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
774 }
775
776 i = m.SeekToLast()
777 defer i.Close()
778
779 if i.Key().(int) != 2 || i.Value().(int) != 2 {
780 t.Errorf("Expected iterator to reach key 2 and value 2, got %v and %v.", i.Key(), i.Value())
781 }
782
783 if !i.Seek(2) {
784 t.Error("Expected iterator to seek to key.")
785 }
786
787 if i.Key().(int) != 2 || i.Value().(int) != 2 {
788 t.Errorf("Expected iterator to reach key 2 and value 2, got %v and %v.", i.Key(), i.Value())
789 }
790
791 if !i.Seek(1) {
792 t.Error("Expected iterator to seek to key.")
793 }
794
795 if i.Key().(int) != 1 || i.Value().(int) != 1 {
796 t.Errorf("Expected iterator to reach key 1 and value 1, got %v and %v.", i.Key(), i.Value())
797 }
798
799 if !i.Seek(0) {
800 t.Error("Expected iterator to seek to key.")
801 }
802
803 if i.Key().(int) != 0 || i.Value().(int) != 0 {
804 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
805 }
806
807 i = m.SeekToFirst()
808 defer i.Close()
809
810 if !i.Seek(0) {
811 t.Error("Expected iterator to seek to key.")
812 }
813
814 if i.Key().(int) != 0 || i.Value().(int) != 0 {
815 t.Errorf("Expected iterator to reach key 0 and value 0, got %v and %v.", i.Key(), i.Value())
816 }
817 }
818
819 func BenchmarkLookup16(b *testing.B) {
820 LookupBenchmark(b, 16)
821 }
822
823 func BenchmarkLookup256(b *testing.B) {
824 LookupBenchmark(b, 256)
825 }
826
827 func BenchmarkLookup65536(b *testing.B) {
828 LookupBenchmark(b, 65536)
829 }
830
831 func BenchmarkSet16(b *testing.B) {
832 SetBenchmark(b, 16)
833 }
834
835 func BenchmarkSet256(b *testing.B) {
836 SetBenchmark(b, 256)
837 }
838
839 func BenchmarkSet65536(b *testing.B) {
840 SetBenchmark(b, 65536)
841 }
842
843 func BenchmarkRandomSeek(b *testing.B) {
844 b.StopTimer()
845 values := []int{}
846 s := NewIntMap()
847 for i := 0; i < b.N; i++ {
848 r := rand.Int()
849 values = append(values, r)
850 s.Set(r, r)
851 }
852 b.StartTimer()
853 for i := 0; i < b.N; i++ {
854 iterator := s.Seek(values[i])
855 if iterator == nil {
856 b.Errorf("got incorrect value for index %d", i)
857 }
858 }
859 }
860
861 const (
862 lookAhead = 10
863 )
864
865 // This test is used for the baseline comparison of Iterator.Seek when
866 // performing forward sequential seek operations.
867 func BenchmarkForwardSeek(b *testing.B) {
868 b.StopTimer()
869
870 values := []int{}
871 s := NewIntMap()
872 valueCount := b.N
873 for i := 0; i < valueCount; i++ {
874 r := rand.Int()
875 values = append(values, r)
876 s.Set(r, r)
877 }
878 sort.Ints(values)
879
880 b.StartTimer()
881 for i := 0; i < b.N; i++ {
882 key := values[i]
883 iterator := s.Seek(key)
884 if i < valueCount-lookAhead {
885 nextKey := values[i+lookAhead]
886
887 iterator = s.Seek(nextKey)
888 if iterator.Key().(int) != nextKey || iterator.Value().(int) != nextKey {
889 b.Errorf("%d. expected %d key and %d value, got %d key and %d value", i, nextKey, nextKey, iterator.Key(), iterator.Value())
890 }
891 }
892 }
893 }
894
895 // This test demonstrates the amortized cost of a forward sequential seek.
896 func BenchmarkForwardSeekReusedIterator(b *testing.B) {
897 b.StopTimer()
898
899 values := []int{}
900 s := NewIntMap()
901 valueCount := b.N
902 for i := 0; i < valueCount; i++ {
903 r := rand.Int()
904 values = append(values, r)
905 s.Set(r, r)
906
907 }
908 sort.Ints(values)
909
910 b.StartTimer()
911 for i := 0; i < b.N; i++ {
912 key := values[i]
913 iterator := s.Seek(key)
914 if i < valueCount-lookAhead {
915 nextKey := values[i+lookAhead]
916
917 if !iterator.Seek(nextKey) {
918 b.Errorf("%d. expected iterator to seek to %d key; failed.", i, nextKey)
919 } else if iterator.Key().(int) != nextKey || iterator.Value().(int) != nextKey {
920 b.Errorf("%d. expected %d key and %d value, got %d key and %d value", i, nextKey, nextKey, iterator.Key(), iterator.Value())
921 }
922 }
923 }
924 }