]> git.lizzy.rs Git - rust.git/blob - src/libstd/path/posix.rs
bd4031e623085ee0c1263833ba9eef187918c109
[rust.git] / src / libstd / path / posix.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! POSIX file path handling
12
13 use c_str::{CString, ToCStr};
14 use clone::Clone;
15 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
16 use hash;
17 use io::Writer;
18 use iter::{DoubleEndedIteratorExt, AdditiveIterator, Extend};
19 use iter::{Iterator, IteratorExt, Map};
20 use option::Option;
21 use option::Option::{None, Some};
22 use kinds::Sized;
23 use str::{FromStr, Str};
24 use str;
25 use slice::{CloneSliceExt, Split, AsSlice, SliceConcatExt,
26             PartialEqSliceExt, SliceExt};
27 use vec::Vec;
28
29 use super::{BytesContainer, GenericPath, GenericPathUnsafe};
30
31 /// Iterator that yields successive components of a Path as &[u8]
32 pub type Components<'a> = Split<'a, u8, fn(&u8) -> bool>;
33
34 /// Iterator that yields successive components of a Path as Option<&str>
35 pub type StrComponents<'a> =
36     Map<&'a [u8], Option<&'a str>, Components<'a>, fn(&[u8]) -> Option<&str>>;
37
38 /// Represents a POSIX file path
39 #[deriving(Clone)]
40 pub struct Path {
41     repr: Vec<u8>, // assumed to never be empty or contain NULs
42     sepidx: Option<uint> // index of the final separator in repr
43 }
44
45 /// The standard path separator character
46 pub const SEP: char = '/';
47
48 /// The standard path separator byte
49 pub const SEP_BYTE: u8 = SEP as u8;
50
51 /// Returns whether the given byte is a path separator
52 #[inline]
53 pub fn is_sep_byte(u: &u8) -> bool {
54     *u as char == SEP
55 }
56
57 /// Returns whether the given char is a path separator
58 #[inline]
59 pub fn is_sep(c: char) -> bool {
60     c == SEP
61 }
62
63 impl PartialEq for Path {
64     #[inline]
65     fn eq(&self, other: &Path) -> bool {
66         self.repr == other.repr
67     }
68 }
69
70 impl Eq for Path {}
71
72 impl PartialOrd for Path {
73     fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
74         Some(self.cmp(other))
75     }
76 }
77
78 impl Ord for Path {
79     fn cmp(&self, other: &Path) -> Ordering {
80         self.repr.cmp(&other.repr)
81     }
82 }
83
84 impl FromStr for Path {
85     fn from_str(s: &str) -> Option<Path> {
86         Path::new_opt(s)
87     }
88 }
89
90 // FIXME (#12938): Until DST lands, we cannot decompose &str into & and str, so
91 // we cannot usefully take ToCStr arguments by reference (without forcing an
92 // additional & around &str). So we are instead temporarily adding an instance
93 // for &Path, so that we can take ToCStr as owned. When DST lands, the &Path
94 // instance should be removed, and arguments bound by ToCStr should be passed by
95 // reference.
96
97 impl ToCStr for Path {
98     #[inline]
99     fn to_c_str(&self) -> CString {
100         // The Path impl guarantees no internal NUL
101         unsafe { self.to_c_str_unchecked() }
102     }
103
104     #[inline]
105     unsafe fn to_c_str_unchecked(&self) -> CString {
106         self.as_vec().to_c_str_unchecked()
107     }
108 }
109
110 impl<S: hash::Writer> hash::Hash<S> for Path {
111     #[inline]
112     fn hash(&self, state: &mut S) {
113         self.repr.hash(state)
114     }
115 }
116
117 impl BytesContainer for Path {
118     #[inline]
119     fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
120         self.as_vec()
121     }
122 }
123
124 impl GenericPathUnsafe for Path {
125     unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
126         let path = Path::normalize(path.container_as_bytes());
127         assert!(!path.is_empty());
128         let idx = path.as_slice().rposition_elem(&SEP_BYTE);
129         Path{ repr: path, sepidx: idx }
130     }
131
132     unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
133         let filename = filename.container_as_bytes();
134         match self.sepidx {
135             None if b".." == self.repr => {
136                 let mut v = Vec::with_capacity(3 + filename.len());
137                 v.push_all(dot_dot_static);
138                 v.push(SEP_BYTE);
139                 v.push_all(filename);
140                 // FIXME: this is slow
141                 self.repr = Path::normalize(v.as_slice());
142             }
143             None => {
144                 self.repr = Path::normalize(filename);
145             }
146             Some(idx) if self.repr[idx+1..] == b".." => {
147                 let mut v = Vec::with_capacity(self.repr.len() + 1 + filename.len());
148                 v.push_all(self.repr.as_slice());
149                 v.push(SEP_BYTE);
150                 v.push_all(filename);
151                 // FIXME: this is slow
152                 self.repr = Path::normalize(v.as_slice());
153             }
154             Some(idx) => {
155                 let mut v = Vec::with_capacity(idx + 1 + filename.len());
156                 v.push_all(self.repr[..idx+1]);
157                 v.push_all(filename);
158                 // FIXME: this is slow
159                 self.repr = Path::normalize(v.as_slice());
160             }
161         }
162         self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
163     }
164
165     unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
166         let path = path.container_as_bytes();
167         if !path.is_empty() {
168             if path[0] == SEP_BYTE {
169                 self.repr = Path::normalize(path);
170             }  else {
171                 let mut v = Vec::with_capacity(self.repr.len() + path.len() + 1);
172                 v.push_all(self.repr.as_slice());
173                 v.push(SEP_BYTE);
174                 v.push_all(path);
175                 // FIXME: this is slow
176                 self.repr = Path::normalize(v.as_slice());
177             }
178             self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
179         }
180     }
181 }
182
183 impl GenericPath for Path {
184     #[inline]
185     fn as_vec<'a>(&'a self) -> &'a [u8] {
186         self.repr.as_slice()
187     }
188
189     fn into_vec(self) -> Vec<u8> {
190         self.repr
191     }
192
193     fn dirname<'a>(&'a self) -> &'a [u8] {
194         match self.sepidx {
195             None if b".." == self.repr => self.repr.as_slice(),
196             None => dot_static,
197             Some(0) => self.repr[..1],
198             Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(),
199             Some(idx) => self.repr[..idx]
200         }
201     }
202
203     fn filename<'a>(&'a self) -> Option<&'a [u8]> {
204         match self.sepidx {
205             None if b"." == self.repr ||
206                 b".." == self.repr => None,
207             None => Some(self.repr.as_slice()),
208             Some(idx) if self.repr[idx+1..] == b".." => None,
209             Some(0) if self.repr[1..].is_empty() => None,
210             Some(idx) => Some(self.repr[idx+1..])
211         }
212     }
213
214     fn pop(&mut self) -> bool {
215         match self.sepidx {
216             None if b"." == self.repr => false,
217             None => {
218                 self.repr = vec![b'.'];
219                 self.sepidx = None;
220                 true
221             }
222             Some(0) if b"/" == self.repr => false,
223             Some(idx) => {
224                 if idx == 0 {
225                     self.repr.truncate(idx+1);
226                 } else {
227                     self.repr.truncate(idx);
228                 }
229                 self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
230                 true
231             }
232         }
233     }
234
235     fn root_path(&self) -> Option<Path> {
236         if self.is_absolute() {
237             Some(Path::new("/"))
238         } else {
239             None
240         }
241     }
242
243     #[inline]
244     fn is_absolute(&self) -> bool {
245         self.repr[0] == SEP_BYTE
246     }
247
248     fn is_ancestor_of(&self, other: &Path) -> bool {
249         if self.is_absolute() != other.is_absolute() {
250             false
251         } else {
252             let mut ita = self.components();
253             let mut itb = other.components();
254             if b"." == self.repr {
255                 return match itb.next() {
256                     None => true,
257                     Some(b) => b != b".."
258                 };
259             }
260             loop {
261                 match (ita.next(), itb.next()) {
262                     (None, _) => break,
263                     (Some(a), Some(b)) if a == b => { continue },
264                     (Some(a), _) if a == b".." => {
265                         // if ita contains only .. components, it's an ancestor
266                         return ita.all(|x| x == b"..");
267                     }
268                     _ => return false
269                 }
270             }
271             true
272         }
273     }
274
275     fn path_relative_from(&self, base: &Path) -> Option<Path> {
276         if self.is_absolute() != base.is_absolute() {
277             if self.is_absolute() {
278                 Some(self.clone())
279             } else {
280                 None
281             }
282         } else {
283             let mut ita = self.components();
284             let mut itb = base.components();
285             let mut comps = vec![];
286             loop {
287                 match (ita.next(), itb.next()) {
288                     (None, None) => break,
289                     (Some(a), None) => {
290                         comps.push(a);
291                         comps.extend(ita.by_ref());
292                         break;
293                     }
294                     (None, _) => comps.push(dot_dot_static),
295                     (Some(a), Some(b)) if comps.is_empty() && a == b => (),
296                     (Some(a), Some(b)) if b == b"." => comps.push(a),
297                     (Some(_), Some(b)) if b == b".." => return None,
298                     (Some(a), Some(_)) => {
299                         comps.push(dot_dot_static);
300                         for _ in itb {
301                             comps.push(dot_dot_static);
302                         }
303                         comps.push(a);
304                         comps.extend(ita.by_ref());
305                         break;
306                     }
307                 }
308             }
309             Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
310         }
311     }
312
313     fn ends_with_path(&self, child: &Path) -> bool {
314         if !child.is_relative() { return false; }
315         let mut selfit = self.components().rev();
316         let mut childit = child.components().rev();
317         loop {
318             match (selfit.next(), childit.next()) {
319                 (Some(a), Some(b)) => if a != b { return false; },
320                 (Some(_), None) => break,
321                 (None, Some(_)) => return false,
322                 (None, None) => break
323             }
324         }
325         true
326     }
327 }
328
329 impl Path {
330     /// Returns a new Path from a byte vector or string
331     ///
332     /// # Panics
333     ///
334     /// Panics the task if the vector contains a NUL.
335     #[inline]
336     pub fn new<T: BytesContainer>(path: T) -> Path {
337         GenericPath::new(path)
338     }
339
340     /// Returns a new Path from a byte vector or string, if possible
341     #[inline]
342     pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
343         GenericPath::new_opt(path)
344     }
345
346     /// Returns a normalized byte vector representation of a path, by removing all empty
347     /// components, and unnecessary . and .. components.
348     fn normalize<Sized? V: AsSlice<u8>>(v: &V) -> Vec<u8> {
349         // borrowck is being very picky
350         let val = {
351             let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
352             let v_ = if is_abs { v.as_slice()[1..] } else { v.as_slice() };
353             let comps = normalize_helper(v_, is_abs);
354             match comps {
355                 None => None,
356                 Some(comps) => {
357                     if is_abs && comps.is_empty() {
358                         Some(vec![SEP_BYTE])
359                     } else {
360                         let n = if is_abs { comps.len() } else { comps.len() - 1} +
361                                 comps.iter().map(|v| v.len()).sum();
362                         let mut v = Vec::with_capacity(n);
363                         let mut it = comps.into_iter();
364                         if !is_abs {
365                             match it.next() {
366                                 None => (),
367                                 Some(comp) => v.push_all(comp)
368                             }
369                         }
370                         for comp in it {
371                             v.push(SEP_BYTE);
372                             v.push_all(comp);
373                         }
374                         Some(v)
375                     }
376                 }
377             }
378         };
379         match val {
380             None => v.as_slice().to_vec(),
381             Some(val) => val
382         }
383     }
384
385     /// Returns an iterator that yields each component of the path in turn.
386     /// Does not distinguish between absolute and relative paths, e.g.
387     /// /a/b/c and a/b/c yield the same set of components.
388     /// A path of "/" yields no components. A path of "." yields one component.
389     pub fn components<'a>(&'a self) -> Components<'a> {
390         let v = if self.repr[0] == SEP_BYTE {
391             self.repr[1..]
392         } else { self.repr.as_slice() };
393         let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr
394         let mut ret = v.split(is_sep_byte);
395         if v.is_empty() {
396             // consume the empty "" component
397             ret.next();
398         }
399         ret
400     }
401
402     /// Returns an iterator that yields each component of the path as Option<&str>.
403     /// See components() for details.
404     pub fn str_components<'a>(&'a self) -> StrComponents<'a> {
405         fn from_utf8(s: &[u8]) -> Option<&str> {
406             str::from_utf8(s).ok()
407         }
408         let f: fn(&[u8]) -> Option<&str> = from_utf8; // coerce to fn ptr
409         self.components().map(f)
410     }
411 }
412
413 // None result means the byte vector didn't need normalizing
414 fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<Vec<&'a [u8]>> {
415     if is_abs && v.is_empty() {
416         return None;
417     }
418     let mut comps: Vec<&'a [u8]> = vec![];
419     let mut n_up = 0u;
420     let mut changed = false;
421     for comp in v.split(is_sep_byte) {
422         if comp.is_empty() { changed = true }
423         else if comp == b"." { changed = true }
424         else if comp == b".." {
425             if is_abs && comps.is_empty() { changed = true }
426             else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
427             else { comps.pop().unwrap(); changed = true }
428         } else { comps.push(comp) }
429     }
430     if changed {
431         if comps.is_empty() && !is_abs {
432             if v == b"." {
433                 return None;
434             }
435             comps.push(dot_static);
436         }
437         Some(comps)
438     } else {
439         None
440     }
441 }
442
443 #[allow(non_upper_case_globals)]
444 static dot_static: &'static [u8] = b".";
445 #[allow(non_upper_case_globals)]
446 static dot_dot_static: &'static [u8] = b"..";
447
448 #[cfg(test)]
449 mod tests {
450     use super::*;
451     use prelude::Option::{mod, Some, None};
452     use prelude::{Vec, Clone, AsSlice, SliceExt, CloneSliceExt, IteratorExt};
453     use prelude::{DoubleEndedIteratorExt, Str, StrExt, ToString, GenericPath};
454     use str;
455
456     macro_rules! t {
457         (s: $path:expr, $exp:expr) => (
458             {
459                 let path = $path;
460                 assert!(path.as_str() == Some($exp));
461             }
462         );
463         (v: $path:expr, $exp:expr) => (
464             {
465                 let path = $path;
466                 assert!(path.as_vec() == $exp);
467             }
468         )
469     }
470
471     #[test]
472     fn test_paths() {
473         let empty: &[u8] = &[];
474         t!(v: Path::new(empty), b".");
475         t!(v: Path::new(b"/"), b"/");
476         t!(v: Path::new(b"a/b/c"), b"a/b/c");
477         t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
478         t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
479         let p = Path::new(b"a/b/c\xFF");
480         assert!(p.as_str() == None);
481
482         t!(s: Path::new(""), ".");
483         t!(s: Path::new("/"), "/");
484         t!(s: Path::new("hi"), "hi");
485         t!(s: Path::new("hi/"), "hi");
486         t!(s: Path::new("/lib"), "/lib");
487         t!(s: Path::new("/lib/"), "/lib");
488         t!(s: Path::new("hi/there"), "hi/there");
489         t!(s: Path::new("hi/there.txt"), "hi/there.txt");
490
491         t!(s: Path::new("hi/there/"), "hi/there");
492         t!(s: Path::new("hi/../there"), "there");
493         t!(s: Path::new("../hi/there"), "../hi/there");
494         t!(s: Path::new("/../hi/there"), "/hi/there");
495         t!(s: Path::new("foo/.."), ".");
496         t!(s: Path::new("/foo/.."), "/");
497         t!(s: Path::new("/foo/../.."), "/");
498         t!(s: Path::new("/foo/../../bar"), "/bar");
499         t!(s: Path::new("/./hi/./there/."), "/hi/there");
500         t!(s: Path::new("/./hi/./there/./.."), "/hi");
501         t!(s: Path::new("foo/../.."), "..");
502         t!(s: Path::new("foo/../../.."), "../..");
503         t!(s: Path::new("foo/../../bar"), "../bar");
504
505         assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
506         assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
507                    b"/bar");
508
509         let p = Path::new(b"foo/bar\x80");
510         assert!(p.as_str() == None);
511     }
512
513     #[test]
514     fn test_opt_paths() {
515         assert!(Path::new_opt(b"foo/bar\0") == None);
516         t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
517         assert!(Path::new_opt("foo/bar\0") == None);
518         t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
519     }
520
521     #[test]
522     fn test_null_byte() {
523         use thread::Thread;
524         let result = Thread::spawn(move|| {
525             Path::new(b"foo/bar\0")
526         }).join();
527         assert!(result.is_err());
528
529         let result = Thread::spawn(move|| {
530             Path::new("test").set_filename(b"f\0o")
531         }).join();
532         assert!(result.is_err());
533
534         let result = Thread::spawn(move|| {
535             Path::new("test").push(b"f\0o");
536         }).join();
537         assert!(result.is_err());
538     }
539
540     #[test]
541     fn test_display_str() {
542         macro_rules! t {
543             ($path:expr, $disp:ident, $exp:expr) => (
544                 {
545                     let path = Path::new($path);
546                     assert!(path.$disp().to_string() == $exp);
547                 }
548             )
549         }
550         t!("foo", display, "foo");
551         t!(b"foo\x80", display, "foo\u{FFFD}");
552         t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
553         t!(b"foo\xFF/bar", filename_display, "bar");
554         t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
555         t!(b"/", filename_display, "");
556
557         macro_rules! t(
558             ($path:expr, $exp:expr) => (
559                 {
560                     let path = Path::new($path);
561                     let mo = path.display().as_cow();
562                     assert!(mo.as_slice() == $exp);
563                 }
564             );
565             ($path:expr, $exp:expr, filename) => (
566                 {
567                     let path = Path::new($path);
568                     let mo = path.filename_display().as_cow();
569                     assert!(mo.as_slice() == $exp);
570                 }
571             )
572         );
573
574         t!("foo", "foo");
575         t!(b"foo\x80", "foo\u{FFFD}");
576         t!(b"foo\xFFbar", "foo\u{FFFD}bar");
577         t!(b"foo\xFF/bar", "bar", filename);
578         t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
579         t!(b"/", "", filename);
580     }
581
582     #[test]
583     fn test_display() {
584         macro_rules! t(
585             ($path:expr, $exp:expr, $expf:expr) => (
586                 {
587                     let path = Path::new($path);
588                     let f = format!("{}", path.display());
589                     assert!(f == $exp);
590                     let f = format!("{}", path.filename_display());
591                     assert!(f == $expf);
592                 }
593             )
594         );
595
596         t!(b"foo", "foo", "foo");
597         t!(b"foo/bar", "foo/bar", "bar");
598         t!(b"/", "/", "");
599         t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
600         t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
601         t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
602         t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
603     }
604
605     #[test]
606     fn test_components() {
607         macro_rules! t(
608             (s: $path:expr, $op:ident, $exp:expr) => (
609                 {
610                     let path = Path::new($path);
611                     assert!(path.$op() == ($exp).as_bytes());
612                 }
613             );
614             (s: $path:expr, $op:ident, $exp:expr, opt) => (
615                 {
616                     let path = Path::new($path);
617                     let left = path.$op().map(|x| str::from_utf8(x).unwrap());
618                     assert!(left == $exp);
619                 }
620             );
621             (v: $path:expr, $op:ident, $exp:expr) => (
622                 {
623                     let arg = $path;
624                     let path = Path::new(arg);
625                     assert!(path.$op() == $exp);
626                 }
627             );
628         );
629
630         t!(v: b"a/b/c", filename, Some(b"c"));
631         t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
632         t!(v: b"a/b\xFF/c", filename, Some(b"c"));
633         t!(s: "a/b/c", filename, Some("c"), opt);
634         t!(s: "/a/b/c", filename, Some("c"), opt);
635         t!(s: "a", filename, Some("a"), opt);
636         t!(s: "/a", filename, Some("a"), opt);
637         t!(s: ".", filename, None, opt);
638         t!(s: "/", filename, None, opt);
639         t!(s: "..", filename, None, opt);
640         t!(s: "../..", filename, None, opt);
641
642         t!(v: b"a/b/c", dirname, b"a/b");
643         t!(v: b"a/b/c\xFF", dirname, b"a/b");
644         t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
645         t!(s: "a/b/c", dirname, "a/b");
646         t!(s: "/a/b/c", dirname, "/a/b");
647         t!(s: "a", dirname, ".");
648         t!(s: "/a", dirname, "/");
649         t!(s: ".", dirname, ".");
650         t!(s: "/", dirname, "/");
651         t!(s: "..", dirname, "..");
652         t!(s: "../..", dirname, "../..");
653
654         t!(v: b"hi/there.txt", filestem, Some(b"there"));
655         t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
656         t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
657         t!(s: "hi/there.txt", filestem, Some("there"), opt);
658         t!(s: "hi/there", filestem, Some("there"), opt);
659         t!(s: "there.txt", filestem, Some("there"), opt);
660         t!(s: "there", filestem, Some("there"), opt);
661         t!(s: ".", filestem, None, opt);
662         t!(s: "/", filestem, None, opt);
663         t!(s: "foo/.bar", filestem, Some(".bar"), opt);
664         t!(s: ".bar", filestem, Some(".bar"), opt);
665         t!(s: "..bar", filestem, Some("."), opt);
666         t!(s: "hi/there..txt", filestem, Some("there."), opt);
667         t!(s: "..", filestem, None, opt);
668         t!(s: "../..", filestem, None, opt);
669
670         t!(v: b"hi/there.txt", extension, Some(b"txt"));
671         t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
672         t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
673         t!(v: b"hi/there", extension, None);
674         t!(v: b"hi/there\x80", extension, None);
675         t!(s: "hi/there.txt", extension, Some("txt"), opt);
676         t!(s: "hi/there", extension, None, opt);
677         t!(s: "there.txt", extension, Some("txt"), opt);
678         t!(s: "there", extension, None, opt);
679         t!(s: ".", extension, None, opt);
680         t!(s: "/", extension, None, opt);
681         t!(s: "foo/.bar", extension, None, opt);
682         t!(s: ".bar", extension, None, opt);
683         t!(s: "..bar", extension, Some("bar"), opt);
684         t!(s: "hi/there..txt", extension, Some("txt"), opt);
685         t!(s: "..", extension, None, opt);
686         t!(s: "../..", extension, None, opt);
687     }
688
689     #[test]
690     fn test_push() {
691         macro_rules! t(
692             (s: $path:expr, $join:expr) => (
693                 {
694                     let path = $path;
695                     let join = $join;
696                     let mut p1 = Path::new(path);
697                     let p2 = p1.clone();
698                     p1.push(join);
699                     assert!(p1 == p2.join(join));
700                 }
701             )
702         );
703
704         t!(s: "a/b/c", "..");
705         t!(s: "/a/b/c", "d");
706         t!(s: "a/b", "c/d");
707         t!(s: "a/b", "/c/d");
708     }
709
710     #[test]
711     fn test_push_path() {
712         macro_rules! t(
713             (s: $path:expr, $push:expr, $exp:expr) => (
714                 {
715                     let mut p = Path::new($path);
716                     let push = Path::new($push);
717                     p.push(&push);
718                     assert!(p.as_str() == Some($exp));
719                 }
720             )
721         );
722
723         t!(s: "a/b/c", "d", "a/b/c/d");
724         t!(s: "/a/b/c", "d", "/a/b/c/d");
725         t!(s: "a/b", "c/d", "a/b/c/d");
726         t!(s: "a/b", "/c/d", "/c/d");
727         t!(s: "a/b", ".", "a/b");
728         t!(s: "a/b", "../c", "a/c");
729     }
730
731     #[test]
732     fn test_push_many() {
733         macro_rules! t(
734             (s: $path:expr, $push:expr, $exp:expr) => (
735                 {
736                     let mut p = Path::new($path);
737                     p.push_many(&$push);
738                     assert!(p.as_str() == Some($exp));
739                 }
740             );
741             (v: $path:expr, $push:expr, $exp:expr) => (
742                 {
743                     let mut p = Path::new($path);
744                     p.push_many(&$push);
745                     assert!(p.as_vec() == $exp);
746                 }
747             )
748         );
749
750         t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
751         t!(s: "a/b/c", ["d", "/e"], "/e");
752         t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
753         t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
754         t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
755         t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
756         t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
757     }
758
759     #[test]
760     fn test_pop() {
761         macro_rules! t(
762             (s: $path:expr, $left:expr, $right:expr) => (
763                 {
764                     let mut p = Path::new($path);
765                     let result = p.pop();
766                     assert!(p.as_str() == Some($left));
767                     assert!(result == $right);
768                 }
769             );
770             (b: $path:expr, $left:expr, $right:expr) => (
771                 {
772                     let mut p = Path::new($path);
773                     let result = p.pop();
774                     assert!(p.as_vec() == $left);
775                     assert!(result == $right);
776                 }
777             )
778         );
779
780         t!(b: b"a/b/c", b"a/b", true);
781         t!(b: b"a", b".", true);
782         t!(b: b".", b".", false);
783         t!(b: b"/a", b"/", true);
784         t!(b: b"/", b"/", false);
785         t!(b: b"a/b/c\x80", b"a/b", true);
786         t!(b: b"a/b\x80/c", b"a/b\x80", true);
787         t!(b: b"\xFF", b".", true);
788         t!(b: b"/\xFF", b"/", true);
789         t!(s: "a/b/c", "a/b", true);
790         t!(s: "a", ".", true);
791         t!(s: ".", ".", false);
792         t!(s: "/a", "/", true);
793         t!(s: "/", "/", false);
794     }
795
796     #[test]
797     fn test_root_path() {
798         assert!(Path::new(b"a/b/c").root_path() == None);
799         assert!(Path::new(b"/a/b/c").root_path() == Some(Path::new("/")));
800     }
801
802     #[test]
803     fn test_join() {
804         t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
805         t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
806         t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
807         t!(s: Path::new("a/b/c").join(".."), "a/b");
808         t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
809         t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
810         t!(s: Path::new("a/b").join("/c/d"), "/c/d");
811         t!(s: Path::new(".").join("a/b"), "a/b");
812         t!(s: Path::new("/").join("a/b"), "/a/b");
813     }
814
815     #[test]
816     fn test_join_path() {
817         macro_rules! t(
818             (s: $path:expr, $join:expr, $exp:expr) => (
819                 {
820                     let path = Path::new($path);
821                     let join = Path::new($join);
822                     let res = path.join(&join);
823                     assert!(res.as_str() == Some($exp));
824                 }
825             )
826         );
827
828         t!(s: "a/b/c", "..", "a/b");
829         t!(s: "/a/b/c", "d", "/a/b/c/d");
830         t!(s: "a/b", "c/d", "a/b/c/d");
831         t!(s: "a/b", "/c/d", "/c/d");
832         t!(s: ".", "a/b", "a/b");
833         t!(s: "/", "a/b", "/a/b");
834     }
835
836     #[test]
837     fn test_join_many() {
838         macro_rules! t(
839             (s: $path:expr, $join:expr, $exp:expr) => (
840                 {
841                     let path = Path::new($path);
842                     let res = path.join_many(&$join);
843                     assert!(res.as_str() == Some($exp));
844                 }
845             );
846             (v: $path:expr, $join:expr, $exp:expr) => (
847                 {
848                     let path = Path::new($path);
849                     let res = path.join_many(&$join);
850                     assert!(res.as_vec() == $exp);
851                 }
852             )
853         );
854
855         t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
856         t!(s: "a/b/c", ["..", "d"], "a/b/d");
857         t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
858         t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
859         t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
860         t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
861     }
862
863     #[test]
864     fn test_with_helpers() {
865         let empty: &[u8] = &[];
866
867         t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
868         t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
869         t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
870               b"/\xFF/\xCD");
871         t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
872         t!(s: Path::new(".").with_filename("foo"), "foo");
873         t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
874         t!(s: Path::new("/").with_filename("foo"), "/foo");
875         t!(s: Path::new("/a").with_filename("foo"), "/foo");
876         t!(s: Path::new("foo").with_filename("bar"), "bar");
877         t!(s: Path::new("/").with_filename("foo/"), "/foo");
878         t!(s: Path::new("/a").with_filename("foo/"), "/foo");
879         t!(s: Path::new("a/b/c").with_filename(""), "a/b");
880         t!(s: Path::new("a/b/c").with_filename("."), "a/b");
881         t!(s: Path::new("a/b/c").with_filename(".."), "a");
882         t!(s: Path::new("/a").with_filename(""), "/");
883         t!(s: Path::new("foo").with_filename(""), ".");
884         t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
885         t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
886         t!(s: Path::new("..").with_filename("foo"), "../foo");
887         t!(s: Path::new("../..").with_filename("foo"), "../../foo");
888         t!(s: Path::new("..").with_filename(""), "..");
889         t!(s: Path::new("../..").with_filename(""), "../..");
890
891         t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
892               b"hi/there\x80.exe");
893         t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
894               b"hi/there.\xFF");
895         t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
896               b"hi/there\x80.\xFF");
897         t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
898         t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
899         t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
900         t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
901         t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
902         t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
903         t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
904         t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
905         t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
906         t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
907         t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
908         t!(s: Path::new("/").with_extension("txt"), "/");
909         t!(s: Path::new("/").with_extension("."), "/");
910         t!(s: Path::new("/").with_extension(".."), "/");
911         t!(s: Path::new(".").with_extension("txt"), ".");
912     }
913
914     #[test]
915     fn test_setters() {
916         macro_rules! t(
917             (s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
918                 {
919                     let path = $path;
920                     let arg = $arg;
921                     let mut p1 = Path::new(path);
922                     p1.$set(arg);
923                     let p2 = Path::new(path);
924                     assert!(p1 == p2.$with(arg));
925                 }
926             );
927             (v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
928                 {
929                     let path = $path;
930                     let arg = $arg;
931                     let mut p1 = Path::new(path);
932                     p1.$set(arg);
933                     let p2 = Path::new(path);
934                     assert!(p1 == p2.$with(arg));
935                 }
936             )
937         );
938
939         t!(v: b"a/b/c", set_filename, with_filename, b"d");
940         t!(v: b"/", set_filename, with_filename, b"foo");
941         t!(v: b"\x80", set_filename, with_filename, b"\xFF");
942         t!(s: "a/b/c", set_filename, with_filename, "d");
943         t!(s: "/", set_filename, with_filename, "foo");
944         t!(s: ".", set_filename, with_filename, "foo");
945         t!(s: "a/b", set_filename, with_filename, "");
946         t!(s: "a", set_filename, with_filename, "");
947
948         t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
949         t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
950         t!(s: "hi/there.txt", set_extension, with_extension, "exe");
951         t!(s: "hi/there.", set_extension, with_extension, "txt");
952         t!(s: "hi/there", set_extension, with_extension, "txt");
953         t!(s: "hi/there.txt", set_extension, with_extension, "");
954         t!(s: "hi/there", set_extension, with_extension, "");
955         t!(s: ".", set_extension, with_extension, "txt");
956     }
957
958     #[test]
959     fn test_getters() {
960         macro_rules! t(
961             (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
962                 {
963                     let path = $path;
964                     let filename = $filename;
965                     assert!(path.filename_str() == filename,
966                             "{}.filename_str(): Expected `{}`, found {}",
967                             path.as_str().unwrap(), filename, path.filename_str());
968                     let dirname = $dirname;
969                     assert!(path.dirname_str() == dirname,
970                             "`{}`.dirname_str(): Expected `{}`, found `{}`",
971                             path.as_str().unwrap(), dirname, path.dirname_str());
972                     let filestem = $filestem;
973                     assert!(path.filestem_str() == filestem,
974                             "`{}`.filestem_str(): Expected `{}`, found `{}`",
975                             path.as_str().unwrap(), filestem, path.filestem_str());
976                     let ext = $ext;
977                     assert!(path.extension_str() == ext,
978                             "`{}`.extension_str(): Expected `{}`, found `{}`",
979                             path.as_str().unwrap(), ext, path.extension_str());
980                 }
981             );
982             (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
983                 {
984                     let path = $path;
985                     assert!(path.filename() == $filename);
986                     assert!(path.dirname() == $dirname);
987                     assert!(path.filestem() == $filestem);
988                     assert!(path.extension() == $ext);
989                 }
990             )
991         );
992
993         t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
994         t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
995         t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
996               Some(b"there"), Some(b"\xFF"));
997         t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
998         t!(s: Path::new("."), None, Some("."), None, None);
999         t!(s: Path::new("/"), None, Some("/"), None, None);
1000         t!(s: Path::new(".."), None, Some(".."), None, None);
1001         t!(s: Path::new("../.."), None, Some("../.."), None, None);
1002         t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
1003               Some("there"), Some("txt"));
1004         t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
1005         t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
1006               Some("there"), Some(""));
1007         t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
1008         t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
1009               Some("."), Some("there"));
1010         t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
1011         t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
1012         t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
1013         t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
1014     }
1015
1016     #[test]
1017     fn test_dir_path() {
1018         t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
1019         t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
1020         t!(s: Path::new("hi/there").dir_path(), "hi");
1021         t!(s: Path::new("hi").dir_path(), ".");
1022         t!(s: Path::new("/hi").dir_path(), "/");
1023         t!(s: Path::new("/").dir_path(), "/");
1024         t!(s: Path::new("..").dir_path(), "..");
1025         t!(s: Path::new("../..").dir_path(), "../..");
1026     }
1027
1028     #[test]
1029     fn test_is_absolute() {
1030         macro_rules! t(
1031             (s: $path:expr, $abs:expr, $rel:expr) => (
1032                 {
1033                     let path = Path::new($path);
1034                     assert_eq!(path.is_absolute(), $abs);
1035                     assert_eq!(path.is_relative(), $rel);
1036                 }
1037             )
1038         );
1039         t!(s: "a/b/c", false, true);
1040         t!(s: "/a/b/c", true, false);
1041         t!(s: "a", false, true);
1042         t!(s: "/a", true, false);
1043         t!(s: ".", false, true);
1044         t!(s: "/", true, false);
1045         t!(s: "..", false, true);
1046         t!(s: "../..", false, true);
1047     }
1048
1049     #[test]
1050     fn test_is_ancestor_of() {
1051         macro_rules! t(
1052             (s: $path:expr, $dest:expr, $exp:expr) => (
1053                 {
1054                     let path = Path::new($path);
1055                     let dest = Path::new($dest);
1056                     assert_eq!(path.is_ancestor_of(&dest), $exp);
1057                 }
1058             )
1059         );
1060
1061         t!(s: "a/b/c", "a/b/c/d", true);
1062         t!(s: "a/b/c", "a/b/c", true);
1063         t!(s: "a/b/c", "a/b", false);
1064         t!(s: "/a/b/c", "/a/b/c", true);
1065         t!(s: "/a/b", "/a/b/c", true);
1066         t!(s: "/a/b/c/d", "/a/b/c", false);
1067         t!(s: "/a/b", "a/b/c", false);
1068         t!(s: "a/b", "/a/b/c", false);
1069         t!(s: "a/b/c", "a/b/d", false);
1070         t!(s: "../a/b/c", "a/b/c", false);
1071         t!(s: "a/b/c", "../a/b/c", false);
1072         t!(s: "a/b/c", "a/b/cd", false);
1073         t!(s: "a/b/cd", "a/b/c", false);
1074         t!(s: "../a/b", "../a/b/c", true);
1075         t!(s: ".", "a/b", true);
1076         t!(s: ".", ".", true);
1077         t!(s: "/", "/", true);
1078         t!(s: "/", "/a/b", true);
1079         t!(s: "..", "a/b", true);
1080         t!(s: "../..", "a/b", true);
1081     }
1082
1083     #[test]
1084     fn test_ends_with_path() {
1085         macro_rules! t(
1086             (s: $path:expr, $child:expr, $exp:expr) => (
1087                 {
1088                     let path = Path::new($path);
1089                     let child = Path::new($child);
1090                     assert_eq!(path.ends_with_path(&child), $exp);
1091                 }
1092             );
1093             (v: $path:expr, $child:expr, $exp:expr) => (
1094                 {
1095                     let path = Path::new($path);
1096                     let child = Path::new($child);
1097                     assert_eq!(path.ends_with_path(&child), $exp);
1098                 }
1099             )
1100         );
1101
1102         t!(s: "a/b/c", "c", true);
1103         t!(s: "a/b/c", "d", false);
1104         t!(s: "foo/bar/quux", "bar", false);
1105         t!(s: "foo/bar/quux", "barquux", false);
1106         t!(s: "a/b/c", "b/c", true);
1107         t!(s: "a/b/c", "a/b/c", true);
1108         t!(s: "a/b/c", "foo/a/b/c", false);
1109         t!(s: "/a/b/c", "a/b/c", true);
1110         t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
1111         t!(s: "/a/b/c", "foo/a/b/c", false);
1112         t!(s: "a/b/c", "", false);
1113         t!(s: "", "", true);
1114         t!(s: "/a/b/c", "d/e/f", false);
1115         t!(s: "a/b/c", "a/b", false);
1116         t!(s: "a/b/c", "b", false);
1117         t!(v: b"a/b/c", b"b/c", true);
1118         t!(v: b"a/b/\xFF", b"\xFF", true);
1119         t!(v: b"a/b/\xFF", b"b/\xFF", true);
1120     }
1121
1122     #[test]
1123     fn test_path_relative_from() {
1124         macro_rules! t(
1125             (s: $path:expr, $other:expr, $exp:expr) => (
1126                 {
1127                     let path = Path::new($path);
1128                     let other = Path::new($other);
1129                     let res = path.path_relative_from(&other);
1130                     assert_eq!(res.as_ref().and_then(|x| x.as_str()), $exp);
1131                 }
1132             )
1133         );
1134
1135         t!(s: "a/b/c", "a/b", Some("c"));
1136         t!(s: "a/b/c", "a/b/d", Some("../c"));
1137         t!(s: "a/b/c", "a/b/c/d", Some(".."));
1138         t!(s: "a/b/c", "a/b/c", Some("."));
1139         t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
1140         t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
1141         t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
1142         t!(s: "a/b/c", "/a/b/c", None);
1143         t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
1144         t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
1145         t!(s: "/a/b/c", "/a/b", Some("c"));
1146         t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
1147         t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
1148         t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
1149         t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
1150         t!(s: ".", "a", Some(".."));
1151         t!(s: ".", "a/b", Some("../.."));
1152         t!(s: ".", ".", Some("."));
1153         t!(s: "a", ".", Some("a"));
1154         t!(s: "a/b", ".", Some("a/b"));
1155         t!(s: "..", ".", Some(".."));
1156         t!(s: "a/b/c", "a/b/c", Some("."));
1157         t!(s: "/a/b/c", "/a/b/c", Some("."));
1158         t!(s: "/", "/", Some("."));
1159         t!(s: "/", ".", Some("/"));
1160         t!(s: "../../a", "b", Some("../../../a"));
1161         t!(s: "a", "../../b", None);
1162         t!(s: "../../a", "../../b", Some("../a"));
1163         t!(s: "../../a", "../../a/b", Some(".."));
1164         t!(s: "../../a/b", "../../a", Some("b"));
1165     }
1166
1167     #[test]
1168     fn test_components_iter() {
1169         macro_rules! t(
1170             (s: $path:expr, $exp:expr) => (
1171                 {
1172                     let path = Path::new($path);
1173                     let comps = path.components().collect::<Vec<&[u8]>>();
1174                     let exp: &[&str] = &$exp;
1175                     let exps = exp.iter().map(|x| x.as_bytes()).collect::<Vec<&[u8]>>();
1176                     assert!(comps == exps, "components: Expected {}, found {}",
1177                             comps, exps);
1178                     let comps = path.components().rev().collect::<Vec<&[u8]>>();
1179                     let exps = exps.into_iter().rev().collect::<Vec<&[u8]>>();
1180                     assert!(comps == exps, "rev_components: Expected {}, found {}",
1181                             comps, exps);
1182                 }
1183             );
1184             (b: $arg:expr, [$($exp:expr),*]) => (
1185                 {
1186                     let path = Path::new($arg);
1187                     let comps = path.components().collect::<Vec<&[u8]>>();
1188                     let exp: &[&[u8]] = &[$($exp),*];
1189                     assert_eq!(comps, exp);
1190                     let comps = path.components().rev().collect::<Vec<&[u8]>>();
1191                     let exp = exp.iter().rev().map(|&x|x).collect::<Vec<&[u8]>>();
1192                     assert_eq!(comps, exp)
1193                 }
1194             )
1195         );
1196
1197         t!(b: b"a/b/c", [b"a", b"b", b"c"]);
1198         t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
1199         t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
1200         t!(s: "a/b/c", ["a", "b", "c"]);
1201         t!(s: "a/b/d", ["a", "b", "d"]);
1202         t!(s: "a/b/cd", ["a", "b", "cd"]);
1203         t!(s: "/a/b/c", ["a", "b", "c"]);
1204         t!(s: "a", ["a"]);
1205         t!(s: "/a", ["a"]);
1206         t!(s: "/", []);
1207         t!(s: ".", ["."]);
1208         t!(s: "..", [".."]);
1209         t!(s: "../..", ["..", ".."]);
1210         t!(s: "../../foo", ["..", "..", "foo"]);
1211     }
1212
1213     #[test]
1214     fn test_str_components() {
1215         macro_rules! t(
1216             (b: $arg:expr, $exp:expr) => (
1217                 {
1218                     let path = Path::new($arg);
1219                     let comps = path.str_components().collect::<Vec<Option<&str>>>();
1220                     let exp: &[Option<&str>] = &$exp;
1221                     assert_eq!(comps, exp);
1222                     let comps = path.str_components().rev().collect::<Vec<Option<&str>>>();
1223                     let exp = exp.iter().rev().map(|&x|x).collect::<Vec<Option<&str>>>();
1224                     assert_eq!(comps, exp);
1225                 }
1226             )
1227         );
1228
1229         t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
1230         t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
1231         t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
1232         // str_components is a wrapper around components, so no need to do
1233         // the full set of tests
1234     }
1235 }
1236
1237 #[cfg(test)]
1238 mod bench {
1239     extern crate test;
1240     use self::test::Bencher;
1241     use super::*;
1242     use prelude::{Clone, GenericPath};
1243
1244     #[bench]
1245     fn join_home_dir(b: &mut Bencher) {
1246         let posix_path = Path::new("/");
1247         b.iter(|| {
1248             posix_path.join("home");
1249         });
1250     }
1251
1252     #[bench]
1253     fn join_abs_path_home_dir(b: &mut Bencher) {
1254         let posix_path = Path::new("/");
1255         b.iter(|| {
1256             posix_path.join("/home");
1257         });
1258     }
1259
1260     #[bench]
1261     fn join_many_home_dir(b: &mut Bencher) {
1262         let posix_path = Path::new("/");
1263         b.iter(|| {
1264             posix_path.join_many(&["home"]);
1265         });
1266     }
1267
1268     #[bench]
1269     fn join_many_abs_path_home_dir(b: &mut Bencher) {
1270         let posix_path = Path::new("/");
1271         b.iter(|| {
1272             posix_path.join_many(&["/home"]);
1273         });
1274     }
1275
1276     #[bench]
1277     fn push_home_dir(b: &mut Bencher) {
1278         let mut posix_path = Path::new("/");
1279         b.iter(|| {
1280             posix_path.push("home");
1281         });
1282     }
1283
1284     #[bench]
1285     fn push_abs_path_home_dir(b: &mut Bencher) {
1286         let mut posix_path = Path::new("/");
1287         b.iter(|| {
1288             posix_path.push("/home");
1289         });
1290     }
1291
1292     #[bench]
1293     fn push_many_home_dir(b: &mut Bencher) {
1294         let mut posix_path = Path::new("/");
1295         b.iter(|| {
1296             posix_path.push_many(&["home"]);
1297         });
1298     }
1299
1300     #[bench]
1301     fn push_many_abs_path_home_dir(b: &mut Bencher) {
1302         let mut posix_path = Path::new("/");
1303         b.iter(|| {
1304             posix_path.push_many(&["/home"]);
1305         });
1306     }
1307
1308     #[bench]
1309     fn ends_with_path_home_dir(b: &mut Bencher) {
1310         let posix_home_path = Path::new("/home");
1311         b.iter(|| {
1312             posix_home_path.ends_with_path(&Path::new("home"));
1313         });
1314     }
1315
1316     #[bench]
1317     fn ends_with_path_missmatch_jome_home(b: &mut Bencher) {
1318         let posix_home_path = Path::new("/home");
1319         b.iter(|| {
1320             posix_home_path.ends_with_path(&Path::new("jome"));
1321         });
1322     }
1323
1324     #[bench]
1325     fn is_ancestor_of_path_with_10_dirs(b: &mut Bencher) {
1326         let path = Path::new("/home/1/2/3/4/5/6/7/8/9");
1327         let mut sub = path.clone();
1328         sub.pop();
1329         b.iter(|| {
1330             path.is_ancestor_of(&sub);
1331         });
1332     }
1333
1334     #[bench]
1335     fn path_relative_from_forward(b: &mut Bencher) {
1336         let path = Path::new("/a/b/c");
1337         let mut other = path.clone();
1338         other.pop();
1339         b.iter(|| {
1340             path.path_relative_from(&other);
1341         });
1342     }
1343
1344     #[bench]
1345     fn path_relative_from_same_level(b: &mut Bencher) {
1346         let path = Path::new("/a/b/c");
1347         let mut other = path.clone();
1348         other.pop();
1349         other.push("d");
1350         b.iter(|| {
1351             path.path_relative_from(&other);
1352         });
1353     }
1354
1355     #[bench]
1356     fn path_relative_from_backward(b: &mut Bencher) {
1357         let path = Path::new("/a/b");
1358         let mut other = path.clone();
1359         other.push("c");
1360         b.iter(|| {
1361             path.path_relative_from(&other);
1362         });
1363     }
1364 }