summaryrefslogtreecommitdiff
path: root/libgo/go/strconv/itoa_test.go
blob: 8514b21e47ca7bf6235aeb484ee8d405df0896fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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_test

import (
	. "strconv"
	"testing"
)

type itob64Test struct {
	in   int64
	base uint
	out  string
}

var itob64tests = []itob64Test{
	{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",
				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",
					test.in, test.base, s, test.out)
			}
		}

		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",
					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",
						test.in, test.base, s, test.out)
				}
			}
		}

		if test.base == 10 {
			s := Itoa64(test.in)
			if s != test.out {
				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",
						test.in, test.base, s, test.out)
				}
			}

			if int64(int(test.in)) == test.in {
				s := Itoa(int(test.in))
				if s != test.out {
					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",
							test.in, s, test.out)
					}
				}
			}
		}
	}
}

type uitob64Test struct {
	in   uint64
	base uint
	out  string
}

var uitob64tests = []uitob64Test{
	{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",
				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",
					test.in, test.base, s, test.out)
			}
		}

		if test.base == 10 {
			s := Uitoa64(test.in)
			if s != test.out {
				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",
						test.in, s, test.out)
				}
			}
		}
	}
}