@@ -291,113 +291,160 @@ fn test_replace_pattern() {
291
291
assert_eq ! ( data. replace( |c| c == 'γ' , "😺😺😺" ) , "abcdαβ😺😺😺δabcdαβ😺😺😺δ" ) ;
292
292
}
293
293
294
- #[ test]
295
- fn test_slice ( ) {
296
- assert_eq ! ( "ab" , & "abc" [ 0 ..2 ] ) ;
297
- assert_eq ! ( "bc" , & "abc" [ 1 ..3 ] ) ;
298
- assert_eq ! ( "" , & "abc" [ 1 ..1 ] ) ;
299
- assert_eq ! ( "\u{65e5} " , & "\u{65e5} \u{672c} " [ 0 ..3 ] ) ;
300
-
301
- let data = "ประเทศไทย中华" ;
302
- assert_eq ! ( "ป" , & data[ 0 ..3 ] ) ;
303
- assert_eq ! ( "ร" , & data[ 3 ..6 ] ) ;
304
- assert_eq ! ( "" , & data[ 3 ..3 ] ) ;
305
- assert_eq ! ( "华" , & data[ 30 ..33 ] ) ;
306
-
307
- fn a_million_letter_x ( ) -> String {
308
- let mut i = 0 ;
309
- let mut rs = String :: new ( ) ;
310
- while i < 100000 {
311
- rs. push_str ( "华华华华华华华华华华" ) ;
312
- i += 1 ;
294
+ mod slice_index {
295
+ #[ test]
296
+ fn test_slice ( ) {
297
+ assert_eq ! ( "ab" , & "abc" [ 0 ..2 ] ) ;
298
+ assert_eq ! ( "bc" , & "abc" [ 1 ..3 ] ) ;
299
+ assert_eq ! ( "" , & "abc" [ 1 ..1 ] ) ;
300
+ assert_eq ! ( "\u{65e5} " , & "\u{65e5} \u{672c} " [ 0 ..3 ] ) ;
301
+
302
+ let data = "ประเทศไทย中华" ;
303
+ assert_eq ! ( "ป" , & data[ 0 ..3 ] ) ;
304
+ assert_eq ! ( "ร" , & data[ 3 ..6 ] ) ;
305
+ assert_eq ! ( "" , & data[ 3 ..3 ] ) ;
306
+ assert_eq ! ( "华" , & data[ 30 ..33 ] ) ;
307
+
308
+ fn a_million_letter_x ( ) -> String {
309
+ let mut i = 0 ;
310
+ let mut rs = String :: new ( ) ;
311
+ while i < 100000 {
312
+ rs. push_str ( "华华华华华华华华华华" ) ;
313
+ i += 1 ;
314
+ }
315
+ rs
313
316
}
314
- rs
315
- }
316
- fn half_a_million_letter_x ( ) -> String {
317
- let mut i = 0 ;
318
- let mut rs = String :: new ( ) ;
319
- while i < 100000 {
320
- rs . push_str ( "华华华华华" ) ;
321
- i += 1 ;
317
+ fn half_a_million_letter_x ( ) -> String {
318
+ let mut i = 0 ;
319
+ let mut rs = String :: new ( ) ;
320
+ while i < 100000 {
321
+ rs . push_str ( "华华华华华" ) ;
322
+ i += 1 ;
323
+ }
324
+ rs
322
325
}
323
- rs
326
+ let letters = a_million_letter_x ( ) ;
327
+ assert_eq ! ( half_a_million_letter_x( ) , & letters[ 0 ..3 * 500000 ] ) ;
324
328
}
325
- let letters = a_million_letter_x ( ) ;
326
- assert_eq ! ( half_a_million_letter_x( ) , & letters[ 0 ..3 * 500000 ] ) ;
327
- }
328
329
329
- #[ test]
330
- fn test_slice_2 ( ) {
331
- let ss = "中华Việt Nam" ;
330
+ #[ test]
331
+ fn test_slice_2 ( ) {
332
+ let ss = "中华Việt Nam" ;
333
+
334
+ assert_eq ! ( "华" , & ss[ 3 ..6 ] ) ;
335
+ assert_eq ! ( "Việt Nam" , & ss[ 6 ..16 ] ) ;
336
+
337
+ assert_eq ! ( "ab" , & "abc" [ 0 ..2 ] ) ;
338
+ assert_eq ! ( "bc" , & "abc" [ 1 ..3 ] ) ;
339
+ assert_eq ! ( "" , & "abc" [ 1 ..1 ] ) ;
340
+
341
+ assert_eq ! ( "中" , & ss[ 0 ..3 ] ) ;
342
+ assert_eq ! ( "华V" , & ss[ 3 ..7 ] ) ;
343
+ assert_eq ! ( "" , & ss[ 3 ..3 ] ) ;
344
+ /*0: 中
345
+ 3: 华
346
+ 6: V
347
+ 7: i
348
+ 8: ệ
349
+ 11: t
350
+ 12:
351
+ 13: N
352
+ 14: a
353
+ 15: m */
354
+ }
332
355
333
- assert_eq ! ( "华" , & ss[ 3 ..6 ] ) ;
334
- assert_eq ! ( "Việt Nam" , & ss[ 6 ..16 ] ) ;
356
+ #[ test]
357
+ #[ should_panic]
358
+ fn test_slice_fail ( ) {
359
+ & "中华Việt Nam" [ 0 ..2 ] ;
360
+ }
335
361
336
- assert_eq ! ( "ab" , & "abc" [ 0 ..2 ] ) ;
337
- assert_eq ! ( "bc" , & "abc" [ 1 ..3 ] ) ;
338
- assert_eq ! ( "" , & "abc" [ 1 ..1 ] ) ;
362
+ #[ test]
363
+ #[ should_panic]
364
+ fn test_str_slice_rangetoinclusive_max_panics ( ) {
365
+ & "hello" [ ..=usize:: max_value ( ) ] ;
366
+ }
339
367
340
- assert_eq ! ( "中" , & ss[ 0 ..3 ] ) ;
341
- assert_eq ! ( "华V" , & ss[ 3 ..7 ] ) ;
342
- assert_eq ! ( "" , & ss[ 3 ..3 ] ) ;
343
- /*0: 中
344
- 3: 华
345
- 6: V
346
- 7: i
347
- 8: ệ
348
- 11: t
349
- 12:
350
- 13: N
351
- 14: a
352
- 15: m */
353
- }
368
+ #[ test]
369
+ #[ should_panic]
370
+ fn test_str_slice_rangeinclusive_max_panics ( ) {
371
+ & "hello" [ 1 ..=usize:: max_value ( ) ] ;
372
+ }
354
373
355
- #[ test]
356
- #[ should_panic]
357
- fn test_slice_fail ( ) {
358
- & "中华Việt Nam" [ 0 ..2 ] ;
359
- }
374
+ #[ test]
375
+ #[ should_panic]
376
+ fn test_str_slicemut_rangetoinclusive_max_panics ( ) {
377
+ let mut s = "hello" . to_owned ( ) ;
378
+ let s: & mut str = & mut s;
379
+ & mut s[ ..=usize:: max_value ( ) ] ;
380
+ }
360
381
361
- #[ test]
362
- #[ should_panic]
363
- fn test_str_slice_rangetoinclusive_max_panics ( ) {
364
- & "hello" [ ..=usize:: max_value ( ) ] ;
365
- }
382
+ #[ test]
383
+ #[ should_panic]
384
+ fn test_str_slicemut_rangeinclusive_max_panics ( ) {
385
+ let mut s = "hello" . to_owned ( ) ;
386
+ let s: & mut str = & mut s;
387
+ & mut s[ 1 ..=usize:: max_value ( ) ] ;
388
+ }
366
389
367
- #[ test]
368
- #[ should_panic]
369
- fn test_str_slice_rangeinclusive_max_panics ( ) {
370
- & "hello" [ 1 ..=usize:: max_value ( ) ] ;
371
- }
390
+ #[ test]
391
+ fn test_str_get_maxinclusive ( ) {
392
+ let mut s = "hello" . to_owned ( ) ;
393
+ {
394
+ let s: & str = & s;
395
+ assert_eq ! ( s. get( ..=usize :: max_value( ) ) , None ) ;
396
+ assert_eq ! ( s. get( 1 ..=usize :: max_value( ) ) , None ) ;
397
+ }
398
+ {
399
+ let s: & mut str = & mut s;
400
+ assert_eq ! ( s. get( ..=usize :: max_value( ) ) , None ) ;
401
+ assert_eq ! ( s. get( 1 ..=usize :: max_value( ) ) , None ) ;
402
+ }
403
+ }
372
404
373
- #[ test]
374
- #[ should_panic]
375
- fn test_str_slicemut_rangetoinclusive_max_panics ( ) {
376
- let mut s = "hello" . to_owned ( ) ;
377
- let s: & mut str = & mut s;
378
- & mut s[ ..=usize:: max_value ( ) ] ;
379
- }
405
+ const LOREM_PARAGRAPH : & ' static str = "\
406
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
407
+ ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
408
+ eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
409
+ sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
410
+ tempus vel, gravida nec quam.";
411
+
412
+ // check the panic includes the prefix of the sliced string
413
+ #[ test]
414
+ #[ should_panic( expected="byte index 1024 is out of bounds of `Lorem ipsum dolor sit amet" ) ]
415
+ fn test_slice_fail_truncated_1 ( ) {
416
+ & LOREM_PARAGRAPH [ ..1024 ] ;
417
+ }
418
+ // check the truncation in the panic message
419
+ #[ test]
420
+ #[ should_panic( expected="luctus, im`[...]" ) ]
421
+ fn test_slice_fail_truncated_2 ( ) {
422
+ & LOREM_PARAGRAPH [ ..1024 ] ;
423
+ }
380
424
381
- #[ test]
382
- #[ should_panic]
383
- fn test_str_slicemut_rangeinclusive_max_panics ( ) {
384
- let mut s = "hello" . to_owned ( ) ;
385
- let s: & mut str = & mut s;
386
- & mut s[ 1 ..=usize:: max_value ( ) ] ;
387
- }
425
+ #[ test]
426
+ #[ should_panic( expected="byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of" ) ]
427
+ fn test_slice_fail_boundary_1 ( ) {
428
+ & "abcαβγ" [ 4 ..] ;
429
+ }
388
430
389
- #[ test]
390
- fn test_str_get_maxinclusive ( ) {
391
- let mut s = "hello" . to_owned ( ) ;
392
- {
393
- let s: & str = & s;
394
- assert_eq ! ( s. get( ..=usize :: max_value( ) ) , None ) ;
395
- assert_eq ! ( s. get( 1 ..=usize :: max_value( ) ) , None ) ;
431
+ #[ test]
432
+ #[ should_panic( expected="byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of" ) ]
433
+ fn test_slice_fail_boundary_2 ( ) {
434
+ & "abcαβγ" [ 2 ..6 ] ;
396
435
}
397
- {
398
- let s: & mut str = & mut s;
399
- assert_eq ! ( s. get( ..=usize :: max_value( ) ) , None ) ;
400
- assert_eq ! ( s. get( 1 ..=usize :: max_value( ) ) , None ) ;
436
+
437
+ #[ test]
438
+ fn test_slice_from ( ) {
439
+ assert_eq ! ( & "abcd" [ 0 ..] , "abcd" ) ;
440
+ assert_eq ! ( & "abcd" [ 2 ..] , "cd" ) ;
441
+ assert_eq ! ( & "abcd" [ 4 ..] , "" ) ;
442
+ }
443
+ #[ test]
444
+ fn test_slice_to ( ) {
445
+ assert_eq ! ( & "abcd" [ ..0 ] , "" ) ;
446
+ assert_eq ! ( & "abcd" [ ..2 ] , "ab" ) ;
447
+ assert_eq ! ( & "abcd" [ ..4 ] , "abcd" ) ;
401
448
}
402
449
}
403
450
@@ -446,50 +493,6 @@ fn test_is_char_boundary() {
446
493
}
447
494
}
448
495
}
449
- const LOREM_PARAGRAPH : & ' static str = "\
450
- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis lorem sit amet dolor \
451
- ultricies condimentum. Praesent iaculis purus elit, ac malesuada quam malesuada in. Duis sed orci \
452
- eros. Suspendisse sit amet magna mollis, mollis nunc luctus, imperdiet mi. Integer fringilla non \
453
- sem ut lacinia. Fusce varius tortor a risus porttitor hendrerit. Morbi mauris dui, ultricies nec \
454
- tempus vel, gravida nec quam.";
455
-
456
- // check the panic includes the prefix of the sliced string
457
- #[ test]
458
- #[ should_panic( expected="byte index 1024 is out of bounds of `Lorem ipsum dolor sit amet" ) ]
459
- fn test_slice_fail_truncated_1 ( ) {
460
- & LOREM_PARAGRAPH [ ..1024 ] ;
461
- }
462
- // check the truncation in the panic message
463
- #[ test]
464
- #[ should_panic( expected="luctus, im`[...]" ) ]
465
- fn test_slice_fail_truncated_2 ( ) {
466
- & LOREM_PARAGRAPH [ ..1024 ] ;
467
- }
468
-
469
- #[ test]
470
- #[ should_panic( expected="byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of" ) ]
471
- fn test_slice_fail_boundary_1 ( ) {
472
- & "abcαβγ" [ 4 ..] ;
473
- }
474
-
475
- #[ test]
476
- #[ should_panic( expected="byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of" ) ]
477
- fn test_slice_fail_boundary_2 ( ) {
478
- & "abcαβγ" [ 2 ..6 ] ;
479
- }
480
-
481
- #[ test]
482
- fn test_slice_from ( ) {
483
- assert_eq ! ( & "abcd" [ 0 ..] , "abcd" ) ;
484
- assert_eq ! ( & "abcd" [ 2 ..] , "cd" ) ;
485
- assert_eq ! ( & "abcd" [ 4 ..] , "" ) ;
486
- }
487
- #[ test]
488
- fn test_slice_to ( ) {
489
- assert_eq ! ( & "abcd" [ ..0 ] , "" ) ;
490
- assert_eq ! ( & "abcd" [ ..2 ] , "ab" ) ;
491
- assert_eq ! ( & "abcd" [ ..4 ] , "abcd" ) ;
492
- }
493
496
494
497
#[ test]
495
498
fn test_trim_left_matches ( ) {
0 commit comments