1 use super::*; 2 use crate::std::sys_common::{AsInner, IntoInner}; 3 4 use crate::std::rc::Rc; 5 use crate::std::sync::Arc; 6 7 #[test] 8 fn test_os_string_with_capacity() { 9 let os_string = OsString::with_capacity(0); 10 assert_eq!(0, os_string.inner.into_inner().capacity()); 11 12 let os_string = OsString::with_capacity(10); 13 assert_eq!(10, os_string.inner.into_inner().capacity()); 14 15 let mut os_string = OsString::with_capacity(0); 16 os_string.push("abc"); 17 assert!(os_string.inner.into_inner().capacity() >= 3); 18 } 19 20 #[test] 21 fn test_os_string_clear() { 22 let mut os_string = OsString::from("abc"); 23 assert_eq!(3, os_string.inner.as_inner().len()); 24 25 os_string.clear(); 26 assert_eq!(&os_string, ""); 27 assert_eq!(0, os_string.inner.as_inner().len()); 28 } 29 30 #[test] 31 fn test_os_string_capacity() { 32 let os_string = OsString::with_capacity(0); 33 assert_eq!(0, os_string.capacity()); 34 35 let os_string = OsString::with_capacity(10); 36 assert_eq!(10, os_string.capacity()); 37 38 let mut os_string = OsString::with_capacity(0); 39 os_string.push("abc"); 40 assert!(os_string.capacity() >= 3); 41 } 42 43 #[test] 44 fn test_os_string_reserve() { 45 let mut os_string = OsString::new(); 46 assert_eq!(os_string.capacity(), 0); 47 48 os_string.reserve(2); 49 assert!(os_string.capacity() >= 2); 50 51 for _ in 0..16 { 52 os_string.push("a"); 53 } 54 55 assert!(os_string.capacity() >= 16); 56 os_string.reserve(16); 57 assert!(os_string.capacity() >= 32); 58 59 os_string.push("a"); 60 61 os_string.reserve(16); 62 assert!(os_string.capacity() >= 33) 63 } 64 65 #[test] 66 fn test_os_string_reserve_exact() { 67 let mut os_string = OsString::new(); 68 assert_eq!(os_string.capacity(), 0); 69 70 os_string.reserve_exact(2); 71 assert!(os_string.capacity() >= 2); 72 73 for _ in 0..16 { 74 os_string.push("a"); 75 } 76 77 assert!(os_string.capacity() >= 16); 78 os_string.reserve_exact(16); 79 assert!(os_string.capacity() >= 32); 80 81 os_string.push("a"); 82 83 os_string.reserve_exact(16); 84 assert!(os_string.capacity() >= 33) 85 } 86 87 #[test] 88 fn test_os_string_join() { 89 let strings = [OsStr::new("hello"), OsStr::new("dear"), OsStr::new("world")]; 90 assert_eq!("hello", strings[..1].join(OsStr::new(" "))); 91 assert_eq!("hello dear world", strings.join(OsStr::new(" "))); 92 assert_eq!("hellodearworld", strings.join(OsStr::new(""))); 93 assert_eq!("hello.\n dear.\n world", strings.join(OsStr::new(".\n "))); 94 95 assert_eq!("dear world", strings[1..].join(&OsString::from(" "))); 96 97 let strings_abc = [ 98 OsString::from("a"), 99 OsString::from("b"), 100 OsString::from("c"), 101 ]; 102 assert_eq!("a b c", strings_abc.join(OsStr::new(" "))); 103 } 104 105 #[test] 106 fn test_os_string_default() { 107 let os_string: OsString = Default::default(); 108 assert_eq!("", &os_string); 109 } 110 111 #[test] 112 fn test_os_str_is_empty() { 113 let mut os_string = OsString::new(); 114 assert!(os_string.is_empty()); 115 116 os_string.push("abc"); 117 assert!(!os_string.is_empty()); 118 119 os_string.clear(); 120 assert!(os_string.is_empty()); 121 } 122 123 #[test] 124 fn test_os_str_len() { 125 let mut os_string = OsString::new(); 126 assert_eq!(0, os_string.len()); 127 128 os_string.push("abc"); 129 assert_eq!(3, os_string.len()); 130 131 os_string.clear(); 132 assert_eq!(0, os_string.len()); 133 } 134 135 #[test] 136 fn test_os_str_default() { 137 let os_str: &OsStr = Default::default(); 138 assert_eq!("", os_str); 139 } 140 141 #[test] 142 fn into_boxed() { 143 let orig = "Hello, world!"; 144 let os_str = OsStr::new(orig); 145 let boxed: Box<OsStr> = Box::from(os_str); 146 let os_string = os_str.to_owned().into_boxed_os_str().into_os_string(); 147 assert_eq!(os_str, &*boxed); 148 assert_eq!(&*boxed, &*os_string); 149 assert_eq!(&*os_string, os_str); 150 } 151 152 #[test] 153 fn boxed_default() { 154 let boxed = <Box<OsStr>>::default(); 155 assert!(boxed.is_empty()); 156 } 157 158 #[test] 159 fn test_os_str_clone_into() { 160 let mut os_string = OsString::with_capacity(123); 161 os_string.push("hello"); 162 let os_str = OsStr::new("bonjour"); 163 os_str.clone_into(&mut os_string); 164 assert_eq!(os_str, os_string); 165 assert!(os_string.capacity() >= 123); 166 } 167 168 #[test] 169 fn into_rc() { 170 let orig = "Hello, world!"; 171 let os_str = OsStr::new(orig); 172 let rc: Rc<OsStr> = Rc::from(os_str); 173 let arc: Arc<OsStr> = Arc::from(os_str); 174 175 assert_eq!(&*rc, os_str); 176 assert_eq!(&*arc, os_str); 177 178 let rc2: Rc<OsStr> = Rc::from(os_str.to_owned()); 179 let arc2: Arc<OsStr> = Arc::from(os_str.to_owned()); 180 181 assert_eq!(&*rc2, os_str); 182 assert_eq!(&*arc2, os_str); 183 } 184