diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
commit | 3e45412327a2654a77944249962b3652e6142299 (patch) | |
tree | bc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/strconv | |
parent | c533680039762cacbc37db8dc7eed074c3e497be (diff) | |
download | golang-upstream/2011.01.12.tar.gz |
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/strconv')
-rw-r--r-- | src/pkg/strconv/Makefile | 2 | ||||
-rw-r--r-- | src/pkg/strconv/atob_test.go | 26 | ||||
-rw-r--r-- | src/pkg/strconv/atof.go | 44 | ||||
-rw-r--r-- | src/pkg/strconv/atof_test.go | 131 | ||||
-rw-r--r-- | src/pkg/strconv/atoi.go | 2 | ||||
-rw-r--r-- | src/pkg/strconv/atoi_test.go | 204 | ||||
-rw-r--r-- | src/pkg/strconv/decimal.go | 56 | ||||
-rw-r--r-- | src/pkg/strconv/decimal_test.go | 72 | ||||
-rw-r--r-- | src/pkg/strconv/fp_test.go | 2 | ||||
-rw-r--r-- | src/pkg/strconv/ftoa.go | 12 | ||||
-rw-r--r-- | src/pkg/strconv/ftoa_test.go | 178 | ||||
-rw-r--r-- | src/pkg/strconv/itoa_test.go | 116 | ||||
-rw-r--r-- | src/pkg/strconv/quote.go | 13 | ||||
-rw-r--r-- | src/pkg/strconv/quote_test.go | 154 |
14 files changed, 534 insertions, 478 deletions
diff --git a/src/pkg/strconv/Makefile b/src/pkg/strconv/Makefile index 57849a821..823355d85 100644 --- a/src/pkg/strconv/Makefile +++ b/src/pkg/strconv/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../Make.$(GOARCH) +include ../../Make.inc TARG=strconv GOFILES=\ diff --git a/src/pkg/strconv/atob_test.go b/src/pkg/strconv/atob_test.go index ffad4b21b..497df5b18 100644 --- a/src/pkg/strconv/atob_test.go +++ b/src/pkg/strconv/atob_test.go @@ -17,18 +17,18 @@ type atobTest struct { } var atobtests = []atobTest{ - atobTest{"", false, os.EINVAL}, - atobTest{"asdf", false, os.EINVAL}, - atobTest{"0", false, nil}, - atobTest{"f", false, nil}, - atobTest{"F", false, nil}, - atobTest{"FALSE", false, nil}, - atobTest{"false", false, nil}, - atobTest{"1", true, nil}, - atobTest{"t", true, nil}, - atobTest{"T", true, nil}, - atobTest{"TRUE", true, nil}, - atobTest{"true", true, nil}, + {"", false, os.EINVAL}, + {"asdf", false, os.EINVAL}, + {"0", false, nil}, + {"f", false, nil}, + {"F", false, nil}, + {"FALSE", false, nil}, + {"false", false, nil}, + {"1", true, nil}, + {"t", true, nil}, + {"T", true, nil}, + {"TRUE", true, nil}, + {"true", true, nil}, } func TestAtob(t *testing.T) { @@ -46,7 +46,7 @@ func TestAtob(t *testing.T) { } } else { if e != nil { - t.Errorf("%s: expected no error but got %s", test.in, test.err, e) + t.Errorf("%s: expected no error but got %s", test.in, e) } if b != test.out { t.Errorf("%s: expected %t but got %t", test.in, test.out, b) diff --git a/src/pkg/strconv/atof.go b/src/pkg/strconv/atof.go index 262a8b53c..bcb138f7a 100644 --- a/src/pkg/strconv/atof.go +++ b/src/pkg/strconv/atof.go @@ -19,6 +19,40 @@ import ( var optimize = true // can change for testing +func equalIgnoreCase(s1, s2 string) bool { + if len(s1) != len(s2) { + return false + } + for i := 0; i < len(s1); i++ { + c1 := s1[i] + if 'A' <= c1 && c1 <= 'Z' { + c1 += 'a' - 'A' + } + c2 := s2[i] + if 'A' <= c2 && c2 <= 'Z' { + c2 += 'a' - 'A' + } + if c1 != c2 { + return false + } + } + return true +} + +func special(s string) (f float64, ok bool) { + switch { + case equalIgnoreCase(s, "nan"): + return math.NaN(), true + case equalIgnoreCase(s, "-inf"): + return math.Inf(-1), true + case equalIgnoreCase(s, "+inf"): + return math.Inf(1), true + case equalIgnoreCase(s, "inf"): + return math.Inf(1), true + } + return +} + // TODO(rsc): Better truncation handling. func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { i := 0 @@ -73,7 +107,7 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { // just be sure to move the decimal point by // a lot (say, 100000). it doesn't matter if it's // not the exact number. - if i < len(s) && s[i] == 'e' { + if i < len(s) && (s[i] == 'e' || s[i] == 'E') { i++ if i >= len(s) { return @@ -320,6 +354,10 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { // away from the largest floating point number of the given size, // Atof32 returns f = ±Inf, err.Error = os.ERANGE. func Atof32(s string) (f float32, err os.Error) { + if val, ok := special(s); ok { + return float32(val), nil + } + neg, d, trunc, ok := stringToDecimal(s) if !ok { return 0, &NumError{s, os.EINVAL} @@ -341,6 +379,10 @@ func Atof32(s string) (f float32, err os.Error) { // Except for the type of its result, its definition is the same as that // of Atof32. func Atof64(s string) (f float64, err os.Error) { + if val, ok := special(s); ok { + return val, nil + } + neg, d, trunc, ok := stringToDecimal(s) if !ok { return 0, &NumError{s, os.EINVAL} diff --git a/src/pkg/strconv/atof_test.go b/src/pkg/strconv/atof_test.go index 0039a6e44..68c50bfbe 100644 --- a/src/pkg/strconv/atof_test.go +++ b/src/pkg/strconv/atof_test.go @@ -18,80 +18,91 @@ type atofTest struct { } var atoftests = []atofTest{ - atofTest{"", "0", os.EINVAL}, - atofTest{"1", "1", nil}, - atofTest{"+1", "1", nil}, - atofTest{"1x", "0", os.EINVAL}, - atofTest{"1.1.", "0", os.EINVAL}, - atofTest{"1e23", "1e+23", nil}, - atofTest{"100000000000000000000000", "1e+23", nil}, - atofTest{"1e-100", "1e-100", nil}, - atofTest{"123456700", "1.234567e+08", nil}, - atofTest{"99999999999999974834176", "9.999999999999997e+22", nil}, - atofTest{"100000000000000000000001", "1.0000000000000001e+23", nil}, - atofTest{"100000000000000008388608", "1.0000000000000001e+23", nil}, - atofTest{"100000000000000016777215", "1.0000000000000001e+23", nil}, - atofTest{"100000000000000016777216", "1.0000000000000003e+23", nil}, - atofTest{"-1", "-1", nil}, - atofTest{"-0", "-0", nil}, - atofTest{"1e-20", "1e-20", nil}, - atofTest{"625e-3", "0.625", nil}, + {"", "0", os.EINVAL}, + {"1", "1", nil}, + {"+1", "1", nil}, + {"1x", "0", os.EINVAL}, + {"1.1.", "0", os.EINVAL}, + {"1e23", "1e+23", nil}, + {"1E23", "1e+23", nil}, + {"100000000000000000000000", "1e+23", nil}, + {"1e-100", "1e-100", nil}, + {"123456700", "1.234567e+08", nil}, + {"99999999999999974834176", "9.999999999999997e+22", nil}, + {"100000000000000000000001", "1.0000000000000001e+23", nil}, + {"100000000000000008388608", "1.0000000000000001e+23", nil}, + {"100000000000000016777215", "1.0000000000000001e+23", nil}, + {"100000000000000016777216", "1.0000000000000003e+23", nil}, + {"-1", "-1", nil}, + {"-0", "-0", nil}, + {"1e-20", "1e-20", nil}, + {"625e-3", "0.625", nil}, + + // NaNs + {"nan", "NaN", nil}, + {"NaN", "NaN", nil}, + {"NAN", "NaN", nil}, + + // Infs + {"inf", "+Inf", nil}, + {"-Inf", "-Inf", nil}, + {"+INF", "+Inf", nil}, // largest float64 - atofTest{"1.7976931348623157e308", "1.7976931348623157e+308", nil}, - atofTest{"-1.7976931348623157e308", "-1.7976931348623157e+308", nil}, + {"1.7976931348623157e308", "1.7976931348623157e+308", nil}, + {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil}, // next float64 - too large - atofTest{"1.7976931348623159e308", "+Inf", os.ERANGE}, - atofTest{"-1.7976931348623159e308", "-Inf", os.ERANGE}, + {"1.7976931348623159e308", "+Inf", os.ERANGE}, + {"-1.7976931348623159e308", "-Inf", os.ERANGE}, // the border is ...158079 // borderline - okay - atofTest{"1.7976931348623158e308", "1.7976931348623157e+308", nil}, - atofTest{"-1.7976931348623158e308", "-1.7976931348623157e+308", nil}, + {"1.7976931348623158e308", "1.7976931348623157e+308", nil}, + {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil}, // borderline - too large - atofTest{"1.797693134862315808e308", "+Inf", os.ERANGE}, - atofTest{"-1.797693134862315808e308", "-Inf", os.ERANGE}, + {"1.797693134862315808e308", "+Inf", os.ERANGE}, + {"-1.797693134862315808e308", "-Inf", os.ERANGE}, // a little too large - atofTest{"1e308", "1e+308", nil}, - atofTest{"2e308", "+Inf", os.ERANGE}, - atofTest{"1e309", "+Inf", os.ERANGE}, + {"1e308", "1e+308", nil}, + {"2e308", "+Inf", os.ERANGE}, + {"1e309", "+Inf", os.ERANGE}, // way too large - atofTest{"1e310", "+Inf", os.ERANGE}, - atofTest{"-1e310", "-Inf", os.ERANGE}, - atofTest{"1e400", "+Inf", os.ERANGE}, - atofTest{"-1e400", "-Inf", os.ERANGE}, - atofTest{"1e400000", "+Inf", os.ERANGE}, - atofTest{"-1e400000", "-Inf", os.ERANGE}, + {"1e310", "+Inf", os.ERANGE}, + {"-1e310", "-Inf", os.ERANGE}, + {"1e400", "+Inf", os.ERANGE}, + {"-1e400", "-Inf", os.ERANGE}, + {"1e400000", "+Inf", os.ERANGE}, + {"-1e400000", "-Inf", os.ERANGE}, // denormalized - atofTest{"1e-305", "1e-305", nil}, - atofTest{"1e-306", "1e-306", nil}, - atofTest{"1e-307", "1e-307", nil}, - atofTest{"1e-308", "1e-308", nil}, - atofTest{"1e-309", "1e-309", nil}, - atofTest{"1e-310", "1e-310", nil}, - atofTest{"1e-322", "1e-322", nil}, + {"1e-305", "1e-305", nil}, + {"1e-306", "1e-306", nil}, + {"1e-307", "1e-307", nil}, + {"1e-308", "1e-308", nil}, + {"1e-309", "1e-309", nil}, + {"1e-310", "1e-310", nil}, + {"1e-322", "1e-322", nil}, // smallest denormal - atofTest{"5e-324", "5e-324", nil}, - atofTest{"4e-324", "5e-324", nil}, - atofTest{"3e-324", "5e-324", nil}, + {"5e-324", "5e-324", nil}, + {"4e-324", "5e-324", nil}, + {"3e-324", "5e-324", nil}, // too small - atofTest{"2e-324", "0", nil}, + {"2e-324", "0", nil}, // way too small - atofTest{"1e-350", "0", nil}, - atofTest{"1e-400000", "0", nil}, + {"1e-350", "0", nil}, + {"1e-400000", "0", nil}, // try to overflow exponent - atofTest{"1e-4294967296", "0", nil}, - atofTest{"1e+4294967296", "+Inf", os.ERANGE}, - atofTest{"1e-18446744073709551616", "0", nil}, - atofTest{"1e+18446744073709551616", "+Inf", os.ERANGE}, + {"1e-4294967296", "0", nil}, + {"1e+4294967296", "+Inf", os.ERANGE}, + {"1e-18446744073709551616", "0", nil}, + {"1e+18446744073709551616", "+Inf", os.ERANGE}, // Parse errors - atofTest{"1e", "0", os.EINVAL}, - atofTest{"1e-", "0", os.EINVAL}, - atofTest{".e-1", "0", os.EINVAL}, + {"1e", "0", os.EINVAL}, + {"1e-", "0", os.EINVAL}, + {".e-1", "0", os.EINVAL}, } func init() { @@ -112,14 +123,14 @@ func testAtof(t *testing.T, opt bool) { out, err := Atof64(test.in) outs := Ftoa64(out, 'g', -1) if outs != test.out || !reflect.DeepEqual(err, test.err) { - t.Errorf("Atof64(%v) = %v, %v want %v, %v\n", + t.Errorf("Atof64(%v) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } out, err = AtofN(test.in, 64) outs = FtoaN(out, 'g', -1, 64) if outs != test.out || !reflect.DeepEqual(err, test.err) { - t.Errorf("AtofN(%v, 64) = %v, %v want %v, %v\n", + t.Errorf("AtofN(%v, 64) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } @@ -127,7 +138,7 @@ func testAtof(t *testing.T, opt bool) { out32, err := Atof32(test.in) outs := Ftoa32(out32, 'g', -1) if outs != test.out || !reflect.DeepEqual(err, test.err) { - t.Errorf("Atof32(%v) = %v, %v want %v, %v # %v\n", + t.Errorf("Atof32(%v) = %v, %v want %v, %v # %v", test.in, out32, err, test.out, test.err, out) } @@ -135,7 +146,7 @@ func testAtof(t *testing.T, opt bool) { out32 = float32(out) outs = FtoaN(float64(out32), 'g', -1, 32) if outs != test.out || !reflect.DeepEqual(err, test.err) { - t.Errorf("AtofN(%v, 32) = %v, %v want %v, %v # %v\n", + t.Errorf("AtofN(%v, 32) = %v, %v want %v, %v # %v", test.in, out32, err, test.out, test.err, out) } } @@ -144,7 +155,7 @@ func testAtof(t *testing.T, opt bool) { outf, err := Atof(test.in) outs := Ftoa(outf, 'g', -1) if outs != test.out || !reflect.DeepEqual(err, test.err) { - t.Errorf("Ftoa(%v) = %v, %v want %v, %v # %v\n", + t.Errorf("Ftoa(%v) = %v, %v want %v, %v # %v", test.in, outf, err, test.out, test.err, out) } } diff --git a/src/pkg/strconv/atoi.go b/src/pkg/strconv/atoi.go index e82b6cdba..f7b845672 100644 --- a/src/pkg/strconv/atoi.go +++ b/src/pkg/strconv/atoi.go @@ -11,7 +11,7 @@ type NumError struct { Error os.Error } -func (e *NumError) String() string { return "parsing " + e.Num + ": " + e.Error.String() } +func (e *NumError) String() string { return `parsing "` + e.Num + `": ` + e.Error.String() } func computeIntsize() uint { diff --git a/src/pkg/strconv/atoi_test.go b/src/pkg/strconv/atoi_test.go index 7df930342..0b9f29553 100644 --- a/src/pkg/strconv/atoi_test.go +++ b/src/pkg/strconv/atoi_test.go @@ -18,37 +18,37 @@ type atoui64Test struct { } var atoui64tests = []atoui64Test{ - atoui64Test{"", 0, os.EINVAL}, - atoui64Test{"0", 0, nil}, - atoui64Test{"1", 1, nil}, - atoui64Test{"12345", 12345, nil}, - atoui64Test{"012345", 12345, nil}, - atoui64Test{"12345x", 0, os.EINVAL}, - atoui64Test{"98765432100", 98765432100, nil}, - atoui64Test{"18446744073709551615", 1<<64 - 1, nil}, - atoui64Test{"18446744073709551616", 1<<64 - 1, os.ERANGE}, - atoui64Test{"18446744073709551620", 1<<64 - 1, os.ERANGE}, + {"", 0, os.EINVAL}, + {"0", 0, nil}, + {"1", 1, nil}, + {"12345", 12345, nil}, + {"012345", 12345, nil}, + {"12345x", 0, os.EINVAL}, + {"98765432100", 98765432100, nil}, + {"18446744073709551615", 1<<64 - 1, nil}, + {"18446744073709551616", 1<<64 - 1, os.ERANGE}, + {"18446744073709551620", 1<<64 - 1, os.ERANGE}, } var btoui64tests = []atoui64Test{ - atoui64Test{"", 0, os.EINVAL}, - atoui64Test{"0", 0, nil}, - atoui64Test{"1", 1, nil}, - atoui64Test{"12345", 12345, nil}, - atoui64Test{"012345", 012345, nil}, - atoui64Test{"0x12345", 0x12345, nil}, - atoui64Test{"0X12345", 0x12345, nil}, - atoui64Test{"12345x", 0, os.EINVAL}, - atoui64Test{"98765432100", 98765432100, nil}, - atoui64Test{"18446744073709551615", 1<<64 - 1, nil}, - atoui64Test{"18446744073709551616", 1<<64 - 1, os.ERANGE}, - atoui64Test{"18446744073709551620", 1<<64 - 1, os.ERANGE}, - atoui64Test{"0xFFFFFFFFFFFFFFFF", 1<<64 - 1, nil}, - atoui64Test{"0x10000000000000000", 1<<64 - 1, os.ERANGE}, - atoui64Test{"01777777777777777777777", 1<<64 - 1, nil}, - atoui64Test{"01777777777777777777778", 0, os.EINVAL}, - atoui64Test{"02000000000000000000000", 1<<64 - 1, os.ERANGE}, - atoui64Test{"0200000000000000000000", 1 << 61, nil}, + {"", 0, os.EINVAL}, + {"0", 0, nil}, + {"1", 1, nil}, + {"12345", 12345, nil}, + {"012345", 012345, nil}, + {"0x12345", 0x12345, nil}, + {"0X12345", 0x12345, nil}, + {"12345x", 0, os.EINVAL}, + {"98765432100", 98765432100, nil}, + {"18446744073709551615", 1<<64 - 1, nil}, + {"18446744073709551616", 1<<64 - 1, os.ERANGE}, + {"18446744073709551620", 1<<64 - 1, os.ERANGE}, + {"0xFFFFFFFFFFFFFFFF", 1<<64 - 1, nil}, + {"0x10000000000000000", 1<<64 - 1, os.ERANGE}, + {"01777777777777777777777", 1<<64 - 1, nil}, + {"01777777777777777777778", 0, os.EINVAL}, + {"02000000000000000000000", 1<<64 - 1, os.ERANGE}, + {"0200000000000000000000", 1 << 61, nil}, } type atoi64Test struct { @@ -58,47 +58,47 @@ type atoi64Test struct { } var atoi64tests = []atoi64Test{ - atoi64Test{"", 0, os.EINVAL}, - atoi64Test{"0", 0, nil}, - atoi64Test{"-0", 0, nil}, - atoi64Test{"1", 1, nil}, - atoi64Test{"-1", -1, nil}, - atoi64Test{"12345", 12345, nil}, - atoi64Test{"-12345", -12345, nil}, - atoi64Test{"012345", 12345, nil}, - atoi64Test{"-012345", -12345, nil}, - atoi64Test{"98765432100", 98765432100, nil}, - atoi64Test{"-98765432100", -98765432100, nil}, - atoi64Test{"9223372036854775807", 1<<63 - 1, nil}, - atoi64Test{"-9223372036854775807", -(1<<63 - 1), nil}, - atoi64Test{"9223372036854775808", 1<<63 - 1, os.ERANGE}, - atoi64Test{"-9223372036854775808", -1 << 63, nil}, - atoi64Test{"9223372036854775809", 1<<63 - 1, os.ERANGE}, - atoi64Test{"-9223372036854775809", -1 << 63, os.ERANGE}, + {"", 0, os.EINVAL}, + {"0", 0, nil}, + {"-0", 0, nil}, + {"1", 1, nil}, + {"-1", -1, nil}, + {"12345", 12345, nil}, + {"-12345", -12345, nil}, + {"012345", 12345, nil}, + {"-012345", -12345, nil}, + {"98765432100", 98765432100, nil}, + {"-98765432100", -98765432100, nil}, + {"9223372036854775807", 1<<63 - 1, nil}, + {"-9223372036854775807", -(1<<63 - 1), nil}, + {"9223372036854775808", 1<<63 - 1, os.ERANGE}, + {"-9223372036854775808", -1 << 63, nil}, + {"9223372036854775809", 1<<63 - 1, os.ERANGE}, + {"-9223372036854775809", -1 << 63, os.ERANGE}, } var btoi64tests = []atoi64Test{ - atoi64Test{"", 0, os.EINVAL}, - atoi64Test{"0", 0, nil}, - atoi64Test{"-0", 0, nil}, - atoi64Test{"1", 1, nil}, - atoi64Test{"-1", -1, nil}, - atoi64Test{"12345", 12345, nil}, - atoi64Test{"-12345", -12345, nil}, - atoi64Test{"012345", 012345, nil}, - atoi64Test{"-012345", -012345, nil}, - atoi64Test{"0x12345", 0x12345, nil}, - atoi64Test{"-0X12345", -0x12345, nil}, - atoi64Test{"12345x", 0, os.EINVAL}, - atoi64Test{"-12345x", 0, os.EINVAL}, - atoi64Test{"98765432100", 98765432100, nil}, - atoi64Test{"-98765432100", -98765432100, nil}, - atoi64Test{"9223372036854775807", 1<<63 - 1, nil}, - atoi64Test{"-9223372036854775807", -(1<<63 - 1), nil}, - atoi64Test{"9223372036854775808", 1<<63 - 1, os.ERANGE}, - atoi64Test{"-9223372036854775808", -1 << 63, nil}, - atoi64Test{"9223372036854775809", 1<<63 - 1, os.ERANGE}, - atoi64Test{"-9223372036854775809", -1 << 63, os.ERANGE}, + {"", 0, os.EINVAL}, + {"0", 0, nil}, + {"-0", 0, nil}, + {"1", 1, nil}, + {"-1", -1, nil}, + {"12345", 12345, nil}, + {"-12345", -12345, nil}, + {"012345", 012345, nil}, + {"-012345", -012345, nil}, + {"0x12345", 0x12345, nil}, + {"-0X12345", -0x12345, nil}, + {"12345x", 0, os.EINVAL}, + {"-12345x", 0, os.EINVAL}, + {"98765432100", 98765432100, nil}, + {"-98765432100", -98765432100, nil}, + {"9223372036854775807", 1<<63 - 1, nil}, + {"-9223372036854775807", -(1<<63 - 1), nil}, + {"9223372036854775808", 1<<63 - 1, os.ERANGE}, + {"-9223372036854775808", -1 << 63, nil}, + {"9223372036854775809", 1<<63 - 1, os.ERANGE}, + {"-9223372036854775809", -1 << 63, os.ERANGE}, } type atoui32Test struct { @@ -108,15 +108,15 @@ type atoui32Test struct { } var atoui32tests = []atoui32Test{ - atoui32Test{"", 0, os.EINVAL}, - atoui32Test{"0", 0, nil}, - atoui32Test{"1", 1, nil}, - atoui32Test{"12345", 12345, nil}, - atoui32Test{"012345", 12345, nil}, - atoui32Test{"12345x", 0, os.EINVAL}, - atoui32Test{"987654321", 987654321, nil}, - atoui32Test{"4294967295", 1<<32 - 1, nil}, - atoui32Test{"4294967296", 1<<32 - 1, os.ERANGE}, + {"", 0, os.EINVAL}, + {"0", 0, nil}, + {"1", 1, nil}, + {"12345", 12345, nil}, + {"012345", 12345, nil}, + {"12345x", 0, os.EINVAL}, + {"987654321", 987654321, nil}, + {"4294967295", 1<<32 - 1, nil}, + {"4294967296", 1<<32 - 1, os.ERANGE}, } type atoi32Test struct { @@ -126,25 +126,25 @@ type atoi32Test struct { } var atoi32tests = []atoi32Test{ - atoi32Test{"", 0, os.EINVAL}, - atoi32Test{"0", 0, nil}, - atoi32Test{"-0", 0, nil}, - atoi32Test{"1", 1, nil}, - atoi32Test{"-1", -1, nil}, - atoi32Test{"12345", 12345, nil}, - atoi32Test{"-12345", -12345, nil}, - atoi32Test{"012345", 12345, nil}, - atoi32Test{"-012345", -12345, nil}, - atoi32Test{"12345x", 0, os.EINVAL}, - atoi32Test{"-12345x", 0, os.EINVAL}, - atoi32Test{"987654321", 987654321, nil}, - atoi32Test{"-987654321", -987654321, nil}, - atoi32Test{"2147483647", 1<<31 - 1, nil}, - atoi32Test{"-2147483647", -(1<<31 - 1), nil}, - atoi32Test{"2147483648", 1<<31 - 1, os.ERANGE}, - atoi32Test{"-2147483648", -1 << 31, nil}, - atoi32Test{"2147483649", 1<<31 - 1, os.ERANGE}, - atoi32Test{"-2147483649", -1 << 31, os.ERANGE}, + {"", 0, os.EINVAL}, + {"0", 0, nil}, + {"-0", 0, nil}, + {"1", 1, nil}, + {"-1", -1, nil}, + {"12345", 12345, nil}, + {"-12345", -12345, nil}, + {"012345", 12345, nil}, + {"-012345", -12345, nil}, + {"12345x", 0, os.EINVAL}, + {"-12345x", 0, os.EINVAL}, + {"987654321", 987654321, nil}, + {"-987654321", -987654321, nil}, + {"2147483647", 1<<31 - 1, nil}, + {"-2147483647", -(1<<31 - 1), nil}, + {"2147483648", 1<<31 - 1, os.ERANGE}, + {"-2147483648", -1 << 31, nil}, + {"2147483649", 1<<31 - 1, os.ERANGE}, + {"-2147483649", -1 << 31, os.ERANGE}, } func init() { @@ -193,7 +193,7 @@ func TestAtoui64(t *testing.T) { test := &atoui64tests[i] out, err := Atoui64(test.in) if test.out != out || !reflect.DeepEqual(test.err, err) { - t.Errorf("Atoui64(%q) = %v, %v want %v, %v\n", + t.Errorf("Atoui64(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -204,7 +204,7 @@ func TestBtoui64(t *testing.T) { test := &btoui64tests[i] out, err := Btoui64(test.in, 0) if test.out != out || !reflect.DeepEqual(test.err, err) { - t.Errorf("Btoui64(%q) = %v, %v want %v, %v\n", + t.Errorf("Btoui64(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -215,7 +215,7 @@ func TestAtoi64(t *testing.T) { test := &atoi64tests[i] out, err := Atoi64(test.in) if test.out != out || !reflect.DeepEqual(test.err, err) { - t.Errorf("Atoi64(%q) = %v, %v want %v, %v\n", + t.Errorf("Atoi64(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -226,7 +226,7 @@ func TestBtoi64(t *testing.T) { test := &btoi64tests[i] out, err := Btoi64(test.in, 0) if test.out != out || !reflect.DeepEqual(test.err, err) { - t.Errorf("Btoi64(%q) = %v, %v want %v, %v\n", + t.Errorf("Btoi64(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -239,7 +239,7 @@ func TestAtoui(t *testing.T) { test := &atoui32tests[i] out, err := Atoui(test.in) if test.out != uint32(out) || !reflect.DeepEqual(test.err, err) { - t.Errorf("Atoui(%q) = %v, %v want %v, %v\n", + t.Errorf("Atoui(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -248,7 +248,7 @@ func TestAtoui(t *testing.T) { test := &atoui64tests[i] out, err := Atoui(test.in) if test.out != uint64(out) || !reflect.DeepEqual(test.err, err) { - t.Errorf("Atoui(%q) = %v, %v want %v, %v\n", + t.Errorf("Atoui(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -262,7 +262,7 @@ func TestAtoi(t *testing.T) { test := &atoi32tests[i] out, err := Atoi(test.in) if test.out != int32(out) || !reflect.DeepEqual(test.err, err) { - t.Errorf("Atoi(%q) = %v, %v want %v, %v\n", + t.Errorf("Atoi(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } @@ -271,7 +271,7 @@ func TestAtoi(t *testing.T) { test := &atoi64tests[i] out, err := Atoi(test.in) if test.out != int64(out) || !reflect.DeepEqual(test.err, err) { - t.Errorf("Atoi(%q) = %v, %v want %v, %v\n", + t.Errorf("Atoi(%q) = %v, %v want %v, %v", test.in, out, err, test.out, test.err) } } diff --git a/src/pkg/strconv/decimal.go b/src/pkg/strconv/decimal.go index 3be61d7bc..3a5cf1ba6 100644 --- a/src/pkg/strconv/decimal.go +++ b/src/pkg/strconv/decimal.go @@ -187,34 +187,34 @@ var leftcheats = []leftCheat{ int(log2*NR+1), $0, 2**NR) }' */ - leftCheat{0, ""}, - leftCheat{1, "5"}, // * 2 - leftCheat{1, "25"}, // * 4 - leftCheat{1, "125"}, // * 8 - leftCheat{2, "625"}, // * 16 - leftCheat{2, "3125"}, // * 32 - leftCheat{2, "15625"}, // * 64 - leftCheat{3, "78125"}, // * 128 - leftCheat{3, "390625"}, // * 256 - leftCheat{3, "1953125"}, // * 512 - leftCheat{4, "9765625"}, // * 1024 - leftCheat{4, "48828125"}, // * 2048 - leftCheat{4, "244140625"}, // * 4096 - leftCheat{4, "1220703125"}, // * 8192 - leftCheat{5, "6103515625"}, // * 16384 - leftCheat{5, "30517578125"}, // * 32768 - leftCheat{5, "152587890625"}, // * 65536 - leftCheat{6, "762939453125"}, // * 131072 - leftCheat{6, "3814697265625"}, // * 262144 - leftCheat{6, "19073486328125"}, // * 524288 - leftCheat{7, "95367431640625"}, // * 1048576 - leftCheat{7, "476837158203125"}, // * 2097152 - leftCheat{7, "2384185791015625"}, // * 4194304 - leftCheat{7, "11920928955078125"}, // * 8388608 - leftCheat{8, "59604644775390625"}, // * 16777216 - leftCheat{8, "298023223876953125"}, // * 33554432 - leftCheat{8, "1490116119384765625"}, // * 67108864 - leftCheat{9, "7450580596923828125"}, // * 134217728 + {0, ""}, + {1, "5"}, // * 2 + {1, "25"}, // * 4 + {1, "125"}, // * 8 + {2, "625"}, // * 16 + {2, "3125"}, // * 32 + {2, "15625"}, // * 64 + {3, "78125"}, // * 128 + {3, "390625"}, // * 256 + {3, "1953125"}, // * 512 + {4, "9765625"}, // * 1024 + {4, "48828125"}, // * 2048 + {4, "244140625"}, // * 4096 + {4, "1220703125"}, // * 8192 + {5, "6103515625"}, // * 16384 + {5, "30517578125"}, // * 32768 + {5, "152587890625"}, // * 65536 + {6, "762939453125"}, // * 131072 + {6, "3814697265625"}, // * 262144 + {6, "19073486328125"}, // * 524288 + {7, "95367431640625"}, // * 1048576 + {7, "476837158203125"}, // * 2097152 + {7, "2384185791015625"}, // * 4194304 + {7, "11920928955078125"}, // * 8388608 + {8, "59604644775390625"}, // * 16777216 + {8, "298023223876953125"}, // * 33554432 + {8, "1490116119384765625"}, // * 67108864 + {9, "7450580596923828125"}, // * 134217728 } // Is the leading prefix of b lexicographically less than s? diff --git a/src/pkg/strconv/decimal_test.go b/src/pkg/strconv/decimal_test.go index 5f73450cd..9b7903516 100644 --- a/src/pkg/strconv/decimal_test.go +++ b/src/pkg/strconv/decimal_test.go @@ -16,17 +16,17 @@ type shiftTest struct { } var shifttests = []shiftTest{ - shiftTest{0, -100, "0"}, - shiftTest{0, 100, "0"}, - shiftTest{1, 100, "1267650600228229401496703205376"}, - shiftTest{1, -100, + {0, -100, "0"}, + {0, 100, "0"}, + {1, 100, "1267650600228229401496703205376"}, + {1, -100, "0.00000000000000000000000000000078886090522101180541" + "17285652827862296732064351090230047702789306640625", }, - shiftTest{12345678, 8, "3160493568"}, - shiftTest{12345678, -8, "48225.3046875"}, - shiftTest{195312, 9, "99999744"}, - shiftTest{1953125, 9, "1000000000"}, + {12345678, 8, "3160493568"}, + {12345678, -8, "48225.3046875"}, + {195312, 9, "99999744"}, + {1953125, 9, "1000000000"}, } func TestDecimalShift(t *testing.T) { @@ -34,7 +34,7 @@ func TestDecimalShift(t *testing.T) { test := &shifttests[i] s := NewDecimal(test.i).Shift(test.shift).String() if s != test.out { - t.Errorf("Decimal %v << %v = %v, want %v\n", + t.Errorf("Decimal %v << %v = %v, want %v", test.i, test.shift, s, test.out) } } @@ -48,21 +48,21 @@ type roundTest struct { } 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}, + {0, 4, "0", "0", "0", 0}, + {12344999, 4, "12340000", "12340000", "12350000", 12340000}, + {12345000, 4, "12340000", "12340000", "12350000", 12340000}, + {12345001, 4, "12340000", "12350000", "12350000", 12350000}, + {23454999, 4, "23450000", "23450000", "23460000", 23450000}, + {23455000, 4, "23450000", "23460000", "23460000", 23460000}, + {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}, + {99994999, 4, "99990000", "99990000", "100000000", 99990000}, + {99995000, 4, "99990000", "100000000", "100000000", 100000000}, + {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}, + {12994999, 4, "12990000", "12990000", "13000000", 12990000}, + {12995000, 4, "12990000", "13000000", "13000000", 13000000}, + {12999999, 4, "12990000", "13000000", "13000000", 13000000}, } func TestDecimalRound(t *testing.T) { @@ -70,17 +70,17 @@ func TestDecimalRound(t *testing.T) { test := &roundtests[i] s := NewDecimal(test.i).RoundDown(test.nd).String() if s != test.down { - t.Errorf("Decimal %v RoundDown %d = %v, want %v\n", + t.Errorf("Decimal %v RoundDown %d = %v, want %v", test.i, test.nd, s, test.down) } s = NewDecimal(test.i).Round(test.nd).String() if s != test.round { - t.Errorf("Decimal %v Round %d = %v, want %v\n", + t.Errorf("Decimal %v Round %d = %v, want %v", test.i, test.nd, s, test.down) } s = NewDecimal(test.i).RoundUp(test.nd).String() if s != test.up { - t.Errorf("Decimal %v RoundUp %d = %v, want %v\n", + t.Errorf("Decimal %v RoundUp %d = %v, want %v", test.i, test.nd, s, test.up) } } @@ -93,16 +93,16 @@ type roundIntTest struct { } 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}, + {0, 100, 0}, + {512, -8, 2}, + {513, -8, 2}, + {640, -8, 2}, + {641, -8, 3}, + {384, -8, 2}, + {385, -8, 2}, + {383, -8, 1}, + {1, 100, 1<<64 - 1}, + {1000, 0, 1000}, } func TestDecimalRoundedInteger(t *testing.T) { @@ -110,7 +110,7 @@ func TestDecimalRoundedInteger(t *testing.T) { test := roundinttests[i] int := NewDecimal(test.i).Shift(test.shift).RoundedInteger() if int != test.int { - t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v\n", + t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v", test.i, test.shift, int, test.int) } } diff --git a/src/pkg/strconv/fp_test.go b/src/pkg/strconv/fp_test.go index 4cbadf316..305adcc0c 100644 --- a/src/pkg/strconv/fp_test.go +++ b/src/pkg/strconv/fp_test.go @@ -118,7 +118,7 @@ func TestFp(t *testing.T) { } a := strings.Split(line, " ", -1) if len(a) != 4 { - t.Error("testfp.txt:", lineno, ": wrong field count\n") + t.Error("testfp.txt:", lineno, ": wrong field count") continue } var s string diff --git a/src/pkg/strconv/ftoa.go b/src/pkg/strconv/ftoa.go index 3659243c7..a6091fc6c 100644 --- a/src/pkg/strconv/ftoa.go +++ b/src/pkg/strconv/ftoa.go @@ -42,13 +42,15 @@ var FloatSize = floatsize() // The format fmt is one of // 'b' (-ddddp±ddd, a binary exponent), // 'e' (-d.dddde±dd, a decimal exponent), -// 'f' (-ddd.dddd, no exponent), or -// 'g' ('e' for large exponents, 'f' otherwise). +// 'E' (-d.ddddE±dd, a decimal exponent), +// 'f' (-ddd.dddd, no exponent), +// 'g' ('e' for large exponents, 'f' otherwise), or +// 'G' ('E' for large exponents, 'f' otherwise). // // The precision prec controls the number of digits -// (excluding the exponent) printed by the 'e', 'f', and 'g' formats. -// For 'e' and 'f' it is the number of digits after the decimal point. -// For 'g' it is the total number of digits. +// (excluding the exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats. +// For 'e', 'E', and 'f' it is the number of digits after the decimal point. +// For 'g' and 'G' it is the total number of digits. // The special precision -1 uses the smallest number of digits // necessary such that Atof32 will return f exactly. // diff --git a/src/pkg/strconv/ftoa_test.go b/src/pkg/strconv/ftoa_test.go index 49832b626..6044afdae 100644 --- a/src/pkg/strconv/ftoa_test.go +++ b/src/pkg/strconv/ftoa_test.go @@ -25,99 +25,99 @@ const ( ) var ftoatests = []ftoaTest{ - ftoaTest{1, 'e', 5, "1.00000e+00"}, - ftoaTest{1, 'f', 5, "1.00000"}, - ftoaTest{1, 'g', 5, "1"}, - ftoaTest{1, 'g', -1, "1"}, - ftoaTest{20, 'g', -1, "20"}, - ftoaTest{1234567.8, 'g', -1, "1.2345678e+06"}, - ftoaTest{200000, 'g', -1, "200000"}, - ftoaTest{2000000, 'g', -1, "2e+06"}, + {1, 'e', 5, "1.00000e+00"}, + {1, 'f', 5, "1.00000"}, + {1, 'g', 5, "1"}, + {1, 'g', -1, "1"}, + {20, 'g', -1, "20"}, + {1234567.8, 'g', -1, "1.2345678e+06"}, + {200000, 'g', -1, "200000"}, + {2000000, 'g', -1, "2e+06"}, // g conversion and zero suppression - ftoaTest{400, 'g', 2, "4e+02"}, - ftoaTest{40, 'g', 2, "40"}, - ftoaTest{4, 'g', 2, "4"}, - ftoaTest{.4, 'g', 2, "0.4"}, - ftoaTest{.04, 'g', 2, "0.04"}, - ftoaTest{.004, 'g', 2, "0.004"}, - ftoaTest{.0004, 'g', 2, "0.0004"}, - ftoaTest{.00004, 'g', 2, "4e-05"}, - ftoaTest{.000004, 'g', 2, "4e-06"}, - - ftoaTest{0, 'e', 5, "0.00000e+00"}, - ftoaTest{0, 'f', 5, "0.00000"}, - ftoaTest{0, 'g', 5, "0"}, - ftoaTest{0, 'g', -1, "0"}, - - ftoaTest{-1, 'e', 5, "-1.00000e+00"}, - ftoaTest{-1, 'f', 5, "-1.00000"}, - ftoaTest{-1, 'g', 5, "-1"}, - ftoaTest{-1, 'g', -1, "-1"}, - - ftoaTest{12, 'e', 5, "1.20000e+01"}, - ftoaTest{12, 'f', 5, "12.00000"}, - ftoaTest{12, 'g', 5, "12"}, - ftoaTest{12, 'g', -1, "12"}, - - ftoaTest{123456700, 'e', 5, "1.23457e+08"}, - ftoaTest{123456700, 'f', 5, "123456700.00000"}, - ftoaTest{123456700, 'g', 5, "1.2346e+08"}, - ftoaTest{123456700, 'g', -1, "1.234567e+08"}, - - ftoaTest{1.2345e6, 'e', 5, "1.23450e+06"}, - ftoaTest{1.2345e6, 'f', 5, "1234500.00000"}, - ftoaTest{1.2345e6, 'g', 5, "1.2345e+06"}, - - ftoaTest{1e23, 'e', 17, "9.99999999999999916e+22"}, - ftoaTest{1e23, 'f', 17, "99999999999999991611392.00000000000000000"}, - ftoaTest{1e23, 'g', 17, "9.9999999999999992e+22"}, - - ftoaTest{1e23, 'e', -1, "1e+23"}, - ftoaTest{1e23, 'f', -1, "100000000000000000000000"}, - ftoaTest{1e23, 'g', -1, "1e+23"}, - - ftoaTest{below1e23, 'e', 17, "9.99999999999999748e+22"}, - ftoaTest{below1e23, 'f', 17, "99999999999999974834176.00000000000000000"}, - ftoaTest{below1e23, 'g', 17, "9.9999999999999975e+22"}, - - ftoaTest{below1e23, 'e', -1, "9.999999999999997e+22"}, - ftoaTest{below1e23, 'f', -1, "99999999999999970000000"}, - ftoaTest{below1e23, 'g', -1, "9.999999999999997e+22"}, - - ftoaTest{above1e23, 'e', 17, "1.00000000000000008e+23"}, - ftoaTest{above1e23, 'f', 17, "100000000000000008388608.00000000000000000"}, - ftoaTest{above1e23, 'g', 17, "1.0000000000000001e+23"}, - - ftoaTest{above1e23, 'e', -1, "1.0000000000000001e+23"}, - ftoaTest{above1e23, 'f', -1, "100000000000000010000000"}, - ftoaTest{above1e23, 'g', -1, "1.0000000000000001e+23"}, - - ftoaTest{fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, - ftoaTest{fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, - - ftoaTest{32, 'g', -1, "32"}, - ftoaTest{32, 'g', 0, "3e+01"}, - - ftoaTest{100, 'x', -1, "%x"}, - - ftoaTest{math.NaN(), 'g', -1, "NaN"}, - ftoaTest{-math.NaN(), 'g', -1, "NaN"}, - ftoaTest{math.Inf(0), 'g', -1, "+Inf"}, - ftoaTest{math.Inf(-1), 'g', -1, "-Inf"}, - ftoaTest{-math.Inf(0), 'g', -1, "-Inf"}, - - ftoaTest{-1, 'b', -1, "-4503599627370496p-52"}, + {400, 'g', 2, "4e+02"}, + {40, 'g', 2, "40"}, + {4, 'g', 2, "4"}, + {.4, 'g', 2, "0.4"}, + {.04, 'g', 2, "0.04"}, + {.004, 'g', 2, "0.004"}, + {.0004, 'g', 2, "0.0004"}, + {.00004, 'g', 2, "4e-05"}, + {.000004, 'g', 2, "4e-06"}, + + {0, 'e', 5, "0.00000e+00"}, + {0, 'f', 5, "0.00000"}, + {0, 'g', 5, "0"}, + {0, 'g', -1, "0"}, + + {-1, 'e', 5, "-1.00000e+00"}, + {-1, 'f', 5, "-1.00000"}, + {-1, 'g', 5, "-1"}, + {-1, 'g', -1, "-1"}, + + {12, 'e', 5, "1.20000e+01"}, + {12, 'f', 5, "12.00000"}, + {12, 'g', 5, "12"}, + {12, 'g', -1, "12"}, + + {123456700, 'e', 5, "1.23457e+08"}, + {123456700, 'f', 5, "123456700.00000"}, + {123456700, 'g', 5, "1.2346e+08"}, + {123456700, 'g', -1, "1.234567e+08"}, + + {1.2345e6, 'e', 5, "1.23450e+06"}, + {1.2345e6, 'f', 5, "1234500.00000"}, + {1.2345e6, 'g', 5, "1.2345e+06"}, + + {1e23, 'e', 17, "9.99999999999999916e+22"}, + {1e23, 'f', 17, "99999999999999991611392.00000000000000000"}, + {1e23, 'g', 17, "9.9999999999999992e+22"}, + + {1e23, 'e', -1, "1e+23"}, + {1e23, 'f', -1, "100000000000000000000000"}, + {1e23, 'g', -1, "1e+23"}, + + {below1e23, 'e', 17, "9.99999999999999748e+22"}, + {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"}, + {below1e23, 'g', 17, "9.9999999999999975e+22"}, + + {below1e23, 'e', -1, "9.999999999999997e+22"}, + {below1e23, 'f', -1, "99999999999999970000000"}, + {below1e23, 'g', -1, "9.999999999999997e+22"}, + + {above1e23, 'e', 17, "1.00000000000000008e+23"}, + {above1e23, 'f', 17, "100000000000000008388608.00000000000000000"}, + {above1e23, 'g', 17, "1.0000000000000001e+23"}, + + {above1e23, 'e', -1, "1.0000000000000001e+23"}, + {above1e23, 'f', -1, "100000000000000010000000"}, + {above1e23, 'g', -1, "1.0000000000000001e+23"}, + + {fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, + {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, + + {32, 'g', -1, "32"}, + {32, 'g', 0, "3e+01"}, + + {100, 'x', -1, "%x"}, + + {math.NaN(), 'g', -1, "NaN"}, + {-math.NaN(), 'g', -1, "NaN"}, + {math.Inf(0), 'g', -1, "+Inf"}, + {math.Inf(-1), 'g', -1, "-Inf"}, + {-math.Inf(0), 'g', -1, "-Inf"}, + + {-1, 'b', -1, "-4503599627370496p-52"}, // fixed bugs - ftoaTest{0.9, 'f', 1, "0.9"}, - ftoaTest{0.09, 'f', 1, "0.1"}, - ftoaTest{0.0999, 'f', 1, "0.1"}, - ftoaTest{0.05, 'f', 1, "0.1"}, - ftoaTest{0.05, 'f', 0, "0"}, - ftoaTest{0.5, 'f', 1, "0.5"}, - ftoaTest{0.5, 'f', 0, "0"}, - ftoaTest{1.5, 'f', 0, "2"}, + {0.9, 'f', 1, "0.9"}, + {0.09, 'f', 1, "0.1"}, + {0.0999, 'f', 1, "0.1"}, + {0.05, 'f', 1, "0.1"}, + {0.05, 'f', 0, "0"}, + {0.5, 'f', 1, "0.5"}, + {0.5, 'f', 0, "0"}, + {1.5, 'f', 0, "2"}, } func TestFtoa(t *testing.T) { diff --git a/src/pkg/strconv/itoa_test.go b/src/pkg/strconv/itoa_test.go index 039ef4446..8514b21e4 100644 --- a/src/pkg/strconv/itoa_test.go +++ b/src/pkg/strconv/itoa_test.go @@ -16,60 +16,60 @@ type itob64Test struct { } var itob64tests = []itob64Test{ - itob64Test{0, 10, "0"}, - itob64Test{1, 10, "1"}, - itob64Test{-1, 10, "-1"}, - itob64Test{12345678, 10, "12345678"}, - itob64Test{-987654321, 10, "-987654321"}, - itob64Test{1<<31 - 1, 10, "2147483647"}, - itob64Test{-1<<31 + 1, 10, "-2147483647"}, - itob64Test{1 << 31, 10, "2147483648"}, - itob64Test{-1 << 31, 10, "-2147483648"}, - itob64Test{1<<31 + 1, 10, "2147483649"}, - itob64Test{-1<<31 - 1, 10, "-2147483649"}, - itob64Test{1<<32 - 1, 10, "4294967295"}, - itob64Test{-1<<32 + 1, 10, "-4294967295"}, - itob64Test{1 << 32, 10, "4294967296"}, - itob64Test{-1 << 32, 10, "-4294967296"}, - itob64Test{1<<32 + 1, 10, "4294967297"}, - itob64Test{-1<<32 - 1, 10, "-4294967297"}, - itob64Test{1 << 50, 10, "1125899906842624"}, - itob64Test{1<<63 - 1, 10, "9223372036854775807"}, - itob64Test{-1<<63 + 1, 10, "-9223372036854775807"}, - itob64Test{-1 << 63, 10, "-9223372036854775808"}, - - itob64Test{0, 2, "0"}, - itob64Test{10, 2, "1010"}, - itob64Test{-1, 2, "-1"}, - itob64Test{1 << 15, 2, "1000000000000000"}, - - itob64Test{-8, 8, "-10"}, - itob64Test{057635436545, 8, "57635436545"}, - itob64Test{1 << 24, 8, "100000000"}, - - itob64Test{16, 16, "10"}, - itob64Test{-0x123456789abcdef, 16, "-123456789abcdef"}, - itob64Test{1<<63 - 1, 16, "7fffffffffffffff"}, - itob64Test{1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"}, - - itob64Test{16, 17, "g"}, - itob64Test{25, 25, "10"}, - itob64Test{(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"}, - itob64Test{(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"}, + {0, 10, "0"}, + {1, 10, "1"}, + {-1, 10, "-1"}, + {12345678, 10, "12345678"}, + {-987654321, 10, "-987654321"}, + {1<<31 - 1, 10, "2147483647"}, + {-1<<31 + 1, 10, "-2147483647"}, + {1 << 31, 10, "2147483648"}, + {-1 << 31, 10, "-2147483648"}, + {1<<31 + 1, 10, "2147483649"}, + {-1<<31 - 1, 10, "-2147483649"}, + {1<<32 - 1, 10, "4294967295"}, + {-1<<32 + 1, 10, "-4294967295"}, + {1 << 32, 10, "4294967296"}, + {-1 << 32, 10, "-4294967296"}, + {1<<32 + 1, 10, "4294967297"}, + {-1<<32 - 1, 10, "-4294967297"}, + {1 << 50, 10, "1125899906842624"}, + {1<<63 - 1, 10, "9223372036854775807"}, + {-1<<63 + 1, 10, "-9223372036854775807"}, + {-1 << 63, 10, "-9223372036854775808"}, + + {0, 2, "0"}, + {10, 2, "1010"}, + {-1, 2, "-1"}, + {1 << 15, 2, "1000000000000000"}, + + {-8, 8, "-10"}, + {057635436545, 8, "57635436545"}, + {1 << 24, 8, "100000000"}, + + {16, 16, "10"}, + {-0x123456789abcdef, 16, "-123456789abcdef"}, + {1<<63 - 1, 16, "7fffffffffffffff"}, + {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"}, + + {16, 17, "g"}, + {25, 25, "10"}, + {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"}, + {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"}, } func TestItoa(t *testing.T) { for _, test := range itob64tests { s := Itob64(test.in, test.base) if s != test.out { - t.Errorf("Itob64(%v, %v) = %v want %v\n", + t.Errorf("Itob64(%v, %v) = %v want %v", test.in, test.base, s, test.out) } if test.in >= 0 { s := Uitob64(uint64(test.in), test.base) if s != test.out { - t.Errorf("Uitob64(%v, %v) = %v want %v\n", + t.Errorf("Uitob64(%v, %v) = %v want %v", test.in, test.base, s, test.out) } } @@ -77,14 +77,14 @@ func TestItoa(t *testing.T) { if int64(int(test.in)) == test.in { s := Itob(int(test.in), test.base) if s != test.out { - t.Errorf("Itob(%v, %v) = %v want %v\n", + t.Errorf("Itob(%v, %v) = %v want %v", test.in, test.base, s, test.out) } if test.in >= 0 { s := Uitob(uint(test.in), test.base) if s != test.out { - t.Errorf("Uitob(%v, %v) = %v want %v\n", + t.Errorf("Uitob(%v, %v) = %v want %v", test.in, test.base, s, test.out) } } @@ -93,14 +93,14 @@ func TestItoa(t *testing.T) { if test.base == 10 { s := Itoa64(test.in) if s != test.out { - t.Errorf("Itoa64(%v) = %v want %v\n", + t.Errorf("Itoa64(%v) = %v want %v", test.in, s, test.out) } if test.in >= 0 { s := Uitob64(uint64(test.in), test.base) if s != test.out { - t.Errorf("Uitob64(%v, %v) = %v want %v\n", + t.Errorf("Uitob64(%v, %v) = %v want %v", test.in, test.base, s, test.out) } } @@ -108,14 +108,14 @@ func TestItoa(t *testing.T) { if int64(int(test.in)) == test.in { s := Itoa(int(test.in)) if s != test.out { - t.Errorf("Itoa(%v) = %v want %v\n", + t.Errorf("Itoa(%v) = %v want %v", test.in, s, test.out) } if test.in >= 0 { s := Uitoa(uint(test.in)) if s != test.out { - t.Errorf("Uitoa(%v) = %v want %v\n", + t.Errorf("Uitoa(%v) = %v want %v", test.in, s, test.out) } } @@ -131,26 +131,26 @@ type uitob64Test struct { } var uitob64tests = []uitob64Test{ - uitob64Test{1<<63 - 1, 10, "9223372036854775807"}, - uitob64Test{1 << 63, 10, "9223372036854775808"}, - uitob64Test{1<<63 + 1, 10, "9223372036854775809"}, - uitob64Test{1<<64 - 2, 10, "18446744073709551614"}, - uitob64Test{1<<64 - 1, 10, "18446744073709551615"}, - uitob64Test{1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"}, + {1<<63 - 1, 10, "9223372036854775807"}, + {1 << 63, 10, "9223372036854775808"}, + {1<<63 + 1, 10, "9223372036854775809"}, + {1<<64 - 2, 10, "18446744073709551614"}, + {1<<64 - 1, 10, "18446744073709551615"}, + {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"}, } func TestUitoa(t *testing.T) { for _, test := range uitob64tests { s := Uitob64(test.in, test.base) if s != test.out { - t.Errorf("Uitob64(%v, %v) = %v want %v\n", + t.Errorf("Uitob64(%v, %v) = %v want %v", test.in, test.base, s, test.out) } if uint64(uint(test.in)) == test.in { s := Uitob(uint(test.in), test.base) if s != test.out { - t.Errorf("Uitob(%v, %v) = %v want %v\n", + t.Errorf("Uitob(%v, %v) = %v want %v", test.in, test.base, s, test.out) } } @@ -158,14 +158,14 @@ func TestUitoa(t *testing.T) { if test.base == 10 { s := Uitoa64(test.in) if s != test.out { - t.Errorf("Uitoa64(%v) = %v want %v\n", + t.Errorf("Uitoa64(%v) = %v want %v", test.in, s, test.out) } if uint64(uint(test.in)) == test.in { s := Uitoa(uint(test.in)) if s != test.out { - t.Errorf("Uitoa(%v) = %v want %v\n", + t.Errorf("Uitoa(%v) = %v want %v", test.in, s, test.out) } } diff --git a/src/pkg/strconv/quote.go b/src/pkg/strconv/quote.go index 53774ee6f..ed5889723 100644 --- a/src/pkg/strconv/quote.go +++ b/src/pkg/strconv/quote.go @@ -100,11 +100,12 @@ func unhex(b byte) (v int, ok bool) { // UnquoteChar decodes the first character or byte in the escaped string // or character literal represented by the string s. // It returns four values: -// 1) value, the decoded Unicode code point or byte value; -// 2) multibyte, a boolean indicating whether the decoded character -// requires a multibyte UTF-8 representation; -// 3) tail, the remainder of the string after the character; and -// 4) an error that will be nil if the character is syntactically valid. +// +// 1) value, the decoded Unicode code point or byte value; +// 2) multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation; +// 3) tail, the remainder of the string after the character; and +// 4) an error that will be nil if the character is syntactically valid. +// // The second argument, quote, specifies the type of literal being parsed // and therefore which escaped quote character is permitted. // If set to a single quote, it permits the sequence \' and disallows unescaped '. @@ -233,7 +234,7 @@ func Unquote(s string) (t string, err os.Error) { s = s[1 : n-1] if quote == '`' { - if strings.Index(s, "`") >= 0 { + if strings.Contains(s, "`") { return "", os.EINVAL } return s, nil diff --git a/src/pkg/strconv/quote_test.go b/src/pkg/strconv/quote_test.go index 31784879a..1235fcb9a 100644 --- a/src/pkg/strconv/quote_test.go +++ b/src/pkg/strconv/quote_test.go @@ -16,12 +16,12 @@ type quoteTest struct { } var quotetests = []quoteTest{ - quoteTest{"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`}, - quoteTest{"\\", `"\\"`}, - quoteTest{"abc\xffdef", `"abc\xffdef"`}, - quoteTest{"\u263a", `"\u263a"`}, - quoteTest{"\U0010ffff", `"\U0010ffff"`}, - quoteTest{"\x04", `"\x04"`}, + {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`}, + {"\\", `"\\"`}, + {"abc\xffdef", `"abc\xffdef"`}, + {"\u263a", `"\u263a"`}, + {"\U0010ffff", `"\U0010ffff"`}, + {"\x04", `"\x04"`}, } func TestQuote(t *testing.T) { @@ -39,44 +39,44 @@ type canBackquoteTest struct { } var canbackquotetests = []canBackquoteTest{ - canBackquoteTest{"`", false}, - canBackquoteTest{string(0), false}, - canBackquoteTest{string(1), false}, - canBackquoteTest{string(2), false}, - canBackquoteTest{string(3), false}, - canBackquoteTest{string(4), false}, - canBackquoteTest{string(5), false}, - canBackquoteTest{string(6), false}, - canBackquoteTest{string(7), false}, - canBackquoteTest{string(8), false}, - canBackquoteTest{string(9), true}, // \t - canBackquoteTest{string(10), false}, - canBackquoteTest{string(11), false}, - canBackquoteTest{string(12), false}, - canBackquoteTest{string(13), false}, - canBackquoteTest{string(14), false}, - canBackquoteTest{string(15), false}, - canBackquoteTest{string(16), false}, - canBackquoteTest{string(17), false}, - canBackquoteTest{string(18), false}, - canBackquoteTest{string(19), false}, - canBackquoteTest{string(20), false}, - canBackquoteTest{string(21), false}, - canBackquoteTest{string(22), false}, - canBackquoteTest{string(23), false}, - canBackquoteTest{string(24), false}, - canBackquoteTest{string(25), false}, - canBackquoteTest{string(26), false}, - canBackquoteTest{string(27), false}, - canBackquoteTest{string(28), false}, - canBackquoteTest{string(29), false}, - canBackquoteTest{string(30), false}, - canBackquoteTest{string(31), false}, - canBackquoteTest{`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true}, - canBackquoteTest{`0123456789`, true}, - canBackquoteTest{`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true}, - canBackquoteTest{`abcdefghijklmnopqrstuvwxyz`, true}, - canBackquoteTest{`☺`, true}, + {"`", false}, + {string(0), false}, + {string(1), false}, + {string(2), false}, + {string(3), false}, + {string(4), false}, + {string(5), false}, + {string(6), false}, + {string(7), false}, + {string(8), false}, + {string(9), true}, // \t + {string(10), false}, + {string(11), false}, + {string(12), false}, + {string(13), false}, + {string(14), false}, + {string(15), false}, + {string(16), false}, + {string(17), false}, + {string(18), false}, + {string(19), false}, + {string(20), false}, + {string(21), false}, + {string(22), false}, + {string(23), false}, + {string(24), false}, + {string(25), false}, + {string(26), false}, + {string(27), false}, + {string(28), false}, + {string(29), false}, + {string(30), false}, + {string(31), false}, + {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true}, + {`0123456789`, true}, + {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true}, + {`abcdefghijklmnopqrstuvwxyz`, true}, + {`☺`, true}, } func TestCanBackquote(t *testing.T) { @@ -89,41 +89,41 @@ func TestCanBackquote(t *testing.T) { } var unquotetests = []quoteTest{ - quoteTest{`""`, ""}, - quoteTest{`"a"`, "a"}, - quoteTest{`"abc"`, "abc"}, - quoteTest{`"☺"`, "☺"}, - quoteTest{`"hello world"`, "hello world"}, - quoteTest{`"\xFF"`, "\xFF"}, - quoteTest{`"\377"`, "\377"}, - quoteTest{`"\u1234"`, "\u1234"}, - quoteTest{`"\U00010111"`, "\U00010111"}, - quoteTest{`"\U0001011111"`, "\U0001011111"}, - quoteTest{`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""}, - quoteTest{`"'"`, "'"}, + {`""`, ""}, + {`"a"`, "a"}, + {`"abc"`, "abc"}, + {`"☺"`, "☺"}, + {`"hello world"`, "hello world"}, + {`"\xFF"`, "\xFF"}, + {`"\377"`, "\377"}, + {`"\u1234"`, "\u1234"}, + {`"\U00010111"`, "\U00010111"}, + {`"\U0001011111"`, "\U0001011111"}, + {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""}, + {`"'"`, "'"}, - quoteTest{`'a'`, "a"}, - quoteTest{`'☹'`, "☹"}, - quoteTest{`'\a'`, "\a"}, - quoteTest{`'\x10'`, "\x10"}, - quoteTest{`'\377'`, "\377"}, - quoteTest{`'\u1234'`, "\u1234"}, - quoteTest{`'\U00010111'`, "\U00010111"}, - quoteTest{`'\t'`, "\t"}, - quoteTest{`' '`, " "}, - quoteTest{`'\''`, "'"}, - quoteTest{`'"'`, "\""}, + {`'a'`, "a"}, + {`'☹'`, "☹"}, + {`'\a'`, "\a"}, + {`'\x10'`, "\x10"}, + {`'\377'`, "\377"}, + {`'\u1234'`, "\u1234"}, + {`'\U00010111'`, "\U00010111"}, + {`'\t'`, "\t"}, + {`' '`, " "}, + {`'\''`, "'"}, + {`'"'`, "\""}, - quoteTest{"``", ``}, - quoteTest{"`a`", `a`}, - quoteTest{"`abc`", `abc`}, - quoteTest{"`☺`", `☺`}, - quoteTest{"`hello world`", `hello world`}, - quoteTest{"`\\xFF`", `\xFF`}, - quoteTest{"`\\377`", `\377`}, - quoteTest{"`\\`", `\`}, - quoteTest{"` `", ` `}, - quoteTest{"` `", ` `}, + {"``", ``}, + {"`a`", `a`}, + {"`abc`", `abc`}, + {"`☺`", `☺`}, + {"`hello world`", `hello world`}, + {"`\\xFF`", `\xFF`}, + {"`\\377`", `\377`}, + {"`\\`", `\`}, + {"` `", ` `}, + {"` `", ` `}, } var misquoted = []string{ |