]> git.lizzy.rs Git - rust.git/blob - library/std/src/ffi/os_str/tests.rs
Rollup merge of #96642 - thomcc:thinbox-zst-ugh, r=yaahc
[rust.git] / library / std / src / ffi / os_str / tests.rs
1 use super::*;
2 use crate::sys_common::{AsInner, IntoInner};
3
4 use crate::rc::Rc;
5 use crate::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 = [OsString::from("a"), OsString::from("b"), OsString::from("c")];
98     assert_eq!("a b c", strings_abc.join(OsStr::new(" ")));
99 }
100
101 #[test]
102 fn test_os_string_default() {
103     let os_string: OsString = Default::default();
104     assert_eq!("", &os_string);
105 }
106
107 #[test]
108 fn test_os_str_is_empty() {
109     let mut os_string = OsString::new();
110     assert!(os_string.is_empty());
111
112     os_string.push("abc");
113     assert!(!os_string.is_empty());
114
115     os_string.clear();
116     assert!(os_string.is_empty());
117 }
118
119 #[test]
120 fn test_os_str_len() {
121     let mut os_string = OsString::new();
122     assert_eq!(0, os_string.len());
123
124     os_string.push("abc");
125     assert_eq!(3, os_string.len());
126
127     os_string.clear();
128     assert_eq!(0, os_string.len());
129 }
130
131 #[test]
132 fn test_os_str_default() {
133     let os_str: &OsStr = Default::default();
134     assert_eq!("", os_str);
135 }
136
137 #[test]
138 fn into_boxed() {
139     let orig = "Hello, world!";
140     let os_str = OsStr::new(orig);
141     let boxed: Box<OsStr> = Box::from(os_str);
142     let os_string = os_str.to_owned().into_boxed_os_str().into_os_string();
143     assert_eq!(os_str, &*boxed);
144     assert_eq!(&*boxed, &*os_string);
145     assert_eq!(&*os_string, os_str);
146 }
147
148 #[test]
149 fn boxed_default() {
150     let boxed = <Box<OsStr>>::default();
151     assert!(boxed.is_empty());
152 }
153
154 #[test]
155 fn test_os_str_clone_into() {
156     let mut os_string = OsString::with_capacity(123);
157     os_string.push("hello");
158     let os_str = OsStr::new("bonjour");
159     os_str.clone_into(&mut os_string);
160     assert_eq!(os_str, os_string);
161     assert!(os_string.capacity() >= 123);
162 }
163
164 #[test]
165 fn into_rc() {
166     let orig = "Hello, world!";
167     let os_str = OsStr::new(orig);
168     let rc: Rc<OsStr> = Rc::from(os_str);
169     let arc: Arc<OsStr> = Arc::from(os_str);
170
171     assert_eq!(&*rc, os_str);
172     assert_eq!(&*arc, os_str);
173
174     let rc2: Rc<OsStr> = Rc::from(os_str.to_owned());
175     let arc2: Arc<OsStr> = Arc::from(os_str.to_owned());
176
177     assert_eq!(&*rc2, os_str);
178     assert_eq!(&*arc2, os_str);
179 }