OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package strconv_test | 5 package strconv_test |
6 | 6 |
7 import ( | 7 import ( |
8 » . "strconv"; | 8 » . "strconv" |
9 » "testing"; | 9 » "testing" |
10 ) | 10 ) |
11 | 11 |
12 type itob64Test struct { | 12 type itob64Test struct { |
13 » in» int64; | 13 » in int64 |
14 » base» uint; | 14 » base uint |
15 » out» string; | 15 » out string |
16 } | 16 } |
17 | 17 |
18 var itob64tests = []itob64Test{ | 18 var itob64tests = []itob64Test{ |
19 itob64Test{0, 10, "0"}, | 19 itob64Test{0, 10, "0"}, |
20 itob64Test{1, 10, "1"}, | 20 itob64Test{1, 10, "1"}, |
21 itob64Test{-1, 10, "-1"}, | 21 itob64Test{-1, 10, "-1"}, |
22 itob64Test{12345678, 10, "12345678"}, | 22 itob64Test{12345678, 10, "12345678"}, |
23 itob64Test{-987654321, 10, "-987654321"}, | 23 itob64Test{-987654321, 10, "-987654321"}, |
24 itob64Test{1<<31 - 1, 10, "2147483647"}, | 24 itob64Test{1<<31 - 1, 10, "2147483647"}, |
25 itob64Test{-1<<31 + 1, 10, "-2147483647"}, | 25 itob64Test{-1<<31 + 1, 10, "-2147483647"}, |
(...skipping 26 matching lines...) Expand all Loading... |
52 itob64Test{1<<63 - 1, 16, "7fffffffffffffff"}, | 52 itob64Test{1<<63 - 1, 16, "7fffffffffffffff"}, |
53 | 53 |
54 itob64Test{16, 17, "g"}, | 54 itob64Test{16, 17, "g"}, |
55 itob64Test{25, 25, "10"}, | 55 itob64Test{25, 25, "10"}, |
56 itob64Test{(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holy
cow"}, | 56 itob64Test{(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holy
cow"}, |
57 itob64Test{(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holy
cow"}, | 57 itob64Test{(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holy
cow"}, |
58 } | 58 } |
59 | 59 |
60 func TestItoa(t *testing.T) { | 60 func TestItoa(t *testing.T) { |
61 for _, test := range itob64tests { | 61 for _, test := range itob64tests { |
62 » » s := Itob64(test.in, test.base); | 62 » » s := Itob64(test.in, test.base) |
63 if s != test.out { | 63 if s != test.out { |
64 t.Errorf("Itob64(%v, %v) = %v want %v\n", | 64 t.Errorf("Itob64(%v, %v) = %v want %v\n", |
65 test.in, test.base, s, test.out) | 65 test.in, test.base, s, test.out) |
66 } | 66 } |
67 | 67 |
68 if test.in >= 0 { | 68 if test.in >= 0 { |
69 » » » s := Uitob64(uint64(test.in), test.base); | 69 » » » s := Uitob64(uint64(test.in), test.base) |
70 if s != test.out { | 70 if s != test.out { |
71 t.Errorf("Uitob64(%v, %v) = %v want %v\n", | 71 t.Errorf("Uitob64(%v, %v) = %v want %v\n", |
72 test.in, test.base, s, test.out) | 72 test.in, test.base, s, test.out) |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 if int64(int(test.in)) == test.in { | 76 if int64(int(test.in)) == test.in { |
77 » » » s := Itob(int(test.in), test.base); | 77 » » » s := Itob(int(test.in), test.base) |
78 if s != test.out { | 78 if s != test.out { |
79 t.Errorf("Itob(%v, %v) = %v want %v\n", | 79 t.Errorf("Itob(%v, %v) = %v want %v\n", |
80 test.in, test.base, s, test.out) | 80 test.in, test.base, s, test.out) |
81 } | 81 } |
82 | 82 |
83 if test.in >= 0 { | 83 if test.in >= 0 { |
84 » » » » s := Uitob(uint(test.in), test.base); | 84 » » » » s := Uitob(uint(test.in), test.base) |
85 if s != test.out { | 85 if s != test.out { |
86 t.Errorf("Uitob(%v, %v) = %v want %v\n", | 86 t.Errorf("Uitob(%v, %v) = %v want %v\n", |
87 test.in, test.base, s, test.out) | 87 test.in, test.base, s, test.out) |
88 } | 88 } |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 if test.base == 10 { | 92 if test.base == 10 { |
93 » » » s := Itoa64(test.in); | 93 » » » s := Itoa64(test.in) |
94 if s != test.out { | 94 if s != test.out { |
95 t.Errorf("Itoa64(%v) = %v want %v\n", | 95 t.Errorf("Itoa64(%v) = %v want %v\n", |
96 test.in, s, test.out) | 96 test.in, s, test.out) |
97 } | 97 } |
98 | 98 |
99 if test.in >= 0 { | 99 if test.in >= 0 { |
100 » » » » s := Uitob64(uint64(test.in), test.base); | 100 » » » » s := Uitob64(uint64(test.in), test.base) |
101 if s != test.out { | 101 if s != test.out { |
102 t.Errorf("Uitob64(%v, %v) = %v want %v\n
", | 102 t.Errorf("Uitob64(%v, %v) = %v want %v\n
", |
103 test.in, test.base, s, test.out) | 103 test.in, test.base, s, test.out) |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 if int64(int(test.in)) == test.in { | 107 if int64(int(test.in)) == test.in { |
108 » » » » s := Itoa(int(test.in)); | 108 » » » » s := Itoa(int(test.in)) |
109 if s != test.out { | 109 if s != test.out { |
110 t.Errorf("Itoa(%v) = %v want %v\n", | 110 t.Errorf("Itoa(%v) = %v want %v\n", |
111 test.in, s, test.out) | 111 test.in, s, test.out) |
112 } | 112 } |
113 | 113 |
114 if test.in >= 0 { | 114 if test.in >= 0 { |
115 » » » » » s := Uitoa(uint(test.in)); | 115 » » » » » s := Uitoa(uint(test.in)) |
116 if s != test.out { | 116 if s != test.out { |
117 t.Errorf("Uitoa(%v) = %v want %v
\n", | 117 t.Errorf("Uitoa(%v) = %v want %v
\n", |
118 test.in, s, test.out) | 118 test.in, s, test.out) |
119 } | 119 } |
120 } | 120 } |
121 } | 121 } |
122 } | 122 } |
123 } | 123 } |
124 } | 124 } |
125 | 125 |
126 type uitob64Test struct { | 126 type uitob64Test struct { |
127 » in» uint64; | 127 » in uint64 |
128 » base» uint; | 128 » base uint |
129 » out» string; | 129 » out string |
130 } | 130 } |
131 | 131 |
132 var uitob64tests = []uitob64Test{ | 132 var uitob64tests = []uitob64Test{ |
133 uitob64Test{1<<63 - 1, 10, "9223372036854775807"}, | 133 uitob64Test{1<<63 - 1, 10, "9223372036854775807"}, |
134 uitob64Test{1 << 63, 10, "9223372036854775808"}, | 134 uitob64Test{1 << 63, 10, "9223372036854775808"}, |
135 uitob64Test{1<<63 + 1, 10, "9223372036854775809"}, | 135 uitob64Test{1<<63 + 1, 10, "9223372036854775809"}, |
136 uitob64Test{1<<64 - 2, 10, "18446744073709551614"}, | 136 uitob64Test{1<<64 - 2, 10, "18446744073709551614"}, |
137 uitob64Test{1<<64 - 1, 10, "18446744073709551615"}, | 137 uitob64Test{1<<64 - 1, 10, "18446744073709551615"}, |
138 } | 138 } |
139 | 139 |
140 func TestUitoa(t *testing.T) { | 140 func TestUitoa(t *testing.T) { |
141 for _, test := range uitob64tests { | 141 for _, test := range uitob64tests { |
142 » » s := Uitob64(test.in, test.base); | 142 » » s := Uitob64(test.in, test.base) |
143 if s != test.out { | 143 if s != test.out { |
144 t.Errorf("Uitob64(%v, %v) = %v want %v\n", | 144 t.Errorf("Uitob64(%v, %v) = %v want %v\n", |
145 test.in, test.base, s, test.out) | 145 test.in, test.base, s, test.out) |
146 } | 146 } |
147 | 147 |
148 if uint64(uint(test.in)) == test.in { | 148 if uint64(uint(test.in)) == test.in { |
149 » » » s := Uitob(uint(test.in), test.base); | 149 » » » s := Uitob(uint(test.in), test.base) |
150 if s != test.out { | 150 if s != test.out { |
151 t.Errorf("Uitob(%v, %v) = %v want %v\n", | 151 t.Errorf("Uitob(%v, %v) = %v want %v\n", |
152 test.in, test.base, s, test.out) | 152 test.in, test.base, s, test.out) |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 if test.base == 10 { | 156 if test.base == 10 { |
157 » » » s := Uitoa64(test.in); | 157 » » » s := Uitoa64(test.in) |
158 if s != test.out { | 158 if s != test.out { |
159 t.Errorf("Uitoa64(%v) = %v want %v\n", | 159 t.Errorf("Uitoa64(%v) = %v want %v\n", |
160 test.in, s, test.out) | 160 test.in, s, test.out) |
161 } | 161 } |
162 | 162 |
163 if uint64(uint(test.in)) == test.in { | 163 if uint64(uint(test.in)) == test.in { |
164 » » » » s := Uitoa(uint(test.in)); | 164 » » » » s := Uitoa(uint(test.in)) |
165 if s != test.out { | 165 if s != test.out { |
166 t.Errorf("Uitoa(%v) = %v want %v\n", | 166 t.Errorf("Uitoa(%v) = %v want %v\n", |
167 test.in, s, test.out) | 167 test.in, s, test.out) |
168 } | 168 } |
169 } | 169 } |
170 } | 170 } |
171 } | 171 } |
172 } | 172 } |
OLD | NEW |