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 fc63597 commit cb7cbad
Show file tree
Hide file tree
Showing 6 changed files with 111 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
2 changes: 2 additions & 0 deletions awk/gigasecond/gigasecond.awk
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/gawk --lint --file

BEGIN {
print "Implement this solution" > "/dev/stderr"
exit 1
Expand Down
83 changes: 83 additions & 0 deletions awk/gigasecond/gigasecond_test.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/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_short() {
input = "2011-04-25"
want = "2043-01-01T01:46:40"

_debugTestPre()
got = reverseString(input)

assertEquals(want, got)
_debugTestPost()
}

function testGigasecond_long() {
input = "2015-01-24T22:00:00"
want = "2046-10-03T01:46:39"

_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)

# running tests with a lot of duplication
testGigasecond_short()
testGigasecond_long()

# running tests with reduced duplication
casesGigasecond()

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

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

# key: input
# value: output

BEGIN {
cases["2011-04-25"]="2043-01-01T01:46:40"
cases["1977-06-13"]="2009-02-19T01:46:40"
cases["1959-07-19"]="1991-03-27T01:46:40"
cases["2015-01-24T22:00:00"]="2046-10-02T23:46:40"
cases["2015-01-24T23:59:59"]="2046-10-03T01:46:39"

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

0 comments on commit cb7cbad

Please sign in to comment.