aboutsummaryrefslogtreecommitdiffstats
path: root/testcases-case
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-03-27 23:42:26 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-03-27 23:42:26 -0700
commit23249ef206ef23760e814cb9f8170e7ae93dabcc (patch)
tree89a6aa1b17ad433ddb739c65f85e840fd7d041d0 /testcases-case
parent0801dbaee02a811a412f22a71f9f869c90554f24 (diff)
downloadcppawk-23249ef206ef23760e814cb9f8170e7ae93dabcc.tar.gz
cppawk-23249ef206ef23760e814cb9f8170e7ae93dabcc.tar.bz2
cppawk-23249ef206ef23760e814cb9f8170e7ae93dabcc.zip
New <case.h> header and macro.
This provides a portable case statement implemented directly as switch on GNU Awk, and using other approaches on other Awks that do not have switch.
Diffstat (limited to 'testcases-case')
-rw-r--r--testcases-case54
1 files changed, 54 insertions, 0 deletions
diff --git a/testcases-case b/testcases-case
new file mode 100644
index 0000000..6e62412
--- /dev/null
+++ b/testcases-case
@@ -0,0 +1,54 @@
+1:
+$cppawk '
+#include <case.h>
+BEGIN {
+#if defined case_temps && defined case && defined of && \
+ defined matching && defined cbreak && defined otherwise
+ print 1
+#endif
+}'
+:
+1
+--
+2:
+printf "%s\n" foo bar xyzzy x b | $cppawk '
+#include <case.h>
+{
+ case ($0) {
+ of ("foo")
+ print "** foo case: fallthrough!"
+ cfall;
+ of ("bar", "xyzzy") {
+ print "** bar/xyzzy case"
+ cbreak;
+ }
+ matching (/x/)
+ print "** /x/ case"
+ cbreak;
+ otherwise
+ print "** default"
+ cbreak;
+ }
+}'
+:
+** foo case: fallthrough!
+** bar/xyzzy case
+** bar/xyzzy case
+** bar/xyzzy case
+** /x/ case
+** default
+--
+3:
+$cppawk '
+#include <case.h>
+function f(arg, case_temps)
+{
+ case (arg) {
+ of (1, 2, 3) cret(arg * 10)
+ otherwise cret(-arg)
+ }
+}
+
+BEGIN { print f(1), f(2), f(3), f(4) }'
+:
+10 20 30 -4