Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWK skeleton #240

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/awk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: AWK CI

on:
push:
paths:
- 'awk/**'
- '.github/workflows/awk**'
pull_request:
paths:
- 'awk/**'
- '.github/workflows/awk**'

schedule:
# weekly build every FRI
- cron: "0 6 * * 5"

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: One test FAILs
working-directory: awk
run: |
rm -rf result.txt | true
make test > result.txt
grep "1 failed" result.txt

- name: The others PASS
working-directory: awk
run: |
rm -rf result.txt | true
make test > result.txt
grep "3 passed" result.txt
17 changes: 17 additions & 0 deletions awk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
VPATH = src test

.DEFAULT_GOAL:= run

.PHONY: run test TAGS

run:
@./src/wee.awk input

test:
@# Toggle lines for colored output
@# @echo "" | awk -f test/tests.awk | pygmentize -l tap -f terminal256 -O style=friendly
@echo "" | awk -f test/tests.awk

TAGS:
ctags -R

1 change: 1 addition & 0 deletions awk/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
80 changes: 80 additions & 0 deletions awk/src/apb.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# file : apb.awk
# desc : APB a testing library for AWK
# @include this and all files to test into a awk test file e.g. test_prog.awk
# auth : Jens Koesling

number_of_tests = 0
passed_tests = 0
failed_tests = 0

function test_w_cmp (name, fct, expr, result) {
# Main testing funtion
# name: description of the test, e.g. "year is leapyear if divisible by 400"
# fct : function used to compare expression expr to the expected result result
number_of_tests += 1
if (@fct(expr, result)) {
status = "ok"
passed_tests += 1
}
else {
status = "not ok"
failed_tests += 1
}

ret = sprintf("%s %d - %s", status, ++testnum, name)
# printf ret
return ret
}

# test_equal() ###################################
function eq (a, b) {
return (a == b)
}

function test_equal (name, expr, result) {
return (test_w_cmp(name, "eq", expr, result))
}

# test_leq() #####################################
function leq (a, b) {
return (a <= b)
}

function test_leq (name, expr, result) {
return (test_w_cmp(name, "leq", expr, result))
}

# comparison for arrays
function lcmp (l,r) {
equiv = 1
if (isarray(l)) {
for (i in l) {
if (i in r) {
# dark corner:
# (i in r) is not the same as (r[i] == val)
# the second changes the array and sets previously uninitialized entries to ""
equiv = equiv && (lcmp(l[i],r[i]))
}
else {
equiv = 0
}
}
}
else {
equiv = (l == r)
}
return equiv
}

function arr_eq (l,r) {
return (lcmp(l,r) && lcmp(r,l))
}

##################################################
function test_report () {
print ""
print "From", number_of_tests, "tests"
print (passed_tests + 0), "passed and"
print (failed_tests + 0), "failed."
}

29 changes: 29 additions & 0 deletions awk/src/wee.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/gawk -E

# file : wee.awk
# desc : A wee program in awk

function min(a, b) {
if ( a <= b ) { return a }
else { return b }
}
function max(a, b) {
if ( a >= b ) { return a }
else { return b }
}

BEGIN {
knowledge = "p"
answer = 42
}

{
if ( min($1, answer) == 42 ) {
knowledge="poste"
}
}

END {
print "(wee.awk): I knew that a " knowledge "riori."
}

39 changes: 39 additions & 0 deletions awk/test/tests.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#! /usr/bin/gawk -E

# FILE : test/tests.awk
# desc : Testing src/wee.awk

@include "src/wee.awk"
@include "src/apb.awk"

BEGIN {
# ##################################################
# FIXTURES

answer_exp = 42
split("ABCDE", letters_exp, "")


# ##################################################
# TESTS

print "Basic tests"

print test_equal("This test passes", 1, 1)
print test_equal("This test fails", 0, 1)

arr_in[1] = "A"
arr_in[2] = "B"
arr_in[3] = "C"
arr_in[4] = "D"
arr_in[5] = "E"
print test_equal("Testing arrays", arr_eq(arr_in, letters_exp), 1)

delete arr_in

print test_leq("The minimum of two numbers is smaller than their maximum", min(23, 42), max(23, 42) )

# ##################################################
# REPORT
test_report()
}