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 bytes_test | 5 package bytes_test |
6 | 6 |
7 import ( | 7 import ( |
8 . "bytes" | 8 . "bytes" |
9 "testing" | 9 "testing" |
10 "unicode" | 10 "unicode" |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 buf[n-1] = '0' | 204 buf[n-1] = '0' |
205 } | 205 } |
206 | 206 |
207 type ExplodeTest struct { | 207 type ExplodeTest struct { |
208 s string | 208 s string |
209 n int | 209 n int |
210 a []string | 210 a []string |
211 } | 211 } |
212 | 212 |
213 var explodetests = []ExplodeTest{ | 213 var explodetests = []ExplodeTest{ |
214 » ExplodeTest{abcd, 0, []string{"a", "b", "c", "d"}}, | 214 » ExplodeTest{abcd, -1, []string{"a", "b", "c", "d"}}, |
215 » ExplodeTest{faces, 0, []string{"☺", "☻", "☹"}}, | 215 » ExplodeTest{faces, -1, []string{"☺", "☻", "☹"}}, |
216 ExplodeTest{abcd, 2, []string{"a", "bcd"}}, | 216 ExplodeTest{abcd, 2, []string{"a", "bcd"}}, |
217 } | 217 } |
218 | 218 |
219 func TestExplode(t *testing.T) { | 219 func TestExplode(t *testing.T) { |
220 for _, tt := range explodetests { | 220 for _, tt := range explodetests { |
221 a := Split([]byte(tt.s), nil, tt.n) | 221 a := Split([]byte(tt.s), nil, tt.n) |
222 result := arrayOfString(a) | 222 result := arrayOfString(a) |
223 if !eq(result, tt.a) { | 223 if !eq(result, tt.a) { |
224 t.Errorf(`Explode("%s", %d) = %v; want %v`, tt.s, tt.n,
result, tt.a) | 224 t.Errorf(`Explode("%s", %d) = %v; want %v`, tt.s, tt.n,
result, tt.a) |
225 continue | 225 continue |
226 } | 226 } |
227 s := Join(a, []byte{}) | 227 s := Join(a, []byte{}) |
228 if string(s) != tt.s { | 228 if string(s) != tt.s { |
229 t.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt.s, tt.
n, s) | 229 t.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt.s, tt.
n, s) |
230 } | 230 } |
231 } | 231 } |
232 } | 232 } |
233 | 233 |
234 | 234 |
235 type SplitTest struct { | 235 type SplitTest struct { |
236 s string | 236 s string |
237 sep string | 237 sep string |
238 n int | 238 n int |
239 a []string | 239 a []string |
240 } | 240 } |
241 | 241 |
242 var splittests = []SplitTest{ | 242 var splittests = []SplitTest{ |
243 » SplitTest{abcd, "a", 0, []string{"", "bcd"}}, | 243 » SplitTest{abcd, "a", 0, nil}, |
244 » SplitTest{abcd, "z", 0, []string{"abcd"}}, | 244 » SplitTest{abcd, "a", -1, []string{"", "bcd"}}, |
245 » SplitTest{abcd, "", 0, []string{"a", "b", "c", "d"}}, | 245 » SplitTest{abcd, "z", -1, []string{"abcd"}}, |
246 » SplitTest{commas, ",", 0, []string{"1", "2", "3", "4"}}, | 246 » SplitTest{abcd, "", -1, []string{"a", "b", "c", "d"}}, |
247 » SplitTest{dots, "...", 0, []string{"1", ".2", ".3", ".4"}}, | 247 » SplitTest{commas, ",", -1, []string{"1", "2", "3", "4"}}, |
248 » SplitTest{faces, "☹", 0, []string{"☺☻", ""}}, | 248 » SplitTest{dots, "...", -1, []string{"1", ".2", ".3", ".4"}}, |
249 » SplitTest{faces, "~", 0, []string{faces}}, | 249 » SplitTest{faces, "☹", -1, []string{"☺☻", ""}}, |
250 » SplitTest{faces, "", 0, []string{"☺", "☻", "☹"}}, | 250 » SplitTest{faces, "~", -1, []string{faces}}, |
| 251 » SplitTest{faces, "", -1, []string{"☺", "☻", "☹"}}, |
251 SplitTest{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}}, | 252 SplitTest{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}}, |
252 SplitTest{"1 2 3", " ", 3, []string{"1", "2", "3"}}, | |
253 SplitTest{"1 2", " ", 3, []string{"1", "2"}}, | 253 SplitTest{"1 2", " ", 3, []string{"1", "2"}}, |
254 SplitTest{"123", "", 2, []string{"1", "23"}}, | 254 SplitTest{"123", "", 2, []string{"1", "23"}}, |
255 SplitTest{"123", "", 17, []string{"1", "2", "3"}}, | 255 SplitTest{"123", "", 17, []string{"1", "2", "3"}}, |
256 } | 256 } |
257 | 257 |
258 func TestSplit(t *testing.T) { | 258 func TestSplit(t *testing.T) { |
259 for _, tt := range splittests { | 259 for _, tt := range splittests { |
260 a := Split([]byte(tt.s), []byte(tt.sep), tt.n) | 260 a := Split([]byte(tt.s), []byte(tt.sep), tt.n) |
261 result := arrayOfString(a) | 261 result := arrayOfString(a) |
262 if !eq(result, tt.a) { | 262 if !eq(result, tt.a) { |
263 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep
, tt.n, result, tt.a) | 263 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep
, tt.n, result, tt.a) |
264 continue | 264 continue |
265 } | 265 } |
| 266 if tt.n == 0 { |
| 267 continue |
| 268 } |
266 s := Join(a, []byte(tt.sep)) | 269 s := Join(a, []byte(tt.sep)) |
267 if string(s) != tt.s { | 270 if string(s) != tt.s { |
268 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.se
p, tt.n, tt.sep, s) | 271 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.se
p, tt.n, tt.sep, s) |
269 } | 272 } |
270 } | 273 } |
271 } | 274 } |
272 | 275 |
273 var splitaftertests = []SplitTest{ | 276 var splitaftertests = []SplitTest{ |
274 » SplitTest{abcd, "a", 0, []string{"a", "bcd"}}, | 277 » SplitTest{abcd, "a", -1, []string{"a", "bcd"}}, |
275 » SplitTest{abcd, "z", 0, []string{"abcd"}}, | 278 » SplitTest{abcd, "z", -1, []string{"abcd"}}, |
276 » SplitTest{abcd, "", 0, []string{"a", "b", "c", "d"}}, | 279 » SplitTest{abcd, "", -1, []string{"a", "b", "c", "d"}}, |
277 » SplitTest{commas, ",", 0, []string{"1,", "2,", "3,", "4"}}, | 280 » SplitTest{commas, ",", -1, []string{"1,", "2,", "3,", "4"}}, |
278 » SplitTest{dots, "...", 0, []string{"1...", ".2...", ".3...", ".4"}}, | 281 » SplitTest{dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}}, |
279 » SplitTest{faces, "☹", 0, []string{"☺☻☹", ""}}, | 282 » SplitTest{faces, "☹", -1, []string{"☺☻☹", ""}}, |
280 » SplitTest{faces, "~", 0, []string{faces}}, | 283 » SplitTest{faces, "~", -1, []string{faces}}, |
281 » SplitTest{faces, "", 0, []string{"☺", "☻", "☹"}}, | 284 » SplitTest{faces, "", -1, []string{"☺", "☻", "☹"}}, |
282 SplitTest{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}}, | 285 SplitTest{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}}, |
283 SplitTest{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}}, | 286 SplitTest{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}}, |
284 SplitTest{"1 2", " ", 3, []string{"1 ", "2"}}, | 287 SplitTest{"1 2", " ", 3, []string{"1 ", "2"}}, |
285 SplitTest{"123", "", 2, []string{"1", "23"}}, | 288 SplitTest{"123", "", 2, []string{"1", "23"}}, |
286 SplitTest{"123", "", 17, []string{"1", "2", "3"}}, | 289 SplitTest{"123", "", 17, []string{"1", "2", "3"}}, |
287 } | 290 } |
288 | 291 |
289 func TestSplitAfter(t *testing.T) { | 292 func TestSplitAfter(t *testing.T) { |
290 for _, tt := range splitaftertests { | 293 for _, tt := range splitaftertests { |
291 a := SplitAfter([]byte(tt.s), []byte(tt.sep), tt.n) | 294 a := SplitAfter([]byte(tt.s), []byte(tt.sep), tt.n) |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 } | 650 } |
648 | 651 |
649 type ReplaceTest struct { | 652 type ReplaceTest struct { |
650 in string | 653 in string |
651 old, new string | 654 old, new string |
652 n int | 655 n int |
653 out string | 656 out string |
654 } | 657 } |
655 | 658 |
656 var ReplaceTests = []ReplaceTest{ | 659 var ReplaceTests = []ReplaceTest{ |
657 » ReplaceTest{"hello", "l", "L", 0, "heLLo"}, | 660 » ReplaceTest{"hello", "l", "L", 0, "hello"}, |
658 » ReplaceTest{"hello", "x", "X", 0, "hello"}, | 661 » ReplaceTest{"hello", "l", "L", -1, "heLLo"}, |
659 » ReplaceTest{"", "x", "X", 0, ""}, | 662 » ReplaceTest{"hello", "x", "X", -1, "hello"}, |
660 » ReplaceTest{"radar", "r", "<r>", 0, "<r>ada<r>"}, | 663 » ReplaceTest{"", "x", "X", -1, ""}, |
661 » ReplaceTest{"", "", "<>", 0, "<>"}, | 664 » ReplaceTest{"radar", "r", "<r>", -1, "<r>ada<r>"}, |
662 » ReplaceTest{"banana", "a", "<>", 0, "b<>n<>n<>"}, | 665 » ReplaceTest{"", "", "<>", -1, "<>"}, |
| 666 » ReplaceTest{"banana", "a", "<>", -1, "b<>n<>n<>"}, |
663 ReplaceTest{"banana", "a", "<>", 1, "b<>nana"}, | 667 ReplaceTest{"banana", "a", "<>", 1, "b<>nana"}, |
664 ReplaceTest{"banana", "a", "<>", 1000, "b<>n<>n<>"}, | 668 ReplaceTest{"banana", "a", "<>", 1000, "b<>n<>n<>"}, |
665 » ReplaceTest{"banana", "an", "<>", 0, "b<><>a"}, | 669 » ReplaceTest{"banana", "an", "<>", -1, "b<><>a"}, |
666 » ReplaceTest{"banana", "ana", "<>", 0, "b<>na"}, | 670 » ReplaceTest{"banana", "ana", "<>", -1, "b<>na"}, |
667 » ReplaceTest{"banana", "", "<>", 0, "<>b<>a<>n<>a<>n<>a<>"}, | 671 » ReplaceTest{"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"}, |
668 ReplaceTest{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"}, | 672 ReplaceTest{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"}, |
669 ReplaceTest{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"}, | 673 ReplaceTest{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"}, |
670 ReplaceTest{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"}, | 674 ReplaceTest{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"}, |
671 ReplaceTest{"banana", "", "<>", 1, "<>banana"}, | 675 ReplaceTest{"banana", "", "<>", 1, "<>banana"}, |
672 » ReplaceTest{"banana", "a", "a", 0, "banana"}, | 676 » ReplaceTest{"banana", "a", "a", -1, "banana"}, |
673 ReplaceTest{"banana", "a", "a", 1, "banana"}, | 677 ReplaceTest{"banana", "a", "a", 1, "banana"}, |
674 » ReplaceTest{"☺☻☹", "", "<>", 0, "<>☺<>☻<>☹<>"}, | 678 » ReplaceTest{"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"}, |
675 } | 679 } |
676 | 680 |
677 func TestReplace(t *testing.T) { | 681 func TestReplace(t *testing.T) { |
678 for _, tt := range ReplaceTests { | 682 for _, tt := range ReplaceTests { |
679 if s := string(Replace([]byte(tt.in), []byte(tt.old), []byte(tt.
new), tt.n)); s != tt.out { | 683 if s := string(Replace([]byte(tt.in), []byte(tt.old), []byte(tt.
new), tt.n)); s != tt.out { |
680 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in,
tt.old, tt.new, tt.n, s, tt.out) | 684 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in,
tt.old, tt.new, tt.n, s, tt.out) |
681 } | 685 } |
682 } | 686 } |
683 } | 687 } |
OLD | NEW |