diff options
Diffstat (limited to 'tests')
-rwxr-xr-x[-rw-r--r--] | tests/assert-catgets.sh | 67 | ||||
-rwxr-xr-x[-rw-r--r--] | tests/c-lint.sh | 65 |
2 files changed, 132 insertions, 0 deletions
diff --git a/tests/assert-catgets.sh b/tests/assert-catgets.sh index e69de29..885e186 100644..100755 --- a/tests/assert-catgets.sh +++ b/tests/assert-catgets.sh @@ -0,0 +1,67 @@ +#!/bin/sh +set -eu + +assert_sequential_ids() { + awk ' + BEGIN { + n = 0 + } + + /^#define MSG_/ { + if (++n != $3) { + print "Bad sequential ID:" + printf "%s:%s:%s\n", FILENAME, NR, $0 + printf "expected: %s\ngot: %s\n", n, $3 + exit 1 + } + } + ' "$1" +} + +assert_consistent_msg_definitions() { + awk ' + BEGIN { + i = 0 + j = 0 + } + + /^#define MSG_/ { + defines[i++] = $2 + } + + /^\t\[MSG_/ { + msgs[j++] = substr($0, 3, index($0, "]") - 3) + } + + END { + for (n in defines) { + if (defines[n] != msgs[n]) { + printf "Order mismatch between #define" + printf " and usage in MSGS[]:\n" + printf "#define: %s\nMSGS[]: %s\n", + defines[n], msgs[n] + exit 1 + } + } + } + ' "$1" +} + +assert_no_unused_msgs() { + DEFINES="$(mktemp)" + USAGES="$(mktemp)" + awk '/^#define MSG_/ { print $2 }' "$f" | sort > "$DEFINES" + awk -F'_\\(' ' + /_\(MSG_/ { print substr($2, 0, index($2, ")") - 1) } + ' "$f" | sort | uniq > "$USAGES" + if ! diff "$DEFINES" "$USAGES"; then + echo 'Some defined MSG_ items not being used' >&2 + exit 1 + fi +} + +for f in "$@"; do + assert_sequential_ids "$f" + assert_consistent_msg_definitions "$f" + assert_no_unused_msgs "$f" +done diff --git a/tests/c-lint.sh b/tests/c-lint.sh index e69de29..0fa0b01 100644..100755 --- a/tests/c-lint.sh +++ 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") { + 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 |