set_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 set
  9. import (
  10. "math/rand"
  11. "testing"
  12. )
  13. func TestSet(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. // Fill the set with the data and verify that they're all set
  21. set := New()
  22. for _, val := range data {
  23. set.Insert(val)
  24. }
  25. for _, val := range data {
  26. if !set.Exists(val) {
  27. t.Errorf("failed to locate element in set: %v in %v", val, set)
  28. }
  29. }
  30. // Remove a few elements and ensure they're out
  31. rems := data[:1024]
  32. for _, val := range rems {
  33. set.Remove(val)
  34. }
  35. for _, val := range rems {
  36. if set.Exists(val) {
  37. t.Errorf("element exists after remove: %v in %v", val, set)
  38. }
  39. }
  40. // Calcualte the sum of the remainder and verify
  41. sumSet := int64(0)
  42. set.Do(func(val interface{}) {
  43. sumSet += int64(val.(int))
  44. })
  45. sumDat := int64(0)
  46. for _, val := range data {
  47. sumDat += int64(val)
  48. }
  49. for _, val := range rems {
  50. sumDat -= int64(val)
  51. }
  52. if sumSet != sumDat {
  53. t.Errorf("iteration sum mismatch: have %v, want %v", sumSet, sumDat)
  54. }
  55. // Clear the set and ensure nothing's left
  56. set.Reset()
  57. for _, val := range data {
  58. if set.Exists(val) {
  59. t.Errorf("element exists after reset: %v in %v", val, set)
  60. }
  61. }
  62. }
  63. func BenchmarkInsert(b *testing.B) {
  64. // Create some initial data
  65. data := make([]int, b.N)
  66. for i := 0; i < len(data); i++ {
  67. data[i] = rand.Int()
  68. }
  69. // Execute the benchmark
  70. b.ResetTimer()
  71. set := New()
  72. for _, val := range data {
  73. set.Insert(val)
  74. }
  75. }
  76. func BenchmarkRemove(b *testing.B) {
  77. // Create some initial data and fill the set
  78. data := rand.Perm(b.N)
  79. set := New()
  80. for _, val := range data {
  81. set.Insert(val)
  82. }
  83. // Execute the benchmark (different order)
  84. rems := rand.Perm(b.N)
  85. b.ResetTimer()
  86. for _, val := range rems {
  87. set.Remove(val)
  88. }
  89. }
  90. func BenchmarkDo(b *testing.B) {
  91. // Create some initial data
  92. data := make([]int, b.N)
  93. for i := 0; i < len(data); i++ {
  94. data[i] = rand.Int()
  95. }
  96. // Fill the set with it
  97. set := New()
  98. for _, val := range data {
  99. set.Insert(val)
  100. }
  101. // Execute the benchmark
  102. b.ResetTimer()
  103. set.Do(func(val interface{}) {
  104. // Do nothing
  105. })
  106. }