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