2008-11-19 13:50:34 -07:00
|
|
|
// 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.
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
package strconv_test
|
2008-11-19 13:50:34 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt";
|
|
|
|
"os";
|
2009-08-12 14:18:37 -06:00
|
|
|
. "strconv";
|
2008-11-19 15:38:05 -07:00
|
|
|
"testing";
|
2008-11-19 13:50:34 -07:00
|
|
|
)
|
|
|
|
|
2009-03-02 18:52:58 -07:00
|
|
|
type itob64Test struct {
|
2008-11-19 13:50:34 -07:00
|
|
|
in int64;
|
2009-03-02 18:52:58 -07:00
|
|
|
base uint;
|
2008-11-19 13:50:34 -07:00
|
|
|
out string;
|
|
|
|
}
|
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
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{ 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" },
|
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestItoa(t *testing.T) {
|
2009-07-02 17:57:46 -06:00
|
|
|
for i, test := range itob64tests {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Itob64(test.in, test.base);
|
2008-11-19 15:38:05 -07:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Itob64(%v, %v) = %v want %v\n",
|
2009-03-02 18:52:58 -07:00
|
|
|
test.in, test.base, s, test.out);
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|
2009-03-02 18:52:58 -07:00
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
if test.in >= 0 {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitob64(uint64(test.in), test.base);
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitob64(%v, %v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, test.base, s, test.out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 15:38:05 -07:00
|
|
|
if int64(int(test.in)) == test.in {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Itob(int(test.in), test.base);
|
2008-11-19 15:38:05 -07:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Itob(%v, %v) = %v want %v\n",
|
2009-03-02 18:52:58 -07:00
|
|
|
test.in, test.base, s, test.out);
|
|
|
|
}
|
2009-07-02 17:57:46 -06:00
|
|
|
|
|
|
|
if test.in >= 0 {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitob(uint(test.in), test.base);
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitob(%v, %v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, test.base, s, test.out);
|
|
|
|
}
|
|
|
|
}
|
2009-03-02 18:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if test.base == 10 {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Itoa64(test.in);
|
2009-03-02 18:52:58 -07:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Itoa64(%v) = %v want %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, s, test.out);
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|
2009-03-02 18:52:58 -07:00
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
if test.in >= 0 {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitob64(uint64(test.in), test.base);
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitob64(%v, %v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, test.base, s, test.out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-02 18:52:58 -07:00
|
|
|
if int64(int(test.in)) == test.in {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Itoa(int(test.in));
|
2009-03-02 18:52:58 -07:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Itoa(%v) = %v want %v\n",
|
2009-03-02 18:52:58 -07:00
|
|
|
test.in, s, test.out);
|
|
|
|
}
|
2009-07-02 17:57:46 -06:00
|
|
|
|
|
|
|
if test.in >= 0 {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitoa(uint(test.in));
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitoa(%v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, s, test.out);
|
|
|
|
}
|
|
|
|
}
|
2009-03-02 18:52:58 -07:00
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
type uitob64Test struct {
|
2008-11-19 13:50:34 -07:00
|
|
|
in uint64;
|
2009-07-02 17:57:46 -06:00
|
|
|
base uint;
|
2008-11-19 13:50:34 -07:00
|
|
|
out string;
|
|
|
|
}
|
|
|
|
|
2009-07-02 17:57:46 -06:00
|
|
|
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" },
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUitoa(t *testing.T) {
|
|
|
|
for i, test := range uitob64tests {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitob64(test.in, test.base);
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitob64(%v, %v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, test.base, s, test.out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(uint(test.in)) == test.in {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitob(uint(test.in), test.base);
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitob(%v, %v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, test.base, s, test.out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.base == 10 {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitoa64(test.in);
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitoa64(%v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, s, test.out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(uint(test.in)) == test.in {
|
2009-08-12 14:18:37 -06:00
|
|
|
s := Uitoa(uint(test.in));
|
2009-07-02 17:57:46 -06:00
|
|
|
if s != test.out {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Uitoa(%v) = %v want %v\n",
|
2009-07-02 17:57:46 -06:00
|
|
|
test.in, s, test.out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|