aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-09-13 00:05:12 +0900
committernsfisis <nsfisis@gmail.com>2025-09-13 01:38:56 +0900
commit8de7fa9da5fd8015f4fcc826b9270061b7b89478 (patch)
treeec9c1e56f179be207c31a113a0a96210f7509431 /tests
parentd41a97e957ef616d194f60b9b79820cd0162d920 (diff)
downloadducc-8de7fa9da5fd8015f4fcc826b9270061b7b89478.tar.gz
ducc-8de7fa9da5fd8015f4fcc826b9270061b7b89478.tar.zst
ducc-8de7fa9da5fd8015f4fcc826b9270061b7b89478.zip
feat: implement -E flag (preprocess only)
Diffstat (limited to 'tests')
-rw-r--r--tests/helpers.sh7
-rw-r--r--tests/test_function_macros.sh16
-rw-r--r--tests/test_if_elif_else.sh56
-rw-r--r--tests/test_ifdef_ifndef.sh39
-rw-r--r--tests/test_keywords.sh11
-rw-r--r--tests/test_macro_operators.sh45
-rw-r--r--tests/test_preprocess_flag.sh122
-rw-r--r--tests/test_undef.sh14
8 files changed, 261 insertions, 49 deletions
diff --git a/tests/helpers.sh b/tests/helpers.sh
index eaccd5c..a69e213 100644
--- a/tests/helpers.sh
+++ b/tests/helpers.sh
@@ -49,6 +49,13 @@ function test_compile_error() {
diff -u expected output
}
+function test_cpp() {
+ cat > main.c
+
+ "$ducc" -E main.c > output
+ diff -u -Z expected output
+}
+
function test_example() {
filename="../../../examples/$1.c"
diff --git a/tests/test_function_macros.sh b/tests/test_function_macros.sh
index 4bc0cc2..1108650 100644
--- a/tests/test_function_macros.sh
+++ b/tests/test_function_macros.sh
@@ -83,19 +83,24 @@ int main() {
EOF
cat <<'EOF' > expected
-42
-123
+int printf ( const char *, ...);
+
+int main () {
+ int foo = 42;
+ printf ( %d\n, foo);
+
+ int a = 123;
+ printf ( %d\n, a);
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf(const char*, ...);
int main() {
- // TODO: check if "foo" is expanded to "foo" or not once '#' operator is implemented.
#define foo foo
int foo = 42;
printf("%d\n", foo);
- // TODO: check if "a" is expanded to "a" or not once '#' operator is implemented.
#define a b
#define b c
#define c a
@@ -103,4 +108,3 @@ int main() {
printf("%d\n", a);
}
EOF
-
diff --git a/tests/test_if_elif_else.sh b/tests/test_if_elif_else.sh
index a07dd5c..e0adee0 100644
--- a/tests/test_if_elif_else.sh
+++ b/tests/test_if_elif_else.sh
@@ -1,8 +1,13 @@
cat <<'EOF' > expected
-1
+int printf ();
+
+
+int main () {
+ printf ( 1\n);
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
#define A
@@ -18,13 +23,20 @@ int main() {
EOF
cat <<'EOF' > expected
-1
-1
-2
-3
+int printf ();
+
+int main () {
+ printf ( 1\n);
+
+ printf ( 1\n);
+
+ printf ( 2\n);
+
+ printf ( 3\n);
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
int main() {
@@ -63,17 +75,28 @@ int main() {
EOF
cat <<'EOF' > expected
-1
-1
-2
-2
-3
-4
-3
-4
+int printf ();
+
+int main () {
+ printf ( 1\n);
+
+ printf ( 1\n);
+
+ printf ( 2\n);
+
+ printf ( 2\n);
+
+ printf ( 3\n);
+
+ printf ( 4\n);
+
+ printf ( 3\n);
+
+ printf ( 4\n);
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
int main() {
@@ -190,4 +213,3 @@ int main() {
#endif
}
EOF
-
diff --git a/tests/test_ifdef_ifndef.sh b/tests/test_ifdef_ifndef.sh
index f59b87b..683a1dc 100644
--- a/tests/test_ifdef_ifndef.sh
+++ b/tests/test_ifdef_ifndef.sh
@@ -1,10 +1,21 @@
cat <<'EOF' > expected
-__ducc__ is defined.
-A is defined.
-B is undefined.
+int printf ();
+
+
+int main () {
+
+ printf ( __ducc__ is defined.\n);
+
+ printf ( A is defined.\n);
+
+ printf ( B is undefined.\n);
+
+
+ return 0;
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
#define A 123
@@ -36,12 +47,23 @@ int main() {
EOF
cat <<'EOF' > expected
-__ducc__ is defined.
-A is defined.
-B is undefined.
+int printf ();
+
+
+int main () {
+
+ printf ( __ducc__ is defined.\n);
+
+ printf ( A is defined.\n);
+
+ printf ( B is undefined.\n);
+
+
+ return 0;
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
#define A 123
@@ -71,4 +93,3 @@ int main() {
return 0;
}
EOF
-
diff --git a/tests/test_keywords.sh b/tests/test_keywords.sh
index 1b99b67..0bdb6f7 100644
--- a/tests/test_keywords.sh
+++ b/tests/test_keywords.sh
@@ -1,9 +1,14 @@
-touch expected
-test_diff <<'EOF'
+cat <<'EOF' > expected
+
+
+int printf ();
+int main () {}
+EOF
+
+test_cpp <<'EOF'
// A keyword is treated as a normal identifier in preprocessing phase.
#define auto int
auto printf();
auto main() {}
EOF
-
diff --git a/tests/test_macro_operators.sh b/tests/test_macro_operators.sh
index a77d3ca..f9cc385 100644
--- a/tests/test_macro_operators.sh
+++ b/tests/test_macro_operators.sh
@@ -1,14 +1,31 @@
cat <<'EOF' > expected
-foobar=100
-prefix_test=200
-test_suffix=300
-var_1=10
-var_2=20
-var_A=30
-number_12=12
+int printf ();
+
+
+int foobar = 100;
+int prefix_test = 200;
+int test_suffix = 300;
+
+int var_1 = 10;
+int var_2 = 20;
+
+int var_A = 30;
+
+int number_12 = 12;
+
+int main () {
+ printf ( foobar=%d\n, foobar);
+ printf ( prefix_test=%d\n, prefix_test);
+ printf ( test_suffix=%d\n, test_suffix);
+ printf ( var_1=%d\n, var_1);
+ printf ( var_2=%d\n, var_2);
+ printf ( var_A=%d\n, var_A);
+ printf ( number_12=%d\n, number_12);
+ return 0;
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
#define CONCAT(a, b) a##b
@@ -42,9 +59,17 @@ int main() {
EOF
cat <<'EOF' > expected
-123
+int printf ( const char *, ...);
+
+
+int H ( int n) { return n; }
+
+int main () {
+ printf ( %d\n, H ( 123));
+}
EOF
-test_diff <<'EOF'
+
+test_cpp <<'EOF'
int printf(const char*, ...);
#define F(x) CHECK(G(x))
diff --git a/tests/test_preprocess_flag.sh b/tests/test_preprocess_flag.sh
new file mode 100644
index 0000000..3b52bc0
--- /dev/null
+++ b/tests/test_preprocess_flag.sh
@@ -0,0 +1,122 @@
+cat <<'EOF' > expected
+
+int main () {
+ int x = 100;
+ int y = 0;
+ return 0;
+}
+EOF
+
+test_cpp <<'EOF'
+#define MAX 100
+#define MIN 0
+
+int main() {
+ int x = MAX;
+ int y = MIN;
+ return 0;
+}
+EOF
+
+cat <<'EOF' > expected
+
+int main () {
+ int sum = ( ( 10) + ( 20));
+ int product = ( ( 3) * ( 4));
+ return 0;
+}
+EOF
+
+test_cpp <<'EOF'
+#define ADD(a, b) ((a) + (b))
+#define MUL(x, y) ((x) * (y))
+
+int main() {
+ int sum = ADD(10, 20);
+ int product = MUL(3, 4);
+ return 0;
+}
+EOF
+
+cat <<'EOF' > expected
+int main () {
+ return 42;
+}
+EOF
+
+test_cpp <<'EOF'
+#define FOO 42
+int main() {
+ return FOO;
+}
+EOF
+
+cat <<'EOF' > expected
+int foo () { return 1; }
+EOF
+
+test_cpp <<'EOF'
+#define X 1
+#ifdef X
+int foo() { return 1; }
+#else
+int foo() { return 0; }
+#endif
+EOF
+
+cat <<'EOF' > expected
+
+int main () {
+ int x = 1 + 2 * 3;
+ return 0;
+}
+EOF
+
+test_cpp <<'EOF'
+#define A 1
+#define B A + 2
+#define C B * 3
+
+int main() {
+ int x = C;
+ return 0;
+}
+EOF
+
+cat <<'EOF' > expected
+
+int main () {
+ int x = ( 5 + 3);
+ int y = ( ( 5) + ( 3));
+ return 0;
+}
+EOF
+
+test_cpp <<'EOF'
+#define SIMPLE (5 + 3)
+#define COMPLEX(a, b) ((a) + (b))
+
+int main() {
+ int x = SIMPLE;
+ int y = COMPLEX(5, 3);
+ return 0;
+}
+EOF
+
+cat <<'EOF' > expected
+
+int main () {
+ int bar = 0;
+ bar ++;
+}
+EOF
+
+test_cpp <<'EOF'
+#define A(a) a
+#define B(b) b
+
+int main() {
+ A(int)B(bar) = 0;
+ bar++;
+}
+EOF
diff --git a/tests/test_undef.sh b/tests/test_undef.sh
index 34c3dad..27d03c6 100644
--- a/tests/test_undef.sh
+++ b/tests/test_undef.sh
@@ -1,9 +1,16 @@
cat <<'EOF' > expected
-A is defined
-A is undefined
+int printf ();
+
+int main () {
+
+ printf ( A is defined\n);
+
+
+ printf ( A is undefined\n);
+}
EOF
-test_diff <<'EOF'
+test_cpp <<'EOF'
int printf();
int main() {
@@ -24,4 +31,3 @@ int main() {
#endif
}
EOF
-