summaryrefslogtreecommitdiffstats
path: root/tests/test_quine.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_quine.c')
-rw-r--r--tests/test_quine.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/test_quine.c b/tests/test_quine.c
new file mode 100644
index 0000000..c010539
--- /dev/null
+++ b/tests/test_quine.c
@@ -0,0 +1,27 @@
+/* test_quine.c — a C quine: running it prints its own source.
+ *
+ * How it works: the file is a fixed point of a "quinate" function.
+ * The two strings below are that function's data; everything around
+ * them is code that re-emits the data verbatim, spliced back into the
+ * same program shape. No argv, no /proc/self, no tricks — just two
+ * printf calls.
+ *
+ * Build: musl-gcc -std=gnu11 -Oz -Wall -Wextra -o test_quine tests/test_quine.c
+ * Check: ./test_quine | diff - tests/test_quine.c (no output = quine) */
+
+#include <stdio.h>
+
+int main(void) {
+ const char *s =
+ "const char *s = \"%s\";\n"
+ "const char *f = \"%s\";\n"
+ "printf(f, s, s, f);\n"
+ "return 0;\n";
+ const char *f =
+ "int main(void) {\n"
+ " %s printf(f, s, s, f);\n"
+ " return 0;\n"
+ "}\n";
+ printf(f, s, s, f);
+ return 0;
+}