]> git.lizzy.rs Git - rust.git/commitdiff
StrSearcher: Add tests for rfind(&str)
authorUlrik Sverdrup <bluss@users.noreply.github.com>
Sun, 2 Aug 2015 17:03:01 +0000 (19:03 +0200)
committerUlrik Sverdrup <bluss@users.noreply.github.com>
Sun, 2 Aug 2015 18:08:35 +0000 (20:08 +0200)
Add tests for .rfind(&str), using the reverse searcher case for
substring search.

src/libcollectionstest/str.rs

index 4cccb29b41cdd31a90b99ad4071705c8fba9bbc6..ac9c7908ab8f9e0e767e6491e2f7fe2eefc07a68 100644 (file)
@@ -115,6 +115,26 @@ fn test_find_str() {
     assert_eq!(data[43..86].find("ย中"), Some(67 - 43));
     assert_eq!(data[43..86].find("iệt"), Some(77 - 43));
     assert_eq!(data[43..86].find("Nam"), Some(83 - 43));
+
+    // find every substring -- assert that it finds it, or an earlier occurence.
+    let string = "Việt Namacbaabcaabaaba";
+    for (i, ci) in string.char_indices() {
+        let ip = i + ci.len_utf8();
+        for j in string[ip..].char_indices()
+                             .map(|(i, _)| i)
+                             .chain(Some(string.len() - ip))
+        {
+            let pat = &string[i..ip + j];
+            assert!(match string.find(pat) {
+                None => false,
+                Some(x) => x <= i,
+            });
+            assert!(match string.rfind(pat) {
+                None => false,
+                Some(x) => x >= i,
+            });
+        }
+    }
 }
 
 #[test]