OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / libgo / go / strconv / itoa_test.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package strconv_test
6
7 import (
8         . "strconv"
9         "testing"
10 )
11
12 type itob64Test struct {
13         in   int64
14         base uint
15         out  string
16 }
17
18 var itob64tests = []itob64Test{
19         {0, 10, "0"},
20         {1, 10, "1"},
21         {-1, 10, "-1"},
22         {12345678, 10, "12345678"},
23         {-987654321, 10, "-987654321"},
24         {1<<31 - 1, 10, "2147483647"},
25         {-1<<31 + 1, 10, "-2147483647"},
26         {1 << 31, 10, "2147483648"},
27         {-1 << 31, 10, "-2147483648"},
28         {1<<31 + 1, 10, "2147483649"},
29         {-1<<31 - 1, 10, "-2147483649"},
30         {1<<32 - 1, 10, "4294967295"},
31         {-1<<32 + 1, 10, "-4294967295"},
32         {1 << 32, 10, "4294967296"},
33         {-1 << 32, 10, "-4294967296"},
34         {1<<32 + 1, 10, "4294967297"},
35         {-1<<32 - 1, 10, "-4294967297"},
36         {1 << 50, 10, "1125899906842624"},
37         {1<<63 - 1, 10, "9223372036854775807"},
38         {-1<<63 + 1, 10, "-9223372036854775807"},
39         {-1 << 63, 10, "-9223372036854775808"},
40
41         {0, 2, "0"},
42         {10, 2, "1010"},
43         {-1, 2, "-1"},
44         {1 << 15, 2, "1000000000000000"},
45
46         {-8, 8, "-10"},
47         {057635436545, 8, "57635436545"},
48         {1 << 24, 8, "100000000"},
49
50         {16, 16, "10"},
51         {-0x123456789abcdef, 16, "-123456789abcdef"},
52         {1<<63 - 1, 16, "7fffffffffffffff"},
53         {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"},
54
55         {16, 17, "g"},
56         {25, 25, "10"},
57         {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"},
58         {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"},
59 }
60
61 func TestItoa(t *testing.T) {
62         for _, test := range itob64tests {
63                 s := Itob64(test.in, test.base)
64                 if s != test.out {
65                         t.Errorf("Itob64(%v, %v) = %v want %v",
66                                 test.in, test.base, s, test.out)
67                 }
68
69                 if test.in >= 0 {
70                         s := Uitob64(uint64(test.in), test.base)
71                         if s != test.out {
72                                 t.Errorf("Uitob64(%v, %v) = %v want %v",
73                                         test.in, test.base, s, test.out)
74                         }
75                 }
76
77                 if int64(int(test.in)) == test.in {
78                         s := Itob(int(test.in), test.base)
79                         if s != test.out {
80                                 t.Errorf("Itob(%v, %v) = %v want %v",
81                                         test.in, test.base, s, test.out)
82                         }
83
84                         if test.in >= 0 {
85                                 s := Uitob(uint(test.in), test.base)
86                                 if s != test.out {
87                                         t.Errorf("Uitob(%v, %v) = %v want %v",
88                                                 test.in, test.base, s, test.out)
89                                 }
90                         }
91                 }
92
93                 if test.base == 10 {
94                         s := Itoa64(test.in)
95                         if s != test.out {
96                                 t.Errorf("Itoa64(%v) = %v want %v",
97                                         test.in, s, test.out)
98                         }
99
100                         if test.in >= 0 {
101                                 s := Uitob64(uint64(test.in), test.base)
102                                 if s != test.out {
103                                         t.Errorf("Uitob64(%v, %v) = %v want %v",
104                                                 test.in, test.base, s, test.out)
105                                 }
106                         }
107
108                         if int64(int(test.in)) == test.in {
109                                 s := Itoa(int(test.in))
110                                 if s != test.out {
111                                         t.Errorf("Itoa(%v) = %v want %v",
112                                                 test.in, s, test.out)
113                                 }
114
115                                 if test.in >= 0 {
116                                         s := Uitoa(uint(test.in))
117                                         if s != test.out {
118                                                 t.Errorf("Uitoa(%v) = %v want %v",
119                                                         test.in, s, test.out)
120                                         }
121                                 }
122                         }
123                 }
124         }
125 }
126
127 type uitob64Test struct {
128         in   uint64
129         base uint
130         out  string
131 }
132
133 var uitob64tests = []uitob64Test{
134         {1<<63 - 1, 10, "9223372036854775807"},
135         {1 << 63, 10, "9223372036854775808"},
136         {1<<63 + 1, 10, "9223372036854775809"},
137         {1<<64 - 2, 10, "18446744073709551614"},
138         {1<<64 - 1, 10, "18446744073709551615"},
139         {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"},
140 }
141
142 func TestUitoa(t *testing.T) {
143         for _, test := range uitob64tests {
144                 s := Uitob64(test.in, test.base)
145                 if s != test.out {
146                         t.Errorf("Uitob64(%v, %v) = %v want %v",
147                                 test.in, test.base, s, test.out)
148                 }
149
150                 if uint64(uint(test.in)) == test.in {
151                         s := Uitob(uint(test.in), test.base)
152                         if s != test.out {
153                                 t.Errorf("Uitob(%v, %v) = %v want %v",
154                                         test.in, test.base, s, test.out)
155                         }
156                 }
157
158                 if test.base == 10 {
159                         s := Uitoa64(test.in)
160                         if s != test.out {
161                                 t.Errorf("Uitoa64(%v) = %v want %v",
162                                         test.in, s, test.out)
163                         }
164
165                         if uint64(uint(test.in)) == test.in {
166                                 s := Uitoa(uint(test.in))
167                                 if s != test.out {
168                                         t.Errorf("Uitoa(%v) = %v want %v",
169                                                 test.in, s, test.out)
170                                 }
171                         }
172                 }
173         }
174 }