Skip to content

Commit

Permalink
Merge pull request pharo-project#8446 from cdlm/8444-Interval--rangeI…
Browse files Browse the repository at this point in the history
…ncludes-should-be-private-and-have-a-better-comment

Comment Interval>>#rangeIncludes: and more complete test
  • Loading branch information
Ducasse authored Jan 30, 2021
2 parents 1ea8b3c + 5d1c1d7 commit 97b1fde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
31 changes: 13 additions & 18 deletions src/Collections-Sequenceable-Tests/IntervalTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -635,24 +635,19 @@ IntervalTest >> testPermutationsDo [

{ #category : #tests }
IntervalTest >> testRangeIncludes [
self
assert: ((1 to: 10)
rangeIncludes: 3).
self
assert: ((1 to: 10 by: 2)
rangeIncludes: 3).
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 assert: ((1 to: 10) rangeIncludes: 3).
self assert: ((1 to: 10 by: 2) rangeIncludes: 3).
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: ((10 to: 1) rangeIncludes: 3).
self deny: ((10 to: 1) rangeIncludes: -3)
]

{ #category : #tests }
Expand Down
6 changes: 5 additions & 1 deletion src/Collections-Sequenceable/Interval.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ Interval >> printOn: aStream [

{ #category : #testing }
Interval >> rangeIncludes: aNumber [
"Return true if the number lies in the interval between start and stop."
"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 ]
Expand Down

0 comments on commit 97b1fde

Please sign in to comment.