bitutil_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Adapted from: https://golang.org/src/crypto/cipher/xor_test.go
  5. package bitutil
  6. import (
  7. "bytes"
  8. "testing"
  9. )
  10. // Tests that bitwise XOR works for various alignments.
  11. func TestXOR(t *testing.T) {
  12. for alignP := 0; alignP < 2; alignP++ {
  13. for alignQ := 0; alignQ < 2; alignQ++ {
  14. for alignD := 0; alignD < 2; alignD++ {
  15. p := make([]byte, 1023)[alignP:]
  16. q := make([]byte, 1023)[alignQ:]
  17. for i := 0; i < len(p); i++ {
  18. p[i] = byte(i)
  19. }
  20. for i := 0; i < len(q); i++ {
  21. q[i] = byte(len(q) - i)
  22. }
  23. d1 := make([]byte, 1023+alignD)[alignD:]
  24. d2 := make([]byte, 1023+alignD)[alignD:]
  25. XORBytes(d1, p, q)
  26. safeXORBytes(d2, p, q)
  27. if !bytes.Equal(d1, d2) {
  28. t.Error("not equal", d1, d2)
  29. }
  30. }
  31. }
  32. }
  33. }
  34. // Tests that bitwise AND works for various alignments.
  35. func TestAND(t *testing.T) {
  36. for alignP := 0; alignP < 2; alignP++ {
  37. for alignQ := 0; alignQ < 2; alignQ++ {
  38. for alignD := 0; alignD < 2; alignD++ {
  39. p := make([]byte, 1023)[alignP:]
  40. q := make([]byte, 1023)[alignQ:]
  41. for i := 0; i < len(p); i++ {
  42. p[i] = byte(i)
  43. }
  44. for i := 0; i < len(q); i++ {
  45. q[i] = byte(len(q) - i)
  46. }
  47. d1 := make([]byte, 1023+alignD)[alignD:]
  48. d2 := make([]byte, 1023+alignD)[alignD:]
  49. ANDBytes(d1, p, q)
  50. safeANDBytes(d2, p, q)
  51. if !bytes.Equal(d1, d2) {
  52. t.Error("not equal")
  53. }
  54. }
  55. }
  56. }
  57. }
  58. // Tests that bitwise OR works for various alignments.
  59. func TestOR(t *testing.T) {
  60. for alignP := 0; alignP < 2; alignP++ {
  61. for alignQ := 0; alignQ < 2; alignQ++ {
  62. for alignD := 0; alignD < 2; alignD++ {
  63. p := make([]byte, 1023)[alignP:]
  64. q := make([]byte, 1023)[alignQ:]
  65. for i := 0; i < len(p); i++ {
  66. p[i] = byte(i)
  67. }
  68. for i := 0; i < len(q); i++ {
  69. q[i] = byte(len(q) - i)
  70. }
  71. d1 := make([]byte, 1023+alignD)[alignD:]
  72. d2 := make([]byte, 1023+alignD)[alignD:]
  73. ORBytes(d1, p, q)
  74. safeORBytes(d2, p, q)
  75. if !bytes.Equal(d1, d2) {
  76. t.Error("not equal")
  77. }
  78. }
  79. }
  80. }
  81. }
  82. // Tests that bit testing works for various alignments.
  83. func TestTest(t *testing.T) {
  84. for align := 0; align < 2; align++ {
  85. // Test for bits set in the bulk part
  86. p := make([]byte, 1023)[align:]
  87. p[100] = 1
  88. if TestBytes(p) != safeTestBytes(p) {
  89. t.Error("not equal")
  90. }
  91. // Test for bits set in the tail part
  92. q := make([]byte, 1023)[align:]
  93. q[len(q)-1] = 1
  94. if TestBytes(q) != safeTestBytes(q) {
  95. t.Error("not equal")
  96. }
  97. }
  98. }
  99. // Benchmarks the potentially optimized XOR performance.
  100. func BenchmarkFastXOR1KB(b *testing.B) { benchmarkFastXOR(b, 1024) }
  101. func BenchmarkFastXOR2KB(b *testing.B) { benchmarkFastXOR(b, 2048) }
  102. func BenchmarkFastXOR4KB(b *testing.B) { benchmarkFastXOR(b, 4096) }
  103. func benchmarkFastXOR(b *testing.B, size int) {
  104. p, q := make([]byte, size), make([]byte, size)
  105. for i := 0; i < b.N; i++ {
  106. XORBytes(p, p, q)
  107. }
  108. }
  109. // Benchmarks the baseline XOR performance.
  110. func BenchmarkBaseXOR1KB(b *testing.B) { benchmarkBaseXOR(b, 1024) }
  111. func BenchmarkBaseXOR2KB(b *testing.B) { benchmarkBaseXOR(b, 2048) }
  112. func BenchmarkBaseXOR4KB(b *testing.B) { benchmarkBaseXOR(b, 4096) }
  113. func benchmarkBaseXOR(b *testing.B, size int) {
  114. p, q := make([]byte, size), make([]byte, size)
  115. for i := 0; i < b.N; i++ {
  116. safeXORBytes(p, p, q)
  117. }
  118. }
  119. // Benchmarks the potentially optimized AND performance.
  120. func BenchmarkFastAND1KB(b *testing.B) { benchmarkFastAND(b, 1024) }
  121. func BenchmarkFastAND2KB(b *testing.B) { benchmarkFastAND(b, 2048) }
  122. func BenchmarkFastAND4KB(b *testing.B) { benchmarkFastAND(b, 4096) }
  123. func benchmarkFastAND(b *testing.B, size int) {
  124. p, q := make([]byte, size), make([]byte, size)
  125. for i := 0; i < b.N; i++ {
  126. ANDBytes(p, p, q)
  127. }
  128. }
  129. // Benchmarks the baseline AND performance.
  130. func BenchmarkBaseAND1KB(b *testing.B) { benchmarkBaseAND(b, 1024) }
  131. func BenchmarkBaseAND2KB(b *testing.B) { benchmarkBaseAND(b, 2048) }
  132. func BenchmarkBaseAND4KB(b *testing.B) { benchmarkBaseAND(b, 4096) }
  133. func benchmarkBaseAND(b *testing.B, size int) {
  134. p, q := make([]byte, size), make([]byte, size)
  135. for i := 0; i < b.N; i++ {
  136. safeANDBytes(p, p, q)
  137. }
  138. }
  139. // Benchmarks the potentially optimized OR performance.
  140. func BenchmarkFastOR1KB(b *testing.B) { benchmarkFastOR(b, 1024) }
  141. func BenchmarkFastOR2KB(b *testing.B) { benchmarkFastOR(b, 2048) }
  142. func BenchmarkFastOR4KB(b *testing.B) { benchmarkFastOR(b, 4096) }
  143. func benchmarkFastOR(b *testing.B, size int) {
  144. p, q := make([]byte, size), make([]byte, size)
  145. for i := 0; i < b.N; i++ {
  146. ORBytes(p, p, q)
  147. }
  148. }
  149. // Benchmarks the baseline OR performance.
  150. func BenchmarkBaseOR1KB(b *testing.B) { benchmarkBaseOR(b, 1024) }
  151. func BenchmarkBaseOR2KB(b *testing.B) { benchmarkBaseOR(b, 2048) }
  152. func BenchmarkBaseOR4KB(b *testing.B) { benchmarkBaseOR(b, 4096) }
  153. func benchmarkBaseOR(b *testing.B, size int) {
  154. p, q := make([]byte, size), make([]byte, size)
  155. for i := 0; i < b.N; i++ {
  156. safeORBytes(p, p, q)
  157. }
  158. }
  159. // Benchmarks the potentially optimized bit testing performance.
  160. func BenchmarkFastTest1KB(b *testing.B) { benchmarkFastTest(b, 1024) }
  161. func BenchmarkFastTest2KB(b *testing.B) { benchmarkFastTest(b, 2048) }
  162. func BenchmarkFastTest4KB(b *testing.B) { benchmarkFastTest(b, 4096) }
  163. func benchmarkFastTest(b *testing.B, size int) {
  164. p := make([]byte, size)
  165. for i := 0; i < b.N; i++ {
  166. TestBytes(p)
  167. }
  168. }
  169. // Benchmarks the baseline bit testing performance.
  170. func BenchmarkBaseTest1KB(b *testing.B) { benchmarkBaseTest(b, 1024) }
  171. func BenchmarkBaseTest2KB(b *testing.B) { benchmarkBaseTest(b, 2048) }
  172. func BenchmarkBaseTest4KB(b *testing.B) { benchmarkBaseTest(b, 4096) }
  173. func benchmarkBaseTest(b *testing.B, size int) {
  174. p := make([]byte, size)
  175. for i := 0; i < b.N; i++ {
  176. safeTestBytes(p)
  177. }
  178. }