Skip to content

Commit

Permalink
Auto merge of #28052 - Manishearth:rollup, r=Manishearth
Browse files Browse the repository at this point in the history
- Successful merges: #28010, #28013, #28022, #28029, #28033, #28039, #28045, #28048
- Failed merges:
  • Loading branch information
bors committed Aug 27, 2015
2 parents 79dd92f + a63cd9b commit 8dba06a
Show file tree
Hide file tree
Showing 21 changed files with 430 additions and 145 deletions.
15 changes: 14 additions & 1 deletion src/doc/trpl/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,26 @@ And that's reflected in the summary line:
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
```

We also get a non-zero status code:
We also get a non-zero status code. We can use `$?` on OS X and Linux:

```bash
$ echo $?
101
```

On Windows, if you’re using `cmd`:

```bash
> echo %ERRORLEVEL%
```

And if you’re using PowerShell:

```bash
> echo $LASTEXITCODE # the code itself
> echo $? # a boolean, fail or succeed
```

This is useful if you want to integrate `cargo test` into other tooling.

We can invert our test's failure with another attribute: `should_panic`:
Expand Down
45 changes: 45 additions & 0 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,51 @@ macro_rules! unreachable {

/// A standardised placeholder for marking unfinished code. It panics with the
/// message `"not yet implemented"` when executed.
///
/// This can be useful if you are prototyping and are just looking to have your
/// code typecheck, or if you're implementing a trait that requires multiple
/// methods, and you're only planning on using one of them.
///
/// # Examples
///
/// Here's an example of some in-progress code. We have a trait `Foo`:
///
/// ```
/// trait Foo {
/// fn bar(&self);
/// fn baz(&self);
/// }
/// ```
///
/// We want to implement `Foo` on one of our types, but we also want to work on
/// just `bar()` first. In order for our code to compile, we need to implement
/// `baz()`, so we can use `unimplemented!`:
///
/// ```
/// # trait Foo {
/// # fn foo(&self);
/// # fn bar(&self);
/// # }
/// struct MyStruct;
///
/// impl Foo for MyStruct {
/// fn foo(&self) {
/// // implementation goes here
/// }
///
/// fn bar(&self) {
/// // let's not worry about implementing bar() for now
/// unimplemented!();
/// }
/// }
///
/// fn main() {
/// let s = MyStruct;
/// s.foo();
///
/// // we aren't even using bar() yet, so this is fine.
/// }
/// ```
#[macro_export]
#[unstable(feature = "core",
reason = "relationship with panic is unclear")]
Expand Down
20 changes: 20 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,26 @@ impl<T> IntoIterator for Option<T> {
}
}

#[stable(since = "1.4.0", feature = "option_iter")]
impl<'a, T> IntoIterator for &'a Option<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}

#[stable(since = "1.4.0", feature = "option_iter")]
impl<'a, T> IntoIterator for &'a mut Option<T> {
type Item = &'a mut T;
type IntoIter = IterMut<'a, T>;

fn into_iter(mut self) -> IterMut<'a, T> {
self.iter_mut()
}
}

/////////////////////////////////////////////////////////////////////////////
// The Option Iterators
/////////////////////////////////////////////////////////////////////////////
Expand Down
20 changes: 20 additions & 0 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,26 @@ impl<T, E> IntoIterator for Result<T, E> {
}
}

#[stable(since = "1.4.0", feature = "result_iter")]
impl<'a, T, E> IntoIterator for &'a Result<T, E> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}

#[stable(since = "1.4.0", feature = "result_iter")]
impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
type Item = &'a mut T;
type IntoIter = IterMut<'a, T>;

fn into_iter(mut self) -> IterMut<'a, T> {
self.iter_mut()
}
}

/////////////////////////////////////////////////////////////////////////////
// The Result Iterators
/////////////////////////////////////////////////////////////////////////////
Expand Down
53 changes: 26 additions & 27 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use core::iter::*;
use core::iter::order::*;
use core::{i8, i16, isize};
use core::usize;

Expand All @@ -21,51 +20,51 @@ fn test_lt() {
let xs = [1,2,3];
let ys = [1,2,0];

assert!(!lt(xs.iter(), ys.iter()));
assert!(!le(xs.iter(), ys.iter()));
assert!( gt(xs.iter(), ys.iter()));
assert!( ge(xs.iter(), ys.iter()));
assert!(!xs.iter().lt(ys.iter()));
assert!(!xs.iter().le(ys.iter()));
assert!( xs.iter().gt(ys.iter()));
assert!( xs.iter().ge(ys.iter()));

assert!( lt(ys.iter(), xs.iter()));
assert!( le(ys.iter(), xs.iter()));
assert!(!gt(ys.iter(), xs.iter()));
assert!(!ge(ys.iter(), xs.iter()));
assert!( ys.iter().lt(xs.iter()));
assert!( ys.iter().le(xs.iter()));
assert!(!ys.iter().gt(xs.iter()));
assert!(!ys.iter().ge(xs.iter()));

assert!( lt(empty.iter(), xs.iter()));
assert!( le(empty.iter(), xs.iter()));
assert!(!gt(empty.iter(), xs.iter()));
assert!(!ge(empty.iter(), xs.iter()));
assert!( empty.iter().lt(xs.iter()));
assert!( empty.iter().le(xs.iter()));
assert!(!empty.iter().gt(xs.iter()));
assert!(!empty.iter().ge(xs.iter()));

// Sequence with NaN
let u = [1.0f64, 2.0];
let v = [0.0f64/0.0, 3.0];

assert!(!lt(u.iter(), v.iter()));
assert!(!le(u.iter(), v.iter()));
assert!(!gt(u.iter(), v.iter()));
assert!(!ge(u.iter(), v.iter()));
assert!(!u.iter().lt(v.iter()));
assert!(!u.iter().le(v.iter()));
assert!(!u.iter().gt(v.iter()));
assert!(!u.iter().ge(v.iter()));

let a = [0.0f64/0.0];
let b = [1.0f64];
let c = [2.0f64];

assert!(lt(a.iter(), b.iter()) == (a[0] < b[0]));
assert!(le(a.iter(), b.iter()) == (a[0] <= b[0]));
assert!(gt(a.iter(), b.iter()) == (a[0] > b[0]));
assert!(ge(a.iter(), b.iter()) == (a[0] >= b[0]));
assert!(a.iter().lt(b.iter()) == (a[0] < b[0]));
assert!(a.iter().le(b.iter()) == (a[0] <= b[0]));
assert!(a.iter().gt(b.iter()) == (a[0] > b[0]));
assert!(a.iter().ge(b.iter()) == (a[0] >= b[0]));

assert!(lt(c.iter(), b.iter()) == (c[0] < b[0]));
assert!(le(c.iter(), b.iter()) == (c[0] <= b[0]));
assert!(gt(c.iter(), b.iter()) == (c[0] > b[0]));
assert!(ge(c.iter(), b.iter()) == (c[0] >= b[0]));
assert!(c.iter().lt(b.iter()) == (c[0] < b[0]));
assert!(c.iter().le(b.iter()) == (c[0] <= b[0]));
assert!(c.iter().gt(b.iter()) == (c[0] > b[0]));
assert!(c.iter().ge(b.iter()) == (c[0] >= b[0]));
}

#[test]
fn test_multi_iter() {
let xs = [1,2,3,4];
let ys = [4,3,2,1];
assert!(eq(xs.iter(), ys.iter().rev()));
assert!(lt(xs.iter(), xs.iter().skip(2)));
assert!(xs.iter().eq(ys.iter().rev()));
assert!(xs.iter().lt(xs.iter().skip(2)));
}

#[test]
Expand Down
9 changes: 8 additions & 1 deletion src/libcoretest/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ fn test_iter() {
assert_eq!(it.next(), Some(&val));
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());

let mut it = (&x).into_iter();
assert_eq!(it.next(), Some(&val));
}

#[test]
fn test_mut_iter() {
let val = 5;
let mut val = 5;
let new_val = 11;

let mut x = Some(val);
Expand All @@ -205,6 +208,10 @@ fn test_mut_iter() {
assert!(it.next().is_none());
}
assert_eq!(x, Some(new_val));

let mut y = Some(val);
let mut it = (&mut y).into_iter();
assert_eq!(it.next(), Some(&mut val));
}

#[test]
Expand Down
33 changes: 33 additions & 0 deletions src/libcoretest/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,36 @@ pub fn test_expect_err() {
let err: Result<isize, &'static str> = Err("All good");
err.expect("Got expected error");
}

#[test]
pub fn test_iter() {
let ok: Result<isize, &'static str> = Ok(100);
let mut it = ok.iter();
assert_eq!(it.size_hint(), (1, Some(1)));
assert_eq!(it.next(), Some(&100));
assert_eq!(it.size_hint(), (0, Some(0)));
assert!(it.next().is_none());
assert_eq!((&ok).into_iter().next(), Some(&100));

let err: Result<isize, &'static str> = Err("error");
assert_eq!(err.iter().next(), None);
}

#[test]
pub fn test_iter_mut() {
let mut ok: Result<isize, &'static str> = Ok(100);
for loc in ok.iter_mut() {
*loc = 200;
}
assert_eq!(ok, Ok(200));
for loc in &mut ok {
*loc = 300;
}
assert_eq!(ok, Ok(300));

let mut err: Result<isize, &'static str> = Err("error");
for loc in err.iter_mut() {
*loc = 200;
}
assert_eq!(err, Err("error"));
}
Loading

0 comments on commit 8dba06a

Please sign in to comment.