diff options
Diffstat (limited to 'src/lib/strconv')
-rw-r--r-- | src/lib/strconv/atof.go | 2 | ||||
-rw-r--r-- | src/lib/strconv/atof_test.go | 116 | ||||
-rw-r--r-- | src/lib/strconv/atoi_test.go | 154 | ||||
-rw-r--r-- | src/lib/strconv/ftoa.go | 6 | ||||
-rw-r--r-- | src/lib/strconv/ftoa_test.go | 157 | ||||
-rw-r--r-- | src/lib/strconv/itoa_test.go | 68 | ||||
-rw-r--r-- | src/lib/strconv/quote.go | 13 |
7 files changed, 256 insertions, 260 deletions
diff --git a/src/lib/strconv/atof.go b/src/lib/strconv/atof.go index 8869e2032..de2464a8a 100644 --- a/src/lib/strconv/atof.go +++ b/src/lib/strconv/atof.go @@ -355,7 +355,7 @@ export func atof32(s string) (f float32, err *os.Error) { } export func atof(s string) (f float, err *os.Error) { - if floatsize == 32 { + if FloatSize == 32 { f1, err1 := atof32(s); return float(f1), err1; } diff --git a/src/lib/strconv/atof_test.go b/src/lib/strconv/atof_test.go index ab4fcd146..5292b3d0d 100644 --- a/src/lib/strconv/atof_test.go +++ b/src/lib/strconv/atof_test.go @@ -10,92 +10,92 @@ import ( "testing" ) -type Test struct { +type AtofTest struct { in string; out string; err *os.Error; } -var tests = []Test { - Test{ "", "0", os.EINVAL }, - 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{ "100000000000000000000000", "1e+23", nil }, - Test{ "1e-100", "1e-100", nil }, - Test{ "123456700", "1.234567e+08", nil }, - Test{ "99999999999999974834176", "9.999999999999997e+22", nil }, - Test{ "100000000000000000000001", "1.0000000000000001e+23", nil }, - Test{ "100000000000000008388608", "1.0000000000000001e+23", nil }, - Test{ "100000000000000016777215", "1.0000000000000001e+23", nil }, - Test{ "100000000000000016777216", "1.0000000000000003e+23", nil }, - Test{ "-1", "-1", nil }, - Test{ "-0", "-0", nil }, - Test{ "1e-20", "1e-20", nil }, - Test{ "625e-3", "0.625", nil }, +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 }, // largest float64 - Test{ "1.7976931348623157e308", "1.7976931348623157e+308", nil }, - Test{ "-1.7976931348623157e308", "-1.7976931348623157e+308", nil }, + AtofTest{ "1.7976931348623157e308", "1.7976931348623157e+308", nil }, + AtofTest{ "-1.7976931348623157e308", "-1.7976931348623157e+308", nil }, // next float64 - too large - Test{ "1.7976931348623159e308", "+Inf", os.ERANGE }, - Test{ "-1.7976931348623159e308", "-Inf", os.ERANGE }, + AtofTest{ "1.7976931348623159e308", "+Inf", os.ERANGE }, + AtofTest{ "-1.7976931348623159e308", "-Inf", os.ERANGE }, // the border is ...158079 // borderline - okay - Test{ "1.7976931348623158e308", "1.7976931348623157e+308", nil }, - Test{ "-1.7976931348623158e308", "-1.7976931348623157e+308", nil }, + AtofTest{ "1.7976931348623158e308", "1.7976931348623157e+308", nil }, + AtofTest{ "-1.7976931348623158e308", "-1.7976931348623157e+308", nil }, // borderline - too large - Test{ "1.797693134862315808e308", "+Inf", os.ERANGE }, - Test{ "-1.797693134862315808e308", "-Inf", os.ERANGE }, + AtofTest{ "1.797693134862315808e308", "+Inf", os.ERANGE }, + AtofTest{ "-1.797693134862315808e308", "-Inf", os.ERANGE }, // a little too large - Test{ "1e308", "1e+308", nil }, - Test{ "2e308", "+Inf", os.ERANGE }, - Test{ "1e309", "+Inf", os.ERANGE }, + AtofTest{ "1e308", "1e+308", nil }, + AtofTest{ "2e308", "+Inf", os.ERANGE }, + AtofTest{ "1e309", "+Inf", os.ERANGE }, // way too large - Test{ "1e310", "+Inf", os.ERANGE }, - Test{ "-1e310", "-Inf", os.ERANGE }, - Test{ "1e400", "+Inf", os.ERANGE }, - Test{ "-1e400", "-Inf", os.ERANGE }, - Test{ "1e400000", "+Inf", os.ERANGE }, - Test{ "-1e400000", "-Inf", os.ERANGE }, + 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 }, // denormalized - Test{ "1e-305", "1e-305", nil }, - Test{ "1e-306", "1e-306", nil }, - Test{ "1e-307", "1e-307", nil }, - Test{ "1e-308", "1e-308", nil }, - Test{ "1e-309", "1e-309", nil }, - Test{ "1e-310", "1e-310", nil }, - Test{ "1e-322", "1e-322", nil }, + 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 }, // smallest denormal - Test{ "5e-324", "5e-324", nil }, + AtofTest{ "5e-324", "5e-324", nil }, // too small - Test{ "4e-324", "0", nil }, + AtofTest{ "4e-324", "0", nil }, // way too small - Test{ "1e-350", "0", nil }, - Test{ "1e-400000", "0", nil }, + AtofTest{ "1e-350", "0", nil }, + AtofTest{ "1e-400000", "0", nil }, // try to overflow exponent - Test{ "1e-4294967296", "0", nil }, - Test{ "1e+4294967296", "+Inf", os.ERANGE }, - Test{ "1e-18446744073709551616", "0", nil }, - Test{ "1e+18446744073709551616", "+Inf", os.ERANGE }, + AtofTest{ "1e-4294967296", "0", nil }, + AtofTest{ "1e+4294967296", "+Inf", os.ERANGE }, + AtofTest{ "1e-18446744073709551616", "0", nil }, + AtofTest{ "1e+18446744073709551616", "+Inf", os.ERANGE }, // Parse errors - Test{ "1e", "0", os.EINVAL }, - Test{ "1e-", "0", os.EINVAL }, - Test{ ".e-1", "0", os.EINVAL }, + AtofTest{ "1e", "0", os.EINVAL }, + AtofTest{ "1e-", "0", os.EINVAL }, + AtofTest{ ".e-1", "0", os.EINVAL }, } func XTestAtof(t *testing.T, opt bool) { oldopt := strconv.optimize; strconv.optimize = opt; - for i := 0; i < len(tests); i++ { - test := &tests[i]; + for i := 0; i < len(atoftests); i++ { + test := &atoftests[i]; out, err := strconv.atof64(test.in); outs := strconv.ftoa64(out, 'g', -1); if outs != test.out || err != test.err { @@ -112,7 +112,7 @@ func XTestAtof(t *testing.T, opt bool) { } } - if floatsize == 64 || float64(float32(out)) == out { + if FloatSize == 64 || float64(float32(out)) == out { outf, err := strconv.atof(test.in); outs := strconv.ftoa(outf, 'g', -1); if outs != test.out || err != test.err { diff --git a/src/lib/strconv/atoi_test.go b/src/lib/strconv/atoi_test.go index 166c2e433..a071b7435 100644 --- a/src/lib/strconv/atoi_test.go +++ b/src/lib/strconv/atoi_test.go @@ -10,102 +10,102 @@ import ( "testing" ) -type Uint64Test struct { +type Atoui64Test struct { in string; out uint64; err *os.Error; } -var uint64tests = []Uint64Test { - Uint64Test{ "", 0, os.EINVAL }, - Uint64Test{ "0", 0, nil }, - Uint64Test{ "1", 1, nil }, - Uint64Test{ "12345", 12345, nil }, - Uint64Test{ "012345", 0, os.EINVAL }, - Uint64Test{ "12345x", 0, os.EINVAL }, - Uint64Test{ "98765432100", 98765432100, nil }, - Uint64Test{ "18446744073709551615", 1<<64-1, nil }, - Uint64Test{ "18446744073709551616", 1<<64-1, os.ERANGE }, - Uint64Test{ "18446744073709551620", 1<<64-1, os.ERANGE }, +var atoui64tests = []Atoui64Test { + Atoui64Test{ "", 0, os.EINVAL }, + Atoui64Test{ "0", 0, nil }, + Atoui64Test{ "1", 1, nil }, + Atoui64Test{ "12345", 12345, nil }, + Atoui64Test{ "012345", 0, os.EINVAL }, + 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 }, } -type Int64Test struct { +type Atoi64Test struct { in string; out int64; err *os.Error; } -var int64tests = []Int64Test { - Int64Test{ "", 0, os.EINVAL }, - Int64Test{ "0", 0, nil }, - Int64Test{ "-0", 0, nil }, - Int64Test{ "1", 1, nil }, - Int64Test{ "-1", -1, nil }, - Int64Test{ "12345", 12345, nil }, - Int64Test{ "-12345", -12345, nil }, - Int64Test{ "012345", 0, os.EINVAL }, - Int64Test{ "-012345", 0, os.EINVAL }, - Int64Test{ "12345x", 0, os.EINVAL }, - Int64Test{ "-12345x", 0, os.EINVAL }, - Int64Test{ "98765432100", 98765432100, nil }, - Int64Test{ "-98765432100", -98765432100, nil }, - Int64Test{ "9223372036854775807", 1<<63-1, nil }, - Int64Test{ "-9223372036854775807", -(1<<63-1), nil }, - Int64Test{ "9223372036854775808", 1<<63-1, os.ERANGE }, - Int64Test{ "-9223372036854775808", -1<<63, nil }, - Int64Test{ "9223372036854775809", 1<<63-1, os.ERANGE }, - Int64Test{ "-9223372036854775809", -1<<63, os.ERANGE }, +var atoi64test = []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", 0, os.EINVAL }, + Atoi64Test{ "-012345", 0, os.EINVAL }, + 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 }, } -type Uint32Test struct { +type Atoui32Test struct { in string; out uint32; err *os.Error; } -var uint32tests = []Uint32Test { - Uint32Test{ "", 0, os.EINVAL }, - Uint32Test{ "0", 0, nil }, - Uint32Test{ "1", 1, nil }, - Uint32Test{ "12345", 12345, nil }, - Uint32Test{ "012345", 0, os.EINVAL }, - Uint32Test{ "12345x", 0, os.EINVAL }, - Uint32Test{ "987654321", 987654321, nil }, - Uint32Test{ "4294967295", 1<<32-1, nil }, - Uint32Test{ "4294967296", 1<<32-1, os.ERANGE }, +var atoui32tests = []Atoui32Test { + Atoui32Test{ "", 0, os.EINVAL }, + Atoui32Test{ "0", 0, nil }, + Atoui32Test{ "1", 1, nil }, + Atoui32Test{ "12345", 12345, nil }, + Atoui32Test{ "012345", 0, os.EINVAL }, + Atoui32Test{ "12345x", 0, os.EINVAL }, + Atoui32Test{ "987654321", 987654321, nil }, + Atoui32Test{ "4294967295", 1<<32-1, nil }, + Atoui32Test{ "4294967296", 1<<32-1, os.ERANGE }, } -type Int32Test struct { +type Atoi32Test struct { in string; out int32; err *os.Error; } -var int32tests = []Int32Test { - Int32Test{ "", 0, os.EINVAL }, - Int32Test{ "0", 0, nil }, - Int32Test{ "-0", 0, nil }, - Int32Test{ "1", 1, nil }, - Int32Test{ "-1", -1, nil }, - Int32Test{ "12345", 12345, nil }, - Int32Test{ "-12345", -12345, nil }, - Int32Test{ "012345", 0, os.EINVAL }, - Int32Test{ "-012345", 0, os.EINVAL }, - Int32Test{ "12345x", 0, os.EINVAL }, - Int32Test{ "-12345x", 0, os.EINVAL }, - Int32Test{ "987654321", 987654321, nil }, - Int32Test{ "-987654321", -987654321, nil }, - Int32Test{ "2147483647", 1<<31-1, nil }, - Int32Test{ "-2147483647", -(1<<31-1), nil }, - Int32Test{ "2147483648", 1<<31-1, os.ERANGE }, - Int32Test{ "-2147483648", -1<<31, nil }, - Int32Test{ "2147483649", 1<<31-1, os.ERANGE }, - Int32Test{ "-2147483649", -1<<31, os.ERANGE }, +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", 0, os.EINVAL }, + Atoi32Test{ "-012345", 0, os.EINVAL }, + 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 }, } export func TestAtoui64(t *testing.T) { - for i := 0; i < len(uint64tests); i++ { - test := &uint64tests[i]; + for i := 0; i < len(atoui64tests); i++ { + test := &atoui64tests[i]; out, err := strconv.atoui64(test.in); if test.out != out || test.err != err { t.Errorf("strconv.atoui64(%v) = %v, %v want %v, %v\n", @@ -115,8 +115,8 @@ export func TestAtoui64(t *testing.T) { } export func TestAtoi64(t *testing.T) { - for i := 0; i < len(int64tests); i++ { - test := &int64tests[i]; + for i := 0; i < len(atoi64test); i++ { + test := &atoi64test[i]; out, err := strconv.atoi64(test.in); if test.out != out || test.err != err { t.Errorf("strconv.atoi64(%v) = %v, %v want %v, %v\n", @@ -136,8 +136,8 @@ func IntSize1() uint { export func TestAtoui(t *testing.T) { switch IntSize1() { case 32: - for i := 0; i < len(uint32tests); i++ { - test := &uint32tests[i]; + for i := 0; i < len(atoui32tests); i++ { + test := &atoui32tests[i]; out, err := strconv.atoui(test.in); if test.out != uint32(out) || test.err != err { t.Errorf("strconv.atoui(%v) = %v, %v want %v, %v\n", @@ -145,8 +145,8 @@ export func TestAtoui(t *testing.T) { } } case 64: - for i := 0; i < len(uint64tests); i++ { - test := &uint64tests[i]; + for i := 0; i < len(atoui64tests); i++ { + test := &atoui64tests[i]; out, err := strconv.atoui(test.in); if test.out != uint64(out) || test.err != err { t.Errorf("strconv.atoui(%v) = %v, %v want %v, %v\n", @@ -159,8 +159,8 @@ export func TestAtoui(t *testing.T) { export func TestAtoi(t *testing.T) { switch IntSize1() { case 32: - for i := 0; i < len(int32tests); i++ { - test := &int32tests[i]; + for i := 0; i < len(atoi32tests); i++ { + test := &atoi32tests[i]; out, err := strconv.atoi(test.in); if test.out != int32(out) || test.err != err { t.Errorf("strconv.atoi(%v) = %v, %v want %v, %v\n", @@ -168,8 +168,8 @@ export func TestAtoi(t *testing.T) { } } case 64: - for i := 0; i < len(int64tests); i++ { - test := &int64tests[i]; + for i := 0; i < len(atoi64test); i++ { + test := &atoi64test[i]; out, err := strconv.atoi(test.in); if test.out != int64(out) || test.err != err { t.Errorf("strconv.atoi(%v) = %v, %v want %v, %v\n", diff --git a/src/lib/strconv/ftoa.go b/src/lib/strconv/ftoa.go index b99d9d593..a0f5efcd0 100644 --- a/src/lib/strconv/ftoa.go +++ b/src/lib/strconv/ftoa.go @@ -28,7 +28,7 @@ func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string func Max(a, b int) int func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) -func FloatSize() int { +func floatsize() int { // Figure out whether float is float32 or float64. // 1e-35 is representable in both, but 1e-70 // is too small for a float32. @@ -38,7 +38,7 @@ func FloatSize() int { } return 64; } -export var floatsize = FloatSize() +export var FloatSize = floatsize() export func ftoa32(f float32, fmt byte, prec int) string { return GenericFtoa(uint64(sys.float32bits(f)), fmt, prec, &float32info); @@ -49,7 +49,7 @@ export func ftoa64(f float64, fmt byte, prec int) string { } export func ftoa(f float, fmt byte, prec int) string { - if floatsize == 32 { + if FloatSize == 32 { return ftoa32(float32(f), fmt, prec); } return ftoa64(float64(f), fmt, prec); diff --git a/src/lib/strconv/ftoa_test.go b/src/lib/strconv/ftoa_test.go index 6b90ff2f3..8dbe7f3c2 100644 --- a/src/lib/strconv/ftoa_test.go +++ b/src/lib/strconv/ftoa_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -type Test struct { +type FtoaTest struct { f float64; fmt byte; prec int; @@ -23,88 +23,87 @@ const ( Above1e23 = 100000000000000008388608; ) -// TODO: Should be able to call this tests but it conflicts with testatof.go -var ftests = []Test { - Test{ 1, 'e', 5, "1.00000e+00" }, - Test{ 1, 'f', 5, "1.00000" }, - Test{ 1, 'g', 5, "1" }, - Test{ 1, 'g', -1, "1" }, - Test{ 20, 'g', -1, "20" }, - Test{ 1234567.8, 'g', -1, "1.2345678e+06" }, - Test{ 200000, 'g', -1, "200000" }, - Test{ 2000000, 'g', -1, "2e+06" }, - - Test{ 0, 'e', 5, "0.00000e+00" }, - Test{ 0, 'f', 5, "0.00000" }, - Test{ 0, 'g', 5, "0" }, - Test{ 0, 'g', -1, "0" }, - - Test{ -1, 'e', 5, "-1.00000e+00" }, - Test{ -1, 'f', 5, "-1.00000" }, - Test{ -1, 'g', 5, "-1" }, - Test{ -1, 'g', -1, "-1" }, - - Test{ 12, 'e', 5, "1.20000e+01" }, - Test{ 12, 'f', 5, "12.00000" }, - Test{ 12, 'g', 5, "12" }, - Test{ 12, 'g', -1, "12" }, - - Test{ 123456700, 'e', 5, "1.23457e+08" }, - Test{ 123456700, 'f', 5, "123456700.00000" }, - Test{ 123456700, 'g', 5, "1.2346e+08" }, - Test{ 123456700, 'g', -1, "1.234567e+08" }, - - Test{ 1.2345e6, 'e', 5, "1.23450e+06" }, - Test{ 1.2345e6, 'f', 5, "1234500.00000" }, - Test{ 1.2345e6, 'g', 5, "1.2345e+06" }, - - Test{ 1e23, 'e', 17, "9.99999999999999916e+22" }, - Test{ 1e23, 'f', 17, "99999999999999991611392.00000000000000000" }, - Test{ 1e23, 'g', 17, "9.9999999999999992e+22" }, - - Test{ 1e23, 'e', -1, "1e+23" }, - Test{ 1e23, 'f', -1, "100000000000000000000000" }, - Test{ 1e23, 'g', -1, "1e+23" }, - - Test{ Below1e23, 'e', 17, "9.99999999999999748e+22" }, - Test{ Below1e23, 'f', 17, "99999999999999974834176.00000000000000000" }, - Test{ Below1e23, 'g', 17, "9.9999999999999975e+22" }, - - Test{ Below1e23, 'e', -1, "9.999999999999997e+22" }, - Test{ Below1e23, 'f', -1, "99999999999999970000000" }, - Test{ Below1e23, 'g', -1, "9.999999999999997e+22" }, - - Test{ Above1e23, 'e', 17, "1.00000000000000008e+23" }, - Test{ Above1e23, 'f', 17, "100000000000000008388608.00000000000000000" }, - Test{ Above1e23, 'g', 17, "1.0000000000000001e+23" }, - - Test{ Above1e23, 'e', -1, "1.0000000000000001e+23" }, - Test{ Above1e23, 'f', -1, "100000000000000010000000" }, - Test{ Above1e23, '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', 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" }, +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" }, + + 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{ sys.NaN(), 'g', -1, "NaN" }, + FtoaTest{ -sys.NaN(), 'g', -1, "NaN" }, + FtoaTest{ sys.Inf(0), 'g', -1, "+Inf" }, + FtoaTest{ sys.Inf(-1), 'g', -1, "-Inf" }, + FtoaTest{ -sys.Inf(0), 'g', -1, "-Inf" }, + + FtoaTest{ -1, 'b', -1, "-4503599627370496p-52" }, } export func TestFtoa(t *testing.T) { - if strconv.floatsize != 32 { - panic("floatsize: ", strconv.floatsize); + if strconv.FloatSize != 32 { + panic("floatsize: ", strconv.FloatSize); } - for i := 0; i < len(ftests); i++ { - test := &ftests[i]; + for i := 0; i < len(ftoatests); i++ { + test := &ftoatests[i]; s := strconv.ftoa64(test.f, test.fmt, test.prec); if s != test.s { t.Error("test", test.f, string(test.fmt), test.prec, "want", test.s, "got", s); diff --git a/src/lib/strconv/itoa_test.go b/src/lib/strconv/itoa_test.go index 89a97339e..880aff4a9 100644 --- a/src/lib/strconv/itoa_test.go +++ b/src/lib/strconv/itoa_test.go @@ -11,40 +11,38 @@ import ( "testing"; ) -type Int64Test struct { +type Itoa64Test 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" }, +var itoa64tests = []Itoa64Test { + Itoa64Test{ 0, "0" }, + Itoa64Test{ 1, "1" }, + Itoa64Test{ -1, "-1" }, + Itoa64Test{ 12345678, "12345678" }, + Itoa64Test{ -987654321, "-987654321" }, + Itoa64Test{ 1<<31-1, "2147483647" }, + Itoa64Test{ -1<<31+1, "-2147483647" }, + Itoa64Test{ 1<<31, "2147483648" }, + Itoa64Test{ -1<<31, "-2147483648" }, + Itoa64Test{ 1<<31+1, "2147483649" }, + Itoa64Test{ -1<<31-1, "-2147483649" }, + Itoa64Test{ 1<<32-1, "4294967295" }, + Itoa64Test{ -1<<32+1, "-4294967295" }, + Itoa64Test{ 1<<32, "4294967296" }, + Itoa64Test{ -1<<32, "-4294967296" }, + Itoa64Test{ 1<<32+1, "4294967297" }, + Itoa64Test{ -1<<32-1, "-4294967297" }, + Itoa64Test{ 1<<50, "1125899906842624" }, + Itoa64Test{ 1<<63-1, "9223372036854775807" }, + Itoa64Test{ -1<<63+1, "-9223372036854775807" }, + Itoa64Test{ -1<<63, "-9223372036854775808" }, } export func TestItoa(t *testing.T) { - for i := 0; i < len(xint64tests); i++ { - test := xint64tests[i]; + for i := 0; i < len(itoa64tests); i++ { + test := itoa64tests[i]; s := strconv.itoa64(test.in); if s != test.out { t.Error("strconv.itoa64(%v) = %v want %v\n", @@ -61,17 +59,17 @@ export func TestItoa(t *testing.T) { } // TODO: Use once there is a strconv.uitoa -type Uint64Test struct { +type Uitoa64Test 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" }, +// TODO: should be able to call this atoui64tests. +var uitoa64tests = []Uitoa64Test { + Uitoa64Test{ 1<<63-1, "9223372036854775807" }, + Uitoa64Test{ 1<<63, "9223372036854775808" }, + Uitoa64Test{ 1<<63+1, "9223372036854775809" }, + Uitoa64Test{ 1<<64-2, "18446744073709551614" }, + Uitoa64Test{ 1<<64-1, "18446744073709551615" }, } diff --git a/src/lib/strconv/quote.go b/src/lib/strconv/quote.go index 36fa19510..987545052 100644 --- a/src/lib/strconv/quote.go +++ b/src/lib/strconv/quote.go @@ -8,8 +8,7 @@ import ( "utf8"; ) -const ldigits = "0123456789abcdef" -const udigits = "0123456789ABCDEF" +const lowerhex = "0123456789abcdef" export func Quote(s string) string { t := `"`; @@ -37,7 +36,7 @@ export func Quote(s string) string { t += `\v`; case s[i] < utf8.RuneSelf: - t += `\x` + string(ldigits[s[i]>>4]) + string(ldigits[s[i]&0xF]); + t += `\x` + string(lowerhex[s[i]>>4]) + string(lowerhex[s[i]&0xF]); case utf8.FullRuneInString(s, i): r, size := utf8.DecodeRuneInString(s, i); @@ -48,20 +47,20 @@ export func Quote(s string) string { if r < 0x10000 { t += `\u`; for j:=uint(0); j<4; j++ { - t += string(ldigits[(r>>(12-4*j))&0xF]); + t += string(lowerhex[(r>>(12-4*j))&0xF]); } } else { t += `\U`; for j:=uint(0); j<8; j++ { - t += string(ldigits[(r>>(28-4*j))&0xF]); + t += string(lowerhex[(r>>(28-4*j))&0xF]); } } default: EscX: t += `\x`; - t += string(ldigits[s[i]>>4]); - t += string(ldigits[s[i]&0xF]); + t += string(lowerhex[s[i]>>4]); + t += string(lowerhex[s[i]&0xF]); } } t += `"`; |