shithub: rgbds

Download patch

ref: 1aeaca2af602b19c5af2fc2bb2a71934eda2a0d0
parent: 267e4bc25c951c4817012b4696e81f39d08de321
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Tue Apr 20 18:36:56 EDT 2021

Add test case sort-algorithms.asm

This combines 0.5.0 and post-0.5.0 features:
print and println, strfmt, for loops,
def assignments, redef equs, {interpolation},
new macro syntax, and \<bracketed macro args>

--- /dev/null
+++ b/test/asm/sort-algorithms.asm
@@ -1,0 +1,47 @@
+MACRO sort_recursive
+    if _NARG > 0
+        def min_i = 1
+        for i, 2, _NARG + 1
+            if \<i> < \<min_i>
+                def min_i = i
+            endc
+        endr
+        print strfmt("%d", \<min_i>)
+    endc
+    if _NARG > 1
+        print ", "
+        redef _subsort equs "sort_recursive "
+        for i, 1, _NARG + 1
+            if i != min_i
+                redef _subsort equs "{_subsort}\<i>,"
+            endc
+        endr
+        {_subsort}
+    else
+        println
+    endc
+ENDM
+
+MACRO sort_iterative
+    for i, 1, _NARG + 1
+        def got_{d:i} = 0
+    endr
+    for n, 1, _NARG + 1
+        def min_i = 1
+        for i, 2, _NARG + 1
+            if (\<i> < \<min_i> && !got_{d:i}) || got_{d:min_i}
+                def min_i = i
+            endc
+        endr
+        def got_{d:min_i} = 1
+        print strfmt("%d", \<min_i>)
+        if n < _NARG
+            print ", "
+        else
+            println
+        endc
+    endr
+ENDM
+
+    sort_recursive 16, 0, 16, 5, 1, 18, 3, 14, 19, 4, 6, 13, 17, 7, 11, 8, 15, 10, 12, 9, 2
+    sort_iterative 16, 0, 16, 5, 1, 18, 3, 14, 19, 4, 6, 13, 17, 7, 11, 8, 15, 10, 12, 9, 2
--- /dev/null
+++ b/test/asm/sort-algorithms.out
@@ -1,0 +1,2 @@
+0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 17, 18, 19
+0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 17, 18, 19