-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Berry extend
range(lower, upper, incr)
to arbitrary increment (#19120)
- Loading branch information
1 parent
af27d65
commit 46e1706
Showing
3 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# test for ranges | ||
|
||
# expand a range object as list | ||
def expand(iter) | ||
var ret = [] | ||
for i: iter | ||
ret.push(i) | ||
end | ||
return ret | ||
end | ||
|
||
assert(expand(0..5) == [0, 1, 2, 3, 4, 5]) | ||
assert(expand(0..0) == [0]) | ||
assert(expand(5..0) == []) | ||
var r = 1..5 | ||
assert(r.lower() == 1) | ||
assert(r.upper() == 5) | ||
assert(r.incr() == 1) | ||
|
||
assert(expand(range(0,5)) == [0, 1, 2, 3, 4, 5]) | ||
assert(expand(range(0,5,2)) == [0, 2, 4]) | ||
assert(expand(range(0,5,12)) == [0]) | ||
assert(expand(range(0,5,-1)) == []) | ||
|
||
assert(expand(range(5,0,-1)) == [5, 4, 3, 2, 1, 0]) | ||
assert(expand(range(5,0,-2)) == [5, 3, 1]) | ||
assert(expand(range(5,5,-2)) == [5]) | ||
assert(expand(range(0,5,-2)) == []) | ||
|
||
def assert_value_error(c) | ||
try | ||
compile(c)() | ||
assert(false, 'unexpected execution flow') | ||
except 'value_error' as e, m | ||
end | ||
end | ||
|
||
# range with increment zero shoud raise an error | ||
assert_value_error("range(1,2,0)") |