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