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