Skip to content

Commit

Permalink
Add more tests for str Chars iterator
Browse files Browse the repository at this point in the history
Test iterating (decoding) every codepoint.
  • Loading branch information
root committed Jul 17, 2014
1 parent 42357d7 commit 9e59c76
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ impl OwnedStr for String {
#[cfg(test)]
mod tests {
use std::iter::AdditiveIterator;
use std::iter::range;
use std::default::Default;
use std::char::Char;
use std::clone::Clone;
Expand Down Expand Up @@ -1610,6 +1611,30 @@ mod tests {
assert_eq!(pos, v.len());
}

#[test]
fn test_chars_decoding() {
let mut bytes = [0u8, ..4];
for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) {
let len = c.encode_utf8(bytes);
let s = ::core::str::from_utf8(bytes.slice_to(len)).unwrap();
if Some(c) != s.chars().next() {
fail!("character {:x}={} does not decode correctly", c as u32, c);
}
}
}

#[test]
fn test_chars_rev_decoding() {
let mut bytes = [0u8, ..4];
for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) {
let len = c.encode_utf8(bytes);
let s = ::core::str::from_utf8(bytes.slice_to(len)).unwrap();
if Some(c) != s.chars().rev().next() {
fail!("character {:x}={} does not decode correctly", c as u32, c);
}
}
}

#[test]
fn test_iterator_clone() {
let s = "ศไทย中华Việt Nam";
Expand Down

0 comments on commit 9e59c76

Please sign in to comment.