]> git.lizzy.rs Git - rust.git/blob - library/std/src/ffi/os_str/tests.rs
std: move "mod tests/benches" to separate files
[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_default() {
89     let os_string: OsString = Default::default();
90     assert_eq!("", &os_string);
91 }
92
93 #[test]
94 fn test_os_str_is_empty() {
95     let mut os_string = OsString::new();
96     assert!(os_string.is_empty());
97
98     os_string.push("abc");
99     assert!(!os_string.is_empty());
100
101     os_string.clear();
102     assert!(os_string.is_empty());
103 }
104
105 #[test]
106 fn test_os_str_len() {
107     let mut os_string = OsString::new();
108     assert_eq!(0, os_string.len());
109
110     os_string.push("abc");
111     assert_eq!(3, os_string.len());
112
113     os_string.clear();
114     assert_eq!(0, os_string.len());
115 }
116
117 #[test]
118 fn test_os_str_default() {
119     let os_str: &OsStr = Default::default();
120     assert_eq!("", os_str);
121 }
122
123 #[test]
124 fn into_boxed() {
125     let orig = "Hello, world!";
126     let os_str = OsStr::new(orig);
127     let boxed: Box<OsStr> = Box::from(os_str);
128     let os_string = os_str.to_owned().into_boxed_os_str().into_os_string();
129     assert_eq!(os_str, &*boxed);
130     assert_eq!(&*boxed, &*os_string);
131     assert_eq!(&*os_string, os_str);
132 }
133
134 #[test]
135 fn boxed_default() {
136     let boxed = <Box<OsStr>>::default();
137     assert!(boxed.is_empty());
138 }
139
140 #[test]
141 fn test_os_str_clone_into() {
142     let mut os_string = OsString::with_capacity(123);
143     os_string.push("hello");
144     let os_str = OsStr::new("bonjour");
145     os_str.clone_into(&mut os_string);
146     assert_eq!(os_str, os_string);
147     assert!(os_string.capacity() >= 123);
148 }
149
150 #[test]
151 fn into_rc() {
152     let orig = "Hello, world!";
153     let os_str = OsStr::new(orig);
154     let rc: Rc<OsStr> = Rc::from(os_str);
155     let arc: Arc<OsStr> = Arc::from(os_str);
156
157     assert_eq!(&*rc, os_str);
158     assert_eq!(&*arc, os_str);
159
160     let rc2: Rc<OsStr> = Rc::from(os_str.to_owned());
161     let arc2: Arc<OsStr> = Arc::from(os_str.to_owned());
162
163     assert_eq!(&*rc2, os_str);
164     assert_eq!(&*arc2, os_str);
165 }