github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/common/bitutil/bitutil_test.go (about) 1 package bitutil 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestXOR(t *testing.T) { 9 for alignP := 0; alignP < 2; alignP++ { 10 for alignQ := 0; alignQ < 2; alignQ++ { 11 for alignD := 0; alignD < 2; alignD++ { 12 p := make([]byte, 1023)[alignP:] 13 q := make([]byte, 1023)[alignQ:] 14 15 for i := 0; i < len(p); i++ { 16 p[i] = byte(i) 17 } 18 for i := 0; i < len(q); i++ { 19 q[i] = byte(len(q) - i) 20 } 21 d1 := make([]byte, 1023+alignD)[alignD:] 22 d2 := make([]byte, 1023+alignD)[alignD:] 23 24 XORBytes(d1, p, q) 25 safeXORBytes(d2, p, q) 26 if !bytes.Equal(d1, d2) { 27 t.Error("not equal", d1, d2) 28 } 29 } 30 } 31 } 32 } 33 34 func TestAND(t *testing.T) { 35 for alignP := 0; alignP < 2; alignP++ { 36 for alignQ := 0; alignQ < 2; alignQ++ { 37 for alignD := 0; alignD < 2; alignD++ { 38 p := make([]byte, 1023)[alignP:] 39 q := make([]byte, 1023)[alignQ:] 40 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 50 ANDBytes(d1, p, q) 51 safeANDBytes(d2, p, q) 52 if !bytes.Equal(d1, d2) { 53 t.Error("not equal") 54 } 55 } 56 } 57 } 58 } 59 60 func TestOR(t *testing.T) { 61 for alignP := 0; alignP < 2; alignP++ { 62 for alignQ := 0; alignQ < 2; alignQ++ { 63 for alignD := 0; alignD < 2; alignD++ { 64 p := make([]byte, 1023)[alignP:] 65 q := make([]byte, 1023)[alignQ:] 66 67 for i := 0; i < len(p); i++ { 68 p[i] = byte(i) 69 } 70 for i := 0; i < len(q); i++ { 71 q[i] = byte(len(q) - i) 72 } 73 d1 := make([]byte, 1023+alignD)[alignD:] 74 d2 := make([]byte, 1023+alignD)[alignD:] 75 76 ORBytes(d1, p, q) 77 safeORBytes(d2, p, q) 78 if !bytes.Equal(d1, d2) { 79 t.Error("not equal") 80 } 81 } 82 } 83 } 84 } 85 86 func TestTest(t *testing.T) { 87 for align := 0; align < 2; align++ { 88 89 p := make([]byte, 1023)[align:] 90 p[100] = 1 91 92 if TestBytes(p) != safeTestBytes(p) { 93 t.Error("not equal") 94 } 95 96 q := make([]byte, 1023)[align:] 97 q[len(q)-1] = 1 98 99 if TestBytes(q) != safeTestBytes(q) { 100 t.Error("not equal") 101 } 102 } 103 } 104 105 func BenchmarkFastXOR1KB(b *testing.B) { benchmarkFastXOR(b, 1024) } 106 func BenchmarkFastXOR2KB(b *testing.B) { benchmarkFastXOR(b, 2048) } 107 func BenchmarkFastXOR4KB(b *testing.B) { benchmarkFastXOR(b, 4096) } 108 109 func benchmarkFastXOR(b *testing.B, size int) { 110 p, q := make([]byte, size), make([]byte, size) 111 112 for i := 0; i < b.N; i++ { 113 XORBytes(p, p, q) 114 } 115 } 116 117 func BenchmarkBaseXOR1KB(b *testing.B) { benchmarkBaseXOR(b, 1024) } 118 func BenchmarkBaseXOR2KB(b *testing.B) { benchmarkBaseXOR(b, 2048) } 119 func BenchmarkBaseXOR4KB(b *testing.B) { benchmarkBaseXOR(b, 4096) } 120 121 func benchmarkBaseXOR(b *testing.B, size int) { 122 p, q := make([]byte, size), make([]byte, size) 123 124 for i := 0; i < b.N; i++ { 125 safeXORBytes(p, p, q) 126 } 127 } 128 129 func BenchmarkFastAND1KB(b *testing.B) { benchmarkFastAND(b, 1024) } 130 func BenchmarkFastAND2KB(b *testing.B) { benchmarkFastAND(b, 2048) } 131 func BenchmarkFastAND4KB(b *testing.B) { benchmarkFastAND(b, 4096) } 132 133 func benchmarkFastAND(b *testing.B, size int) { 134 p, q := make([]byte, size), make([]byte, size) 135 136 for i := 0; i < b.N; i++ { 137 ANDBytes(p, p, q) 138 } 139 } 140 141 func BenchmarkBaseAND1KB(b *testing.B) { benchmarkBaseAND(b, 1024) } 142 func BenchmarkBaseAND2KB(b *testing.B) { benchmarkBaseAND(b, 2048) } 143 func BenchmarkBaseAND4KB(b *testing.B) { benchmarkBaseAND(b, 4096) } 144 145 func benchmarkBaseAND(b *testing.B, size int) { 146 p, q := make([]byte, size), make([]byte, size) 147 148 for i := 0; i < b.N; i++ { 149 safeANDBytes(p, p, q) 150 } 151 } 152 153 func BenchmarkFastOR1KB(b *testing.B) { benchmarkFastOR(b, 1024) } 154 func BenchmarkFastOR2KB(b *testing.B) { benchmarkFastOR(b, 2048) } 155 func BenchmarkFastOR4KB(b *testing.B) { benchmarkFastOR(b, 4096) } 156 157 func benchmarkFastOR(b *testing.B, size int) { 158 p, q := make([]byte, size), make([]byte, size) 159 160 for i := 0; i < b.N; i++ { 161 ORBytes(p, p, q) 162 } 163 } 164 165 func BenchmarkBaseOR1KB(b *testing.B) { benchmarkBaseOR(b, 1024) } 166 func BenchmarkBaseOR2KB(b *testing.B) { benchmarkBaseOR(b, 2048) } 167 func BenchmarkBaseOR4KB(b *testing.B) { benchmarkBaseOR(b, 4096) } 168 169 func benchmarkBaseOR(b *testing.B, size int) { 170 p, q := make([]byte, size), make([]byte, size) 171 172 for i := 0; i < b.N; i++ { 173 safeORBytes(p, p, q) 174 } 175 } 176 177 func BenchmarkFastTest1KB(b *testing.B) { benchmarkFastTest(b, 1024) } 178 func BenchmarkFastTest2KB(b *testing.B) { benchmarkFastTest(b, 2048) } 179 func BenchmarkFastTest4KB(b *testing.B) { benchmarkFastTest(b, 4096) } 180 181 func benchmarkFastTest(b *testing.B, size int) { 182 p := make([]byte, size) 183 for i := 0; i < b.N; i++ { 184 TestBytes(p) 185 } 186 } 187 188 func BenchmarkBaseTest1KB(b *testing.B) { benchmarkBaseTest(b, 1024) } 189 func BenchmarkBaseTest2KB(b *testing.B) { benchmarkBaseTest(b, 2048) } 190 func BenchmarkBaseTest4KB(b *testing.B) { benchmarkBaseTest(b, 4096) } 191 192 func benchmarkBaseTest(b *testing.B, size int) { 193 p := make([]byte, size) 194 for i := 0; i < b.N; i++ { 195 safeTestBytes(p) 196 } 197 }