Skip to content

Commit

Permalink
Combine +-*/% tests into the same file (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Jan 24, 2023
1 parent 961efb1 commit 93226de
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 106 deletions.
28 changes: 0 additions & 28 deletions tests/should_succeed/add.jou

This file was deleted.

89 changes: 89 additions & 0 deletions tests/should_succeed/add_sub_mul_div_mod.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from "stdlib/io.jou" import printf

def side_effect(ch: int) -> int:
printf("%d ", ch)
return ch

def main() -> int:
printf("%d\n", 7+15) # Output: 22
printf("%d\n", -7+15) # Output: 8
printf("%d\n", 7-15) # Output: -8
printf("%d\n", -7-15) # Output: -22

printf("%d\n", 1+2*3) # Output: 7
printf("%d\n", 1+(2*3)) # Output: 7
printf("%d\n", (1+2)*3) # Output: 9

# '2' converts to int 50
printf("%d\n", 1000 + '2') # Output: 1050

# (3 * ASCII 'a') % 256 = (3*97) % 256 = 35
# TODO: this seems dumb, should they convert to int instead?
printf("%d\n", 'a') # Output: 97
printf("%d\n", 'a'+'a') # Output: 194
printf("%d\n", 'a'+'a'+'a') # Output: 35

# Output: 1 2 3 4 10
printf("%d\n", side_effect(1) + side_effect(2) + side_effect(3) + side_effect(4))

printf("%d\n", 7*7*2) # Output: 98

# '2' converts to int 50
printf("%d\n", 10*'2') # Output: 500

# (ASCII 'a' * ASCII 'e') % 256 = (97*101) % 256 = 9797 % 256 = 69
# TODO: this seems dumb, what would C do here?
printf("%d\n", 'a'*'e') # Output: 69

# Output: 1 2 3 4 24
x = side_effect(1)*side_effect(2)*side_effect(3)*side_effect(4)
printf("%d\n", x)

printf("%d\n", 7/2) # Output: 3
printf("%d\n", 4/2*3) # Output: 6
printf("%d\n", (4/2)*3) # Output: 6
printf("%d\n", 4/(2*3)) # Output: 0

# Dividing bytes is unsigned 8-bit division.
# With signed division, 8-bit 255/2 would be (-1)/2 which truncates to 0.
printf("%d / %d = %d\n", 0xff as byte, 2 as byte, (0xff as byte)/(2 as byte)) # Output: 255 / 2 = 127

# Dividing ints is signed 32-bit floor division.
printf("%d / %d = %d\n", 7, 2, 7/2) # Output: 7 / 2 = 3
printf("%d / %d = %d\n", -7, 2, (-7)/2) # Output: -7 / 2 = -4
printf("%d / %d = %d\n", 7, -2, 7/(-2)) # Output: 7 / -2 = -4
printf("%d / %d = %d\n", -7, -2, (-7)/(-2)) # Output: -7 / -2 = 3

# Output: 6 3 2 1
x = side_effect(6)/side_effect(3)/side_effect(2)
printf("%d\n", x)

# Output: 222222
printf(
"%d%d%d%d%d%d\n",
7 % 5,
(-3) % 5,
7 % (5 as byte),
(-3) % (5 as byte),
(7 as byte) % 5,
(7 as byte) % (5 as byte),
)

# Mod with negative number --> negative result
# Output: -3 -3 -3 -3 -3
printf(
"%d %d %d %d %d\n",
7 % (-5),
2 % (-5),
(-3) % (-5),
(7 as byte) % (-5),
(2 as byte) % (-5),
)

# Consistency between % and /. No output expected.
for a = -5; a < 5; a++:
for b = -5; b < 5; b++:
if b != 0 and a/b*b + a%b != a:
printf("remainder doesn't work correctly when dividing %d by %d (div=%d, rem=%d)\n", a, b, a/b, a%b)

return 0
26 changes: 0 additions & 26 deletions tests/should_succeed/divide.jou

This file was deleted.

32 changes: 0 additions & 32 deletions tests/should_succeed/mod.jou

This file was deleted.

20 changes: 0 additions & 20 deletions tests/should_succeed/multiply.jou

This file was deleted.

0 comments on commit 93226de

Please sign in to comment.