use std::cmp::{PartialEq, Eq, PartialOrd, Ord}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::fmt::{Debug, Display}; use std::borrow::Borrow; use std::mem::size_of; use std::ops::Deref; use std::sync::Arc; use std::rc::Rc; use our_string::{OurString, Comrade}; fn hash(t: &T) -> u64 { let mut s = DefaultHasher::new(); t.hash(&mut s); s.finish() } fn is_inline(v: &OurString) -> bool { let l = v.len(); let s = v.as_str() as *const str as *const () as usize; let v = v as *const OurString as *const () as usize; s >= v && s + l <= v + size_of::>() } #[test] fn test_sizes() { assert_eq!(size_of::>, { size_of::() - 1 }>>(), size_of::()); assert_eq!(size_of::>, { size_of::() - 1 - size_of::() }>>(), size_of::() - size_of::()); assert_eq!(size_of::>, { size_of::() - 1 }>>(), size_of::()); assert_eq!(size_of::>, { size_of::() - 1 - size_of::() }>>(), size_of::() - size_of::()); assert_eq!(size_of::, { size_of::() - 1 }>>(), size_of::()); assert_eq!(size_of::, { size_of::() - 1 }>>(), size_of::()); } #[test] fn test_traits() { macro_rules! assert_impl { ($t:ty : $($tr:tt)*) => {{ fn checker() {} checker::<$t>(); }}; } assert_impl!(OurString>, 8> : Hash + Clone + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Default + AsRef + Borrow + Deref + for<'a> From<&'a str>); assert_impl!(OurString, 8> : Hash + Clone + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Default + AsRef + Borrow + Deref + for<'a> From<&'a str>); assert_impl!(OurString>, 8> : Send + Sync + Hash + Clone + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Default + AsRef + Borrow + Deref + for<'a> From<&'a str>); assert_impl!(OurString, 8> : Send + Sync + Hash + Clone + Debug + Display + PartialEq + Eq + PartialOrd + Ord + Default + AsRef + Borrow + Deref + for<'a> From<&'a str>); } #[test] fn test_clone() { let a = OurString::>, 5>::from("hello world again"); let b = a.clone(); assert_eq!(a, b); assert_eq!(a.as_str(), b.as_str()); assert_eq!(a.as_str() as *const str, b.as_str() as *const str); } #[cfg(not(miri))] proptest::proptest! { #[test] fn proptest_clone(s: String) { let a = OurString::>, 5>::from(s.as_str()); let b = a.clone(); assert_eq!(a, b); assert_eq!(a.as_str(), b.as_str()); if a.len() > 5 { assert_eq!(a.as_str() as *const str, b.as_str() as *const str); } } } #[test] fn test_new_default() { const X: OurString>, 10> = OurString::new(); const Y: OurString, 10> = OurString::new(); assert_eq!(X.len(), 0); assert_eq!(X.is_empty(), true); assert_eq!(X.as_str().is_empty(), true); assert_eq!(Y.len(), 0); assert_eq!(Y.is_empty(), true); assert_eq!(Y.as_str().is_empty(), true); assert_eq!(OurString::>, 10>::default().len(), 0); assert_eq!(OurString::>, 10>::default().is_empty(), true); assert_eq!(OurString::>, 10>::default().as_str().is_empty(), true); assert_eq!(OurString::, 10>::default().len(), 0); assert_eq!(OurString::, 10>::default().is_empty(), true); assert_eq!(OurString::, 10>::default().as_str().is_empty(), true); } #[test] fn test_from_slice_inlining() { assert_eq!(is_inline(&OurString::>, 10>::from("")), true); assert_eq!(is_inline(&OurString::>, 10>::from("h")), true); assert_eq!(is_inline(&OurString::>, 10>::from("he")), true); assert_eq!(is_inline(&OurString::>, 10>::from("hel")), true); assert_eq!(is_inline(&OurString::>, 10>::from("hell")), true); assert_eq!(is_inline(&OurString::>, 10>::from("hello")), true); assert_eq!(is_inline(&OurString::>, 10>::from("hello ")), true); assert_eq!(is_inline(&OurString::>, 10>::from("hello from")), true); assert_eq!(is_inline(&OurString::>, 10>::from("hello from ")), false); assert_eq!(is_inline(&OurString::>, 10>::from("hello from the")), false); assert_eq!(is_inline(&OurString::>, 10>::from("hello from the other")), false); assert_eq!(is_inline(&OurString::>, 4>::from("")), true); assert_eq!(is_inline(&OurString::>, 4>::from("h")), true); assert_eq!(is_inline(&OurString::>, 4>::from("he")), true); assert_eq!(is_inline(&OurString::>, 4>::from("hel")), true); assert_eq!(is_inline(&OurString::>, 4>::from("hell")), true); assert_eq!(is_inline(&OurString::>, 4>::from("hello")), false); assert_eq!(is_inline(&OurString::>, 4>::from("hello ")), false); assert_eq!(is_inline(&OurString::>, 4>::from("hello from")), false); assert_eq!(is_inline(&OurString::>, 4>::from("hello from ")), false); assert_eq!(is_inline(&OurString::>, 4>::from("hello from the")), false); assert_eq!(is_inline(&OurString::>, 4>::from("hello from the other")), false); } #[test] fn test_from_slice() { assert_eq!(&*OurString::>, 10>::from("") as &str, ""); assert_eq!(&*OurString::>, 10>::from("h") as &str, "h"); assert_eq!(&*OurString::>, 10>::from("he") as &str, "he"); assert_eq!(&*OurString::>, 10>::from("hel") as &str, "hel"); assert_eq!(&*OurString::>, 10>::from("hell") as &str, "hell"); assert_eq!(&*OurString::>, 10>::from("hello") as &str, "hello"); assert_eq!(&*OurString::>, 10>::from("hello ") as &str, "hello "); assert_eq!(&*OurString::>, 10>::from("hello from") as &str, "hello from"); assert_eq!(&*OurString::>, 10>::from("hello from ") as &str, "hello from "); assert_eq!(&*OurString::>, 10>::from("hello from the") as &str, "hello from the"); assert_eq!(&*OurString::>, 10>::from("hello from the other") as &str, "hello from the other"); assert_eq!(&*OurString::>, 4>::from("") as &str, ""); assert_eq!(&*OurString::>, 4>::from("h") as &str, "h"); assert_eq!(&*OurString::>, 4>::from("he") as &str, "he"); assert_eq!(&*OurString::>, 4>::from("hel") as &str, "hel"); assert_eq!(&*OurString::>, 4>::from("hell") as &str, "hell"); assert_eq!(&*OurString::>, 4>::from("hello") as &str, "hello"); assert_eq!(&*OurString::>, 4>::from("hello ") as &str, "hello "); assert_eq!(&*OurString::>, 4>::from("hello from") as &str, "hello from"); assert_eq!(&*OurString::>, 4>::from("hello from ") as &str, "hello from "); assert_eq!(&*OurString::>, 4>::from("hello from the") as &str, "hello from the"); assert_eq!(&*OurString::>, 4>::from("hello from the other") as &str, "hello from the other"); } #[cfg(not(miri))] proptest::proptest! { #[test] fn proptest_from_slice(s: String) { assert_eq!(&*OurString::>, 10>::from(s.as_str()) as &str, s.as_str()); assert_eq!(&*OurString::>, 10>::from(s.as_str()) as &str, s.as_str()); assert_eq!(&*OurString::, 10>::from(s.as_str()) as &str, s.as_str()); assert_eq!(&*OurString::, 10>::from(s.as_str()) as &str, s.as_str()); assert_eq!(OurString::>, 10>::from(s.as_str()).as_str() as &str, s.as_str()); assert_eq!(OurString::>, 10>::from(s.as_str()).as_str() as &str, s.as_str()); assert_eq!(OurString::, 10>::from(s.as_str()).as_str() as &str, s.as_str()); assert_eq!(OurString::, 10>::from(s.as_str()).as_str() as &str, s.as_str()); assert_eq!(OurString::>, 10>::from(s.as_str()).as_ref() as &str, s.as_str()); assert_eq!(OurString::>, 10>::from(s.as_str()).as_ref() as &str, s.as_str()); assert_eq!(OurString::, 10>::from(s.as_str()).as_ref() as &str, s.as_str()); assert_eq!(OurString::, 10>::from(s.as_str()).as_ref() as &str, s.as_str()); assert_eq!(<_ as AsRef>::as_ref(&OurString::>, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(<_ as AsRef>::as_ref(&OurString::>, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(<_ as AsRef>::as_ref(&OurString::, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(<_ as AsRef>::as_ref(&OurString::, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(OurString::>, 10>::from(s.as_str()).borrow() as &str, s.as_str()); assert_eq!(OurString::>, 10>::from(s.as_str()).borrow() as &str, s.as_str()); assert_eq!(OurString::, 10>::from(s.as_str()).borrow() as &str, s.as_str()); assert_eq!(OurString::, 10>::from(s.as_str()).borrow() as &str, s.as_str()); assert_eq!(<_ as Borrow>::borrow(&OurString::>, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(<_ as Borrow>::borrow(&OurString::>, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(<_ as Borrow>::borrow(&OurString::, 10>::from(s.as_str())) as &str, s.as_str()); assert_eq!(<_ as Borrow>::borrow(&OurString::, 10>::from(s.as_str())) as &str, s.as_str()); } } #[test] fn test_hash() { assert_eq!(hash(&OurString::>, 4>::from("")), hash(&"")); assert_eq!(hash(&OurString::>, 4>::from("x")), hash(&"x")); assert_eq!(hash(&OurString::>, 4>::from("xy")), hash(&"xy")); assert_eq!(hash(&OurString::>, 4>::from("hello world")), hash(&"hello world")); } #[cfg(not(miri))] proptest::proptest! { #[test] fn proptest_hash(s: String) { assert_eq!(hash(&OurString::>, 4>::from(s.as_str())), hash(&s.as_str())); assert_eq!(hash(&OurString::, 4>::from(s.as_str())), hash(&s.as_str())); assert_eq!(hash(&OurString::>, 4>::from(s.as_str())), hash(&s.as_str())); assert_eq!(hash(&OurString::, 4>::from(s.as_str())), hash(&s.as_str())); } } #[test] fn test_from_comrade_inlining() { assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("h")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("he")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hel")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hell")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello ")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from ")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from the")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from the other")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("h")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("he")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hel")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hell")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello ")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from ")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from the")).into()).unwrap()), false); assert_eq!(is_inline(&OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from the other")).into()).unwrap()), false); } #[test] fn test_from_comrade() { assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("")).into()).unwrap() as &str, ""); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("h")).into()).unwrap() as &str, "h"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("he")).into()).unwrap() as &str, "he"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hel")).into()).unwrap() as &str, "hel"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hell")).into()).unwrap() as &str, "hell"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello")).into()).unwrap() as &str, "hello"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello ")).into()).unwrap() as &str, "hello "); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from")).into()).unwrap() as &str, "hello from"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from ")).into()).unwrap() as &str, "hello from "); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from the")).into()).unwrap() as &str, "hello from the"); assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(Vec::::from("hello from the other")).into()).unwrap() as &str, "hello from the other"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("")).into()).unwrap() as &str, ""); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("h")).into()).unwrap() as &str, "h"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("he")).into()).unwrap() as &str, "he"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hel")).into()).unwrap() as &str, "hel"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hell")).into()).unwrap() as &str, "hell"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello")).into()).unwrap() as &str, "hello"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello ")).into()).unwrap() as &str, "hello "); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from")).into()).unwrap() as &str, "hello from"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from ")).into()).unwrap() as &str, "hello from "); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from the")).into()).unwrap() as &str, "hello from the"); assert_eq!(&*OurString::>, 4>::from_utf8(Arc::new(Vec::::from("hello from the other")).into()).unwrap() as &str, "hello from the other"); } #[cfg(not(miri))] proptest::proptest! { #[test] fn proptest_from_comrade(s: String) { assert_eq!(&*OurString::>, 10>::from_utf8(Rc::new(s.clone().into_bytes()).into()).unwrap() as &str, s.as_str()); assert_eq!(&*OurString::>, 10>::from_utf8(Arc::new(s.clone().into_bytes()).into()).unwrap() as &str, s.as_str()); assert_eq!(&*OurString::, 10>::from_utf8(Rc::<[u8]>::from(s.as_bytes()).into()).unwrap() as &str, s.as_str()); assert_eq!(&*OurString::, 10>::from_utf8(Arc::<[u8]>::from(s.as_bytes()).into()).unwrap() as &str, s.as_str()); } } #[test] fn test_debug_display() { assert_eq!(format!("{:?}", OurString::, 4>::from("wh?")), format!("{:?}", "wh?")); assert_eq!(format!("{:?}", OurString::>, 4>::from("wha?")), format!("{:?}", "wha?")); assert_eq!(format!("{:?}", OurString::>, 4>::from("come again?")), format!("{:?}", "come again?")); assert_eq!(format!("{}", OurString::, 4>::from("wh?")), format!("{}", "wh?")); assert_eq!(format!("{}", OurString::>, 4>::from("wha?")), format!("{}", "wha?")); assert_eq!(format!("{}", OurString::>, 4>::from("come again?")), format!("{}", "come again?")); } #[cfg(not(miri))] proptest::proptest! { #[test] fn proptest_debug_display(s: String) { assert_eq!(format!("{:?}", OurString::, 4>::from_utf8(Rc::<[u8]>::from(s.as_bytes()).into()).unwrap()), format!("{s:?}")); assert_eq!(format!("{:?}", OurString::, 4>::from_utf8(Arc::<[u8]>::from(s.as_bytes()).into()).unwrap()), format!("{s:?}")); assert_eq!(format!("{}", OurString::, 4>::from_utf8(Rc::<[u8]>::from(s.as_bytes()).into()).unwrap()), format!("{s}")); assert_eq!(format!("{}", OurString::, 4>::from_utf8(Arc::<[u8]>::from(s.as_bytes()).into()).unwrap()), format!("{s}")); } } #[test] fn test_comparison() { let a = "help me"; let a1 = OurString::>, 10>::from(a); let a2 = OurString::>, 3>::from(a); let a3 = String::from(a); let b = "please"; let b1 = OurString::, 7>::from(b); let b2 = OurString::, 2>::from(b); let b3 = String::from(b); assert_eq!(a1, a ); assert_eq!(a1, a1); assert_eq!(a1, a2); assert_eq!(a1, a3); assert_eq!(a2, a ); assert_eq!(a2, a1); assert_eq!(a2, a2); assert_eq!(a2, a3); assert_eq!(a, a1); assert_eq!(a, a2); assert_ne!(b1, a ); assert_ne!(b1, a1); assert_ne!(b1, a2); assert_ne!(b1, a3); assert_ne!(b2, a ); assert_ne!(b2, a1); assert_ne!(b2, a2); assert_ne!(b2, a3); assert_ne!(b, a1); assert_ne!(b, a2); assert_ne!(a1, b ); assert_ne!(a1, b1); assert_ne!(a1, b2); assert_ne!(a1, b3); assert_ne!(a2, b ); assert_ne!(a2, b1); assert_ne!(a2, b2); assert_ne!(a2, b3); assert_ne!(a, b1); assert_ne!(a, b2); assert_eq!(b1, b ); assert_eq!(b1, b1); assert_eq!(b1, b2); assert_eq!(b1, b3); assert_eq!(b2, b ); assert_eq!(b2, b1); assert_eq!(b2, b2); assert_eq!(b2, b3); assert_eq!(b, b1); assert_eq!(b, b2); assert_eq!(a1 <= a , true); assert_eq!(a1 <= a1, true); assert_eq!(a1 <= a2, true); assert_eq!(a1 <= a3, true); assert_eq!(a2 <= a , true); assert_eq!(a2 <= a1, true); assert_eq!(a2 <= a2, true); assert_eq!(a2 <= a3, true); assert_eq!(a <= a1, true); assert_eq!(a <= a2, true); assert_eq!(a1 >= a , true); assert_eq!(a1 >= a1, true); assert_eq!(a1 >= a2, true); assert_eq!(a1 >= a3, true); assert_eq!(a2 >= a , true); assert_eq!(a2 >= a1, true); assert_eq!(a2 >= a2, true); assert_eq!(a2 >= a3, true); assert_eq!(a >= a1, true); assert_eq!(a >= a2, true); assert_eq!(a1 < a , false); assert_eq!(a1 < a1, false); assert_eq!(a1 < a2, false); assert_eq!(a1 < a3, false); assert_eq!(a2 < a , false); assert_eq!(a2 < a1, false); assert_eq!(a2 < a2, false); assert_eq!(a2 < a3, false); assert_eq!(a < a1, false); assert_eq!(a < a2, false); assert_eq!(a1 > a , false); assert_eq!(a1 > a1, false); assert_eq!(a1 > a2, false); assert_eq!(a1 > a3, false); assert_eq!(a2 > a , false); assert_eq!(a2 > a1, false); assert_eq!(a2 > a2, false); assert_eq!(a2 > a3, false); assert_eq!(a > a1, false); assert_eq!(a > a2, false); assert_eq!(b1 <= a , false); assert_eq!(b1 <= a1, false); assert_eq!(b1 <= a2, false); assert_eq!(b1 <= a3, false); assert_eq!(b2 <= a , false); assert_eq!(b2 <= a1, false); assert_eq!(b2 <= a2, false); assert_eq!(b2 <= a3, false); assert_eq!(b <= a1, false); assert_eq!(b <= a2, false); assert_eq!(b1 >= a , true ); assert_eq!(b1 >= a1, true ); assert_eq!(b1 >= a2, true ); assert_eq!(b1 >= a3, true ); assert_eq!(b2 >= a , true ); assert_eq!(b2 >= a1, true ); assert_eq!(b2 >= a2, true ); assert_eq!(b2 >= a3, true ); assert_eq!(b >= a1, true ); assert_eq!(b >= a2, true ); assert_eq!(b1 < a , false); assert_eq!(b1 < a1, false); assert_eq!(b1 < a2, false); assert_eq!(b1 < a3, false); assert_eq!(b2 < a , false); assert_eq!(b2 < a1, false); assert_eq!(b2 < a2, false); assert_eq!(b2 < a3, false); assert_eq!(b < a1, false); assert_eq!(b < a2, false); assert_eq!(b1 > a , true ); assert_eq!(b1 > a1, true ); assert_eq!(b1 > a2, true ); assert_eq!(b1 > a3, true ); assert_eq!(b2 > a , true ); assert_eq!(b2 > a1, true ); assert_eq!(b2 > a2, true ); assert_eq!(b2 > a3, true ); assert_eq!(b > a1, true ); assert_eq!(b > a2, true ); } #[cfg(not(miri))] proptest::proptest! { #[test] fn proptest_comparison(a: String, b: String) { fn partial_cmp(a: &A, b: &B) -> Option where A: PartialOrd { a.partial_cmp(b) } fn cmp(a: &A, b: &A) -> core::cmp::Ordering where A: Ord { a.cmp(b) } assert_eq!(partial_cmp(&a.as_str(), &OurString::, 4>::from(b.as_str())), partial_cmp(&a, &b)); assert_eq!(partial_cmp(&OurString::, 4>::from(a.as_str()), &b), partial_cmp(&a, &b)); assert_eq!(partial_cmp(&OurString::, 4>::from(a.as_str()), &OurString::, 4>::from(b.as_str())), partial_cmp(&a, &b)); assert_eq!(partial_cmp(&OurString::, 4>::from(a.as_str()), &OurString::, 8>::from(b.as_str())), partial_cmp(&a, &b)); assert_eq!(partial_cmp(&OurString::, 8>::from(a.as_str()), &OurString::, 4>::from(b.as_str())), partial_cmp(&a, &b)); assert_eq!(cmp(&OurString::, 4>::from(a.as_str()), &OurString::, 4>::from(b.as_str())), cmp(&a, &b)); } } #[test] fn test_index() { let a = OurString::>, 8>::from("test message thing"); assert_eq!(a[..], a.as_str()[..]); assert_eq!(a[3..], a.as_str()[3..]); assert_eq!(a[..7], a.as_str()[..7]); assert_eq!(a[3..7], a.as_str()[3..7]); } #[test] fn test_as_bytes() { let a = OurString::>, 8>::from("test message thing"); assert_eq!(a.as_bytes() as &[u8], b"test message thing"); } #[test] fn test_convert() { let a = OurString::, 8>::from("hello"); assert_eq!(is_inline(&a), true); let b: OurString, 5> = a.convert(); assert_eq!(is_inline(&b), true); let c: OurString, 10> = b.convert(); assert_eq!(is_inline(&c), true); let d: OurString, 4> = c.convert(); assert_eq!(is_inline(&d), false); let e: OurString, 10> = d.clone().convert(); assert_eq!(is_inline(&e), false); assert_eq!(d.as_str().as_ptr(), e.as_str().as_ptr()); } #[test] fn test_utf8() { for value in ["".as_bytes(), b"h", b"he", b"fgf", b"trht", b"sdrgh", b"dfwrgr", b"dfgrtge", b"dfbetehr", b"rewgerghr", b"gerhdffgrr"] { let a = our_string::OurBytes::, 8>::from(value); assert_eq!(a, value); assert_eq!(&*a, value); let b = OurString::from_utf8(a.clone()).unwrap(); assert_eq!(b.as_bytes(), value); assert_eq!((&*b).as_bytes(), value); let c = b.clone().into_bytes(); assert_eq!(c, value); assert_eq!(&*c, value); assert_eq!(a.as_ptr() == b.as_ptr(), value.len() > 8); assert_eq!(a.as_ptr() == c.as_ptr(), value.len() > 8); } let x = our_string::OurBytes::, 8>::from([255, 127, 56].as_slice()); OurString::from_utf8(x).unwrap_err(); }