cdecl99_LDADD = libcdecl.la libgnu.la $(LTLIBINTL) $(LTLIBREADLINE)
$(cdecl99_OBJECTS): $(gnulib_headers)
-check_PROGRAMS =
+check_PROGRAMS = test/crossparse
check_LTLIBRARIES = libtest.la
libtest_la_LIBADD = $(GSL_LIBS)
libtest_la_SOURCES = test/testlib.c
check_PROGRAMS += test/randomdecl
endif
+test_crossparse_LDADD = libcdecl.la libtest.la libgnu.la
+$(test_crossparse_OBJECTS): $(gnulib_headers)
test_randomdecl_LDADD = libcdecl.la libtest.la libgnu.la
$(test_randomdecl_OBJECTS): $(gnulib_headers)
-TESTS_ENVIRONMENT = SHELL='$(SHELL)' LIBTOOL='$(LIBTOOL)'
+TESTS_ENVIRONMENT = SHELL='$(SHELL)' LIBTOOL='$(LIBTOOL)' EXEEXT='$(EXEEXT)'
TEST_EXTENSIONS = .sh
SH_LOG_COMPILER = $(SHELL)
-TESTS = tests/libcdecl-static-symbols.sh
+TESTS = tests/libcdecl-static-symbols.sh tests/crossparse-c-random.sh
EXTRA_DIST += $(TESTS)
src/parse.lo: src/scan.h
--- /dev/null
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <cdecl.h>
+#include "test.h"
+
+#define PROGNAME "crossparse"
+static const char *progname = PROGNAME;
+static const char sopts[] = "ECVH";
+static const struct option lopts[] = {
+ { "cdecl", 0, NULL, 'C' },
+ { "english", 0, NULL, 'E' },
+ { "version", 0, NULL, 'V' },
+ { "help", 0, NULL, 'H' },
+ { 0 }
+};
+
+static void print_usage(FILE *f)
+{
+ fprintf(f, "Usage: %s [options]\n", progname);
+}
+
+static void print_help(void)
+{
+ print_usage(stdout);
+ puts("Detailed help coming soon.");
+}
+
+enum {
+ MODE_CDECL,
+ MODE_ENGLISH,
+};
+
+typedef struct cdecl *parse_func(const char *);
+typedef size_t render_func(char *, size_t, struct cdecl *);
+
+char *rerender(const char *str, const char *parse_name, parse_func *parse,
+ const char *render_name, render_func *render)
+{
+ struct cdecl *decl = NULL;
+ char *buf = NULL;
+ size_t len;
+
+ decl = parse(str);
+ if (!decl) {
+ fprintf(stderr, "%s: %s: failed to parse input: %s\n",
+ progname, parse_name, cdecl_get_error()->str);
+ goto err;
+ }
+
+ len = render(NULL, 0, decl);
+ buf = malloc_nofail(len+1);
+ if (render(buf, len+1, decl) != len) {
+ fprintf(stderr, "%s: %s: inconsistent length returned\n",
+ progname, render_name);
+ goto err;
+ }
+
+ cdecl_free(decl);
+ return buf;
+err:
+ cdecl_free(decl);
+ free(buf);
+
+ fprintf(stderr, "%s: the failed input was: %s\n", progname, str);
+ return NULL;
+}
+#define rerender(str, p, r) (rerender(str, #p, p, #r, r))
+
+int main(int argc, char **argv)
+{
+ char *buf1 = NULL, *buf2 = NULL, *buf3 = NULL;
+ int opt, mode = MODE_CDECL;
+ int ret = EXIT_FAILURE;
+
+ if (argc > 0)
+ progname = argv[0];
+
+ while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+ switch (opt) {
+ case 'C':
+ mode = MODE_CDECL;
+ break;
+ case 'E':
+ mode = MODE_ENGLISH;
+ break;
+ case 'V':
+ test_print_version(PROGNAME);
+ return EXIT_SUCCESS;
+ case 'H':
+ print_help();
+ return EXIT_SUCCESS;
+ default:
+ print_usage(stderr);
+ return EXIT_FAILURE;
+ }
+ }
+
+ if (!argv[optind]) {
+ print_usage(stderr);
+ return EXIT_FAILURE;
+ }
+
+ if (mode == MODE_ENGLISH) {
+ buf1 = rerender(argv[optind], cdecl_parse_english, cdecl_declare);
+ if (!buf1)
+ goto out;
+ buf2 = rerender(buf1, cdecl_parse_decl, cdecl_explain);
+ if (!buf2)
+ goto out;
+ buf3 = rerender(buf2, cdecl_parse_english, cdecl_declare);
+ if (!buf3)
+ goto out;
+ } else {
+ buf1 = rerender(argv[optind], cdecl_parse_decl, cdecl_explain);
+ if (!buf1)
+ goto out;
+ buf2 = rerender(buf1, cdecl_parse_english, cdecl_declare);
+ if (!buf2)
+ goto out;
+ buf3 = rerender(buf2, cdecl_parse_decl, cdecl_explain);
+ if (!buf3)
+ goto out;
+ }
+
+ if (!strcmp(buf1, buf3))
+ ret = EXIT_SUCCESS;
+out:
+ free(buf1);
+ free(buf2);
+ free(buf3);
+ return ret;
+}
--- /dev/null
+#!/bin/sh
+#
+# Copyright © 2012 Nick Bowler
+#
+# Randomized tests that libcdecl can parse its own output.
+#
+# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+# This is free software: you are free to do what the fuck you want to.
+# There is NO WARRANTY, to the extent permitted by law.
+
+: "${RANDOMSEED=$RANDOM}" "${TESTITER=100}"
+
+randomdecl=test/randomdecl$EXEEXT
+crossparse=test/crossparse$EXEEXT
+test -x $randomdecl || exit 77
+
+proc() {
+ result=pass
+ count=0
+
+ while read decl
+ do
+ count=`expr $count + 1`
+ $crossparse "$decl" || { result=fail; break; }
+ done
+
+ echo "result=$result"
+ echo "count=$count"
+}
+
+printf '%s: randomized test using RANDOMSEED=%d\n' "$0" "$RANDOMSEED"
+
+eval_cmd=`exec 3>&1
+ { $randomdecl -s "$RANDOMSEED" -n "$TESTITER" 3>&-
+ echo gen_status=$? >&3
+ } | proc >&3`
+eval "$eval_cmd"
+
+expected_count=`expr "$TESTITER" + 0`
+if test x"$count" != x"$expected_count"; then
+ printf '%s: failed after %d successful tests (out of %d)\n' \
+ "$0" "$count" "$expected_count" 1>&2
+ exit 1
+fi
+
+test x"$result" = x"pass" && test x"$gen_status" = x"0" && exit 0
+exit 1