zero_test.go raw

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