Skip to content

Commit

Permalink
awk/darts: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 1, 2023
1 parent ad554dd commit fdec1a2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Exercism Workspace
| June | Summer-of-Sexps | Common-Lisp (5/5) |
| July | Jurassic | C (5/5), C++ (5/5), Fortran (0/5) |
| August | Apps | Dart (5/5), Java (0/5), Kotlin (0/5) |
| September | Slimline | Awk (1/5), Bash\* (5/5), jq (0/5), Perl (0/5) |
| September | Slimline | Awk (2/5), Bash\* (5/5), jq (0/5), Perl (0/5) |
| October | Object-Oriented | |
| November | Nibbly | |
| December | Diversions | |
Expand Down
1 change: 1 addition & 0 deletions awk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
- [two-fer](./two-fer/README.md)
- [reverse-string](./reverse-string/README.md)
- [gigasecond](./gigasecond/README.md)
- [darts](./darts/README.md)
9 changes: 8 additions & 1 deletion awk/darts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ Write a function that given a point in the target (defined by its [Cartesian coo

### Based on

Inspired by an exercise created by a professor Della Paolera in Argentina
Inspired by an exercise created by a professor Della Paolera in Argentina

### My Solution

- [my solution](./darts.awk)
- [awkunit tests](./darts_test.awk)
- [test cases](./test-cases.awk)
- [run-tests](./run-tests-awk.txt)
1 change: 1 addition & 0 deletions awk/darts/awkunit.awk
71 changes: 71 additions & 0 deletions awk/darts/darts_test.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/gawk --lint --file

@include "awkunit"
@include "test-cases"
@include "darts"

passed = 0
testCount = 0

function _debugTestPre() {
printf "Test %s:\n", (passed + 1)
printf " input -> [%s]\n", input
}

function _debugTestPost() {
passed = passed + 1
printf " output -> [%s]\n", got
printf " result -> passed\n\n"
}

function testDarts_zero() {
input = ""
want = ""

_debugTestPre()
got = darts(input)

assertEquals(want, got)
_debugTestPost()
}

function casesDarts() {
printf "Running %d test cases\n\n", length(cases)
caseNum = 0

# Associative arrays don't preserve insert order.
for (key in cases) {
input = key
want = cases[key]

_debugTestPre()
got = darts(input)

assertEquals(want, got)
_debugTestPost()
}
}

BEGIN {
exit 0
}

END {
cmd = "grep --no-filename --count ^function\\ test *_test.awk"
cmd | getline testCount

printf "\nRunning %d tests...\n\n", testCount

testCount = testCount + length(cases)

# running tests with a lot of duplication
testDarts_zero()

# running tests with reduced duplication
casesDarts()

print "\n" passed " out of " testCount " tests passed!"

# add exit here to keep it from looping
exit 0
}
12 changes: 12 additions & 0 deletions awk/darts/test-cases.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/gawk --lint --file
# test-cases.awk

# key: input
# value: output

BEGIN {
cases[""]="" # wow, this works with a warning

# add exit here to keep it from waiting for input
exit 0
}

0 comments on commit fdec1a2

Please sign in to comment.