aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xruntests3
-rw-r--r--testcases-iter226
2 files changed, 229 insertions, 0 deletions
diff --git a/runtests b/runtests
index fd7bef4..f7f8991 100755
--- a/runtests
+++ b/runtests
@@ -8,3 +8,6 @@ cppawk=./cppawk ./testsuite.awk testcases-case
cppawk="./cppawk --awk=mawk" ./testsuite.awk testcases-case
cppawk=./cppawk ./testsuite.awk testcases-narg
+
+cppawk=./cppawk ./testsuite.awk testcases-iter
+cppawk="./cppawk --awk=mawk" ./testsuite.awk testcases-iter
diff --git a/testcases-iter b/testcases-iter
new file mode 100644
index 0000000..59f9548
--- /dev/null
+++ b/testcases-iter
@@ -0,0 +1,226 @@
+1:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ split("a b c d e f", a)
+
+ doarray (i, v, a)
+ print i, v
+}'
+:
+1 a
+2 b
+3 c
+4 d
+5 e
+6 f
+--
+2:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ doarray (i, v, a)
+ print i, v
+}'
+:
+--
+3:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ $0 = "a b c d e"
+ dofields (i, v)
+ print i, v
+}'
+:
+1 a
+2 b
+3 c
+4 d
+5 e
+--
+4:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ dostring (i, c, 123.0) {
+ print i, c
+ }
+}'
+:
+1 1
+2 2
+3 3
+--
+5:
+$cppawk '
+#include <iter.h>
+BEGIN {
+ dostring (i, c, "");
+}'
+:
+--
+6:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ $0 = "o p q r s t u v w x y z" # set fields
+
+ split("! @ # $ % ^ & * (", arr) # init array
+
+ loop (range(i, 1, 9),
+ from(x, 5),
+ from_step(y, 100, 5),
+ str(j, ch, "abcdefghijklmn"),
+ fields(f),
+ keys(k, arr))
+ {
+ print i, x, y, ch, f, k, arr[k]
+ }
+}'
+:
+1 5 100 a o 1 !
+2 6 105 b p 2 @
+3 7 110 c q 3 #
+4 8 115 d r 4 $
+5 9 120 e s 5 %
+6 10 125 f t 6 ^
+7 11 130 g u 7 &
+8 12 135 h v 8 *
+9 13 140 i w 9 (
+--
+7:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ $0 = "a b c" # set fields
+
+ split("X Y", arr) # init array
+
+ loop_cross (fields(f),
+ keys(k, arr),
+ range(i, 1, 3))
+ {
+ print f, arr[k], i
+ }
+}'
+:
+a X 1
+a X 2
+a X 3
+a Y 1
+a Y 2
+a Y 3
+b X 1
+b X 2
+b X 3
+b Y 1
+b Y 2
+b Y 3
+c X 1
+c X 2
+c X 3
+c Y 1
+c Y 2
+c Y 3
+--
+8:
+$cppawk '
+#include <iter.h>
+
+#define __init_let(var, expr) 1
+#define __test_let(var, expr) (var = (expr))
+#define __prep_let(var, expr) 1
+#define __fini_let(var, expr) 1
+#define __step_let(var, expr) 0
+
+BEGIN {
+ loop (range(i, 1, 10),
+ range_step(j, 1, 10, 2),
+ let(ch, substr("abcdefghijklmn", i, j)))
+ {
+ print i, j, ch
+ }
+}
+'
+:
+1 1 a
+2 3 bcd
+3 5 cdefg
+4 7 defghij
+5 9 efghijklm
+--
+9:
+$cppawk '
+#include <iter.h>
+
+#define __init_first_then_until(var, first, then, until) (var = (first))
+#define __test_first_then_until(var, first, then, until) (!(until))
+#define __prep_first_then_until(var, first, then, until) 1
+#define __fini_first_then_until(var, first, then, until) 1
+#define __step_first_then_until(var, first, then, until) (var = (then))
+
+BEGIN {
+ loop (first_then_until(i, 1, i * 2, i >= 60),
+ first_then_until(s, "x", s s, 0))
+ {
+ print i, s
+ }
+}'
+:
+1 x
+2 xx
+4 xxxx
+8 xxxxxxxx
+16 xxxxxxxxxxxxxxxx
+32 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+--
+10:
+$cppawk '
+#include <iter.h>
+
+BEGIN {
+ $0 = "a b c" # set fields
+
+ split("X Y", arr) # init array
+
+ bags (b1, b2) {
+ loop_cross (lockstep(fields(f),
+ str(i, ch, "IJK")),
+ keys(ky, arr),
+ lockstep(range(j, 1, 3),
+ from(k, 100)))
+ {
+ print f, ch, arr[ky], j, k
+ bag(b1, j)
+ }
+ }
+
+ print sexp(b1)
+}'
+:
+a I X 1 100
+a I X 2 101
+a I X 3 102
+a I Y 1 100
+a I Y 2 101
+a I Y 3 102
+b J X 1 100
+b J X 2 101
+b J X 3 102
+b J Y 1 100
+b J Y 2 101
+b J Y 3 102
+c K X 1 100
+c K X 2 101
+c K X 3 102
+c K Y 1 100
+c K Y 2 101
+c K Y 3 102
+(1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3)