From 5d1c1d75afd7d54dc6594c4894dd98957ac19d27 Mon Sep 17 00:00:00 2001 From: Damien Pollet Date: Fri, 29 Jan 2021 17:31:22 +0100 Subject: [PATCH] Better comment and a few more test assertions --- src/Collections-Sequenceable-Tests/IntervalTest.class.st | 7 +++++-- src/Collections-Sequenceable/Interval.class.st | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Collections-Sequenceable-Tests/IntervalTest.class.st b/src/Collections-Sequenceable-Tests/IntervalTest.class.st index 255c10f600e..9059188e5a3 100644 --- a/src/Collections-Sequenceable-Tests/IntervalTest.class.st +++ b/src/Collections-Sequenceable-Tests/IntervalTest.class.st @@ -638,13 +638,16 @@ IntervalTest >> testRangeIncludes [ self assert: ((1 to: 10) rangeIncludes: 3). self assert: ((1 to: 10 by: 2) rangeIncludes: 3). - self assert: ((1 to: 10 by: 2) rangeIncludes: 4). self assert: ((10 to: 1 by: -2) rangeIncludes: 3). + self assert: ((1 to: 10 by: 2) rangeIncludes: 4). + self assert: ((1 to: 10) rangeIncludes: 3.5). self deny: ((1 to: 10) rangeIncludes: 0). self deny: ((1 to: 10) rangeIncludes: 11). self deny: ((1 to: 10 by: 2) rangeIncludes: 0). - self deny: ((1 to: 10 by: 2) rangeIncludes: 11) + self deny: ((1 to: 10 by: 2) rangeIncludes: 11). + self deny: ((10 to: 1) rangeIncludes: 3). + self deny: ((10 to: 1) rangeIncludes: -3) ] { #category : #tests } diff --git a/src/Collections-Sequenceable/Interval.class.st b/src/Collections-Sequenceable/Interval.class.st index 11bbd8fc7ff..7f99e0e0b4d 100644 --- a/src/Collections-Sequenceable/Interval.class.st +++ b/src/Collections-Sequenceable/Interval.class.st @@ -312,8 +312,11 @@ Interval >> printOn: aStream [ { #category : #testing } Interval >> rangeIncludes: aNumber [ - "Return true if the number lies in the interval between start and stop, considering all values in between as if step was either +1 or -1. - Use #include: to take the exact step into account." + "Return true if aNumber lies anywhere between the interval bounds. + This is a fast O(1) bounds check. + + Beware: because #rangeIncludes: only considers the sign of the step, not its magnitude, it also returns true for values that are not actual elements of the interval. + For precise element inclusion with arbitrary step, use #includes:." ^ step >= 0 ifTrue: [ aNumber between: start and: stop ]