Skip to content

Commit

Permalink
feat(corelib): range contains (#7008)
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoIover authored Jan 6, 2025
1 parent 2ac3e65 commit 56a5850
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
18 changes: 17 additions & 1 deletion corelib/src/ops/range.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,23 @@ pub struct Range<T> {
}

#[generate_trait]
pub impl RangeImpl<T, +Copy<T>, +Drop<T>, +PartialOrd<T>> of RangeTrait<T> {
pub impl RangeImpl<T, +Destruct<T>, +PartialOrd<@T>> of RangeTrait<T> {
/// Returns `true` if `item` is contained in the range.
///
/// # Examples
///
/// ```
/// assert!(!(3..5).contains(@2));
/// assert!( (3..5).contains(@3));
/// assert!( (3..5).contains(@4));
/// assert!(!(3..5).contains(@5));
///
/// assert!(!(3..3).contains(@3));
/// assert!(!(3..2).contains(@3));
fn contains(self: @Range<T>, item: @T) -> bool {
self.start <= item && item < self.end
}

/// Returns `true` if the range contains no items.
///
/// # Examples
Expand Down
11 changes: 11 additions & 0 deletions corelib/src/test/range_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ fn test_range_is_empty() {
assert!((3_u8..2_u8).is_empty());
}

#[test]
fn test_range_contains() {
assert!(!(3_u8..5).contains(@2));
assert!((3_u8..5).contains(@3));
assert!((3_u8..5).contains(@4));
assert!(!(3_u8..5).contains(@5));

assert!(!(3_u8..3).contains(@3));
assert!(!(3_u8..2).contains(@3));
}

#[test]
fn test_range_format() {
assert!(format!("{:?}", 1..5) == "1..5");
Expand Down

0 comments on commit 56a5850

Please sign in to comment.