-
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
457 additions
and
4 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,7 +1,55 @@ | ||
#!/usr/bin/gawk --lint --file | ||
|
||
@load "ordchr" | ||
|
||
# These variables are initialized on the command line (using '-v'): | ||
# - num | ||
# - num -> ^([0-9]+$ | ||
|
||
function raindrops(number) { | ||
sounds = "" | ||
|
||
if (length(number) == 0) { | ||
return "error: nothing has no sound" | ||
} | ||
|
||
if (number == 0) { | ||
return number | ||
} | ||
|
||
if (number % 3 == 0) { | ||
sounds = sounds "Pling" | ||
} | ||
|
||
if (number % 5 == 0) { | ||
sounds = sounds "Plang" | ||
} | ||
|
||
if (number % 7 == 0) { | ||
sounds = sounds "Plong" | ||
} | ||
|
||
if (length(sounds) == 0) { | ||
sounds = number | ||
} | ||
|
||
return sounds | ||
} | ||
|
||
BEGIN { | ||
print "Implement this solution" > "/dev/stderr" | ||
exit 1 | ||
# let's make sure direction isn't being used to inject code | ||
if (! match(num, /^[0-9]+$/)) { | ||
print "error: invalid number [" num "], the only supported numbers are ^[0-9]+$" | ||
exit 1 | ||
} | ||
|
||
print raindrops(num) | ||
|
||
exit 0 | ||
} | ||
|
||
{ | ||
# using this section got us stuck waiting for input that is only given via variables | ||
} | ||
|
||
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,75 @@ | ||
#!/usr/bin/gawk --lint --file | ||
|
||
@include "awkunit" | ||
@include "test-cases" | ||
@include "raindrops" | ||
|
||
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 testRaindrops_empty() { | ||
input = "" | ||
want = "error: nothing has no sound" | ||
|
||
# _ = split(input, a, " ") | ||
|
||
_debugTestPre() | ||
got = raindrops(input) | ||
|
||
assertEquals(got, want) | ||
_debugTestPost() | ||
} | ||
|
||
function casesRaindrops() { | ||
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] | ||
|
||
# _ = split(input, a, " ") | ||
|
||
_debugTestPre() | ||
got = raindrops(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 | ||
testRaindrops_empty() | ||
|
||
# running tests with reduced duplication | ||
casesRaindrops() | ||
|
||
print "\n" passed " out of " testCount " tests passed!" | ||
|
||
# add exit here to keep it from looping | ||
exit 0 | ||
} |
Oops, something went wrong.