From c5a1d8c3db171a4351712c04e6ba6a4e4636a332 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Sun, 2 Aug 2015 19:03:01 +0200 Subject: StrSearcher: Add tests for rfind(&str) Add tests for .rfind(&str), using the reverse searcher case for substring search. --- src/libcollectionstest/str.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcollectionstest/str.rs b/src/libcollectionstest/str.rs index 4cccb29b41c..ac9c7908ab8 100644 --- a/src/libcollectionstest/str.rs +++ b/src/libcollectionstest/str.rs @@ -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] -- cgit 1.4.1-3-g733a5