[PATCH] [NOT FOR MERGE] Add arithmetic expansion conformance test

Details
Message ID
<20181104142009.2510-1-sir@cmpwn.com>
Download raw message
Patch +165 -0
---
We don't actually implement arithmetic expansion yet, but this will be
useful for testing it when we do.

 test/conformance/arithm.sh | 164 +++++++++++++++++++++++++++++++++++++
 test/meson.build           |   1 +
 2 files changed, 165 insertions(+)
 create mode 100644 test/conformance/arithm.sh

diff --git a/test/conformance/arithm.sh b/test/conformance/arithm.sh
new file mode 100644
index 0000000..bfb0536
--- /dev/null
+++ b/test/conformance/arithm.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+set -e
+
+echo >&2 "test unary +"
+[ $((+5)) -eq 5 ]
+
+echo >&2 "test unary -"
+[ $((-5)) -eq -5 ]
+
+echo >&2 "test ~"
+[ $((~5)) -eq -6 ]
+
+echo >&2 "test !"
+[ $((!10)) -eq 0 ]
+
+echo >&2 "test *"
+[ $((2*5)) -eq 10 ]
+
+echo >&2 "test /"
+[ $((10/2)) -eq 5 ]
+
+echo >&2 "test %"
+[ $((12%5)) -eq 2 ]
+
+echo >&2 "test +"
+[ $((5+3)) -eq 8 ]
+
+echo >&2 "test -"
+[ $((10-2)) -eq 8 ]
+
+echo >&2 "test - (with negative result)"
+[ $((5-10)) -eq -5 ]
+
+echo >&2 "test <<"
+[ $((8<<1)) -eq 16 ]
+
+echo >&2 "test >>"
+[ $((8>>1)) -eq 4 ]
+
+echo >&2 "test < (false)"
+[ $((10<5)) -eq 0 ]
+
+echo >&2 "test < (true)"
+[ $((5<10)) -eq 1 ]
+
+echo >&2 "test > (false)"
+[ $((5>10)) -eq 0 ]
+
+echo >&2 "test > (true)"
+[ $((10>5)) -eq 1 ]
+
+echo >&2 "test <= (false)"
+[ $((10<=5)) -eq 0 ]
+
+echo >&2 "test <= (true)"
+[ $((5<=10)) -eq 1 ]
+
+echo >&2 "test <= (equal)"
+[ $((5<=5)) -eq 1 ]
+
+echo >&2 "test >= (false)"
+[ $((5>=10)) -eq 0 ]
+
+echo >&2 "test >= (true)"
+[ $((10>=5)) -eq 1 ]
+
+echo >&2 "test >= (equal)"
+[ $((5>=5)) -eq 1 ]
+
+echo >&2 "test == (true)"
+[ $((5==5)) -eq 1 ]
+
+echo >&2 "test == (false)"
+[ $((5==5)) -eq 1 ]
+
+echo >&2 "test &"
+[ $((12&8)) -eq 8 ]
+
+echo >&2 "test |"
+[ $((8|4)) -eq 12 ]
+
+echo >&2 "test ^"
+[ $((12^8)) -eq 4 ]
+
+echo >&2 "test && (true)"
+[ $((1&&2)) -eq 1 ]
+
+echo >&2 "test && (false)"
+[ $((1&&0)) -eq 0 ]
+
+echo >&2 "test || (true)"
+[ $((1||2)) -eq 1 ]
+
+echo >&2 "test || (false)"
+[ $((0||0)) -eq 0 ]
+
+echo >&2 "test x?y:z (y branch)"
+[ $((1?10:20)) -eq 10 ]
+
+echo >&2 "test x?y:z (z branch)"
+[ $((0?10:20)) -eq 20 ]
+
+echo >&2 "test ="
+[ $((x=20)) -eq 20 ]
+[ $x -eq 20 ]
+
+echo >&2 "test = (time of assignment)"
+[ $((x=x/2)) -eq 10 ]
+[ $x -eq 10 ]
+
+echo >&2 "test +="
+[ $((x+=10)) -eq 20 ]
+[ $x -eq 20 ]
+
+echo >&2 "test -="
+[ $((x-=10)) -eq 10 ]
+[ $x -eq 10 ]
+
+echo >&2 "test *="
+[ $((x*=10)) -eq 100 ]
+[ $x -eq 100 ]
+
+echo >&2 "test /="
+[ $((x/=10)) -eq 10 ]
+[ $x -eq 10 ]
+
+echo >&2 "test %="
+[ $((x%=10)) -eq 0 ]
+[ $x -eq 0 ]
+
+echo >&2 "test >>="
+x=8
+[ $((x>>=1)) -eq 4 ]
+[ $x -eq 4 ]
+
+echo >&2 "test <<="
+[ $((x<<=1)) -eq 8 ]
+[ $x -eq 8 ]
+
+echo >&2 "test |="
+[ $((x|=1)) -eq 9 ]
+[ $x -eq 9 ]
+
+echo >&2 "test &="
+[ $((x&=8)) -eq 8 ]
+[ $x -eq 8 ]
+
+echo >&2 "test ^="
+[ $((x^=2)) -eq 10 ]
+[ $x -eq 10 ]
+
+echo >&2 "test hexadecimal constants"
+[ $((0x10)) -eq 16 ]
+
+echo >&2 "test octal constants"
+[ $((0777)) -eq 511 ]
+
+echo >&2 "test variable expansion syntax"
+x=10
+y=10
+[ $((x+y)) -eq 20 ]
+[ $(($x+$y)) -eq 20 ]
+
+echo >&2 "all tests passed."
diff --git a/test/meson.build b/test/meson.build
index 29516f7..18c8ef5 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -2,6 +2,7 @@ harness = find_program('./harness.sh')
 ref_sh = find_program('sh', required: false)
 
 test_files = [
+	'conformance/arithm.sh',
 	'conformance/if.sh',
 
 	'loop.sh',
-- 
2.19.1