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