Skip to content

Commit

Permalink
Rollup merge of rust-lang#41493 - scottmcm:fix-step-replace, r=sfackler
Browse files Browse the repository at this point in the history
Step::replace_one should put a one, not a zero (Issue rust-lang#41492)

Turns out all six of the replace_* impls were backwards.
  • Loading branch information
frewsxcv authored Apr 26, 2017
2 parents 91ebac3 + f8c6436 commit f11b996
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ macro_rules! step_impl_unsigned {

#[inline]
fn replace_one(&mut self) -> Self {
mem::replace(self, 0)
mem::replace(self, 1)
}

#[inline]
fn replace_zero(&mut self) -> Self {
mem::replace(self, 1)
mem::replace(self, 0)
}

#[inline]
Expand Down Expand Up @@ -157,12 +157,12 @@ macro_rules! step_impl_signed {

#[inline]
fn replace_one(&mut self) -> Self {
mem::replace(self, 0)
mem::replace(self, 1)
}

#[inline]
fn replace_zero(&mut self) -> Self {
mem::replace(self, 1)
mem::replace(self, 0)
}

#[inline]
Expand Down Expand Up @@ -206,12 +206,12 @@ macro_rules! step_impl_no_between {

#[inline]
fn replace_one(&mut self) -> Self {
mem::replace(self, 0)
mem::replace(self, 1)
}

#[inline]
fn replace_zero(&mut self) -> Self {
mem::replace(self, 1)
mem::replace(self, 0)
}

#[inline]
Expand Down
38 changes: 38 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,41 @@ fn test_chain_fold() {
assert_eq!(&[2, 3, 1, 2, 0], &result[..]);
}

#[test]
fn test_step_replace_unsigned() {
let mut x = 4u32;
let y = x.replace_zero();
assert_eq!(x, 0);
assert_eq!(y, 4);

x = 5;
let y = x.replace_one();
assert_eq!(x, 1);
assert_eq!(y, 5);
}

#[test]
fn test_step_replace_signed() {
let mut x = 4i32;
let y = x.replace_zero();
assert_eq!(x, 0);
assert_eq!(y, 4);

x = 5;
let y = x.replace_one();
assert_eq!(x, 1);
assert_eq!(y, 5);
}

#[test]
fn test_step_replace_no_between() {
let mut x = 4u128;
let y = x.replace_zero();
assert_eq!(x, 0);
assert_eq!(y, 4);

x = 5;
let y = x.replace_one();
assert_eq!(x, 1);
assert_eq!(y, 5);
}
2 changes: 2 additions & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![feature(fixed_size_array)]
#![feature(flt2dec)]
#![feature(fmt_internals)]
#![feature(i128_type)]
#![feature(iter_rfind)]
#![feature(libc)]
#![feature(nonzero)]
Expand All @@ -30,6 +31,7 @@
#![feature(sort_internals)]
#![feature(sort_unstable)]
#![feature(step_by)]
#![feature(step_trait)]
#![feature(test)]
#![feature(try_from)]
#![feature(unicode)]
Expand Down

0 comments on commit f11b996

Please sign in to comment.