Skip to content

Commit 030aa9b

Browse files
committed
revise macro in slice tests
1 parent 02b3da1 commit 030aa9b

File tree

1 file changed

+62
-92
lines changed

1 file changed

+62
-92
lines changed

src/libcore/tests/slice.rs

+62-92
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,12 @@ fn test_windows_zip() {
376376
assert_eq!(res, [14, 18, 22, 26]);
377377
}
378378

379+
// The current implementation of SliceIndex fails to handle methods
380+
// orthogonally from range types; therefore, it is worth testing
381+
// all of the indexing operations on each input.
379382
mod slice_index {
380-
// Test a slicing operation that should succeed,
381-
// testing it on all of the indexing methods.
383+
// This checks all six indexing methods, given an input range that
384+
// should succeed. (it is NOT suitable for testing invalid inputs)
382385
macro_rules! assert_range_eq {
383386
($arr:expr, $range:expr, $expected:expr)
384387
=> {
@@ -423,7 +426,7 @@ mod slice_index {
423426
// because if it can't, then what are we even doing here?
424427
//
425428
// (Be aware this only demonstrates the ability to detect bugs
426-
// in the FIRST method it calls, as the macro is not designed
429+
// in the FIRST method that panics, as the macro is not designed
427430
// to be used in `should_panic`)
428431
#[test]
429432
#[should_panic(expected = "out of range")]
@@ -446,30 +449,29 @@ mod slice_index {
446449
// and `None` test cases for get/get_mut.
447450
macro_rules! panic_cases {
448451
($(
449-
mod $case_name:ident {
450-
let DATA: $Data: ty = $data:expr;
452+
// each test case needs a unique name to namespace the tests
453+
in mod $case_name:ident {
454+
data: $data:expr;
451455

452456
// optional:
453457
//
454-
// a similar input for which DATA[input] succeeds, and the corresponding
455-
// output as an array. This helps validate "critical points" where an
456-
// input range straddles the boundary between valid and invalid.
458+
// one or more similar inputs for which data[input] succeeds,
459+
// and the corresponding output as an array. This helps validate
460+
// "critical points" where an input range straddles the boundary
461+
// between valid and invalid.
457462
// (such as the input `len..len`, which is just barely valid)
458463
$(
459-
let GOOD_INPUT = $good:expr;
460-
let GOOD_OUTPUT = $output:expr;
464+
good: data[$good:expr] == $output:expr;
461465
)*
462466

463-
let BAD_INPUT = $bad:expr;
464-
const EXPECT_MSG = $expect_msg:expr;
465-
466-
!!generate_tests!!
467+
bad: data[$bad:expr];
468+
message: $expect_msg:expr;
467469
}
468470
)*) => {$(
469471
mod $case_name {
470472
#[test]
471473
fn pass() {
472-
let mut v: $Data = $data;
474+
let mut v = $data;
473475

474476
$( assert_range_eq!($data, $good, $output); )*
475477

@@ -487,15 +489,15 @@ mod slice_index {
487489
#[test]
488490
#[should_panic(expected = $expect_msg)]
489491
fn index_fail() {
490-
let v: $Data = $data;
492+
let v = $data;
491493
let v: &[_] = &v;
492494
let _v = &v[$bad];
493495
}
494496

495497
#[test]
496498
#[should_panic(expected = $expect_msg)]
497499
fn index_mut_fail() {
498-
let mut v: $Data = $data;
500+
let mut v = $data;
499501
let v: &mut [_] = &mut v;
500502
let _v = &mut v[$bad];
501503
}
@@ -516,112 +518,80 @@ mod slice_index {
516518
}
517519

518520
panic_cases! {
519-
mod rangefrom_len {
520-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
521-
522-
let GOOD_INPUT = 6..;
523-
let GOOD_OUTPUT = [];
521+
in mod rangefrom_len {
522+
data: [0, 1, 2, 3, 4, 5];
524523

525-
let BAD_INPUT = 7..;
526-
const EXPECT_MSG = "but ends at"; // perhaps not ideal
527-
528-
!!generate_tests!!
524+
good: data[6..] == [];
525+
bad: data[7..];
526+
message: "but ends at"; // perhaps not ideal
529527
}
530528

531-
mod rangeto_len {
532-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
533-
534-
let GOOD_INPUT = ..6;
535-
let GOOD_OUTPUT = [0, 1, 2, 3, 4, 5];
529+
in mod rangeto_len {
530+
data: [0, 1, 2, 3, 4, 5];
536531

537-
let BAD_INPUT = ..7;
538-
const EXPECT_MSG = "out of range";
539-
540-
!!generate_tests!!
532+
good: data[..6] == [0, 1, 2, 3, 4, 5];
533+
bad: data[..7];
534+
message: "out of range";
541535
}
542536

543-
mod rangetoinclusive_len {
544-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
545-
546-
let GOOD_INPUT = ..=5;
547-
let GOOD_OUTPUT = [0, 1, 2, 3, 4, 5];
537+
in mod rangetoinclusive_len {
538+
data: [0, 1, 2, 3, 4, 5];
548539

549-
let BAD_INPUT = ..=6;
550-
const EXPECT_MSG = "out of range";
551-
552-
!!generate_tests!!
540+
good: data[..=5] == [0, 1, 2, 3, 4, 5];
541+
bad: data[..=6];
542+
message: "out of range";
553543
}
554544

555-
mod range_len_len {
556-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
557-
558-
let GOOD_INPUT = 6..6;
559-
let GOOD_OUTPUT = [];
545+
in mod range_len_len {
546+
data: [0, 1, 2, 3, 4, 5];
560547

561-
let BAD_INPUT = 7..7;
562-
const EXPECT_MSG = "out of range";
563-
564-
!!generate_tests!!
548+
good: data[6..6] == [];
549+
bad: data[7..7];
550+
message: "out of range";
565551
}
566552

567-
mod rangeinclusive_len_len{
568-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
569-
570-
let GOOD_INPUT = 6..=5;
571-
let GOOD_OUTPUT = [];
572-
573-
let BAD_INPUT = 7..=6;
574-
const EXPECT_MSG = "out of range";
553+
in mod rangeinclusive_len_len {
554+
data: [0, 1, 2, 3, 4, 5];
575555

576-
!!generate_tests!!
556+
good: data[6..=5] == [];
557+
bad: data[7..=6];
558+
message: "out of range";
577559
}
578560
}
579561

580562
panic_cases! {
581-
mod range_neg_width {
582-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
583-
584-
let GOOD_INPUT = 4..4;
585-
let GOOD_OUTPUT = [];
586-
587-
let BAD_INPUT = 4..3;
588-
const EXPECT_MSG = "but ends at";
563+
in mod range_neg_width {
564+
data: [0, 1, 2, 3, 4, 5];
589565

590-
!!generate_tests!!
566+
good: data[4..4] == [];
567+
bad: data[4..3];
568+
message: "but ends at";
591569
}
592570

593-
mod rangeinclusive_neg_width {
594-
let DATA: [i32; 6] = [0, 1, 2, 3, 4, 5];
571+
in mod rangeinclusive_neg_width {
572+
data: [0, 1, 2, 3, 4, 5];
595573

596-
let GOOD_INPUT = 4..=3;
597-
let GOOD_OUTPUT = [];
598-
599-
let BAD_INPUT = 4..=2;
600-
const EXPECT_MSG = "but ends at";
601-
602-
!!generate_tests!!
574+
good: data[4..=3] == [];
575+
bad: data[4..=2];
576+
message: "but ends at";
603577
}
604578
}
605579

606580
panic_cases! {
607-
mod rangeinclusive_overflow {
608-
let DATA: [i32; 2] = [0, 1];
581+
in mod rangeinclusive_overflow {
582+
data: [0, 1];
609583

610584
// note: using 0 specifically ensures that the result of overflowing is 0..0,
611585
// so that `get` doesn't simply return None for the wrong reason.
612-
let BAD_INPUT = 0 ..= ::std::usize::MAX;
613-
const EXPECT_MSG = "maximum usize";
614-
615-
!!generate_tests!!
586+
bad: data[0 ..= ::std::usize::MAX];
587+
message: "maximum usize";
616588
}
617589

618-
mod rangetoinclusive_overflow {
619-
let DATA: [i32; 2] = [0, 1];
620-
621-
let BAD_INPUT = ..= ::std::usize::MAX;
622-
const EXPECT_MSG = "maximum usize";
590+
in mod rangetoinclusive_overflow {
591+
data: [0, 1];
623592

624-
!!generate_tests!!
593+
bad: data[..= ::std::usize::MAX];
594+
message: "maximum usize";
625595
}
626596
} // panic_cases!
627597
}

0 commit comments

Comments
 (0)