about summary refs log tree commit diff
path: root/library/alloc/tests
diff options
context:
space:
mode:
authorThom Chiovoloni <chiovolonit@gmail.com>2021-10-30 03:47:47 -0700
committerThom Chiovoloni <chiovolonit@gmail.com>2022-02-05 11:15:17 -0800
commit628b2173265e19cb5d22b86349618b0ab88cf5c2 (patch)
tree6d8f680d120bd2087bd9531ee691193532e4c03f /library/alloc/tests
parent71226d717a1fb57122e47e63b97295e703319cb0 (diff)
downloadrust-628b2173265e19cb5d22b86349618b0ab88cf5c2.tar.gz
rust-628b2173265e19cb5d22b86349618b0ab88cf5c2.zip
Optimize `core::str::Chars::count`
Diffstat (limited to 'library/alloc/tests')
-rw-r--r--library/alloc/tests/str.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs
index e92881b1049..3dcbc54be4e 100644
--- a/library/alloc/tests/str.rs
+++ b/library/alloc/tests/str.rs
@@ -2230,3 +2230,43 @@ fn utf8_chars() {
     assert!((!from_utf8(&[0xf0, 0xff, 0x10]).is_ok()));
     assert!((!from_utf8(&[0xf0, 0xff, 0xff, 0x10]).is_ok()));
 }
+
+#[test]
+fn utf8_char_counts() {
+    let strs = [("e", 1), ("é", 1), ("€", 1), ("\u{10000}", 1), ("eé€\u{10000}", 4)];
+    let mut reps = vec![1, 8, 64, 256, 512, 1024];
+    if cfg!(not(miri)) {
+        reps.push(1 << 16);
+    }
+    let counts = if cfg!(miri) { 0..1 } else { 0..8 };
+    let padding = counts.map(|len| " ".repeat(len)).collect::<Vec<String>>();
+
+    for repeat in reps {
+        for (tmpl_str, tmpl_char_count) in strs {
+            for pad_start in &padding {
+                for pad_end in &padding {
+                    // Create a string with padding...
+                    let with_padding =
+                        format!("{}{}{}", pad_start, tmpl_str.repeat(repeat), pad_end);
+                    // ...and then skip past that padding. This should ensure
+                    // that we test several different alignments for both head
+                    // and tail.
+                    let si = pad_start.len();
+                    let ei = with_padding.len() - pad_end.len();
+                    let target = &with_padding[si..ei];
+
+                    assert!(!target.starts_with(" ") && !target.ends_with(" "));
+                    let expected_count = tmpl_char_count * repeat;
+                    assert_eq!(
+                        expected_count,
+                        target.chars().count(),
+                        "wrong count for `{:?}.repeat({})` (padding: `{:?}`)",
+                        tmpl_str,
+                        repeat,
+                        (pad_start.len(), pad_end.len()),
+                    );
+                }
+            }
+        }
+    }
+}