Source file
src/strconv/math_test.go
1
2
3
4
5 package strconv_test
6
7 import (
8 "math"
9 . "strconv"
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}, -1157, true},
21 {0, uint128{0x8000000000000000, 0x0000000000000000}, 0, true},
22 {347, uint128{0xD13EB46469447567, 0x4B7195F2D2D1A9FB}, 1152, 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
37 func u128(hi, lo uint64) uint128 {
38 return uint128{Hi: hi, Lo: lo}
39 }
40
41 var umul192Tests = []struct {
42 x uint64
43 y uint128
44 hi uint64
45 mid uint64
46 lo uint64
47 }{
48 {0, u128(0, 0), 0, 0, 0},
49 {^uint64(0), u128(^uint64(0), ^uint64(0)), ^uint64(1), ^uint64(0), 1},
50 }
51
52 func TestUmul192(t *testing.T) {
53 for _, tt := range umul192Tests {
54 hi, mid, lo := Umul192(tt.x, tt.y)
55 if hi != tt.hi || mid != tt.mid || lo != tt.lo {
56 t.Errorf("umul192(%#x, {%#x,%#x}) = %#x, %#x, %#x, want %#x, %#x, %#x",
57 tt.x, tt.y.Hi, tt.y.Lo, hi, mid, lo, tt.hi, tt.mid, tt.lo)
58 }
59 }
60 }
61
62 func TestMulLog10_2(t *testing.T) {
63 for x := -1600; x <= +1600; x++ {
64 iMath := mulLog10_2(x)
65 fMath := int(math.Floor(float64(x) * math.Ln2 / math.Ln10))
66 if iMath != fMath {
67 t.Errorf("mulLog10_2(%d) failed: %d vs %d\n", x, iMath, fMath)
68 }
69 }
70 }
71
72 func TestMulLog2_10(t *testing.T) {
73 for x := -500; x <= +500; x++ {
74 iMath := mulLog2_10(x)
75 fMath := int(math.Floor(float64(x) * math.Ln10 / math.Ln2))
76 if iMath != fMath {
77 t.Errorf("mulLog2_10(%d) failed: %d vs %d\n", x, iMath, fMath)
78 }
79 }
80 }
81
View as plain text