From 6728240f36c7dd1ecb16d396c9c61933d2f01da9 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Sat, 7 Nov 2020 01:02:15 +0100 Subject: [PATCH] Test structural matching for all range types Adds structural match tests for all range types. Note: also adds the otherwise unrelated test `test_range_to_inclusive` for completeness --- library/core/tests/ops.rs | 48 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/library/core/tests/ops.rs b/library/core/tests/ops.rs index 8f0cd3be4066e..e9d595e65e2b2 100644 --- a/library/core/tests/ops.rs +++ b/library/core/tests/ops.rs @@ -1,4 +1,4 @@ -use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo}; +use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; // Test the Range structs and syntax. @@ -59,6 +59,12 @@ fn test_range_inclusive() { assert_eq!(r.next(), None); } +#[test] +fn test_range_to_inclusive() { + // Not much to test. + let _ = RangeToInclusive { end: 42 }; +} + #[test] fn test_range_is_empty() { assert!(!(0.0..10.0).is_empty()); @@ -151,3 +157,43 @@ fn test_range_syntax_in_return_statement() { } // Not much to test. } + +#[test] +fn range_structural_match() { + // test that all range types can be structurally matched upon + + const RANGE: Range = 0..1000; + match RANGE { + RANGE => {} + _ => unreachable!(), + } + + const RANGE_FROM: RangeFrom = 0..; + match RANGE_FROM { + RANGE_FROM => {} + _ => unreachable!(), + } + + const RANGE_FULL: RangeFull = ..; + match RANGE_FULL { + RANGE_FULL => {} + } + + const RANGE_INCLUSIVE: RangeInclusive = 0..=999; + match RANGE_INCLUSIVE { + RANGE_INCLUSIVE => {} + _ => unreachable!(), + } + + const RANGE_TO: RangeTo = ..1000; + match RANGE_TO { + RANGE_TO => {} + _ => unreachable!(), + } + + const RANGE_TO_INCLUSIVE: RangeToInclusive = ..=999; + match RANGE_TO_INCLUSIVE { + RANGE_TO_INCLUSIVE => {} + _ => unreachable!(), + } +}