From 50fc2b44be6f9288cb3a6077cf476aed36b7f168 Mon Sep 17 00:00:00 2001 From: polazarus Date: Fri, 11 Aug 2023 20:42:38 +0200 Subject: [PATCH] add tests for from_utf16* --- src/string.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/string.rs b/src/string.rs index c05caf8..d7b8a13 100644 --- a/src/string.rs +++ b/src/string.rs @@ -425,13 +425,14 @@ where /// Basic usage: /// /// ``` + /// # use hipstr::HipStr; /// // š¯„˛music /// 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] @@ -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}"); + } }