3 use crate::collections::hash_map::DefaultHasher;
4 use crate::collections::{BTreeSet, HashSet};
5 use crate::hash::Hasher;
8 use core::hint::black_box;
11 ($path:expr, iter: $iter:expr) => (
13 let path = Path::new($path);
16 let comps = path.iter()
17 .map(|p| p.to_string_lossy().into_owned())
18 .collect::<Vec<String>>();
19 let exp: &[&str] = &$iter;
20 let exps = exp.iter().map(|s| s.to_string()).collect::<Vec<String>>();
21 assert!(comps == exps, "iter: Expected {:?}, found {:?}",
25 let comps = Path::new($path).iter().rev()
26 .map(|p| p.to_string_lossy().into_owned())
27 .collect::<Vec<String>>();
28 let exps = exps.into_iter().rev().collect::<Vec<String>>();
29 assert!(comps == exps, "iter().rev(): Expected {:?}, found {:?}",
34 ($path:expr, has_root: $has_root:expr, is_absolute: $is_absolute:expr) => (
36 let path = Path::new($path);
38 let act_root = path.has_root();
39 assert!(act_root == $has_root, "has_root: Expected {:?}, found {:?}",
42 let act_abs = path.is_absolute();
43 assert!(act_abs == $is_absolute, "is_absolute: Expected {:?}, found {:?}",
44 $is_absolute, act_abs);
48 ($path:expr, parent: $parent:expr, file_name: $file:expr) => (
50 let path = Path::new($path);
52 let parent = path.parent().map(|p| p.to_str().unwrap());
53 let exp_parent: Option<&str> = $parent;
54 assert!(parent == exp_parent, "parent: Expected {:?}, found {:?}",
57 let file = path.file_name().map(|p| p.to_str().unwrap());
58 let exp_file: Option<&str> = $file;
59 assert!(file == exp_file, "file_name: Expected {:?}, found {:?}",
64 ($path:expr, file_stem: $file_stem:expr, extension: $extension:expr) => (
66 let path = Path::new($path);
68 let stem = path.file_stem().map(|p| p.to_str().unwrap());
69 let exp_stem: Option<&str> = $file_stem;
70 assert!(stem == exp_stem, "file_stem: Expected {:?}, found {:?}",
73 let ext = path.extension().map(|p| p.to_str().unwrap());
74 let exp_ext: Option<&str> = $extension;
75 assert!(ext == exp_ext, "extension: Expected {:?}, found {:?}",
80 ($path:expr, file_prefix: $file_prefix:expr, extension: $extension:expr) => (
82 let path = Path::new($path);
84 let prefix = path.file_prefix().map(|p| p.to_str().unwrap());
85 let exp_prefix: Option<&str> = $file_prefix;
86 assert!(prefix == exp_prefix, "file_prefix: Expected {:?}, found {:?}",
89 let ext = path.extension().map(|p| p.to_str().unwrap());
90 let exp_ext: Option<&str> = $extension;
91 assert!(ext == exp_ext, "extension: Expected {:?}, found {:?}",
96 ($path:expr, iter: $iter:expr,
97 has_root: $has_root:expr, is_absolute: $is_absolute:expr,
98 parent: $parent:expr, file_name: $file:expr,
99 file_stem: $file_stem:expr, extension: $extension:expr,
100 file_prefix: $file_prefix:expr) => (
102 t!($path, iter: $iter);
103 t!($path, has_root: $has_root, is_absolute: $is_absolute);
104 t!($path, parent: $parent, file_name: $file);
105 t!($path, file_stem: $file_stem, extension: $extension);
106 t!($path, file_prefix: $file_prefix, extension: $extension);
113 use crate::borrow::Cow;
115 let static_path = Path::new("/home/foo");
116 let static_cow_path: Cow<'static, Path> = static_path.into();
117 let pathbuf = PathBuf::from("/home/foo");
120 let path: &Path = &pathbuf;
121 let borrowed_cow_path: Cow<'_, Path> = path.into();
123 assert_eq!(static_cow_path, borrowed_cow_path);
126 let owned_cow_path: Cow<'static, Path> = pathbuf.into();
128 assert_eq!(static_cow_path, owned_cow_path);
133 pub fn test_decompositions_unix() {
150 file_name: Some("foo"),
151 file_stem: Some("foo"),
153 file_prefix: Some("foo")
172 file_name: Some("foo"),
173 file_stem: Some("foo"),
175 file_prefix: Some("foo")
183 file_name: Some("foo"),
184 file_stem: Some("foo"),
186 file_prefix: Some("foo")
194 file_name: Some("foo"),
195 file_stem: Some("foo"),
197 file_prefix: Some("foo")
201 iter: ["foo", "bar"],
205 file_name: Some("bar"),
206 file_stem: Some("bar"),
208 file_prefix: Some("bar")
212 iter: ["/", "foo", "bar"],
215 parent: Some("/foo"),
216 file_name: Some("bar"),
217 file_stem: Some("bar"),
219 file_prefix: Some("bar")
227 file_name: Some("foo"),
228 file_stem: Some("foo"),
230 file_prefix: Some("foo")
234 iter: ["/", "foo", "bar"],
237 parent: Some("///foo"),
238 file_name: Some("bar"),
239 file_stem: Some("bar"),
241 file_prefix: Some("bar")
282 file_name: Some("foo"),
283 file_stem: Some("foo"),
285 file_prefix: Some("foo")
304 file_name: Some("foo"),
305 file_stem: Some("foo"),
307 file_prefix: Some("foo")
311 iter: ["foo", "bar"],
315 file_name: Some("bar"),
316 file_stem: Some("bar"),
318 file_prefix: Some("bar")
333 iter: ["foo", "..", "bar"],
336 parent: Some("foo/.."),
337 file_name: Some("bar"),
338 file_stem: Some("bar"),
340 file_prefix: Some("bar")
348 file_name: Some("a"),
349 file_stem: Some("a"),
351 file_prefix: Some("a")
381 file_name: Some("b"),
382 file_stem: Some("b"),
384 file_prefix: Some("b")
392 file_name: Some("b"),
393 file_stem: Some("b"),
395 file_prefix: Some("b")
403 file_name: Some("b"),
404 file_stem: Some("b"),
406 file_prefix: Some("b")
410 iter: ["a", "b", "c"],
414 file_name: Some("c"),
415 file_stem: Some("c"),
417 file_prefix: Some("c")
425 file_name: Some(".foo"),
426 file_stem: Some(".foo"),
428 file_prefix: Some(".foo")
436 file_name: Some(".foo"),
437 file_stem: Some(".foo"),
439 file_prefix: Some(".foo")
442 t!("a/.rustfmt.toml",
443 iter: ["a", ".rustfmt.toml"],
447 file_name: Some(".rustfmt.toml"),
448 file_stem: Some(".rustfmt"),
449 extension: Some("toml"),
450 file_prefix: Some(".rustfmt")
454 iter: ["a", ".x.y.z"],
458 file_name: Some(".x.y.z"),
459 file_stem: Some(".x.y"),
460 extension: Some("z"),
461 file_prefix: Some(".x")
467 pub fn test_decompositions_windows() {
484 file_name: Some("foo"),
485 file_stem: Some("foo"),
487 file_prefix: Some("foo")
550 file_name: Some("foo"),
551 file_stem: Some("foo"),
553 file_prefix: Some("foo")
561 file_name: Some("foo"),
562 file_stem: Some("foo"),
564 file_prefix: Some("foo")
572 file_name: Some("foo"),
573 file_stem: Some("foo"),
575 file_prefix: Some("foo")
579 iter: ["foo", "bar"],
583 file_name: Some("bar"),
584 file_stem: Some("bar"),
586 file_prefix: Some("bar")
590 iter: ["\\", "foo", "bar"],
593 parent: Some("/foo"),
594 file_name: Some("bar"),
595 file_stem: Some("bar"),
597 file_prefix: Some("bar")
605 file_name: Some("foo"),
606 file_stem: Some("foo"),
608 file_prefix: Some("foo")
612 iter: ["\\", "foo", "bar"],
615 parent: Some("///foo"),
616 file_name: Some("bar"),
617 file_stem: Some("bar"),
619 file_prefix: Some("bar")
660 file_name: Some("foo"),
661 file_stem: Some("foo"),
663 file_prefix: Some("foo")
682 file_name: Some("foo"),
683 file_stem: Some("foo"),
685 file_prefix: Some("foo")
689 iter: ["foo", "bar"],
693 file_name: Some("bar"),
694 file_stem: Some("bar"),
696 file_prefix: Some("bar")
711 iter: ["foo", "..", "bar"],
714 parent: Some("foo/.."),
715 file_name: Some("bar"),
716 file_stem: Some("bar"),
718 file_prefix: Some("bar")
726 file_name: Some("a"),
727 file_stem: Some("a"),
729 file_prefix: Some("a")
759 file_name: Some("b"),
760 file_stem: Some("b"),
762 file_prefix: Some("b")
770 file_name: Some("b"),
771 file_stem: Some("b"),
773 file_prefix: Some("b")
781 file_name: Some("b"),
782 file_stem: Some("b"),
784 file_prefix: Some("b")
788 iter: ["a", "b", "c"],
792 file_name: Some("c"),
793 file_stem: Some("c"),
795 file_prefix: Some("c")
799 iter: ["a", "b", "c"],
802 parent: Some("a\\b"),
803 file_name: Some("c"),
804 file_stem: Some("c"),
806 file_prefix: Some("c")
814 file_name: Some("a"),
815 file_stem: Some("a"),
817 file_prefix: Some("a")
821 iter: ["c:", "\\", "foo.txt"],
824 parent: Some("c:\\"),
825 file_name: Some("foo.txt"),
826 file_stem: Some("foo"),
827 extension: Some("txt"),
828 file_prefix: Some("foo")
831 t!("\\\\server\\share\\foo.txt",
832 iter: ["\\\\server\\share", "\\", "foo.txt"],
835 parent: Some("\\\\server\\share\\"),
836 file_name: Some("foo.txt"),
837 file_stem: Some("foo"),
838 extension: Some("txt"),
839 file_prefix: Some("foo")
842 t!("\\\\server\\share",
843 iter: ["\\\\server\\share", "\\"],
854 iter: ["\\", "server"],
858 file_name: Some("server"),
859 file_stem: Some("server"),
861 file_prefix: Some("server")
864 t!("\\\\?\\bar\\foo.txt",
865 iter: ["\\\\?\\bar", "\\", "foo.txt"],
868 parent: Some("\\\\?\\bar\\"),
869 file_name: Some("foo.txt"),
870 file_stem: Some("foo"),
871 extension: Some("txt"),
872 file_prefix: Some("foo")
876 iter: ["\\\\?\\bar"],
897 t!("\\\\?\\UNC\\server\\share\\foo.txt",
898 iter: ["\\\\?\\UNC\\server\\share", "\\", "foo.txt"],
901 parent: Some("\\\\?\\UNC\\server\\share\\"),
902 file_name: Some("foo.txt"),
903 file_stem: Some("foo"),
904 extension: Some("txt"),
905 file_prefix: Some("foo")
908 t!("\\\\?\\UNC\\server",
909 iter: ["\\\\?\\UNC\\server"],
920 iter: ["\\\\?\\UNC\\"],
930 t!("\\\\?\\C:\\foo.txt",
931 iter: ["\\\\?\\C:", "\\", "foo.txt"],
934 parent: Some("\\\\?\\C:\\"),
935 file_name: Some("foo.txt"),
936 file_stem: Some("foo"),
937 extension: Some("txt"),
938 file_prefix: Some("foo")
942 iter: ["\\\\?\\C:", "\\"],
964 iter: ["\\\\?\\foo/bar"],
975 iter: ["\\\\?\\C:/foo"],
985 t!("\\\\.\\foo\\bar",
986 iter: ["\\\\.\\foo", "\\", "bar"],
989 parent: Some("\\\\.\\foo\\"),
990 file_name: Some("bar"),
991 file_stem: Some("bar"),
993 file_prefix: Some("bar")
997 iter: ["\\\\.\\foo", "\\"],
1007 t!("\\\\.\\foo/bar",
1008 iter: ["\\\\.\\foo", "\\", "bar"],
1011 parent: Some("\\\\.\\foo/"),
1012 file_name: Some("bar"),
1013 file_stem: Some("bar"),
1015 file_prefix: Some("bar")
1018 t!("\\\\.\\foo\\bar/baz",
1019 iter: ["\\\\.\\foo", "\\", "bar", "baz"],
1022 parent: Some("\\\\.\\foo\\bar"),
1023 file_name: Some("baz"),
1024 file_stem: Some("baz"),
1026 file_prefix: Some("baz")
1030 iter: ["\\\\.\\", "\\"],
1041 iter: ["\\\\?\\a", "\\", "b"],
1044 parent: Some("\\\\?\\a\\"),
1045 file_name: Some("b"),
1046 file_stem: Some("b"),
1048 file_prefix: Some("b")
1051 t!("\\\\?\\C:\\foo.txt.zip",
1052 iter: ["\\\\?\\C:", "\\", "foo.txt.zip"],
1055 parent: Some("\\\\?\\C:\\"),
1056 file_name: Some("foo.txt.zip"),
1057 file_stem: Some("foo.txt"),
1058 extension: Some("zip"),
1059 file_prefix: Some("foo")
1062 t!("\\\\?\\C:\\.foo.txt.zip",
1063 iter: ["\\\\?\\C:", "\\", ".foo.txt.zip"],
1066 parent: Some("\\\\?\\C:\\"),
1067 file_name: Some(".foo.txt.zip"),
1068 file_stem: Some(".foo.txt"),
1069 extension: Some("zip"),
1070 file_prefix: Some(".foo")
1073 t!("\\\\?\\C:\\.foo",
1074 iter: ["\\\\?\\C:", "\\", ".foo"],
1077 parent: Some("\\\\?\\C:\\"),
1078 file_name: Some(".foo"),
1079 file_stem: Some(".foo"),
1081 file_prefix: Some(".foo")
1085 iter: ["a", ".x.y.z"],
1089 file_name: Some(".x.y.z"),
1090 file_stem: Some(".x.y"),
1091 extension: Some("z"),
1092 file_prefix: Some(".x")
1097 pub fn test_stem_ext() {
1099 file_stem: Some("foo"),
1104 file_stem: Some("foo"),
1109 file_stem: Some(".foo"),
1114 file_stem: Some("foo"),
1115 extension: Some("txt")
1119 file_stem: Some("foo.bar"),
1120 extension: Some("txt")
1124 file_stem: Some("foo.bar"),
1128 t!(".", file_stem: None, extension: None);
1130 t!("..", file_stem: None, extension: None);
1132 t!(".x.y.z", file_stem: Some(".x.y"), extension: Some("z"));
1134 t!("..x.y.z", file_stem: Some("..x.y"), extension: Some("z"));
1136 t!("", file_stem: None, extension: None);
1140 pub fn test_prefix_ext() {
1142 file_prefix: Some("foo"),
1147 file_prefix: Some("foo"),
1152 file_prefix: Some(".foo"),
1157 file_prefix: Some("foo"),
1158 extension: Some("txt")
1162 file_prefix: Some("foo"),
1163 extension: Some("txt")
1167 file_prefix: Some("foo"),
1171 t!(".", file_prefix: None, extension: None);
1173 t!("..", file_prefix: None, extension: None);
1175 t!(".x.y.z", file_prefix: Some(".x"), extension: Some("z"));
1177 t!("..x.y.z", file_prefix: Some("."), extension: Some("z"));
1179 t!("", file_prefix: None, extension: None);
1183 pub fn test_push() {
1185 ($path:expr, $push:expr, $expected:expr) => ( {
1186 let mut actual = PathBuf::from($path);
1188 assert!(actual.to_str() == Some($expected),
1189 "pushing {:?} onto {:?}: Expected {:?}, got {:?}",
1190 $push, $path, $expected, actual.to_str().unwrap());
1194 if cfg!(unix) || cfg!(all(target_env = "sgx", target_vendor = "fortanix")) {
1195 tp!("", "foo", "foo");
1196 tp!("foo", "bar", "foo/bar");
1197 tp!("foo/", "bar", "foo/bar");
1198 tp!("foo//", "bar", "foo//bar");
1199 tp!("foo/.", "bar", "foo/./bar");
1200 tp!("foo./.", "bar", "foo././bar");
1201 tp!("foo", "", "foo/");
1202 tp!("foo", ".", "foo/.");
1203 tp!("foo", "..", "foo/..");
1204 tp!("foo", "/", "/");
1205 tp!("/foo/bar", "/", "/");
1206 tp!("/foo/bar", "/baz", "/baz");
1207 tp!("/foo/bar", "./baz", "/foo/bar/./baz");
1209 tp!("", "foo", "foo");
1210 tp!("foo", "bar", r"foo\bar");
1211 tp!("foo/", "bar", r"foo/bar");
1212 tp!(r"foo\", "bar", r"foo\bar");
1213 tp!("foo//", "bar", r"foo//bar");
1214 tp!(r"foo\\", "bar", r"foo\\bar");
1215 tp!("foo/.", "bar", r"foo/.\bar");
1216 tp!("foo./.", "bar", r"foo./.\bar");
1217 tp!(r"foo\.", "bar", r"foo\.\bar");
1218 tp!(r"foo.\.", "bar", r"foo.\.\bar");
1219 tp!("foo", "", "foo\\");
1220 tp!("foo", ".", r"foo\.");
1221 tp!("foo", "..", r"foo\..");
1222 tp!("foo", "/", "/");
1223 tp!("foo", r"\", r"\");
1224 tp!("/foo/bar", "/", "/");
1225 tp!(r"\foo\bar", r"\", r"\");
1226 tp!("/foo/bar", "/baz", "/baz");
1227 tp!("/foo/bar", r"\baz", r"\baz");
1228 tp!("/foo/bar", "./baz", r"/foo/bar\./baz");
1229 tp!("/foo/bar", r".\baz", r"/foo/bar\.\baz");
1231 tp!("c:\\", "windows", "c:\\windows");
1232 tp!("c:", "windows", "c:windows");
1234 tp!("a\\b\\c", "d", "a\\b\\c\\d");
1235 tp!("\\a\\b\\c", "d", "\\a\\b\\c\\d");
1236 tp!("a\\b", "c\\d", "a\\b\\c\\d");
1237 tp!("a\\b", "\\c\\d", "\\c\\d");
1238 tp!("a\\b", ".", "a\\b\\.");
1239 tp!("a\\b", "..\\c", "a\\b\\..\\c");
1240 tp!("a\\b", "C:a.txt", "C:a.txt");
1241 tp!("a\\b", "C:\\a.txt", "C:\\a.txt");
1242 tp!("C:\\a", "C:\\b.txt", "C:\\b.txt");
1243 tp!("C:\\a\\b\\c", "C:d", "C:d");
1244 tp!("C:a\\b\\c", "C:d", "C:d");
1245 tp!("C:", r"a\b\c", r"C:a\b\c");
1246 tp!("C:", r"..\a", r"C:..\a");
1247 tp!("\\\\server\\share\\foo", "bar", "\\\\server\\share\\foo\\bar");
1248 tp!("\\\\server\\share\\foo", "C:baz", "C:baz");
1249 tp!("\\\\?\\C:\\a\\b", "C:c\\d", "C:c\\d");
1250 tp!("\\\\?\\C:a\\b", "C:c\\d", "C:c\\d");
1251 tp!("\\\\?\\C:\\a\\b", "C:\\c\\d", "C:\\c\\d");
1252 tp!("\\\\?\\foo\\bar", "baz", "\\\\?\\foo\\bar\\baz");
1253 tp!("\\\\?\\UNC\\server\\share\\foo", "bar", "\\\\?\\UNC\\server\\share\\foo\\bar");
1254 tp!("\\\\?\\UNC\\server\\share", "C:\\a", "C:\\a");
1255 tp!("\\\\?\\UNC\\server\\share", "C:a", "C:a");
1257 // Note: modified from old path API
1258 tp!("\\\\?\\UNC\\server", "foo", "\\\\?\\UNC\\server\\foo");
1260 tp!("C:\\a", "\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share");
1261 tp!("\\\\.\\foo\\bar", "baz", "\\\\.\\foo\\bar\\baz");
1262 tp!("\\\\.\\foo\\bar", "C:a", "C:a");
1263 // again, not sure about the following, but I'm assuming \\.\ should be verbatim
1264 tp!("\\\\.\\foo", "..\\bar", "\\\\.\\foo\\..\\bar");
1266 tp!("\\\\?\\C:", "foo", "\\\\?\\C:\\foo"); // this is a weird one
1268 tp!(r"\\?\C:\bar", "../foo", r"\\?\C:\foo");
1269 tp!(r"\\?\C:\bar", "../../foo", r"\\?\C:\foo");
1270 tp!(r"\\?\C:\", "../foo", r"\\?\C:\foo");
1271 tp!(r"\\?\C:", r"D:\foo/./", r"D:\foo/./");
1272 tp!(r"\\?\C:", r"\\?\D:\foo\.\", r"\\?\D:\foo\.\");
1273 tp!(r"\\?\A:\x\y", "/foo", r"\\?\A:\foo");
1274 tp!(r"\\?\A:", r"..\foo\.", r"\\?\A:\foo");
1275 tp!(r"\\?\A:\x\y", r".\foo\.", r"\\?\A:\x\y\foo");
1276 tp!(r"\\?\A:\x\y", r"", r"\\?\A:\x\y\");
1283 ($path:expr, $expected:expr, $output:expr) => ( {
1284 let mut actual = PathBuf::from($path);
1285 let output = actual.pop();
1286 assert!(actual.to_str() == Some($expected) && output == $output,
1287 "popping from {:?}: Expected {:?}/{:?}, got {:?}/{:?}",
1288 $path, $expected, $output,
1289 actual.to_str().unwrap(), output);
1294 tp!("/", "/", false);
1295 tp!("foo", "", true);
1297 tp!("/foo", "/", true);
1298 tp!("/foo/bar", "/foo", true);
1299 tp!("foo/bar", "foo", true);
1300 tp!("foo/.", "", true);
1301 tp!("foo//bar", "foo", true);
1304 tp!("a\\b\\c", "a\\b", true);
1305 tp!("\\a", "\\", true);
1306 tp!("\\", "\\", false);
1308 tp!("C:\\a\\b", "C:\\a", true);
1309 tp!("C:\\a", "C:\\", true);
1310 tp!("C:\\", "C:\\", false);
1311 tp!("C:a\\b", "C:a", true);
1312 tp!("C:a", "C:", true);
1313 tp!("C:", "C:", false);
1314 tp!("\\\\server\\share\\a\\b", "\\\\server\\share\\a", true);
1315 tp!("\\\\server\\share\\a", "\\\\server\\share\\", true);
1316 tp!("\\\\server\\share", "\\\\server\\share", false);
1317 tp!("\\\\?\\a\\b\\c", "\\\\?\\a\\b", true);
1318 tp!("\\\\?\\a\\b", "\\\\?\\a\\", true);
1319 tp!("\\\\?\\a", "\\\\?\\a", false);
1320 tp!("\\\\?\\C:\\a\\b", "\\\\?\\C:\\a", true);
1321 tp!("\\\\?\\C:\\a", "\\\\?\\C:\\", true);
1322 tp!("\\\\?\\C:\\", "\\\\?\\C:\\", false);
1323 tp!("\\\\?\\UNC\\server\\share\\a\\b", "\\\\?\\UNC\\server\\share\\a", true);
1324 tp!("\\\\?\\UNC\\server\\share\\a", "\\\\?\\UNC\\server\\share\\", true);
1325 tp!("\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\server\\share", false);
1326 tp!("\\\\.\\a\\b\\c", "\\\\.\\a\\b", true);
1327 tp!("\\\\.\\a\\b", "\\\\.\\a\\", true);
1328 tp!("\\\\.\\a", "\\\\.\\a", false);
1330 tp!("\\\\?\\a\\b\\", "\\\\?\\a\\", true);
1335 pub fn test_set_file_name() {
1337 ($path:expr, $file:expr, $expected:expr) => ( {
1338 let mut p = PathBuf::from($path);
1339 p.set_file_name($file);
1340 assert!(p.to_str() == Some($expected),
1341 "setting file name of {:?} to {:?}: Expected {:?}, got {:?}",
1342 $path, $file, $expected,
1343 p.to_str().unwrap());
1347 tfn!("foo", "foo", "foo");
1348 tfn!("foo", "bar", "bar");
1349 tfn!("foo", "", "");
1350 tfn!("", "foo", "foo");
1351 if cfg!(unix) || cfg!(all(target_env = "sgx", target_vendor = "fortanix")) {
1352 tfn!(".", "foo", "./foo");
1353 tfn!("foo/", "bar", "bar");
1354 tfn!("foo/.", "bar", "bar");
1355 tfn!("..", "foo", "../foo");
1356 tfn!("foo/..", "bar", "foo/../bar");
1357 tfn!("/", "foo", "/foo");
1359 tfn!(".", "foo", r".\foo");
1360 tfn!(r"foo\", "bar", r"bar");
1361 tfn!(r"foo\.", "bar", r"bar");
1362 tfn!("..", "foo", r"..\foo");
1363 tfn!(r"foo\..", "bar", r"foo\..\bar");
1364 tfn!(r"\", "foo", r"\foo");
1369 pub fn test_set_extension() {
1371 ($path:expr, $ext:expr, $expected:expr, $output:expr) => ( {
1372 let mut p = PathBuf::from($path);
1373 let output = p.set_extension($ext);
1374 assert!(p.to_str() == Some($expected) && output == $output,
1375 "setting extension of {:?} to {:?}: Expected {:?}/{:?}, got {:?}/{:?}",
1376 $path, $ext, $expected, $output,
1377 p.to_str().unwrap(), output);
1381 tfe!("foo", "txt", "foo.txt", true);
1382 tfe!("foo.bar", "txt", "foo.txt", true);
1383 tfe!("foo.bar.baz", "txt", "foo.bar.txt", true);
1384 tfe!(".test", "txt", ".test.txt", true);
1385 tfe!("foo.txt", "", "foo", true);
1386 tfe!("foo", "", "foo", true);
1387 tfe!("", "foo", "", false);
1388 tfe!(".", "foo", ".", false);
1389 tfe!("foo/", "bar", "foo.bar", true);
1390 tfe!("foo/.", "bar", "foo.bar", true);
1391 tfe!("..", "foo", "..", false);
1392 tfe!("foo/..", "bar", "foo/..", false);
1393 tfe!("/", "foo", "/", false);
1397 fn test_eq_receivers() {
1398 use crate::borrow::Cow;
1400 let borrowed: &Path = Path::new("foo/bar");
1401 let mut owned: PathBuf = PathBuf::new();
1404 let borrowed_cow: Cow<'_, Path> = borrowed.into();
1405 let owned_cow: Cow<'_, Path> = owned.clone().into();
1408 ($($current:expr),+) => {
1410 assert_eq!($current, borrowed);
1411 assert_eq!($current, owned);
1412 assert_eq!($current, borrowed_cow);
1413 assert_eq!($current, owned_cow);
1418 t!(borrowed, owned, borrowed_cow, owned_cow);
1422 pub fn test_compare() {
1423 use crate::collections::hash_map::DefaultHasher;
1424 use crate::hash::{Hash, Hasher};
1426 fn hash<T: Hash>(t: T) -> u64 {
1427 let mut s = DefaultHasher::new();
1433 ($path1:expr, $path2:expr, eq: $eq:expr,
1434 starts_with: $starts_with:expr, ends_with: $ends_with:expr,
1435 relative_from: $relative_from:expr) => ({
1436 let path1 = Path::new($path1);
1437 let path2 = Path::new($path2);
1439 let eq = path1 == path2;
1440 assert!(eq == $eq, "{:?} == {:?}, expected {:?}, got {:?}",
1441 $path1, $path2, $eq, eq);
1442 assert!($eq == (hash(path1) == hash(path2)),
1443 "{:?} == {:?}, expected {:?}, got {} and {}",
1444 $path1, $path2, $eq, hash(path1), hash(path2));
1446 let starts_with = path1.starts_with(path2);
1447 assert!(starts_with == $starts_with,
1448 "{:?}.starts_with({:?}), expected {:?}, got {:?}", $path1, $path2,
1449 $starts_with, starts_with);
1451 let ends_with = path1.ends_with(path2);
1452 assert!(ends_with == $ends_with,
1453 "{:?}.ends_with({:?}), expected {:?}, got {:?}", $path1, $path2,
1454 $ends_with, ends_with);
1456 let relative_from = path1.strip_prefix(path2)
1457 .map(|p| p.to_str().unwrap())
1459 let exp: Option<&str> = $relative_from;
1460 assert!(relative_from == exp,
1461 "{:?}.strip_prefix({:?}), expected {:?}, got {:?}",
1462 $path1, $path2, exp, relative_from);
1470 relative_from: Some("")
1477 relative_from: Some("foo")
1491 relative_from: Some("")
1498 relative_from: Some("")
1505 relative_from: Some("")
1508 tc!("foo/./bar", "foo/bar",
1512 relative_from: Some("")
1515 tc!("foo/bar", "foo",
1519 relative_from: Some("bar")
1522 tc!("foo/bar/baz", "foo/bar",
1526 relative_from: Some("baz")
1529 tc!("foo/bar", "foo/bar/baz",
1536 tc!("./foo/bar/", ".",
1540 relative_from: Some("foo/bar")
1544 tc!(r"C:\src\rust\cargo-test\test\Cargo.toml",
1545 r"c:\src\rust\cargo-test\test",
1549 relative_from: Some("Cargo.toml")
1552 tc!(r"c:\foo", r"C:\foo",
1556 relative_from: Some("")
1559 tc!(r"C:\foo\.\bar.txt", r"C:\foo\bar.txt",
1563 relative_from: Some("")
1566 tc!(r"C:\foo\.", r"C:\foo",
1570 relative_from: Some("")
1573 tc!(r"\\?\C:\foo\.\bar.txt", r"\\?\C:\foo\bar.txt",
1583 fn test_components_debug() {
1584 let path = Path::new("/tmp");
1586 let mut components = path.components();
1588 let expected = "Components([RootDir, Normal(\"tmp\")])";
1589 let actual = format!("{components:?}");
1590 assert_eq!(expected, actual);
1592 let _ = components.next().unwrap();
1593 let expected = "Components([Normal(\"tmp\")])";
1594 let actual = format!("{components:?}");
1595 assert_eq!(expected, actual);
1597 let _ = components.next().unwrap();
1598 let expected = "Components([])";
1599 let actual = format!("{components:?}");
1600 assert_eq!(expected, actual);
1605 fn test_iter_debug() {
1606 let path = Path::new("/tmp");
1608 let mut iter = path.iter();
1610 let expected = "Iter([\"/\", \"tmp\"])";
1611 let actual = format!("{iter:?}");
1612 assert_eq!(expected, actual);
1614 let _ = iter.next().unwrap();
1615 let expected = "Iter([\"tmp\"])";
1616 let actual = format!("{iter:?}");
1617 assert_eq!(expected, actual);
1619 let _ = iter.next().unwrap();
1620 let expected = "Iter([])";
1621 let actual = format!("{iter:?}");
1622 assert_eq!(expected, actual);
1627 let orig: &str = "some/sort/of/path";
1628 let path = Path::new(orig);
1629 let boxed: Box<Path> = Box::from(path);
1630 let path_buf = path.to_owned().into_boxed_path().into_path_buf();
1631 assert_eq!(path, &*boxed);
1632 assert_eq!(&*boxed, &*path_buf);
1633 assert_eq!(&*path_buf, path);
1637 fn test_clone_into() {
1638 let mut path_buf = PathBuf::from("supercalifragilisticexpialidocious");
1639 let path = Path::new("short");
1640 path.clone_into(&mut path_buf);
1641 assert_eq!(path, path_buf);
1642 assert!(path_buf.into_os_string().capacity() >= 15);
1646 fn display_format_flags() {
1647 assert_eq!(format!("a{:#<5}b", Path::new("").display()), "a#####b");
1648 assert_eq!(format!("a{:#<5}b", Path::new("a").display()), "aa####b");
1653 let orig = "hello/world";
1654 let path = Path::new(orig);
1655 let rc: Rc<Path> = Rc::from(path);
1656 let arc: Arc<Path> = Arc::from(path);
1658 assert_eq!(&*rc, path);
1659 assert_eq!(&*arc, path);
1661 let rc2: Rc<Path> = Rc::from(path.to_owned());
1662 let arc2: Arc<Path> = Arc::from(path.to_owned());
1664 assert_eq!(&*rc2, path);
1665 assert_eq!(&*arc2, path);
1671 ($ord:ident, $left:expr, $right:expr) => ( {
1672 use core::cmp::Ordering;
1674 let left = Path::new($left);
1675 let right = Path::new($right);
1676 assert_eq!(left.cmp(&right), Ordering::$ord);
1677 if (core::cmp::Ordering::$ord == Ordering::Equal) {
1678 assert_eq!(left, right);
1680 let mut hasher = DefaultHasher::new();
1681 left.hash(&mut hasher);
1682 let left_hash = hasher.finish();
1683 hasher = DefaultHasher::new();
1684 right.hash(&mut hasher);
1685 let right_hash = hasher.finish();
1687 assert_eq!(left_hash, right_hash, "hashes for {:?} and {:?} must match", left, right);
1689 assert_ne!(left, right);
1694 ord!(Less, "1", "2");
1695 ord!(Less, "/foo/bar", "/foo./bar");
1696 ord!(Less, "foo/bar", "foo/bar.");
1697 ord!(Equal, "foo/./bar", "foo/bar/");
1698 ord!(Equal, "foo/bar", "foo/bar/");
1699 ord!(Equal, "foo/bar", "foo/bar/.");
1700 ord!(Equal, "foo/bar", "foo/bar//");
1705 fn test_unix_absolute() {
1706 use crate::path::absolute;
1708 assert!(absolute("").is_err());
1710 let relative = "a/b";
1711 let mut expected = crate::env::current_dir().unwrap();
1712 expected.push(relative);
1713 assert_eq!(absolute(relative).unwrap().as_os_str(), expected.as_os_str());
1715 // Test how components are collected.
1716 assert_eq!(absolute("/a/b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
1717 assert_eq!(absolute("/a//b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
1718 assert_eq!(absolute("//a/b/c").unwrap().as_os_str(), Path::new("//a/b/c").as_os_str());
1719 assert_eq!(absolute("///a/b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
1720 assert_eq!(absolute("/a/b/c/").unwrap().as_os_str(), Path::new("/a/b/c/").as_os_str());
1722 absolute("/a/./b/../c/.././..").unwrap().as_os_str(),
1723 Path::new("/a/b/../c/../..").as_os_str()
1726 // Test leading `.` and `..` components
1727 let curdir = crate::env::current_dir().unwrap();
1728 assert_eq!(absolute("./a").unwrap().as_os_str(), curdir.join("a").as_os_str());
1729 assert_eq!(absolute("../a").unwrap().as_os_str(), curdir.join("../a").as_os_str()); // return /pwd/../a
1734 fn test_windows_absolute() {
1735 use crate::path::absolute;
1736 // An empty path is an error.
1737 assert!(absolute("").is_err());
1739 let relative = r"a\b";
1740 let mut expected = crate::env::current_dir().unwrap();
1741 expected.push(relative);
1742 assert_eq!(absolute(relative).unwrap(), expected);
1744 macro_rules! unchanged(
1746 assert_eq!(absolute($path).unwrap(), Path::new($path));
1750 unchanged!(r"C:\path\to\file");
1751 unchanged!(r"C:\path\to\file\");
1752 unchanged!(r"\\server\share\to\file");
1753 unchanged!(r"\\server.\share.\to\file");
1754 unchanged!(r"\\.\PIPE\name");
1755 unchanged!(r"\\.\C:\path\to\COM1");
1756 unchanged!(r"\\?\C:\path\to\file");
1757 unchanged!(r"\\?\UNC\server\share\to\file");
1758 unchanged!(r"\\?\PIPE\name");
1759 // Verbatim paths are always unchanged, no matter what.
1760 unchanged!(r"\\?\path.\to/file..");
1762 assert_eq!(absolute(r"C:\path..\to.\file.").unwrap(), Path::new(r"C:\path..\to\file"));
1763 assert_eq!(absolute(r"C:\path\to\COM1").unwrap(), Path::new(r"\\.\COM1"));
1764 assert_eq!(absolute(r"C:\path\to\COM1.txt").unwrap(), Path::new(r"\\.\COM1"));
1765 assert_eq!(absolute(r"C:\path\to\COM1 .txt").unwrap(), Path::new(r"\\.\COM1"));
1766 assert_eq!(absolute(r"C:\path\to\cOnOuT$").unwrap(), Path::new(r"\\.\cOnOuT$"));
1770 fn bench_path_cmp_fast_path_buf_sort(b: &mut test::Bencher) {
1771 let prefix = "my/home";
1772 let mut paths: Vec<_> =
1773 (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
1778 black_box(paths.as_mut_slice()).sort_unstable();
1783 fn bench_path_cmp_fast_path_long(b: &mut test::Bencher) {
1784 let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
1786 (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
1788 let mut set = BTreeSet::new();
1790 paths.iter().for_each(|p| {
1791 set.insert(p.as_path());
1795 set.remove(paths[500].as_path());
1796 set.insert(paths[500].as_path());
1801 fn bench_path_cmp_fast_path_short(b: &mut test::Bencher) {
1802 let prefix = "my/home";
1804 (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
1806 let mut set = BTreeSet::new();
1808 paths.iter().for_each(|p| {
1809 set.insert(p.as_path());
1813 set.remove(paths[500].as_path());
1814 set.insert(paths[500].as_path());
1819 fn bench_path_hashset(b: &mut test::Bencher) {
1820 let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
1822 (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
1824 let mut set = HashSet::new();
1826 paths.iter().for_each(|p| {
1827 set.insert(p.as_path());
1831 set.remove(paths[500].as_path());
1832 set.insert(black_box(paths[500].as_path()))
1837 fn bench_path_hashset_miss(b: &mut test::Bencher) {
1838 let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
1840 (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {num}.rs"))).collect();
1842 let mut set = HashSet::new();
1844 paths.iter().for_each(|p| {
1845 set.insert(p.as_path());
1848 let probe = PathBuf::from(prefix).join("other");
1850 b.iter(|| set.remove(black_box(probe.as_path())));
1854 fn bench_hash_path_short(b: &mut test::Bencher) {
1855 let mut hasher = DefaultHasher::new();
1856 let path = Path::new("explorer.exe");
1858 b.iter(|| black_box(path).hash(&mut hasher));
1860 black_box(hasher.finish());
1864 fn bench_hash_path_long(b: &mut test::Bencher) {
1865 let mut hasher = DefaultHasher::new();
1867 Path::new("/aaaaa/aaaaaa/./../aaaaaaaa/bbbbbbbbbbbbb/ccccccccccc/ddddddddd/eeeeeee.fff");
1869 b.iter(|| black_box(path).hash(&mut hasher));
1871 black_box(hasher.finish());