feat: add isInclusive field to ObjRange and implement proper exclusive range iteration
Add isInclusive boolean to ObjRange struct to distinguish between inclusive (..) and exclusive (...) ranges. Update wrenNewRange signature to accept isInclusive parameter. Implement correct iteration logic for both ascending and descending ranges, handling empty exclusive ranges and floating-point iteration. Add type validation for range RHS operands. Update min/max/to properties to return raw endpoint values instead of adjusted ones. Add comprehensive test coverage for inclusive/exclusive ranges, negative ranges, empty ranges, floating-point iteration, isInclusive property, toString, and type error cases.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
1..."s" // expect runtime error: Right hand side of range must be a number.
|
||||
@@ -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.
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
1.."s" // expect runtime error: Right hand side of range must be a number.
|
||||
@@ -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
|
||||
+28
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
+4
-4
@@ -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
|
||||
|
||||
+5
-6
@@ -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
|
||||
|
||||
+9
-9
@@ -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
|
||||
|
||||
@@ -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