diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/assert-deps.sh | 45 | ||||
-rwxr-xr-x | tests/c-lint.sh | 65 | ||||
-rwxr-xr-x | tests/cli-opts.sh | 4 | ||||
-rw-r--r-- | tests/slurp.c | 69 | ||||
-rw-r--r-- | tests/slurp.h | 11 | ||||
-rw-r--r-- | tests/tests-lib.c | 28 | ||||
-rw-r--r-- | tests/tests-lib.h | 15 |
7 files changed, 197 insertions, 40 deletions
diff --git a/tests/assert-deps.sh b/tests/assert-deps.sh index 9bfc5ea..b73933d 100755 --- a/tests/assert-deps.sh +++ b/tests/assert-deps.sh @@ -1,45 +1,10 @@ #!/bin/sh set -eu -if [ ! -e .git ]; then - echo "Not in a Git repository, skipping \"$0\"." >&2 - exit -fi - - . tools/lib.sh -F="$(mkstemp)" -trap 'rm -f "$F"' EXIT - - -awk ' - $0 == "sources.js = \\" { sources = 1; next } - $0 == "tests.js = \\" { tests = 1; next } - sources == 1 && $0 == "" { sources = 2; next } - tests == 1 && $0 == "" { tests = 2; next } - - sources == 1 || tests == 1 { - print $1 - } - - END { - if (sources != 2) { - print "Could not find $(sources.js) in Makefile." \ - > "/dev/stderr" - exit 2 - } - if (tests != 2) { - print "Could not find $(tests.js) in Makefile." \ - > "/dev/stderr" - exit 2 - } - } -' Makefile | LANG=POSIX.UTF-8 sort > "$F" - -printf '%s: all JavaScript sources are listed in the Makefile...' \ - "$(yellow "$0")" >&2 -git ls-files src/*.js tests/js/*.js | - LANG=POSIX.UTF-8 sort | - diff -U10 "$F" - -printf ' %s\n' "$(green 'OK')" >&2 +{ + printf '%s: all deps.mk is up-to-date...' "$(yellow "$0")" + sh mkdeps.sh | diff -U10 deps.mk - + printf ' %s\n' "$(green 'OK')" +} >&2 diff --git a/tests/c-lint.sh b/tests/c-lint.sh new file mode 100755 index 0000000..37822f3 --- /dev/null +++ b/tests/c-lint.sh @@ -0,0 +1,65 @@ +#!/bin/sh +set -eu + +awk ' +BEGIN { + ret = 0 + msg = "function not on the start of the line:" +} + +/^[a-zA-Z0-9_]+ [^=]+\(/ { + if (ret == 0) { + print msg + } + printf "%s:%s:%s\n", FILENAME, FNR, $0 + ret = 1 +} + +END { + exit ret +} +' "$@" + + +awk ' +BEGIN { + ret = 0 + static = 1 + msg = "non-static function is not declared in a header:" +} + +/^[a-zA-Z0-9_]+\(.*$/ && static == 0 { + split($0, line, /\(/) + fn_name = line[1] + if (fn_name != "main" && fn_name != "LLVMFuzzerTestOneInput") { + header = substr(FILENAME, 0, length(FILENAME) - 2) ".h" + if (system("grep -q ^\"" fn_name "\" \"" header "\"")) { + if (ret == 0) { + print msg + } + printf "%s:%s:%s\n", FILENAME, FNR, $0 + ret = 1 + } + } +} + +/^static / { + static = 1 +} + +!/^static / { + static = 0 +} + +END { + exit ret +} +' "$@" + + +RE='[a-z]+\(\) {' +if grep -Eq "$RE" "$@"; then + echo 'Functions with no argument without explicit "void" parameter:' >&2 + grep -En "$RE" "$@" + exit 1 +fi diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh new file mode 100755 index 0000000..a25b66c --- /dev/null +++ b/tests/cli-opts.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu + +"$@" -V diff --git a/tests/slurp.c b/tests/slurp.c new file mode 100644 index 0000000..683a126 --- /dev/null +++ b/tests/slurp.c @@ -0,0 +1,69 @@ +#include "slurp.h" + +#include <stdio.h> +#include <stdlib.h> + +int +slurp_for_tests(const char *const FNAME, char **strref) { + int rc = 0; + + FILE *file = NULL; + char *str = NULL; + + file = fopen(FNAME, "r"); + if (!file) { + perror("fopen(FNAME, \"r\")"); + rc = -1; + goto out; + } + + if (fseek(file, 0L, SEEK_END)) { + perror("fseek(file, 0L, SEEK_END)"); + rc = -1; + goto out; + } + + const long lsize = ftell(file); + if (lsize == -1) { + perror("ftell(file)"); + rc = -1; + goto out; + } + const size_t size = (size_t)lsize + sizeof(char); + + if (fseek(file, 0L, SEEK_SET)) { + perror("fseek(file, 0L, SEEK_SET)"); + rc = -1; + goto out; + } + + str = malloc(size); + if (!str) { + perror("malloc(...)"); + rc = -1; + goto out; + } + + if (fread(str, sizeof(char), size - 1, file) != size - 1) { + perror("fread(...)"); + rc = -1; + goto out; + } + str[size - 1] = '\0'; + *strref = str; + +out: + if (file) { + if (fclose(file)) { + perror("flcose(file"); + rc = -1; + } + } + + if (rc) { + if (str) { + free(str); + } + } + return rc; +} diff --git a/tests/slurp.h b/tests/slurp.h new file mode 100644 index 0000000..056c77d --- /dev/null +++ b/tests/slurp.h @@ -0,0 +1,11 @@ +#ifndef SLURP_H +#define SLURP_H + +#include "../src/config.h" + + +int +slurp_for_tests(const char *const FNAME, char **strref); + + +#endif diff --git a/tests/tests-lib.c b/tests/tests-lib.c new file mode 100644 index 0000000..e46fb2d --- /dev/null +++ b/tests/tests-lib.c @@ -0,0 +1,28 @@ +#include "tests-lib.h" +#include <stdio.h> +#include <assert.h> + +#define COLOUR_RESET "\033[0m" +#define COLOUR_GREEN "\033[0;32m" +#define COLOUR_YELLOW "\033[0;33m" + +void +test_start(const char *const name) { + assert(fprintf(stdout, "%s:\n", name) > 0); +} + +void +testing(const char *const message) { + assert( + fprintf( + stdout, + COLOUR_YELLOW "testing" COLOUR_RESET ": %s...", + message + ) > 0 + ); +} + +void +test_ok(void) { + assert(fprintf(stdout, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n") > 0); +} diff --git a/tests/tests-lib.h b/tests/tests-lib.h new file mode 100644 index 0000000..a1e67ba --- /dev/null +++ b/tests/tests-lib.h @@ -0,0 +1,15 @@ +#ifndef TESTS_LIB_H +#define TESTS_LIB_H + +#include "../src/config.h" + +void +test_start(const char *const name); + +void +testing(const char *const message); + +void +test_ok(void); + +#endif |