1
0
mirror of https://github.com/golang/go synced 2024-09-25 07:30:13 -06:00

essentially 100% coverage of strconv in tests.

fix a few bugs.

R=r
DELTA=294  (275 added, 9 deleted, 10 changed)
OCL=19595
CL=19595
This commit is contained in:
Russ Cox 2008-11-19 12:50:34 -08:00
parent 572cad8719
commit cf9b7f7534
9 changed files with 297 additions and 23 deletions

View File

@ -15,6 +15,8 @@ import (
"strconv"; "strconv";
) )
package var optimize = true // can change for testing
// TODO(rsc): Better truncation handling. // TODO(rsc): Better truncation handling.
func StringToDecimal(s string) (neg bool, d *Decimal, trunc bool, ok bool) { func StringToDecimal(s string) (neg bool, d *Decimal, trunc bool, ok bool) {
i := 0; i := 0;
@ -182,16 +184,7 @@ func DecimalToFloatBits(neg bool, d *Decimal, trunc bool, flt *FloatInfo) (b uin
// Denormalized? // Denormalized?
if mant&(1<<flt.mantbits) == 0 { if mant&(1<<flt.mantbits) == 0 {
if exp != flt.bias+1 { exp = flt.bias;
// TODO: remove - has no business panicking
panicln("DecimalToFloatBits", exp, flt.bias+1);
}
exp--;
} else {
if exp <= flt.bias {
// TODO: remove - has no business panicking
panicln("DecimalToFloatBits1", exp, flt.bias);
}
} }
goto out; goto out;
@ -327,8 +320,10 @@ export func atof64(s string) (f float64, err *os.Error) {
if !ok { if !ok {
return 0, os.EINVAL; return 0, os.EINVAL;
} }
if f, ok := DecimalToFloat64(neg, d, trunc); ok { if optimize {
return f, nil; if f, ok := DecimalToFloat64(neg, d, trunc); ok {
return f, nil;
}
} }
b, ovf := DecimalToFloatBits(neg, d, trunc, &float64info); b, ovf := DecimalToFloatBits(neg, d, trunc, &float64info);
f = sys.float64frombits(b); f = sys.float64frombits(b);
@ -343,8 +338,10 @@ export func atof32(s string) (f float32, err *os.Error) {
if !ok { if !ok {
return 0, os.EINVAL; return 0, os.EINVAL;
} }
if f, ok := DecimalToFloat32(neg, d, trunc); ok { if optimize {
return f, nil; if f, ok := DecimalToFloat32(neg, d, trunc); ok {
return f, nil;
}
} }
b, ovf := DecimalToFloatBits(neg, d, trunc, &float32info); b, ovf := DecimalToFloatBits(neg, d, trunc, &float32info);
f = sys.float32frombits(uint32(b)); f = sys.float32frombits(uint32(b));

View File

@ -42,6 +42,9 @@ func (a *Decimal) String() string {
buf := new([]byte, n); buf := new([]byte, n);
w := 0; w := 0;
switch { switch {
case a.nd == 0:
return "0";
case a.dp <= 0: case a.dp <= 0:
// zeros fill space between decimal point and digits // zeros fill space between decimal point and digits
buf[w] = '0'; buf[w] = '0';
@ -136,10 +139,11 @@ func RightShift(a *Decimal, k uint) {
for ; n>>k == 0; r++ { for ; n>>k == 0; r++ {
if r >= a.nd { if r >= a.nd {
if n == 0 { if n == 0 {
// a == 0; shouldn't get here, but handle anyway.
a.nd = 0; a.nd = 0;
return; return;
} }
for n >> k == 0 { for n>>k == 0 {
n = n*10; n = n*10;
r++; r++;
} }
@ -276,7 +280,7 @@ func LeftShift(a *Decimal, k uint) {
if w != 0 { if w != 0 {
// TODO: Remove - has no business panicking. // TODO: Remove - has no business panicking.
panic("fmt: bad LeftShift"); panicln("strconv: bad LeftShift", w);
} }
a.nd += delta; a.nd += delta;
a.dp += delta; a.dp += delta;
@ -287,6 +291,8 @@ func LeftShift(a *Decimal, k uint) {
// Returns receiver for convenience. // Returns receiver for convenience.
func (a *Decimal) Shift(k int) *Decimal { func (a *Decimal) Shift(k int) *Decimal {
switch { switch {
case a.nd == 0:
// nothing to do: a == 0
case k > 0: case k > 0:
for k > MaxShift { for k > MaxShift {
LeftShift(a, MaxShift); LeftShift(a, MaxShift);

View File

@ -144,6 +144,12 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string {
// that will let the original floating point value be precisely // that will let the original floating point value be precisely
// reconstructed. Size is original floating point size (64 or 32). // reconstructed. Size is original floating point size (64 or 32).
func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) { func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) {
// If mantissa is zero, the number is zero; stop now.
if mant == 0 {
d.nd = 0;
return;
}
// TODO: Unless exp == minexp, if the number of digits in d // TODO: Unless exp == minexp, if the number of digits in d
// is less than 17, it seems unlikely that it could not be // is less than 17, it seems unlikely that it could not be
// the shortest possible number already. So maybe we can // the shortest possible number already. So maybe we can

View File

@ -8,9 +8,9 @@ export func itoa64(i int64) string {
if i == 0 { if i == 0 {
return "0" return "0"
} }
neg := false; // negative neg := false; // negative
u := uint(i); u := uint64(i);
if i < 0 { if i < 0 {
neg = true; neg = true;
u = -u; u = -u;
@ -27,7 +27,7 @@ export func itoa64(i int64) string {
bp--; bp--;
b[bp] = '-' b[bp] = '-'
} }
// BUG return string(b[bp:len(b)]) // BUG return string(b[bp:len(b)])
return string((&b)[bp:len(b)]) return string((&b)[bp:len(b)])
} }

View File

@ -16,7 +16,11 @@ type Test struct {
} }
var tests = []Test { var tests = []Test {
Test{ "", "0", os.EINVAL },
Test{ "1", "1", nil }, Test{ "1", "1", nil },
Test{ "+1", "1", nil },
Test{ "1x", "0", os.EINVAL },
Test{ "1.1.", "0", os.EINVAL },
Test{ "1e23", "1e+23", nil }, Test{ "1e23", "1e+23", nil },
Test{ "100000000000000000000000", "1e+23", nil }, Test{ "100000000000000000000000", "1e+23", nil },
Test{ "1e-100", "1e-100", nil }, Test{ "1e-100", "1e-100", nil },
@ -29,6 +33,7 @@ var tests = []Test {
Test{ "-1", "-1", nil }, Test{ "-1", "-1", nil },
Test{ "-0", "0", nil }, Test{ "-0", "0", nil },
Test{ "1e-20", "1e-20", nil }, Test{ "1e-20", "1e-20", nil },
Test{ "625e-3", "0.625", nil },
// largest float64 // largest float64
Test{ "1.7976931348623157e308", "1.7976931348623157e+308", nil }, Test{ "1.7976931348623157e308", "1.7976931348623157e+308", nil },
@ -85,7 +90,9 @@ var tests = []Test {
Test{ ".e-1", "0", os.EINVAL }, Test{ ".e-1", "0", os.EINVAL },
} }
export func TestAtof() bool { func XTestAtof(opt bool) bool {
oldopt := strconv.optimize;
strconv.optimize = opt;
ok := true; ok := true;
for i := 0; i < len(tests); i++ { for i := 0; i < len(tests); i++ {
t := &tests[i]; t := &tests[i];
@ -96,6 +103,35 @@ export func TestAtof() bool {
t.in, out, err, t.out, t.err); t.in, out, err, t.out, t.err);
ok = false; ok = false;
} }
if float64(float32(out)) == out {
out32, err := strconv.atof32(t.in);
outs := strconv.ftoa32(out32, 'g', -1);
if outs != t.out || err != t.err {
fmt.printf("strconv.atof32(%v) = %v, %v want %v, %v # %v\n",
t.in, out32, err, t.out, t.err, out);
ok = false;
}
}
if floatsize == 64 || float64(float32(out)) == out {
outf, err := strconv.atof(t.in);
outs := strconv.ftoa(outf, 'g', -1);
if outs != t.out || err != t.err {
fmt.printf("strconv.ftoa(%v) = %v, %v want %v, %v # %v\n",
t.in, outf, err, t.out, t.err, out);
ok = false;
}
}
} }
strconv.optimize = oldopt;
return ok; return ok;
} }
export func TestAtof() bool {
return XTestAtof(true);
}
export func TestAtofSlow() bool {
return XTestAtof(false);
}

View File

@ -16,6 +16,7 @@ type Uint64Test struct {
} }
var uint64tests = []Uint64Test { var uint64tests = []Uint64Test {
Uint64Test{ "", 0, os.EINVAL },
Uint64Test{ "0", 0, nil }, Uint64Test{ "0", 0, nil },
Uint64Test{ "1", 1, nil }, Uint64Test{ "1", 1, nil },
Uint64Test{ "12345", 12345, nil }, Uint64Test{ "12345", 12345, nil },
@ -24,6 +25,7 @@ var uint64tests = []Uint64Test {
Uint64Test{ "98765432100", 98765432100, nil }, Uint64Test{ "98765432100", 98765432100, nil },
Uint64Test{ "18446744073709551615", 1<<64-1, nil }, Uint64Test{ "18446744073709551615", 1<<64-1, nil },
Uint64Test{ "18446744073709551616", 1<<64-1, os.ERANGE }, Uint64Test{ "18446744073709551616", 1<<64-1, os.ERANGE },
Uint64Test{ "18446744073709551620", 1<<64-1, os.ERANGE },
} }
type Int64Test struct { type Int64Test struct {
@ -33,6 +35,7 @@ type Int64Test struct {
} }
var int64tests = []Int64Test { var int64tests = []Int64Test {
Int64Test{ "", 0, os.EINVAL },
Int64Test{ "0", 0, nil }, Int64Test{ "0", 0, nil },
Int64Test{ "-0", 0, nil }, Int64Test{ "-0", 0, nil },
Int64Test{ "1", 1, nil }, Int64Test{ "1", 1, nil },
@ -60,6 +63,7 @@ type Uint32Test struct {
} }
var uint32tests = []Uint32Test { var uint32tests = []Uint32Test {
Uint32Test{ "", 0, os.EINVAL },
Uint32Test{ "0", 0, nil }, Uint32Test{ "0", 0, nil },
Uint32Test{ "1", 1, nil }, Uint32Test{ "1", 1, nil },
Uint32Test{ "12345", 12345, nil }, Uint32Test{ "12345", 12345, nil },
@ -77,6 +81,7 @@ type Int32Test struct {
} }
var int32tests = []Int32Test { var int32tests = []Int32Test {
Int32Test{ "", 0, os.EINVAL },
Int32Test{ "0", 0, nil }, Int32Test{ "0", 0, nil },
Int32Test{ "-0", 0, nil }, Int32Test{ "-0", 0, nil },
Int32Test{ "1", 1, nil }, Int32Test{ "1", 1, nil },

View File

@ -0,0 +1,128 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strconv
import (
"fmt";
"strconv"
)
type ShiftTest struct {
i uint64;
shift int;
out string;
}
var shifttests = []ShiftTest {
ShiftTest{ 0, -100, "0" },
ShiftTest{ 0, 100, "0" },
ShiftTest{ 1, 100, "1267650600228229401496703205376" },
ShiftTest{ 1, -100,
"0.00000000000000000000000000000078886090522101180541"
"17285652827862296732064351090230047702789306640625" },
ShiftTest{ 12345678, 8, "3160493568" },
ShiftTest{ 12345678, -8, "48225.3046875" },
ShiftTest{ 195312, 9, "99999744" },
ShiftTest{ 1953125, 9, "1000000000" },
}
export func TestDecimalShift() bool {
ok := true;
for i := 0; i < len(shifttests); i++ {
t := &shifttests[i];
s := strconv.NewDecimal(t.i).Shift(t.shift).String();
if s != t.out {
fmt.printf("Decimal %v << %v = %v, want %v\n",
t.i, t.shift, s, t.out);
ok = false;
}
}
return ok;
}
type RoundTest struct {
i uint64;
nd int;
down, round, up string;
int uint64;
}
var roundtests = []RoundTest {
RoundTest{ 0, 4, "0", "0", "0", 0 },
RoundTest{ 12344999, 4, "12340000", "12340000", "12350000", 12340000 },
RoundTest{ 12345000, 4, "12340000", "12340000", "12350000", 12340000 },
RoundTest{ 12345001, 4, "12340000", "12350000", "12350000", 12350000 },
RoundTest{ 23454999, 4, "23450000", "23450000", "23460000", 23450000 },
RoundTest{ 23455000, 4, "23450000", "23460000", "23460000", 23460000 },
RoundTest{ 23455001, 4, "23450000", "23460000", "23460000", 23460000 },
RoundTest{ 99994999, 4, "99990000", "99990000", "100000000", 99990000 },
RoundTest{ 99995000, 4, "99990000", "100000000", "100000000", 100000000 },
RoundTest{ 99999999, 4, "99990000", "100000000", "100000000", 100000000 },
RoundTest{ 12994999, 4, "12990000", "12990000", "13000000", 12990000 },
RoundTest{ 12995000, 4, "12990000", "13000000", "13000000", 13000000 },
RoundTest{ 12999999, 4, "12990000", "13000000", "13000000", 13000000 },
}
export func TestDecimalRound() bool {
ok := true;
for i := 0; i < len(roundtests); i++ {
t := &roundtests[i];
s := strconv.NewDecimal(t.i).RoundDown(t.nd).String();
if s != t.down {
fmt.printf("Decimal %v RoundDown %d = %v, want %v\n",
t.i, t.nd, s, t.down);
ok = false;
}
s = strconv.NewDecimal(t.i).Round(t.nd).String();
if s != t.round {
fmt.printf("Decimal %v Round %d = %v, want %v\n",
t.i, t.nd, s, t.down);
ok = false;
}
s = strconv.NewDecimal(t.i).RoundUp(t.nd).String();
if s != t.up {
fmt.printf("Decimal %v RoundUp %d = %v, want %v\n",
t.i, t.nd, s, t.up);
ok = false;
}
}
return ok;
}
type RoundIntTest struct {
i uint64;
shift int;
int uint64;
}
var roundinttests = []RoundIntTest {
RoundIntTest{ 0, 100, 0 },
RoundIntTest{ 512, -8, 2 },
RoundIntTest{ 513, -8, 2 },
RoundIntTest{ 640, -8, 2 },
RoundIntTest{ 641, -8, 3 },
RoundIntTest{ 384, -8, 2 },
RoundIntTest{ 385, -8, 2 },
RoundIntTest{ 383, -8, 1 },
RoundIntTest{ 1, 100, 1<<64-1 },
RoundIntTest{ 1000, 0, 1000 },
}
export func TestDecimalRoundedInteger() bool {
ok := true;
for i := 0; i < len(roundinttests); i++ {
t := roundinttests[i];
// TODO: should be able to use int := here.
int1 := strconv.NewDecimal(t.i).Shift(t.shift).RoundedInteger();
if int1 != t.int {
fmt.printf("Decimal %v >> %v RoundedInteger = %v, want %v\n",
t.i, t.shift, int1, t.int);
ok = false;
}
}
return ok;
}

View File

@ -12,6 +12,8 @@ type Test struct {
s string; s string;
} }
func fdiv(a, b float64) float64 { return a / b } // keep compiler in the dark
// TODO: Should be able to call this tests but it conflicts with testatof.go // TODO: Should be able to call this tests but it conflicts with testatof.go
var ftests = []Test { var ftests = []Test {
Test{ 1, 'e', 5, "1.00000e+00" }, Test{ 1, 'e', 5, "1.00000e+00" },
@ -66,8 +68,22 @@ var ftests = []Test {
Test{ 1e23+8.5e6, 'e', -1, "1.0000000000000001e+23" }, Test{ 1e23+8.5e6, 'e', -1, "1.0000000000000001e+23" },
Test{ 1e23+8.5e6, 'f', -1, "100000000000000010000000" }, Test{ 1e23+8.5e6, 'f', -1, "100000000000000010000000" },
Test{ 1e23+8.5e6, 'g', -1, "1.0000000000000001e+23" }, Test{ 1e23+8.5e6, 'g', -1, "1.0000000000000001e+23" },
Test{ fdiv(5e-304, 1e20), 'g', -1, "5e-324" },
Test{ fdiv(-5e-304, 1e20), 'g', -1, "-5e-324" },
Test{ 32, 'g', -1, "32" }, Test{ 32, 'g', -1, "32" },
Test{ 32, 'g', 0, "3e+01" },
Test{ 100, 'x', -1, "%x" },
Test{ sys.NaN(), 'g', -1, "NaN" },
Test{ -sys.NaN(), 'g', -1, "NaN" },
Test{ sys.Inf(0), 'g', -1, "+Inf" },
Test{ sys.Inf(-1), 'g', -1, "-Inf" },
Test{ -sys.Inf(0), 'g', -1, "-Inf" },
Test{ -1, 'b', -1, "-4503599627370496p-52" },
} }
export func TestFtoa() bool { export func TestFtoa() bool {
@ -82,13 +98,13 @@ export func TestFtoa() bool {
println("test", t.f, string(t.fmt), t.prec, "want", t.s, "got", s); println("test", t.f, string(t.fmt), t.prec, "want", t.s, "got", s);
ok = false; ok = false;
} }
if float64(float32(t.f)) == t.f { if float64(float32(t.f)) == t.f && t.fmt != 'b' {
s := strconv.ftoa32(float32(t.f), t.fmt, t.prec); s := strconv.ftoa32(float32(t.f), t.fmt, t.prec);
if s != t.s { if s != t.s {
println("test32", t.f, string(t.fmt), t.prec, "want", t.s, "got", s); println("test32", t.f, string(t.fmt), t.prec, "want", t.s, "got", s);
ok = false; ok = false;
} }
} }
} }
return ok; return ok;
} }

View File

@ -0,0 +1,80 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strconv
import (
"fmt";
"os";
"strconv";
)
type Int64Test struct {
in int64;
out string;
}
// TODO: should be called int64tests
var xint64tests = []Int64Test {
Int64Test{ 0, "0" },
Int64Test{ 1, "1" },
Int64Test{ -1, "-1" },
Int64Test{ 12345678, "12345678" },
Int64Test{ -987654321, "-987654321" },
Int64Test{ 1<<31-1, "2147483647" },
Int64Test{ -1<<31+1, "-2147483647" },
Int64Test{ 1<<31, "2147483648" },
Int64Test{ -1<<31, "-2147483648" },
Int64Test{ 1<<31+1, "2147483649" },
Int64Test{ -1<<31-1, "-2147483649" },
Int64Test{ 1<<32-1, "4294967295" },
Int64Test{ -1<<32+1, "-4294967295" },
Int64Test{ 1<<32, "4294967296" },
Int64Test{ -1<<32, "-4294967296" },
Int64Test{ 1<<32+1, "4294967297" },
Int64Test{ -1<<32-1, "-4294967297" },
Int64Test{ 1<<50, "1125899906842624" },
Int64Test{ 1<<63-1, "9223372036854775807" },
Int64Test{ -1<<63+1, "-9223372036854775807" },
Int64Test{ -1<<63, "-9223372036854775808" },
}
export func TestItoa() bool {
ok := true;
for i := 0; i < len(xint64tests); i++ {
t := xint64tests[i];
s := strconv.itoa64(t.in);
if s != t.out {
fmt.printf("strconv.itoa64(%v) = %v want %v\n",
t.in, s, t.out);
ok = false;
}
if int64(int(t.in)) == t.in {
s := strconv.itoa(int(t.in));
if s != t.out {
fmt.printf("strconv.itoa(%v) = %v want %v\n",
t.in, s, t.out);
ok = false;
}
}
}
return ok;
}
// TODO: Use once there is a strconv.uitoa
type Uint64Test struct {
in uint64;
out string;
}
// TODO: should be able to call this uint64tests.
var xuint64tests = []Uint64Test {
Uint64Test{ 1<<63-1, "9223372036854775807" },
Uint64Test{ 1<<63, "9223372036854775808" },
Uint64Test{ 1<<63+1, "9223372036854775809" },
Uint64Test{ 1<<64-2, "18446744073709551614" },
Uint64Test{ 1<<64-1, "18446744073709551615" },
}