From d368f3d0ea3e93ddd7933831932f3f068926fb7f Mon Sep 17 00:00:00 2001 From: mrdudz Date: Mon, 9 Jun 2025 18:26:41 +0200 Subject: [PATCH] simple script(s) to check if bsearch tables are sorted correctly --- .github/checks/Makefile | 12 +++- .github/checks/sorted.sh | 117 +++++++++++++++++++++++++++++++ .github/checks/sorted_codeopt.sh | 42 +++++++++++ Makefile | 1 + 4 files changed, 170 insertions(+), 2 deletions(-) create mode 100755 .github/checks/sorted.sh create mode 100755 .github/checks/sorted_codeopt.sh diff --git a/.github/checks/Makefile b/.github/checks/Makefile index 93eeddd19..8245958c1 100644 --- a/.github/checks/Makefile +++ b/.github/checks/Makefile @@ -5,14 +5,18 @@ endif ifdef CMD_EXE -.PHONY: checkstyle +.PHONY: checkstyle sorted checkstyle: $(info INFO: style checks require bash.) +sorted: + $(info INFO: table checks require bash.) else -.PHONY: checkstyle lineendings tabs lastline spaces noexec +.PHONY: checkstyle lineendings tabs lastline spaces noexec sorted + +all: checkstyle sorted checkstyle: lineendings tabs lastline spaces noexec @@ -31,4 +35,8 @@ spaces: spaces.sh noexec: noexec.sh @./noexec.sh +sorted: sorted.sh sorted_codeopt.sh + @./sorted.sh + @./sorted_codeopt.sh + endif diff --git a/.github/checks/sorted.sh b/.github/checks/sorted.sh new file mode 100755 index 000000000..4311225cd --- /dev/null +++ b/.github/checks/sorted.sh @@ -0,0 +1,117 @@ +#! /bin/bash +OLDCWD=`pwd` +SCRIPT_PATH=`dirname $0` + +SORT_OPT=-u + +function checkarray_quoted_name +{ + CHECK_FILE="$1" + + awk '/'"$2"'/{flag=1;next}/};/{flag=0}flag' "$CHECK_FILE" | \ + sed -e 's:.*\"\(.*\)\".*:\1:g' | \ + sed '/^\s*$/d' > .a.tmp + + if [[ -z $(grep '[^[:space:]]' .a.tmp) ]] ; then + echo "error: "$2" table is empty" + exit -1 + fi + + LC_COLLATE=C sort $SORT_OPT .a.tmp > .b.tmp + + if cmp --silent -- .a.tmp .b.tmp; then + echo ""$2" definitions OK" + else + echo "error: "$2" definitions are not sorted." + diff -y .a.tmp .b.tmp + exit -1 + fi + rm -rf .a.tmp .b.tmp +} + +function checkinstr_quoted_name +{ + CHECK_FILE="$1" + + awk '/'"$2"'/{flag=1;next}/};/{flag=0}flag' "$CHECK_FILE" | \ + sed -e 's:^ *{$::g' | \ + sed -e 's:^ *}$::g' | \ + sed -e 's:.*\"\(.*\)\".*:\1:g' | \ + sed '/^\s*$/d' > .a.tmp + + if [[ -z $(grep '[^[:space:]]' .a.tmp) ]] ; then + echo "error: "$2" table is empty" + exit -1 + fi + + LC_COLLATE=C sort $SORT_OPT .a.tmp > .b.tmp + + if cmp --silent -- .a.tmp .b.tmp; then + echo ""$2" definitions OK" + else + echo "error: "$2" definitions are not sorted." + diff -y .a.tmp .b.tmp + exit -1 + fi + rm -rf .a.tmp .b.tmp +} + +function checkopcodes_quoted_name +{ + CHECK_FILE="$1" + + awk '/'"$2"'/{flag=1;next}/};/{flag=0}flag' "$CHECK_FILE" | \ + grep "^ *\".*\"," | \ + sed '/^\s*$/d' > .a.tmp + + if [[ -z $(grep '[^[:space:]]' .a.tmp) ]] ; then + echo "error: "$2" table is empty" + exit -1 + fi + + LC_COLLATE=C sort $SORT_OPT .a.tmp > .b.tmp + + if cmp --silent -- .a.tmp .b.tmp; then + echo ""$2" definitions OK" + else + echo "error: "$2" definitions are not sorted." + diff -y .a.tmp .b.tmp + exit -1 + fi + rm -rf .a.tmp .b.tmp +} + +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTab6502\.Ins\) \/ sizeof \(InsTab6502\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTab6502X\.Ins\) \/ sizeof \(InsTab6502X\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTab6502DTV\.Ins\) \/ sizeof \(InsTab6502DTV\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTab65C02\.Ins\) \/ sizeof \(InsTab65C02\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTab4510\.Ins\) \/ sizeof \(InsTab4510\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTab65816\.Ins\) \/ sizeof \(InsTab65816\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTabSweet16\.Ins\) \/ sizeof \(InsTabSweet16\.Ins\[0\]\)," +checkinstr_quoted_name ../../src/ca65/instr.c "sizeof \(InsTabHuC6280\.Ins\) \/ sizeof \(InsTabHuC6280\.Ins\[0\]\)," + +checkarray_quoted_name ../../src/ca65/scanner.c "} DotKeywords \[\] = {" + +checkarray_quoted_name ../../src/cc65/codeinfo.c "static const FuncInfo FuncInfoTable\[\]" +checkarray_quoted_name ../../src/cc65/codeinfo.c "static const ZPInfo ZPInfoTable\[\]" +checkarray_quoted_name ../../src/cc65/codeoptutil.c "static const char\* const Tab\[\]" + +checkarray_quoted_name ../../src/cc65/coptstop.c "static const OptFuncDesc FuncTable\[\]" +checkarray_quoted_name ../../src/cc65/coptstop.c "static const OptFuncDesc FuncRegATable\[\]" + +checkopcodes_quoted_name ../../src/cc65/opcodes.c "const OPCDesc OPCTable\[OP65_COUNT\] = {" +checkarray_quoted_name ../../src/cc65/pragma.c "} Pragmas\[\] = {" +checkarray_quoted_name ../../src/cc65/preproc.c "} PPDTypes\[\] = {" +checkarray_quoted_name ../../src/cc65/scanner.c "} Keywords \[\] = {" +checkarray_quoted_name ../../src/cc65/stdfunc.c "} StdFuncs\[\] = {" + +checkarray_quoted_name ../../src/common/filetype.c "static const FileId TypeTable\[\]" +checkarray_quoted_name ../../src/common/target.c "static const TargetEntry TargetMap\[\]" + +checkarray_quoted_name ../../src/dbginfo/dbginfo.c "} KeywordTable\[\] = {" + +checkarray_quoted_name ../../src/sp65/convert.c "static const ConverterMapEntry ConverterMap\[\]" +checkarray_quoted_name ../../src/sp65/input.c "static const FileId FormatTable\[\]" +checkarray_quoted_name ../../src/sp65/output.c "static const FileId FormatTable\[\]" +checkarray_quoted_name ../../src/sp65/palconv.c "static const PaletteMapEntry PaletteMap\[\]" + diff --git a/.github/checks/sorted_codeopt.sh b/.github/checks/sorted_codeopt.sh new file mode 100755 index 000000000..a248ebc07 --- /dev/null +++ b/.github/checks/sorted_codeopt.sh @@ -0,0 +1,42 @@ +#! /bin/bash +OLDCWD=`pwd` +SCRIPT_PATH=`dirname $0` +CHECK_FILE=../../src/cc65/codeopt.c + +SORT_OPT=-u + +grep "^static OptFunc " $CHECK_FILE | \ + sed -e 's:.*"\(.*\)",.*:\1:g' > .a.tmp + +if [[ -z $(grep '[^[:space:]]' .a.tmp) ]] ; then + echo "error: OptFunc table is empty" + exit -1 +fi + +LC_COLLATE=C sort $SORT_OPT .a.tmp > .b.tmp + +if cmp --silent -- .a.tmp .b.tmp; then + echo "static OptFunc definitions OK" +else + echo "error: static OptFunc definitions are not sorted." + diff -y .a.tmp .b.tmp + exit -1 +fi + +awk '/static OptFunc\* OptFuncs\[\] = {/{flag=1;next}/}/{flag=0}flag' $CHECK_FILE | \ + sed -e 's:.*&D\(.*\),:\1:g' > .a.tmp + +if [[ -z $(grep '[^[:space:]]' .a.tmp) ]] ; then + echo "error: OptFuncs table is empty" + exit -1 +fi + +if cmp --silent -- .a.tmp .b.tmp; then + echo "static OptFuncs* OptFuncs[] definitions OK" +else + echo "error: static OptFuncs* OptFuncs[] definitions are not sorted." + diff -y .a.tmp .b.tmp + exit -1 +fi + +rm -rf .a.tmp .b.tmp diff --git a/Makefile b/Makefile index 29fcbbf96..edfd8166c 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ test: # GNU "check" target, which runs all tests check: @$(MAKE) -C .github/checks checkstyle --no-print-directory + @$(MAKE) -C .github/checks sorted --no-print-directory @$(MAKE) test @$(MAKE) -C targettest platforms --no-print-directory @$(MAKE) -C samples platforms --no-print-directory