@@ -479,3 +479,106 @@ mod tests {
479
479
assert_eq ! ( c_str. as_str( ) , None ) ;
480
480
}
481
481
}
482
+
483
+ #[ cfg( test) ]
484
+ mod bench {
485
+ use iter:: range;
486
+ use libc;
487
+ use option:: Some ;
488
+ use ptr;
489
+ use extra:: test:: BenchHarness ;
490
+
491
+ #[ inline]
492
+ fn check ( s : & str , c_str : * libc:: c_char ) {
493
+ do s. as_imm_buf |s_buf, s_len| {
494
+ for i in range ( 0 , s_len) {
495
+ unsafe {
496
+ assert_eq ! (
497
+ * ptr:: offset( s_buf, i as int) as libc:: c_char,
498
+ * ptr:: offset( c_str, i as int) ) ;
499
+ }
500
+ }
501
+ }
502
+ }
503
+
504
+ static s_short: & ' static str = "Mary" ;
505
+ static s_medium: & ' static str = "Mary had a little lamb" ;
506
+ static s_long: & ' static str = "\
507
+ Mary had a little lamb, Little lamb
508
+ Mary had a little lamb, Little lamb
509
+ Mary had a little lamb, Little lamb
510
+ Mary had a little lamb, Little lamb
511
+ Mary had a little lamb, Little lamb
512
+ Mary had a little lamb, Little lamb" ;
513
+
514
+ fn bench_to_str ( bh : & mut BenchHarness , s : & str ) {
515
+ do bh. iter {
516
+ let c_str = s. to_c_str ( ) ;
517
+ do c_str. with_ref |c_str_buf| {
518
+ check ( s, c_str_buf)
519
+ }
520
+ }
521
+ }
522
+
523
+ #[ bench]
524
+ fn bench_to_c_str_short ( bh : & mut BenchHarness ) {
525
+ bench_to_str ( bh, s_short)
526
+ }
527
+
528
+ #[ bench]
529
+ fn bench_to_c_str_medium ( bh : & mut BenchHarness ) {
530
+ bench_to_str ( bh, s_medium)
531
+ }
532
+
533
+ #[ bench]
534
+ fn bench_to_c_str_long ( bh : & mut BenchHarness ) {
535
+ bench_to_str ( bh, s_long)
536
+ }
537
+
538
+ fn bench_to_c_str_unchecked ( bh : & mut BenchHarness , s : & str ) {
539
+ do bh. iter {
540
+ let c_str = unsafe { s. to_c_str_unchecked ( ) } ;
541
+ do c_str. with_ref |c_str_buf| {
542
+ check ( s, c_str_buf)
543
+ }
544
+ }
545
+ }
546
+
547
+ #[ bench]
548
+ fn bench_to_c_str_unchecked_short ( bh : & mut BenchHarness ) {
549
+ bench_to_c_str_unchecked ( bh, s_short)
550
+ }
551
+
552
+ #[ bench]
553
+ fn bench_to_c_str_unchecked_medium ( bh : & mut BenchHarness ) {
554
+ bench_to_c_str_unchecked ( bh, s_medium)
555
+ }
556
+
557
+ #[ bench]
558
+ fn bench_to_c_str_unchecked_long ( bh : & mut BenchHarness ) {
559
+ bench_to_c_str_unchecked ( bh, s_long)
560
+ }
561
+
562
+ fn bench_with_c_str ( bh : & mut BenchHarness , s : & str ) {
563
+ do bh. iter {
564
+ do s. with_c_str |c_str_buf| {
565
+ check ( s, c_str_buf)
566
+ }
567
+ }
568
+ }
569
+
570
+ #[ bench]
571
+ fn bench_with_c_str_short ( bh : & mut BenchHarness ) {
572
+ bench_with_c_str ( bh, s_short)
573
+ }
574
+
575
+ #[ bench]
576
+ fn bench_with_c_str_medium ( bh : & mut BenchHarness ) {
577
+ bench_with_c_str ( bh, s_medium)
578
+ }
579
+
580
+ #[ bench]
581
+ fn bench_with_c_str_long ( bh : & mut BenchHarness ) {
582
+ bench_with_c_str ( bh, s_long)
583
+ }
584
+ }
0 commit comments