Skip to content

Commit

Permalink
awk/gigasecond: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 1, 2023
1 parent 95dbffa commit bbadb63
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions awk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
- [hello-world](./hello-world/README.md)
- [two-fer](./two-fer/README.md)
- [reverse-string](./reverse-string/README.md)
- [gigasecond](./gigasecond/README.md)
9 changes: 8 additions & 1 deletion awk/gigasecond/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ For the datetime parsing and formatting functions, be sure to set the UTC flag.

### Based on

Chapter 9 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/?Chapter=09
Chapter 9 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/?Chapter=09

### My Solution

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

@include "awkunit"
@include "test-cases"
@include "gigasecond"

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 testGigasecond_zero() {
input = ""
want = ""

_debugTestPre()
got = reverseString(input)

assertEquals(want, got)
_debugTestPost()
}

function casesGigasecond() {
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 = gigasecond(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)

testGigasecond_zero()

casesGigasecond()

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/gigasecond/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 bbadb63

Please sign in to comment.