about summary refs log tree commit diff
diff options
context:
space:
mode:
authorroot <root@localhost>2014-07-17 18:03:34 +0200
committerroot <root@localhost>2014-07-17 20:21:53 +0200
commitd6b42c2463ddfc7d16b9199f7725e704c94c2acf (patch)
tree0ef58d9bca37ebe1cc20c72f7edc93554a860098
parent06c7ee9c56f7c768be94c89f699527b44be664ab (diff)
str: Add better tests for string slice's Chars iterator
Test using for ch s.chars() { black_box(ch) } to have a test that should
force the iterator to run its full decoding computations.
-rw-r--r--src/libcollections/str.rs29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs
index 85921f1176a..f1bb3b40804 100644
--- a/src/libcollections/str.rs
+++ b/src/libcollections/str.rs
@@ -2240,16 +2240,26 @@ mod tests {
 #[cfg(test)]
 mod bench {
     use test::Bencher;
+    use test::black_box;
     use super::*;
+    use std::option::{None, Some};
     use std::iter::{Iterator, DoubleEndedIterator};
     use std::collections::Collection;
 
     #[bench]
     fn char_iterator(b: &mut Bencher) {
         let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
-        let len = s.char_len();
 
-        b.iter(|| assert_eq!(s.chars().count(), len));
+        b.iter(|| s.chars().count());
+    }
+
+    #[bench]
+    fn char_iterator_for(b: &mut Bencher) {
+        let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
+
+        b.iter(|| {
+            for ch in s.chars() { black_box(ch) }
+        });
     }
 
     #[bench]
@@ -2260,17 +2270,24 @@ mod bench {
         Mary had a little lamb, Little lamb
         Mary had a little lamb, Little lamb
         Mary had a little lamb, Little lamb";
-        let len = s.char_len();
 
-        b.iter(|| assert_eq!(s.chars().count(), len));
+        b.iter(|| s.chars().count());
     }
 
     #[bench]
     fn char_iterator_rev(b: &mut Bencher) {
         let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
-        let len = s.char_len();
 
-        b.iter(|| assert_eq!(s.chars().rev().count(), len));
+        b.iter(|| s.chars().rev().count());
+    }
+
+    #[bench]
+    fn char_iterator_rev_for(b: &mut Bencher) {
+        let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
+
+        b.iter(|| {
+            for ch in s.chars().rev() { black_box(ch) }
+        });
     }
 
     #[bench]