revise macro in slice tests

This commit is contained in:
Michael Lamparski 2018-04-30 07:37:29 -04:00
parent 02b3da1200
commit 030aa9b112

View File

@ -376,9 +376,12 @@ fn test_windows_zip() {
assert_eq!(res, [14, 18, 22, 26]); assert_eq!(res, [14, 18, 22, 26]);
} }
// The current implementation of SliceIndex fails to handle methods
// orthogonally from range types; therefore, it is worth testing
// all of the indexing operations on each input.
mod slice_index { mod slice_index {
// Test a slicing operation that should succeed, // This checks all six indexing methods, given an input range that
// testing it on all of the indexing methods. // should succeed. (it is NOT suitable for testing invalid inputs)
macro_rules! assert_range_eq { macro_rules! assert_range_eq {
($arr:expr, $range:expr, $expected:expr) ($arr:expr, $range:expr, $expected:expr)
=> { => {
@ -423,7 +426,7 @@ mod slice_index {
// because if it can't, then what are we even doing here? // because if it can't, then what are we even doing here?
// //
// (Be aware this only demonstrates the ability to detect bugs // (Be aware this only demonstrates the ability to detect bugs
// in the FIRST method it calls, as the macro is not designed // in the FIRST method that panics, as the macro is not designed
// to be used in `should_panic`) // to be used in `should_panic`)
#[test] #[test]
#[should_panic(expected = "out of range")] #[should_panic(expected = "out of range")]
@ -446,30 +449,29 @@ mod slice_index {
// and `None` test cases for get/get_mut. // and `None` test cases for get/get_mut.
macro_rules! panic_cases { macro_rules! panic_cases {
($( ($(
mod $case_name:ident { // each test case needs a unique name to namespace the tests
let DATA: $Data: ty = $data:expr; in mod $case_name:ident {
data: $data:expr;
// optional: // optional:
// //
// a similar input for which DATA[input] succeeds, and the corresponding // one or more similar inputs for which data[input] succeeds,
// output as an array. This helps validate "critical points" where an // and the corresponding output as an array. This helps validate
// input range straddles the boundary between valid and invalid. // "critical points" where an input range straddles the boundary
// between valid and invalid.
// (such as the input `len..len`, which is just barely valid) // (such as the input `len..len`, which is just barely valid)
$( $(
let GOOD_INPUT = $good:expr; good: data[$good:expr] == $output:expr;
let GOOD_OUTPUT = $output:expr;
)* )*
let BAD_INPUT = $bad:expr; bad: data[$bad:expr];
const EXPECT_MSG = $expect_msg:expr; message: $expect_msg:expr;
!!generate_tests!!
} }
)*) => {$( )*) => {$(
mod $case_name { mod $case_name {
#[test] #[test]
fn pass() { fn pass() {
let mut v: $Data = $data; let mut v = $data;
$( assert_range_eq!($data, $good, $output); )* $( assert_range_eq!($data, $good, $output); )*
@ -487,7 +489,7 @@ mod slice_index {
#[test] #[test]
#[should_panic(expected = $expect_msg)] #[should_panic(expected = $expect_msg)]
fn index_fail() { fn index_fail() {
let v: $Data = $data; let v = $data;
let v: &[_] = &v; let v: &[_] = &v;
let _v = &v[$bad]; let _v = &v[$bad];
} }
@ -495,7 +497,7 @@ mod slice_index {
#[test] #[test]
#[should_panic(expected = $expect_msg)] #[should_panic(expected = $expect_msg)]
fn index_mut_fail() { fn index_mut_fail() {
let mut v: $Data = $data; let mut v = $data;
let v: &mut [_] = &mut v; let v: &mut [_] = &mut v;
let _v = &mut v[$bad]; let _v = &mut v[$bad];
} }
@ -516,112 +518,80 @@ mod slice_index {
} }
panic_cases! { panic_cases! {
mod rangefrom_len { in mod rangefrom_len {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = 6..; good: data[6..] == [];
let GOOD_OUTPUT = []; bad: data[7..];
message: "but ends at"; // perhaps not ideal
let BAD_INPUT = 7..;
const EXPECT_MSG = "but ends at"; // perhaps not ideal
!!generate_tests!!
} }
mod rangeto_len { in mod rangeto_len {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = ..6; good: data[..6] == [0, 1, 2, 3, 4, 5];
let GOOD_OUTPUT = [0, 1, 2, 3, 4, 5]; bad: data[..7];
message: "out of range";
let BAD_INPUT = ..7;
const EXPECT_MSG = "out of range";
!!generate_tests!!
} }
mod rangetoinclusive_len { in mod rangetoinclusive_len {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = ..=5; good: data[..=5] == [0, 1, 2, 3, 4, 5];
let GOOD_OUTPUT = [0, 1, 2, 3, 4, 5]; bad: data[..=6];
message: "out of range";
let BAD_INPUT = ..=6;
const EXPECT_MSG = "out of range";
!!generate_tests!!
} }
mod range_len_len { in mod range_len_len {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = 6..6; good: data[6..6] == [];
let GOOD_OUTPUT = []; bad: data[7..7];
message: "out of range";
let BAD_INPUT = 7..7;
const EXPECT_MSG = "out of range";
!!generate_tests!!
} }
mod rangeinclusive_len_len{ in mod rangeinclusive_len_len {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = 6..=5; good: data[6..=5] == [];
let GOOD_OUTPUT = []; bad: data[7..=6];
message: "out of range";
let BAD_INPUT = 7..=6;
const EXPECT_MSG = "out of range";
!!generate_tests!!
} }
} }
panic_cases! { panic_cases! {
mod range_neg_width { in mod range_neg_width {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = 4..4; good: data[4..4] == [];
let GOOD_OUTPUT = []; bad: data[4..3];
message: "but ends at";
let BAD_INPUT = 4..3;
const EXPECT_MSG = "but ends at";
!!generate_tests!!
} }
mod rangeinclusive_neg_width { in mod rangeinclusive_neg_width {
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5]; data: [0, 1, 2, 3, 4, 5];
let GOOD_INPUT = 4..=3; good: data[4..=3] == [];
let GOOD_OUTPUT = []; bad: data[4..=2];
message: "but ends at";
let BAD_INPUT = 4..=2;
const EXPECT_MSG = "but ends at";
!!generate_tests!!
} }
} }
panic_cases! { panic_cases! {
mod rangeinclusive_overflow { in mod rangeinclusive_overflow {
let DATA: [i32; 2] = [0, 1]; data: [0, 1];
// note: using 0 specifically ensures that the result of overflowing is 0..0, // note: using 0 specifically ensures that the result of overflowing is 0..0,
// so that `get` doesn't simply return None for the wrong reason. // so that `get` doesn't simply return None for the wrong reason.
let BAD_INPUT = 0 ..= ::std::usize::MAX; bad: data[0 ..= ::std::usize::MAX];
const EXPECT_MSG = "maximum usize"; message: "maximum usize";
!!generate_tests!!
} }
mod rangetoinclusive_overflow { in mod rangetoinclusive_overflow {
let DATA: [i32; 2] = [0, 1]; data: [0, 1];
let BAD_INPUT = ..= ::std::usize::MAX; bad: data[..= ::std::usize::MAX];
const EXPECT_MSG = "maximum usize"; message: "maximum usize";
!!generate_tests!!
} }
} // panic_cases! } // panic_cases!
} }