github.com/klaytn/klaytn@v1.12.1/common/bitutil/bitutil_test.go (about)

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