mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Fix #2. Finish implementing Range.
Covers error and edge cases and fully tested now.
This commit is contained in:
1
test/range/exclusive_range_wrong_rhs_type.wren
Normal file
1
test/range/exclusive_range_wrong_rhs_type.wren
Normal file
@ -0,0 +1 @@
|
||||
1..."s" // expect runtime error: Right hand side of range must be a number.
|
||||
31
test/range/floating_point.wren
Normal file
31
test/range/floating_point.wren
Normal file
@ -0,0 +1,31 @@
|
||||
// Ordered range.
|
||||
IO.print((2..5).from) // expect: 2
|
||||
IO.print((3..3).from) // expect: 3
|
||||
IO.print((0..3).from) // expect: 0
|
||||
IO.print((-5..3).from) // expect: -5
|
||||
IO.print((-5..-2).from) // expect: -5
|
||||
|
||||
// Backwards range.
|
||||
IO.print((5..2).from) // expect: 5
|
||||
IO.print((3..0).from) // expect: 3
|
||||
IO.print((3..-5).from) // expect: 3
|
||||
IO.print((-2..-5).from) // expect: -2
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).from) // expect: 2
|
||||
IO.print((3...3).from) // expect: 3
|
||||
IO.print((0...3).from) // expect: 0
|
||||
IO.print((-5...3).from) // expect: -5
|
||||
IO.print((-5...-2).from) // expect: -5
|
||||
|
||||
// Exclusive backwards range.
|
||||
IO.print((5...2).from) // expect: 5
|
||||
IO.print((3...0).from) // expect: 3
|
||||
IO.print((3...-5).from) // expect: 3
|
||||
IO.print((-2...-5).from) // expect: -2
|
||||
|
||||
// TODO: Test toString.
|
||||
// TODO: Non-number RHS.
|
||||
// TODO: Non-integer RHS.
|
||||
// TODO: Range iteration.
|
||||
// TODO: Empty or negative ranges.
|
||||
@ -23,9 +23,3 @@ IO.print((5...2).from) // expect: 5
|
||||
IO.print((3...0).from) // expect: 3
|
||||
IO.print((3...-5).from) // expect: 3
|
||||
IO.print((-2...-5).from) // expect: -2
|
||||
|
||||
// TODO: Test toString.
|
||||
// TODO: Non-number RHS.
|
||||
// TODO: Non-integer RHS.
|
||||
// TODO: Range iteration.
|
||||
// TODO: Empty or negative ranges.
|
||||
|
||||
1
test/range/inclusive_range_wrong_rhs_type.wren
Normal file
1
test/range/inclusive_range_wrong_rhs_type.wren
Normal file
@ -0,0 +1 @@
|
||||
1.."s" // expect runtime error: Right hand side of range must be a number.
|
||||
5
test/range/is_inclusive.wren
Normal file
5
test/range/is_inclusive.wren
Normal file
@ -0,0 +1,5 @@
|
||||
IO.print((0..0).isInclusive) // expect: true
|
||||
IO.print((0...0).isInclusive) // expect: false
|
||||
|
||||
IO.print((-1..1).isInclusive) // expect: true
|
||||
IO.print((-1...1).isInclusive) // expect: false
|
||||
@ -1,3 +1,4 @@
|
||||
// Inclusive.
|
||||
var range = 1..3
|
||||
IO.print(range.iterate(null)) // expect: 1
|
||||
IO.print(range.iterate(1)) // expect: 2
|
||||
@ -5,4 +6,30 @@ IO.print(range.iterate(2)) // expect: 3
|
||||
IO.print(range.iterate(3)) // expect: false
|
||||
IO.print(range.iterate(4)) // expect: false
|
||||
|
||||
// TODO: Negative and empty ranges.
|
||||
// Exclusive
|
||||
range = 1...3
|
||||
IO.print(range.iterate(null)) // expect: 1
|
||||
IO.print(range.iterate(1)) // expect: 2
|
||||
IO.print(range.iterate(2)) // expect: false
|
||||
|
||||
// Negative inclusive range.
|
||||
range = 3..1
|
||||
IO.print(range.iterate(null)) // expect: 3
|
||||
IO.print(range.iterate(3)) // expect: 2
|
||||
IO.print(range.iterate(2)) // expect: 1
|
||||
IO.print(range.iterate(1)) // expect: false
|
||||
|
||||
// Negative exclusive range.
|
||||
range = 3...1
|
||||
IO.print(range.iterate(null)) // expect: 3
|
||||
IO.print(range.iterate(3)) // expect: 2
|
||||
IO.print(range.iterate(2)) // expect: false
|
||||
|
||||
// Empty inclusive range.
|
||||
range = 1..1
|
||||
IO.print(range.iterate(null)) // expect: 1
|
||||
IO.print(range.iterate(1)) // expect: false
|
||||
|
||||
// Empty exclusive range.
|
||||
range = 1...1
|
||||
IO.print(range.iterate(null)) // expect: false
|
||||
|
||||
18
test/range/iterate_from_float.wren
Normal file
18
test/range/iterate_from_float.wren
Normal file
@ -0,0 +1,18 @@
|
||||
// Starts at "from" and adds 1.0 each iteration.
|
||||
|
||||
for (n in 1.3..4.5) IO.print(n)
|
||||
// expect: 1.3
|
||||
// expect: 2.3
|
||||
// expect: 3.3
|
||||
// expect: 4.3
|
||||
|
||||
for (n in 1.3...4.5) IO.print(n)
|
||||
// expect: 1.3
|
||||
// expect: 2.3
|
||||
// expect: 3.3
|
||||
// expect: 4.3
|
||||
|
||||
for (n in 1.3...4.3) IO.print(n)
|
||||
// expect: 1.3
|
||||
// expect: 2.3
|
||||
// expect: 3.3
|
||||
@ -12,11 +12,11 @@ IO.print((3..-5).max) // expect: 3
|
||||
IO.print((-2..-5).max) // expect: -2
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).max) // expect: 4
|
||||
IO.print((2...5).max) // expect: 5
|
||||
IO.print((3...3).max) // expect: 3
|
||||
IO.print((0...3).max) // expect: 2
|
||||
IO.print((-5...3).max) // expect: 2
|
||||
IO.print((-5...-2).max) // expect: -3
|
||||
IO.print((0...3).max) // expect: 3
|
||||
IO.print((-5...3).max) // expect: 3
|
||||
IO.print((-5...-2).max) // expect: -2
|
||||
|
||||
// Exclusive backwards range.
|
||||
IO.print((5...2).max) // expect: 5
|
||||
|
||||
@ -13,14 +13,13 @@ IO.print((-2..-5).min) // expect: -5
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).min) // expect: 2
|
||||
IO.print((3...3).min) // expect: 2
|
||||
IO.print((3...3).min) // expect: 3
|
||||
IO.print((0...3).min) // expect: 0
|
||||
IO.print((-5...3).min) // expect: -5
|
||||
IO.print((-5...-2).min) // expect: -5
|
||||
|
||||
// Exclusive backwards range.
|
||||
// TODO: Is this what we want? "..." always means "subtract 1"?
|
||||
IO.print((5...2).min) // expect: 1
|
||||
IO.print((3...0).min) // expect: -1
|
||||
IO.print((3...-5).min) // expect: -6
|
||||
IO.print((-2...-5).min) // expect: -6
|
||||
IO.print((5...2).min) // expect: 2
|
||||
IO.print((3...0).min) // expect: 0
|
||||
IO.print((3...-5).min) // expect: -5
|
||||
IO.print((-2...-5).min) // expect: -5
|
||||
|
||||
@ -12,14 +12,14 @@ IO.print((3..-5).to) // expect: -5
|
||||
IO.print((-2..-5).to) // expect: -5
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).to) // expect: 4
|
||||
IO.print((3...3).to) // expect: 2
|
||||
IO.print((0...3).to) // expect: 2
|
||||
IO.print((-5...3).to) // expect: 2
|
||||
IO.print((-5...-2).to) // expect: -3
|
||||
IO.print((2...5).to) // expect: 5
|
||||
IO.print((3...3).to) // expect: 3
|
||||
IO.print((0...3).to) // expect: 3
|
||||
IO.print((-5...3).to) // expect: 3
|
||||
IO.print((-5...-2).to) // expect: -2
|
||||
|
||||
// Exclusive backwards range.
|
||||
IO.print((5...2).to) // expect: 1
|
||||
IO.print((3...0).to) // expect: -1
|
||||
IO.print((3...-5).to) // expect: -6
|
||||
IO.print((-2...-5).to) // expect: -6
|
||||
IO.print((5...2).to) // expect: 2
|
||||
IO.print((3...0).to) // expect: 0
|
||||
IO.print((3...-5).to) // expect: -5
|
||||
IO.print((-2...-5).to) // expect: -5
|
||||
|
||||
7
test/range/to_string.wren
Normal file
7
test/range/to_string.wren
Normal file
@ -0,0 +1,7 @@
|
||||
IO.print(1..3) // expect: 1..3
|
||||
IO.print(12345.6789..12345.6789) // expect: 12345.6789..12345.6789
|
||||
IO.print(-100..-300) // expect: -100..-300
|
||||
|
||||
IO.print(1...3) // expect: 1...3
|
||||
IO.print(12345.6789...12345.6789) // expect: 12345.6789...12345.6789
|
||||
IO.print(-100...-300) // expect: -100...-300
|
||||
Reference in New Issue
Block a user