From 93226de7e205f69603a29dd44f39a22c82935ad1 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 24 Jan 2023 20:11:34 +0200 Subject: [PATCH] Combine +-*/% tests into the same file (#137) --- tests/should_succeed/add.jou | 28 ------ tests/should_succeed/add_sub_mul_div_mod.jou | 89 ++++++++++++++++++++ tests/should_succeed/divide.jou | 26 ------ tests/should_succeed/mod.jou | 32 ------- tests/should_succeed/multiply.jou | 20 ----- 5 files changed, 89 insertions(+), 106 deletions(-) delete mode 100644 tests/should_succeed/add.jou create mode 100644 tests/should_succeed/add_sub_mul_div_mod.jou delete mode 100644 tests/should_succeed/divide.jou delete mode 100644 tests/should_succeed/mod.jou delete mode 100644 tests/should_succeed/multiply.jou diff --git a/tests/should_succeed/add.jou b/tests/should_succeed/add.jou deleted file mode 100644 index 4149d41f..00000000 --- a/tests/should_succeed/add.jou +++ /dev/null @@ -1,28 +0,0 @@ -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)) - return 0 diff --git a/tests/should_succeed/add_sub_mul_div_mod.jou b/tests/should_succeed/add_sub_mul_div_mod.jou new file mode 100644 index 00000000..fcb457df --- /dev/null +++ b/tests/should_succeed/add_sub_mul_div_mod.jou @@ -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 diff --git a/tests/should_succeed/divide.jou b/tests/should_succeed/divide.jou deleted file mode 100644 index b6df3fcd..00000000 --- a/tests/should_succeed/divide.jou +++ /dev/null @@ -1,26 +0,0 @@ -from "stdlib/io.jou" import printf - -def side_effect(n: int) -> int: - printf("%d ", n) - return n - -def main() -> int: - 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) - return 0 diff --git a/tests/should_succeed/mod.jou b/tests/should_succeed/mod.jou deleted file mode 100644 index 5b8f855f..00000000 --- a/tests/should_succeed/mod.jou +++ /dev/null @@ -1,32 +0,0 @@ -from "stdlib/io.jou" import printf - -def main() -> int: - # 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 diff --git a/tests/should_succeed/multiply.jou b/tests/should_succeed/multiply.jou deleted file mode 100644 index 0daa389a..00000000 --- a/tests/should_succeed/multiply.jou +++ /dev/null @@ -1,20 +0,0 @@ -from "stdlib/io.jou" import printf - -def side_effect(n: int) -> int: - printf("%d ", n) - return n - -def main() -> int: - 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) - return 0