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