# brintOS gawk self-test. Reaching ALL-AWK-OK proves gawk's core paradigm # works on the glibc Emcraft machine: BEGIN-block evaluation, record/field # splitting on real input, pattern+action rules with END aggregation, regexp # matching, string/array built-ins, float math + printf, and file I/O (the # input records are written by the test itself, then consumed by the main # loop). Modeled on the Tcl/Expect self-tests: labelled checks, env-var-free, # one line per check, a pass counter, an ALL-*-OK sentinel. function ok(t) { pass++; printf "A-%s OK\n", t } function bad(t,w) { printf "A-%s FAIL (%s)\n", t, w } BEGIN { total = 8 # A1 - interpreter up: gawk reports its own version. v = PROCINFO["version"] if (v != "") ok("A1ver"); else bad("A1ver", "no PROCINFO version") printf "A1-ver %s\n", v # A2 - core language: arithmetic, string built-ins, split + array walk. if (6 * 7 == 42) ok("A2expr"); else bad("A2expr", 6 * 7) s = toupper("abc") "/" length("hello") if (s == "ABC/5") ok("A2string"); else bad("A2string", s) n = split("2 1 3", a, " "); sum2 = 0 for (i in a) sum2 += a[i] if (n == 3 && sum2 == 6) ok("A2array"); else bad("A2array", n "/" sum2) # A4 - float math + formatted output. pi = sprintf("%.4f", atan2(0, -1)) if (pi == "3.1416") ok("A4float"); else bad("A4float", pi) # Write the input records for the A3 checks, then hand the file to the # main record loop - real field splitting on a real file, not a mock. f = "/tmp/selftest_awk.dat" printf "alpha 1\nbeta 2\ngamma 3\n" > f close(f) ARGV[1] = f; ARGC = 2 } # A3 - the awk paradigm on real records: field access on a numbered record, # a regexp pattern, and a comparison pattern feeding an END aggregate. NR == 2 && $1 == "beta" { seen_beta = 1 } /gamma/ { seen_re = 1 } $2 > 1 { sum3 += $2 } END { if (seen_beta) ok("A3fields"); else bad("A3fields", "record 2 field 1") if (seen_re) ok("A3regexp"); else bad("A3regexp", "no /gamma/ match") if (sum3 == 5) ok("A3agg"); else bad("A3agg", "sum=" sum3) printf "A-SUMMARY pass=%d/%d\n", pass, total if (pass == total) print "ALL-AWK-OK" }