bag_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // CookieJar - A contestant's algorithm toolbox
  2. // Copyright (c) 2013 Peter Szilagyi. All rights reserved.
  3. //
  4. // CookieJar is dual licensed: use of this source code is governed by a BSD
  5. // license that can be found in the LICENSE file. Alternatively, the CookieJar
  6. // toolbox may be used in accordance with the terms and conditions contained
  7. // in a signed written agreement between you and the author(s).
  8. package bag
  9. import (
  10. "math/rand"
  11. "testing"
  12. )
  13. func TestBag(t *testing.T) {
  14. // Create some initial data
  15. size := 65536
  16. data := make([]int, size)
  17. for i := 0; i < size; i++ {
  18. data[i] = rand.Int()
  19. }
  20. // Insert the data into the bag, but remove every second
  21. bag := New()
  22. for i := 0; i < len(data); i++ {
  23. bag.Insert(data[i])
  24. if i%2 == 0 {
  25. bag.Remove(data[i])
  26. }
  27. }
  28. // Make sure size works
  29. if bag.Size() != size/2 {
  30. t.Errorf("size mismatch: have %v, want %v", bag.Size(), size/2)
  31. }
  32. // Calculate the sum of the elements in and out
  33. sumBag := int64(0)
  34. bag.Do(func(val interface{}) {
  35. sumBag += int64(val.(int))
  36. })
  37. sumDat := int64(0)
  38. for i := 1; i < len(data); i += 2 {
  39. sumDat += int64(data[i])
  40. }
  41. if sumBag != sumDat {
  42. t.Errorf("sum mismatch after iteration: have %v, want %v", sumBag, sumDat)
  43. }
  44. // Verify the contents of the bag
  45. for i := 1; i < len(data); i += 2 {
  46. if bag.Count(data[i]) <= 0 {
  47. t.Errorf("expected data, none found: %v in %v", data[i], bag)
  48. }
  49. bag.Remove(data[i])
  50. }
  51. if len(bag.data) != 0 || bag.Size() != 0 {
  52. t.Errorf("leftovers remained in bag: %v", bag)
  53. }
  54. }
  55. func TestReset(t *testing.T) {
  56. // Create some initial data
  57. size := 65536
  58. data := make([]int, size)
  59. for i := 0; i < size; i++ {
  60. data[i] = rand.Int()
  61. }
  62. // Insert the data into the bag, but remove every second
  63. bag := New()
  64. for _, val := range data {
  65. bag.Insert(val)
  66. }
  67. // clear the bag and verify
  68. bag.Reset()
  69. if len(bag.data) != 0 || bag.Size() != 0 {
  70. t.Errorf("leftovers remained in bag: %v", bag)
  71. }
  72. }
  73. func BenchmarkInsert(b *testing.B) {
  74. // Create some initial data
  75. data := make([]int, b.N)
  76. for i := 0; i < len(data); i++ {
  77. data[i] = rand.Int()
  78. }
  79. // Execute the benchmark
  80. b.ResetTimer()
  81. bag := New()
  82. for _, val := range data {
  83. bag.Insert(val)
  84. }
  85. }
  86. func BenchmarkRemove(b *testing.B) {
  87. // Create some initial data
  88. data := make([]int, b.N)
  89. for i := 0; i < len(data); i++ {
  90. data[i] = rand.Int()
  91. }
  92. // Fill the bag with it
  93. bag := New()
  94. for _, val := range data {
  95. bag.Insert(val)
  96. }
  97. // Execute the benchmark
  98. b.ResetTimer()
  99. for _, val := range data {
  100. bag.Remove(val)
  101. }
  102. }
  103. func BenchmarkDo(b *testing.B) {
  104. // Create some initial data
  105. data := make([]int, b.N)
  106. for i := 0; i < len(data); i++ {
  107. data[i] = rand.Int()
  108. }
  109. // Fill the bag with it
  110. bag := New()
  111. for _, val := range data {
  112. bag.Insert(val)
  113. }
  114. // Execute the benchmark
  115. b.ResetTimer()
  116. bag.Do(func(val interface{}) {
  117. // Do nothing
  118. })
  119. }