aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-09-11 00:04:08 +0900
committernsfisis <nsfisis@gmail.com>2025-09-11 00:35:34 +0900
commitb3efe5b6c04e809ba0a67308994c91adfbd15eff (patch)
tree5ac9bf1702a21e9a952bce90ee934dc3bd455a42
parent9a2fbf6f4854b1f2cfd5979b91d5b3d7e183d487 (diff)
downloadducc-b3efe5b6c04e809ba0a67308994c91adfbd15eff.tar.gz
ducc-b3efe5b6c04e809ba0a67308994c91adfbd15eff.tar.zst
ducc-b3efe5b6c04e809ba0a67308994c91adfbd15eff.zip
test: rewrite some tests in C language
-rw-r--r--tests/helpers.h20
-rw-r--r--tests/test_arithmetic_operators.sh33
-rw-r--r--tests/test_assignment_operators.sh43
-rw-r--r--tests/test_bitwise_operators.sh68
-rw-r--r--tests/test_bool_type.sh16
-rw-r--r--tests/test_char_literals.sh72
-rw-r--r--tests/test_comparison_operators.sh52
-rw-r--r--tests/test_function_basics.sh59
-rw-r--r--tests/test_if_else.sh21
-rw-r--r--tests/test_local_variables.sh23
-rw-r--r--tests/test_recursive_functions.sh7
-rw-r--r--tests/test_ternary_operator.sh12
12 files changed, 129 insertions, 297 deletions
diff --git a/tests/helpers.h b/tests/helpers.h
new file mode 100644
index 0000000..f8affe7
--- /dev/null
+++ b/tests/helpers.h
@@ -0,0 +1,20 @@
+#ifndef DUCC_TESTS_HELPERS_H
+#define DUCC_TESTS_HELPERS_H
+
+void exit(int);
+struct FILE;
+typedef struct FILE FILE;
+int fprintf(FILE*, const char*, ...);
+extern FILE* stderr;
+
+#define ASSERT(a, file, line) \
+ do { \
+ if (!(a)) { \
+ fprintf(stderr, "%s:%d: assert failed", file, line); \
+ exit(1); \
+ } \
+ } while (0)
+
+#define ASSERT_EQ(a, b) ASSERT((a) == (b), __FILE__, __LINE__)
+
+#endif
diff --git a/tests/test_arithmetic_operators.sh b/tests/test_arithmetic_operators.sh
index 50cb1ec..a98ca15 100644
--- a/tests/test_arithmetic_operators.sh
+++ b/tests/test_arithmetic_operators.sh
@@ -1,30 +1,11 @@
-test_exit_code 42 <<'EOF'
-int main() {
- return 42;
-}
-EOF
-
-test_exit_code 21 <<'EOF'
-int main() {
- return 5+20-4;
-}
-EOF
-
-test_exit_code 26 <<'EOF'
-int main() {
- return 2*3+4*5;
-}
-EOF
-
-test_exit_code 197 <<'EOF'
-int main() {
- return (((3+5)/2) + (5*(9-6)) * (5+6*7)) % 256;
-}
-EOF
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
-test_exit_code 30 <<'EOF'
int main() {
- return (-10 + 20 * -3) + 100;
+ ASSERT_EQ(42, 42);
+ ASSERT_EQ(21, 5+20-4);
+ ASSERT_EQ(26, 2*3+4*5);
+ ASSERT_EQ(197, (((3+5)/2) + (5*(9-6)) * (5+6*7)) % 256);
+ ASSERT_EQ(30, (-10 + 20 * -3) + 100);
}
EOF
-
diff --git a/tests/test_assignment_operators.sh b/tests/test_assignment_operators.sh
index c1cce28..541abc8 100644
--- a/tests/test_assignment_operators.sh
+++ b/tests/test_assignment_operators.sh
@@ -1,49 +1,26 @@
-cat <<'EOF' > expected
-0
-1
-2
-3
-4
-
-5
-4
-3
-2
-1
-0
-
-56088
-
-24
-
-3
-EOF
-test_diff <<'EOF'
-int printf();
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
int main() {
int i = 0;
- for (; i < 5; i += 1) {
- printf("%d\n", i);
- }
- printf("\n");
- for (i = 5; i >= 0; i -= 1) {
- printf("%d\n", i);
- }
+ for (; i < 5; i += 1) {}
+ ASSERT_EQ(5, i);
+
+ for (i = 5; i >= 0; i -= 1);
+ ASSERT_EQ(-1, i);
int x = 123;
x *= 456;
- printf("\n%d\n", x);
+ ASSERT_EQ(56088, x);
int y = 120;
y /= 5;
- printf("\n%d\n", y);
+ ASSERT_EQ(24, y);
int z = 17;
z %= 7;
- printf("\n%d\n", z);
+ ASSERT_EQ(3, z);
return 0;
}
EOF
-
diff --git a/tests/test_bitwise_operators.sh b/tests/test_bitwise_operators.sh
index 90f553a..fe1f245 100644
--- a/tests/test_bitwise_operators.sh
+++ b/tests/test_bitwise_operators.sh
@@ -1,65 +1,29 @@
-cat <<'EOF' > expected
-123
-460
-EOF
-
-test_diff <<'EOF'
-int printf();
-
-int main() {
- printf("%d\n", 0 | 123);
- printf("%d\n", 12 | 456);
-}
-EOF
-
-cat <<'EOF' > expected
-8
-336
-EOF
-test_diff <<'EOF'
-int printf();
-int main() {
- printf("%d\n", 1 << 3);
- printf("%d\n", 21 << 4);
-}
-EOF
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
-cat <<'EOF' > expected
-13
-0
-EOF
-test_diff <<'EOF'
-int printf();
int main() {
- printf("%d\n", 111 >> 3);
- printf("%d\n", 15 >> 14);
-}
-EOF
+ ASSERT_EQ(123, 0 | 123);
+ ASSERT_EQ(460, 12 | 456);
-cat <<'EOF' > expected
-1
-7
-6
-4
-3 2 3 0
-EOF
-test_diff <<'EOF'
-int printf();
+ ASSERT_EQ(8, 1 << 3);
+ ASSERT_EQ(336, 21 << 4);
+ ASSERT_EQ(13, 111 >> 3);
+ ASSERT_EQ(0, 15 >> 14);
-int main() {
int a = 5;
int b = 3;
-
- printf("%d\n", a & b);
- printf("%d\n", a | b);
- printf("%d\n", a ^ b);
- printf("%d\n", 2 + 3 & 4);
+ ASSERT_EQ(1, a & b);
+ ASSERT_EQ(7, a | b);
+ ASSERT_EQ(6, a ^ b);
+ ASSERT_EQ(4, 2 + 3 & 4);
int c = 1 + 2 & 3;
int d = 4 & 5 ^ 6;
int e = 1 ^ 2 | 3;
int f = 0 | 1 & 2;
- printf("%d %d %d %d\n", c, d, e, f);
+ ASSERT_EQ(3, c);
+ ASSERT_EQ(2, d);
+ ASSERT_EQ(3, e);
+ ASSERT_EQ(0, f);
}
EOF
-
diff --git a/tests/test_bool_type.sh b/tests/test_bool_type.sh
index 16087bf..d18fd30 100644
--- a/tests/test_bool_type.sh
+++ b/tests/test_bool_type.sh
@@ -1,14 +1,12 @@
-cat <<'EOF' > expected
-1,0
-1,1,1
-EOF
-test_diff <<'EOF'
-int printf(const char*, ...);
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
int main() {
bool b1 = true, b0 = false;
- printf("%d,%d\n", b1, b0);
- printf("%d,%d,%d\n", sizeof(b1), sizeof(b0), sizeof(bool));
+ ASSERT_EQ(1, b1);
+ ASSERT_EQ(0, b0);
+ ASSERT_EQ(1, sizeof(b1));
+ ASSERT_EQ(1, sizeof(b0));
+ ASSERT_EQ(1, sizeof(bool));
}
EOF
-
diff --git a/tests/test_char_literals.sh b/tests/test_char_literals.sh
index 3d1bff3..c883eba 100644
--- a/tests/test_char_literals.sh
+++ b/tests/test_char_literals.sh
@@ -1,59 +1,25 @@
-cat <<'EOF' > expected
-97 48
-92 39
-10
-EOF
-test_diff <<'EOF'
-int printf();
-
-int main() {
- printf("%d %d\n", 'a', '0');
- printf("%d %d\n", '\\', '\'');
- printf("%d\n", '\n');
- return 0;
-}
-EOF
-
-cat <<'EOF' > expected
-39
-34
-63
-92
-7
-8
-12
-10
-13
-9
-11
-EOF
-test_diff <<'EOF'
-int printf();
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
int main() {
- printf("%d\n", '\'');
- printf("%d\n", '\"');
- printf("%d\n", '\?');
- printf("%d\n", '\\');
- printf("%d\n", '\a');
- printf("%d\n", '\b');
- printf("%d\n", '\f');
- printf("%d\n", '\n');
- printf("%d\n", '\r');
- printf("%d\n", '\t');
- printf("%d\n", '\v');
- return 0;
-}
-EOF
+ ASSERT_EQ(97, 'a');
+ ASSERT_EQ(48, '0');
+ ASSERT_EQ(92, '\\');
+ ASSERT_EQ(39, '\'');
+ ASSERT_EQ(10, '\n');
-cat <<'EOF' > expected
-0
-EOF
-test_diff <<'EOF'
-int printf();
+ ASSERT_EQ(39, '\'');
+ ASSERT_EQ(34, '\"');
+ ASSERT_EQ(63, '\?');
+ ASSERT_EQ(92, '\\');
+ ASSERT_EQ(7, '\a');
+ ASSERT_EQ(8, '\b');
+ ASSERT_EQ(12, '\f');
+ ASSERT_EQ(10, '\n');
+ ASSERT_EQ(13, '\r');
+ ASSERT_EQ(9, '\t');
+ ASSERT_EQ(11, '\v');
-int main() {
- printf("%d\n", '\0');
+ ASSERT_EQ(0, '\0');
}
EOF
-
diff --git a/tests/test_comparison_operators.sh b/tests/test_comparison_operators.sh
index 3b6d827..7bcb689 100644
--- a/tests/test_comparison_operators.sh
+++ b/tests/test_comparison_operators.sh
@@ -1,48 +1,14 @@
-test_exit_code 1 <<'EOF'
-int main() {
- return 0 == 0;
-}
-EOF
-
test_exit_code 0 <<'EOF'
-int main() {
- return 123 != 123;
-}
-EOF
-
-test_exit_code 1 <<'EOF'
-int main() {
- return 123 != 456;
-}
-EOF
-
-test_exit_code 0 <<'EOF'
-int main() {
- return 123 == 124;
-}
-EOF
-
-test_exit_code 1 <<'EOF'
-int main() {
- return 123 < 567;
-}
-EOF
+#include "../../helpers.h"
-test_exit_code 1 <<'EOF'
int main() {
- return 123 <= 567;
+ ASSERT_EQ(1, 0 == 0);
+ ASSERT_EQ(0, 123 != 123);
+ ASSERT_EQ(1, 123 != 456);
+ ASSERT_EQ(0, 123 == 124);
+ ASSERT_EQ(1, 123 < 567);
+ ASSERT_EQ(1, 123 <= 567);
+ ASSERT_EQ(1, 123 <= 123);
+ ASSERT_EQ(0, 123 < 123);
}
EOF
-
-test_exit_code 1 <<'EOF'
-int main() {
- return 123 <= 123;
-}
-EOF
-
-test_exit_code 0 <<'EOF'
-int main() {
- return 123 < 123;
-}
-EOF
-
diff --git a/tests/test_function_basics.sh b/tests/test_function_basics.sh
index eb99cf5..f064622 100644
--- a/tests/test_function_basics.sh
+++ b/tests/test_function_basics.sh
@@ -1,4 +1,6 @@
-test_exit_code 66 <<'EOF'
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
+
int foo() {
int i;
int ret;
@@ -13,68 +15,38 @@ int foo() {
return ret;
}
-int main() {
- return foo();
-}
-EOF
-
-test_exit_code 10 <<'EOF'
int f(int a, int b, int c, int d, int e, int f) {
return a;
}
-int main() {
- return 10 * f(1, 2, 3, 4, 5, 6);
-}
-EOF
-
-test_exit_code 20 <<'EOF'
-int f(int a, int b, int c, int d, int e, int f) {
+int f2(int a, int b, int c, int d, int e, int f) {
return b;
}
-int main() {
- return 10 * f(1, 2, 3, 4, 5, 6);
-}
-EOF
-
-test_exit_code 30 <<'EOF'
-int f(int a, int b, int c, int d, int e, int f) {
+int f3(int a, int b, int c, int d, int e, int f) {
return c;
}
-int main() {
- return 10 * f(1, 2, 3, 4, 5, 6);
-}
-EOF
-
-test_exit_code 40 <<'EOF'
-int f(int a, int b, int c, int d, int e, int f) {
+int f4(int a, int b, int c, int d, int e, int f) {
return d;
}
-int main() {
- return 10 * f(1, 2, 3, 4, 5, 6);
-}
-EOF
-
-test_exit_code 50 <<'EOF'
-int f(int a, int b, int c, int d, int e, int f) {
+int f5(int a, int b, int c, int d, int e, int f) {
return e;
}
-int main() {
- return 10 * f(1, 2, 3, 4, 5, 6);
-}
-EOF
-
-test_exit_code 60 <<'EOF'
-int f(int a, int b, int c, int d, int e, int f) {
+int f6(int a, int b, int c, int d, int e, int f) {
return f;
}
int main() {
- return 10 * f(1, 2, 3, 4, 5, 6);
+ ASSERT_EQ(66, foo());
+ ASSERT_EQ(10, 10 * f(1, 2, 3, 4, 5, 6));
+ ASSERT_EQ(20, 10 * f2(1, 2, 3, 4, 5, 6));
+ ASSERT_EQ(30, 10 * f3(1, 2, 3, 4, 5, 6));
+ ASSERT_EQ(40, 10 * f4(1, 2, 3, 4, 5, 6));
+ ASSERT_EQ(50, 10 * f5(1, 2, 3, 4, 5, 6));
+ ASSERT_EQ(60, 10 * f6(1, 2, 3, 4, 5, 6));
}
EOF
@@ -84,4 +56,3 @@ int main() {
return 0;
}
EOF
-
diff --git a/tests/test_if_else.sh b/tests/test_if_else.sh
index cad1716..f1da0d1 100644
--- a/tests/test_if_else.sh
+++ b/tests/test_if_else.sh
@@ -1,20 +1,21 @@
-test_exit_code 12 <<'EOF'
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
+
int main() {
+ int result1;
if (1) {
- return 12;
+ result1 = 12;
} else {
- return 34;
+ result1 = 34;
}
-}
-EOF
+ ASSERT_EQ(12, result1);
-test_exit_code 34 <<'EOF'
-int main() {
+ int result2;
if (1 + 1 != 2) {
- return 12;
+ result2 = 12;
} else {
- return 34;
+ result2 = 34;
}
+ ASSERT_EQ(34, result2);
}
EOF
-
diff --git a/tests/test_local_variables.sh b/tests/test_local_variables.sh
index 09c40b9..dfa704b 100644
--- a/tests/test_local_variables.sh
+++ b/tests/test_local_variables.sh
@@ -1,23 +1,15 @@
-test_exit_code 42 <<'EOF'
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
+
int main() {
int foo;
foo = 42;
- return foo;
-}
-EOF
+ ASSERT_EQ(42, foo);
-test_exit_code 70 <<'EOF'
-int main() {
- int foo;
int bar;
- foo = 42;
bar = 28;
- return foo + bar;
-}
-EOF
+ ASSERT_EQ(70, foo + bar);
-test_exit_code 45 <<'EOF'
-int main() {
int a1;
int a2;
int a3;
@@ -38,7 +30,7 @@ int main() {
a8 = 8;
a9 = 9;
- return
+ ASSERT_EQ(45,
a1 +
a2 +
a3 +
@@ -48,7 +40,7 @@ int main() {
a7 +
a8 +
a9 +
- 0;
+ 0);
}
EOF
@@ -64,4 +56,3 @@ int main() {
return 0;
}
EOF
-
diff --git a/tests/test_recursive_functions.sh b/tests/test_recursive_functions.sh
index f9f0ee5..709cfbe 100644
--- a/tests/test_recursive_functions.sh
+++ b/tests/test_recursive_functions.sh
@@ -1,4 +1,6 @@
-test_exit_code 89 <<'EOF'
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
+
int fib(int n) {
if (n <= 1) {
return 1;
@@ -8,7 +10,6 @@ int fib(int n) {
}
int main() {
- return fib(10);
+ ASSERT_EQ(89, fib(10));
}
EOF
-
diff --git a/tests/test_ternary_operator.sh b/tests/test_ternary_operator.sh
index c98ff43..c63f581 100644
--- a/tests/test_ternary_operator.sh
+++ b/tests/test_ternary_operator.sh
@@ -1,12 +1,8 @@
-cat <<'EOF' > expected
-2 5
-EOF
-
-test_diff <<'EOF'
-int printf();
+test_exit_code 0 <<'EOF'
+#include "../../helpers.h"
int main() {
- printf("%d %d\n", 1 ? 2 : 3, 0 ? 4 : 5);
+ ASSERT_EQ(2, 1 ? 2 : 3);
+ ASSERT_EQ(5, 0 ? 4 : 5);
}
EOF
-