Skip to content

Commit 6728240

Browse files
committed
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
1 parent a601302 commit 6728240

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

library/core/tests/ops.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo};
1+
use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
22

33
// Test the Range structs and syntax.
44

@@ -59,6 +59,12 @@ fn test_range_inclusive() {
5959
assert_eq!(r.next(), None);
6060
}
6161

62+
#[test]
63+
fn test_range_to_inclusive() {
64+
// Not much to test.
65+
let _ = RangeToInclusive { end: 42 };
66+
}
67+
6268
#[test]
6369
fn test_range_is_empty() {
6470
assert!(!(0.0..10.0).is_empty());
@@ -151,3 +157,43 @@ fn test_range_syntax_in_return_statement() {
151157
}
152158
// Not much to test.
153159
}
160+
161+
#[test]
162+
fn range_structural_match() {
163+
// test that all range types can be structurally matched upon
164+
165+
const RANGE: Range<usize> = 0..1000;
166+
match RANGE {
167+
RANGE => {}
168+
_ => unreachable!(),
169+
}
170+
171+
const RANGE_FROM: RangeFrom<usize> = 0..;
172+
match RANGE_FROM {
173+
RANGE_FROM => {}
174+
_ => unreachable!(),
175+
}
176+
177+
const RANGE_FULL: RangeFull = ..;
178+
match RANGE_FULL {
179+
RANGE_FULL => {}
180+
}
181+
182+
const RANGE_INCLUSIVE: RangeInclusive<usize> = 0..=999;
183+
match RANGE_INCLUSIVE {
184+
RANGE_INCLUSIVE => {}
185+
_ => unreachable!(),
186+
}
187+
188+
const RANGE_TO: RangeTo<usize> = ..1000;
189+
match RANGE_TO {
190+
RANGE_TO => {}
191+
_ => unreachable!(),
192+
}
193+
194+
const RANGE_TO_INCLUSIVE: RangeToInclusive<usize> = ..=999;
195+
match RANGE_TO_INCLUSIVE {
196+
RANGE_TO_INCLUSIVE => {}
197+
_ => unreachable!(),
198+
}
199+
}

0 commit comments

Comments
 (0)