From fa231d8f0962c3c9b58b09812f50cfc259da7e70 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Fri, 5 Jul 2024 18:25:36 +0200 Subject: [PATCH] tests: expand testing of augmented assignment operators (#70) Add some more tests for augmented assignment operators, and remove some existing ones that had similar coverage. assignops.c4m was similar to basic10.c4m: """ Basic test of augmented assignment. """ """ $output: 45 """ x = 0 for i in 0 to 10 { x += i } print(x) The grammar specifies the augmented assignment operators here: assign ::= expression assignmentOp expression EOS assignmentOp ::= ':' | '=' | '+=' | '-=' | '*=' | '/=' | '|=' | '&=' | '^=' | '>>=' | '<<=' Note that commit b4fdc8e2e70b (2024-07-03, "Reworking of lists, etc") changed the behavior of the /= operator so that: - For division by zero, SIGFPE is no longer produced. - For division by a negative number, the result is now correct. Refs: https://github.com/crashappsec/libcon4m/issues/55 Refs: https://github.com/crashappsec/libcon4m/issues/57 Refs: https://github.com/crashappsec/libcon4m/issues/71 --- tests/assignops.c4m | 12 -------- tests/assignops_all.c4m | 44 ++++++++++++++++++++++++++++ tests/assignops_divide.c4m | 32 ++++++++++++++++++++ tests/assignops_multiply.c4m | 32 ++++++++++++++++++++ tests/assignops_shift_left.c4m | 52 +++++++++++++++++++++++++++++++++ tests/assignops_shift_right.c4m | 42 ++++++++++++++++++++++++++ tests/assignops_xor.c4m | 32 ++++++++++++++++++++ tests/basic26.c4m | 3 -- tests/basic27.c4m | 11 ------- 9 files changed, 234 insertions(+), 26 deletions(-) delete mode 100644 tests/assignops.c4m create mode 100644 tests/assignops_all.c4m create mode 100644 tests/assignops_divide.c4m create mode 100644 tests/assignops_multiply.c4m create mode 100644 tests/assignops_shift_left.c4m create mode 100644 tests/assignops_shift_right.c4m create mode 100644 tests/assignops_xor.c4m delete mode 100644 tests/basic26.c4m delete mode 100644 tests/basic27.c4m diff --git a/tests/assignops.c4m b/tests/assignops.c4m deleted file mode 100644 index 8c46abb2..00000000 --- a/tests/assignops.c4m +++ /dev/null @@ -1,12 +0,0 @@ -x = 2 -x += 2 -x -= 2 -assert x == 2 - -for i from 0 to 10 { - x += 2 - assert $len == 10 -} - -assert x == 22 - diff --git a/tests/assignops_all.c4m b/tests/assignops_all.c4m new file mode 100644 index 00000000..4fbdac00 --- /dev/null +++ b/tests/assignops_all.c4m @@ -0,0 +1,44 @@ +""" +Basic test of every augmented assignment operator. +""" +""" +$output: +3 +-2 +-12 +6 +7 +5 +13 +3 +6 +""" + +x = 2 + +x += 1 +print(x) + +x -= 5 +print(x) + +x *= 6 +print(x) + +x /= -2 +print(x) + +x |= 7 +print(x) + +x &= 13 +print(x) + +x ^= 8 +print(x) + +x >>= 2 +print(x) + +x <<= 1 +print(x) diff --git a/tests/assignops_divide.c4m b/tests/assignops_divide.c4m new file mode 100644 index 00000000..0b225159 --- /dev/null +++ b/tests/assignops_divide.c4m @@ -0,0 +1,32 @@ +""" +Basic test of augmented assignment operator: /= +""" +""" +$output: +12 +6 +-6 +-3 +3 +""" + +x = 12 + +x /= 1 +print(x) + +x /= 2 +print(x) + +x /= -1 +print(x) + +x /= 2 +print(x) + +x /= -1 +print(x) + +# TODO: remove this later. See basic25.c4m. +x /= 0 +print(x) diff --git a/tests/assignops_multiply.c4m b/tests/assignops_multiply.c4m new file mode 100644 index 00000000..7c392444 --- /dev/null +++ b/tests/assignops_multiply.c4m @@ -0,0 +1,32 @@ +""" +Basic test of augmented assignment operator: *= +""" +""" +$output: +3 +6 +-6 +-12 +24 +0 +""" + +x = 3 + +x *= 1 +print(x) + +x *= 2 +print(x) + +x *= -1 +print(x) + +x *= 2 +print(x) + +x *= -2 +print(x) + +x *= 0 +print(x) diff --git a/tests/assignops_shift_left.c4m b/tests/assignops_shift_left.c4m new file mode 100644 index 00000000..9dc0128e --- /dev/null +++ b/tests/assignops_shift_left.c4m @@ -0,0 +1,52 @@ +""" +Basic test of augmented assignment operator: <<= +""" +""" +$output: +0 +0 +1 +2 +8 +4611686018427387904 +-1 +-2 +-2305843009213693952 +-4611686018427387904 +""" + +x = 0 + +x <<= 0 +print(x) + +x <<= 1 +print(x) + +x = 1 + +x <<= 0 +print(x) + +x <<= 1 +print(x) + +x <<= 2 +print(x) + +x <<= 59 +print(x) + +x = -1 + +x <<= 0 +print(x) + +x <<= 1 +print(x) + +x <<= 60 +print(x) + +x <<= 1 +print(x) diff --git a/tests/assignops_shift_right.c4m b/tests/assignops_shift_right.c4m new file mode 100644 index 00000000..0492a7a1 --- /dev/null +++ b/tests/assignops_shift_right.c4m @@ -0,0 +1,42 @@ +""" +Basic test of augmented assignment operator: >>= +""" +""" +$output: +9223372036854775807 +9223372036854775807 +4611686018427387903 +15 +7 +1 +0 +0 +0 +""" + +x = 0x7fffffffffffffff +print(x) + +x >>= 0 +print(x) + +x >>= 1 +print(x) + +x >>= 58 +print(x) + +x >>= 1 +print(x) + +x >>= 2 +print(x) + +x >>= 1 +print(x) + +x >>= 0 +print(x) + +x >>= 1 +print(x) diff --git a/tests/assignops_xor.c4m b/tests/assignops_xor.c4m new file mode 100644 index 00000000..e88f3ea7 --- /dev/null +++ b/tests/assignops_xor.c4m @@ -0,0 +1,32 @@ +""" +Basic test of augmented assignment operator: ^= +""" +""" +$output: +2 +0 +-2 +-4 +-4 +3 +""" + +x = 3 + +x ^= 1 +print(x) + +x ^= 2 +print(x) + +x ^= -2 +print(x) + +x ^= 2 +print(x) + +x ^= 0 +print(x) + +x ^= -1 +print(x) diff --git a/tests/basic26.c4m b/tests/basic26.c4m deleted file mode 100644 index b3766eda..00000000 --- a/tests/basic26.c4m +++ /dev/null @@ -1,3 +0,0 @@ -x = 60 -x *= -2 -print(x) \ No newline at end of file diff --git a/tests/basic27.c4m b/tests/basic27.c4m deleted file mode 100644 index 2eecf026..00000000 --- a/tests/basic27.c4m +++ /dev/null @@ -1,11 +0,0 @@ -""" -Test negation. -""" -""" -$output: -100 -""" -x = 100 -x *= -2 -x /= -2 -print(x) \ No newline at end of file