1  
     2  
     3  
     4  
     5  package strconv_test
     6  
     7  import (
     8  	. "internal/strconv"
     9  	"math"
    10  	"testing"
    11  )
    12  
    13  var pow10Tests = []struct {
    14  	exp10 int
    15  	mant  uint128
    16  	exp2  int
    17  	ok    bool
    18  }{
    19  	{-349, uint128{0, 0}, 0, false},
    20  	{-348, uint128{0xFA8FD5A0081C0288, 0x1732C869CD60E453}, -1156, true},
    21  	{0, uint128{0x8000000000000000, 0x0000000000000000}, 1, true},
    22  	{347, uint128{0xD13EB46469447567, 0x4B7195F2D2D1A9FB}, 1153, true},
    23  	{348, uint128{0, 0}, 0, false},
    24  }
    25  
    26  func TestPow10(t *testing.T) {
    27  	for _, tt := range pow10Tests {
    28  		mant, exp2, ok := pow10(tt.exp10)
    29  		if mant != tt.mant || exp2 != tt.exp2 {
    30  			t.Errorf("pow10(%d) = %#016x, %#016x, %d, %v want %#016x,%#016x, %d, %v",
    31  				tt.exp10, mant.Hi, mant.Lo, exp2, ok,
    32  				tt.mant.Hi, tt.mant.Lo, tt.exp2, tt.ok)
    33  		}
    34  	}
    35  
    36  	for p := pow10Min; p <= pow10Max; p++ {
    37  		mant, exp2, ok := pow10(p)
    38  		if !ok {
    39  			t.Errorf("pow10(%d) not ok", p)
    40  			continue
    41  		}
    42  		
    43  		have := math.Ldexp(float64(mant.Hi), exp2-64)
    44  		want := math.Pow(10, float64(p))
    45  		if math.Abs(have-want)/want > 0.00001 {
    46  			t.Errorf("pow10(%d) = %#016x%016x/2^128 * 2^%d = %g want ~%g", p, mant.Hi, mant.Lo, exp2, have, want)
    47  		}
    48  	}
    49  
    50  }
    51  
    52  func u128(hi, lo uint64) uint128 {
    53  	return uint128{Hi: hi, Lo: lo}
    54  }
    55  
    56  var umul192Tests = []struct {
    57  	x   uint64
    58  	y   uint128
    59  	hi  uint64
    60  	mid uint64
    61  	lo  uint64
    62  }{
    63  	{0, u128(0, 0), 0, 0, 0},
    64  	{^uint64(0), u128(^uint64(0), ^uint64(0)), ^uint64(1), ^uint64(0), 1},
    65  }
    66  
    67  func TestUmul192(t *testing.T) {
    68  	for _, tt := range umul192Tests {
    69  		hi, mid, lo := Umul192(tt.x, tt.y)
    70  		if hi != tt.hi || mid != tt.mid || lo != tt.lo {
    71  			t.Errorf("umul192(%#x, {%#x,%#x}) = %#x, %#x, %#x, want %#x, %#x, %#x",
    72  				tt.x, tt.y.Hi, tt.y.Lo, hi, mid, lo, tt.hi, tt.mid, tt.lo)
    73  		}
    74  	}
    75  }
    76  
    77  func TestMulLog10_2(t *testing.T) {
    78  	for x := -1600; x <= +1600; x++ {
    79  		iMath := mulLog10_2(x)
    80  		fMath := int(math.Floor(float64(x) * math.Ln2 / math.Ln10))
    81  		if iMath != fMath {
    82  			t.Errorf("mulLog10_2(%d) failed: %d vs %d\n", x, iMath, fMath)
    83  		}
    84  	}
    85  }
    86  
    87  func TestMulLog2_10(t *testing.T) {
    88  	for x := -500; x <= +500; x++ {
    89  		iMath := mulLog2_10(x)
    90  		fMath := int(math.Floor(float64(x) * math.Ln10 / math.Ln2))
    91  		if iMath != fMath {
    92  			t.Errorf("mulLog2_10(%d) failed: %d vs %d\n", x, iMath, fMath)
    93  		}
    94  	}
    95  }
    96  
View as plain text