Skip to content

Commit

Permalink
Merge rust-lang#21
Browse files Browse the repository at this point in the history
21: Add a new test for a bad size hint r=bjorn3 a=Dr-Emann

Changes in PR rust-lang#20 allow for an incorrect size hint to create a non-canonical SmolStr. Add a new test which will fail if we ever rely on
SmolStrs to be canonical when comparing for equality.

Co-authored-by: Zachary Dremann <dremann@gmail.com>
  • Loading branch information
bors[bot] and Dr-Emann authored Mar 11, 2022
2 parents 0c2244e + e197fd1 commit 5a828d4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,40 @@ fn test_from_char_iterator() {
assert_eq!(s.as_str(), *raw);
assert_eq!(s.is_heap_allocated(), *is_heap);
}
// String which has too many characters to even consider inlining: Chars::size_hint uses
// (`len` + 3) / 4. With `len` = 89, this results in 23, so `from_iter` will immediately
// heap allocate
let raw: String = std::iter::repeat('a').take(22 * 4 + 1).collect();
let s: SmolStr = raw.chars().collect();
assert_eq!(s.as_str(), raw);
assert!(s.is_heap_allocated());
}

#[test]
fn test_bad_size_hint_char_iter() {
struct BadSizeHint<I>(I);

impl<T, I: Iterator<Item = T>> Iterator for BadSizeHint<I> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
(1024, None)
}
}

let data = "testing";
let collected: SmolStr = BadSizeHint(data.chars()).collect();
let new = SmolStr::new(data);

// Because of the bad size hint, `collected` will be heap allocated, but `new` will be inline

// If we try to use the type of the string (inline/heap) to quickly test for equality, we need to ensure
// `collected` is inline allocated instead
assert!(collected.is_heap_allocated());
assert!(!new.is_heap_allocated());
assert_eq!(new, collected);
}

0 comments on commit 5a828d4

Please sign in to comment.