04d97f1df4cb — Leonard Ritter 15 days ago
* fixed `rrange` overshooting when `from` is zero and the counter is unsigned
3 files changed, 21 insertions(+), 5 deletions(-)

M lib/scopes/core.sc
M testing/test_loop.sc
M testing/test_mutarray.sc
M lib/scopes/core.sc +5 -5
@@ 4362,12 4362,12 @@ inline rrange (a b c)
         static-branch (none? b)
             inline () a
             inline () b
-    let to = (((to - from + (step - 1)) // step) * step - step + from)
+    let to = (((to - from + (step - 1)) // step) * step + from)
     Generator
-        inline () to
-        inline (x) (x >= from)
-        inline (x) x
-        inline (x) (x - step)
+        inline () (_ to (to - step))
+        inline (x0 x-1) (x0 > from)
+        inline (x0 x-1) x-1
+        inline (x0 x-1) (_ x-1 (x-1 - step))
 
 let parse-compile-flags =
     spice-macro

          
M testing/test_loop.sc +8 -0
@@ 136,4 136,12 @@ do
                 break a b
         _ a b c
 
+do
+    # reverse loops
+    for i in (rrange 0 10)
+        test ((i >= 0) & (i < 10))
+    # unsigned values
+    for i in (rrange 0:u32 10:u32)
+        test ((i >= 0:u32) & (i < 10:u32))
+
 true

          
M testing/test_mutarray.sc +8 -0
@@ 219,6 219,14 @@ fn test-remove ()
     test (('value (a @ 2)) == 6)
     test (('value (a @ 3)) == 3)
     test (('value (a @ 4)) == 4)
+    'insert a (One 7) 0
+    test ((countof a) == 6)
+    test (('value (a @ 0)) == 7)
+    test (('value (a @ 1)) == 0)
+    test (('value (a @ 2)) == 1)
+    test (('value (a @ 3)) == 6)
+    test (('value (a @ 4)) == 3)
+    test (('value (a @ 5)) == 4)
     ;
 
 test-remove;