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 61b9a52 commit 04420e5
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 3 deletions.
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
36 changes: 34 additions & 2 deletions awk/gigasecond/gigasecond.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
#!/usr/bin/gawk --lint --file

function gigasecond(time_stamp) {
# our constants
GIGASECOND = 10^9
USE_UTC = 1

# replace delimiters with spaces for mktime()
input_lhs = time_stamp
gsub(/[-:T]/, " ", input_lhs)

input_rhs = ""

if (length(input_lhs) <= 10) {
input_rhs = " 00 00 00"
}

input = input_lhs input_rhs

# https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html
time_seconds = mktime(input, USE_UTC)


# https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html
return strftime("%FT%T", time_seconds + GIGASECOND, USE_UTC)
}

BEGIN {
print "Implement this solution" > "/dev/stderr"
exit 1
}

{
print gigasecond($1)
}

END {
}
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 = gigasecond(input)

assertEquals(got, want)
_debugTestPost()
}

function testGigasecond_long() {
input = "2015-01-24T22:00:00"
want = "2046-10-02T23:46:40"

_debugTestPre()
got = gigasecond(input)

assertEquals(got, want)
_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(got, want)
_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
}
121 changes: 121 additions & 0 deletions awk/gigasecond/run-tests-awk.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Running automated test file(s):


===============================================================================

AWKLIBPATH=../.lib

awkunit.awk
awkunit.so

gawk --lint --file=./awkunit.awk < /dev/null > /dev/null
gawk: ./awkunit.awk:3: warning: `load' is a gawk extension
gawk: warning: function `assertEquals' defined but never called directly
gawk: warning: function `assert' defined but never called directly
gawk: ./awkunit.awk:26: warning: reference to uninitialized variable `_assert_exit'

real 0m0.003s
user 0m0.002s
sys 0m0.001s

gawk --lint --file=./gigasecond.awk < /dev/null > /dev/null
gawk: ./gigasecond.awk:21: warning: `mktime' is a gawk extension
gawk: ./gigasecond.awk:25: warning: `strftime' is a gawk extension

real 0m0.004s
user 0m0.001s
sys 0m0.003s

gawk --lint --file=./test-cases.awk < /dev/null > /dev/null

real 0m0.003s
user 0m0.003s
sys 0m0.000s

exit 0

===============================================================================

Running: bats ./test-gigasecond.bats
1..5
ok 1 date only specificaion of time
ok 2 second test for date only specification of time # skip
ok 3 third test for date only specification of time # skip
ok 4 full time specified # skip
ok 5 full time with day roll-over # skip

real 0m0.226s
user 0m0.136s
sys 0m0.108s

exit 0

===============================================================================

AWKLIBPATH=../.lib

awkunit.awk
awkunit.so

Running: gawk --file ./gigasecond_test.awk && printf \n%s\n Tests Passed! || printf \n%s\n Tests Failed!

Running 2 tests...

Test 1:
input -> [2011-04-25]
output -> [2043-01-01T01:46:40]
result -> passed

Test 2:
input -> [2015-01-24T22:00:00]
output -> [2046-10-02T23:46:40]
result -> passed

Running 5 test cases

Test 3:
input -> [2011-04-25]
output -> [2043-01-01T01:46:40]
result -> passed

Test 4:
input -> [2015-01-24T23:59:59]
output -> [2046-10-03T01:46:39]
result -> passed

Test 5:
input -> [1977-06-13]
output -> [2009-02-19T01:46:40]
result -> passed

Test 6:
input -> [2015-01-24T22:00:00]
output -> [2046-10-02T23:46:40]
result -> passed

Test 7:
input -> [1959-07-19]
output -> [1991-03-27T01:46:40]
result -> passed


7 out of 7 tests passed!

real 0m0.003s
user 0m0.002s
sys 0m0.002s

Tests Passed!

exit 0

===============================================================================

Running: misspell .

real 0m0.025s
user 0m0.030s
sys 0m0.010s

===============================================================================

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 04420e5

Please sign in to comment.