Skip to content

Commit

Permalink
tests: expand testing of augmented assignment operators (#70)
Browse files Browse the repository at this point in the history
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 b4fdc8e (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: #55
Refs: #57
Refs: #71
  • Loading branch information
ee7 authored Jul 5, 2024
1 parent 8696dc7 commit fa231d8
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 26 deletions.
12 changes: 0 additions & 12 deletions tests/assignops.c4m

This file was deleted.

44 changes: 44 additions & 0 deletions tests/assignops_all.c4m
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions tests/assignops_divide.c4m
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions tests/assignops_multiply.c4m
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 52 additions & 0 deletions tests/assignops_shift_left.c4m
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions tests/assignops_shift_right.c4m
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions tests/assignops_xor.c4m
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 0 additions & 3 deletions tests/basic26.c4m

This file was deleted.

11 changes: 0 additions & 11 deletions tests/basic27.c4m

This file was deleted.

0 comments on commit fa231d8

Please sign in to comment.