-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../.lib/awkunit.awk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
=============================================================================== | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |