Skip to content

Commit

Permalink
add tests for from_utf16*
Browse files Browse the repository at this point in the history
  • Loading branch information
polazarus committed Aug 11, 2023
1 parent 89ae261 commit 50fc2b4
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,14 @@ where
/// Basic usage:
///
/// ```
/// # use hipstr::HipStr;
/// // 𝄞mus<invalid>ic<invalid>
/// let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
/// 0x0073, 0xDD1E, 0x0069, 0x0063,
/// 0xD834];
///
/// assert_eq!(String::from("𝄞mus\u{FFFD}ic\u{FFFD}"),
/// String::from_utf16_lossy(v));
/// assert_eq!("𝄞mus\u{FFFD}ic\u{FFFD}",
/// HipStr::from_utf16_lossy(v));
/// ```
#[inline]
#[must_use]
Expand Down Expand Up @@ -1546,4 +1547,20 @@ mod tests {
assert_ne!(a.as_ptr(), b.as_ptr(), "different backend vector");
}
}

#[test]
fn test_from_utf16() {
let v = [b'a' as u16].repeat(42);
assert_eq!(HipStr::from_utf16(&v[0..4]).unwrap(), "a".repeat(4));
assert_eq!(HipStr::from_utf16(&v).unwrap(), "a".repeat(42));
assert!(HipStr::from_utf16(&[0xD834]).is_err());
}

#[test]
fn test_from_utf16_lossy() {
let v = [b'a' as u16].repeat(42);
assert_eq!(HipStr::from_utf16_lossy(&v[0..4]), "a".repeat(4));
assert_eq!(HipStr::from_utf16_lossy(&v), "a".repeat(42));
assert_eq!(HipStr::from_utf16_lossy(&[0xD834]), "\u{FFFD}");
}
}

0 comments on commit 50fc2b4

Please sign in to comment.