github.com/decred/dcrlnd@v0.7.6/internal/zero/zero_test.go (about) 1 // Copyright (c) 2015 The btcsuite developers 2 // Copyright (c) 2015-2019 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package zero 7 8 import ( 9 "fmt" 10 "math/big" 11 "strings" 12 "testing" 13 ) 14 15 func makeOneBytes(n int) []byte { 16 b := make([]byte, n) 17 for i := range b { 18 b[i] = 1 19 } 20 return b 21 } 22 23 func checkZeroBytes(b []byte) error { 24 for i, v := range b { 25 if v != 0 { 26 return fmt.Errorf("b[%d] = %d", i, v) 27 } 28 } 29 return nil 30 } 31 32 func TestBytes(t *testing.T) { 33 tests := []int{ 34 0, 35 31, 36 32, 37 33, 38 127, 39 128, 40 129, 41 255, 42 256, 43 256, 44 257, 45 383, 46 384, 47 385, 48 511, 49 512, 50 513, 51 } 52 53 for i, n := range tests { 54 b := makeOneBytes(n) 55 Bytes(b) 56 err := checkZeroBytes(b) 57 if err != nil { 58 t.Errorf("Test %d (n=%d) failed: %v", i, n, err) 59 continue 60 } 61 } 62 } 63 64 func checkZeroWords(b []big.Word) error { 65 for i, v := range b { 66 if v != 0 { 67 return fmt.Errorf("b[%d] = %d", i, v) 68 } 69 } 70 return nil 71 } 72 73 var bigZero = new(big.Int) 74 75 func TestBigInt(t *testing.T) { 76 tests := []string{ 77 // 16 0xFFFFFFFF 32-bit uintptrs 78 strings.Repeat("FFFFFFFF", 16), 79 80 // 17 32-bit uintptrs, minimum value which enters loop on 32-bit 81 "01" + strings.Repeat("00000000", 16), 82 83 // 32 0xFFFFFFFF 32-bit uintptrs, maximum value which enters loop exactly once on 32-bit 84 strings.Repeat("FFFFFFFF", 32), 85 86 // 33 32-bit uintptrs, minimum value which enters loop twice on 32-bit 87 "01" + strings.Repeat("00000000", 32), 88 89 // 16 0xFFFFFFFFFFFFFFFF 64-bit uintptrs 90 strings.Repeat("FFFFFFFFFFFFFFFF", 16), 91 92 // 17 64-bit uintptrs, minimum value which enters loop on 64-bit 93 "01" + strings.Repeat("0000000000000000", 16), 94 95 // 32 0xFFFFFFFFFFFFFFFF 64-bit uintptrs, maximum value which enters loop exactly once on 64-bit 96 strings.Repeat("FFFFFFFFFFFFFFFF", 32), 97 98 // 33 64-bit uintptrs, minimum value which enters loop twice on 64-bit 99 "01" + strings.Repeat("0000000000000000", 32), 100 } 101 102 for i, s := range tests { 103 v, ok := new(big.Int).SetString(s, 16) 104 if !ok { 105 t.Errorf("Test %d includes invalid hex number %s", i, s) 106 continue 107 } 108 109 BigInt(v) 110 err := checkZeroWords(v.Bits()) 111 if err != nil { 112 t.Errorf("Test %d (s=%s) failed: %v", i, s, err) 113 continue 114 } 115 if v.Cmp(bigZero) != 0 { 116 t.Errorf("Test %d (s=%s) zeroed big.Int represents non-zero number %v", i, s, v) 117 continue 118 } 119 } 120 } 121 122 func TestBytea32(t *testing.T) { 123 const sz = 32 124 var b [sz]byte 125 copy(b[:], makeOneBytes(sz)) 126 127 Bytea32(&b) 128 129 err := checkZeroBytes(b[:]) 130 if err != nil { 131 t.Error(err) 132 } 133 } 134 135 func TestBytea64(t *testing.T) { 136 const sz = 64 137 var b [sz]byte 138 copy(b[:], makeOneBytes(sz)) 139 140 Bytea64(&b) 141 142 err := checkZeroBytes(b[:]) 143 if err != nil { 144 t.Error(err) 145 } 146 }