-
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.
awk/difference-of-squares: 1st iteration
- Loading branch information
Showing
8 changed files
with
307 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
Empty file.
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,47 @@ | ||
#!/usr/bin/gawk -M --lint --file | ||
|
||
function squareOfSum(number) { | ||
return (number * (number + 1) / 2) ^ 2 | ||
} | ||
|
||
function sumOfSquares(number) { | ||
return (number * (number + 1) * (2 * number + 1)) / 6 | ||
} | ||
|
||
function differenceOfSquares(number) { | ||
return squareOfSum(number) - sumOfSquares(number) | ||
} | ||
|
||
BEGIN { | ||
print "Implement this solution" > "/dev/stderr" | ||
exit 1 | ||
} | ||
|
||
{ | ||
if (match($0, /^difference,/)) { | ||
_ = split($0, part, ",") | ||
|
||
print differenceOfSquares(part[2]) | ||
|
||
exit 0 | ||
} | ||
|
||
if (match($0, /^square_of_sum,/)) { | ||
_ = split($0, part, ",") | ||
|
||
print squareOfSum(part[2]) | ||
|
||
exit 0 | ||
} | ||
|
||
if (match($0, /^sum_of_squares,/)) { | ||
_ = split($0, part, ",") | ||
|
||
print sumOfSquares(part[2]) | ||
|
||
exit 0 | ||
} | ||
|
||
print "error" | ||
} | ||
|
||
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,74 @@ | ||
#!/usr/bin/gawk --bignum --lint --file | ||
|
||
@include "awkunit" | ||
@include "test-cases" | ||
@include "difference-of-squares" | ||
|
||
passed = 0 | ||
testCount = 0 | ||
|
||
function _debugTestPre() { | ||
printf "Test %s: %s\n", (passed + 1), test_name | ||
printf " input -> [%s]\n", input | ||
} | ||
|
||
function _debugTestPost() { | ||
passed = passed + 1 | ||
printf " output -> [%s]\n", got | ||
printf " result -> passed\n\n" | ||
} | ||
|
||
function casesDifferenceOfSquares() { | ||
printf "Running %d test cases\n\n", length(cases) | ||
caseNum = 0 | ||
|
||
# orders array by index in for loop | ||
PROCINFO["sorted_in"] = "@ind_str_asc" | ||
|
||
# Associative arrays don't preserve insert order. | ||
for (key in cases) { | ||
input = key | ||
want = cases[key] | ||
|
||
test_name = "[" input "]" | ||
|
||
_ = split(input, part, ",") | ||
|
||
_debugTestPre() | ||
|
||
got = 0 | ||
|
||
if (part[1] == "difference") { | ||
got = differenceOfSquares(part[2]) | ||
} else if (part[1] == "square_of_sum") { | ||
got = squareOfSum(part[2]) | ||
} else if (part[1] == "sum_of_squares") { | ||
got = sumOfSquares(part[2]) | ||
} | ||
|
||
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 reduced duplication | ||
casesDifferenceOfSquares() | ||
|
||
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,158 @@ | ||
Running automated test file(s): | ||
|
||
|
||
=============================================================================== | ||
|
||
AWKLIBPATH=/usr/lib/x86_64-linux-gnu/gawk:../.lib | ||
|
||
/usr/lib/x86_64-linux-gnu/gawk | ||
filefuncs.so | ||
fnmatch.so | ||
fork.so | ||
inplace.so | ||
intdiv.so | ||
ordchr.so | ||
readdir.so | ||
readfile.so | ||
revoutput.so | ||
revtwoway.so | ||
rwarray.so | ||
time.so | ||
|
||
../.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.001s | ||
sys 0m0.002s | ||
|
||
gawk --lint --file=./difference-of-squares.awk < /dev/null > /dev/null | ||
|
||
real 0m0.004s | ||
user 0m0.002s | ||
sys 0m0.002s | ||
|
||
gawk --lint --file=./test-cases.awk < /dev/null > /dev/null | ||
|
||
real 0m0.003s | ||
user 0m0.001s | ||
sys 0m0.002s | ||
|
||
exit 0 | ||
|
||
=============================================================================== | ||
|
||
Running: bats ./test-difference-of-squares.bats | ||
1..9 | ||
ok 1 square of sum 1 | ||
ok 2 square of sum 5 | ||
ok 3 square of sum 100 | ||
ok 4 sum of squares 1 | ||
ok 5 sum of squares 5 | ||
ok 6 sum of squares 100 | ||
ok 7 difference of squares 1 | ||
ok 8 difference of squares 5 | ||
ok 9 difference of squares 100 | ||
|
||
real 0m0.351s | ||
user 0m0.210s | ||
sys 0m0.162s | ||
|
||
exit 0 | ||
|
||
=============================================================================== | ||
|
||
AWKLIBPATH=/usr/lib/x86_64-linux-gnu/gawk:../.lib | ||
|
||
/usr/lib/x86_64-linux-gnu/gawk | ||
filefuncs.so | ||
fnmatch.so | ||
fork.so | ||
inplace.so | ||
intdiv.so | ||
ordchr.so | ||
readdir.so | ||
readfile.so | ||
revoutput.so | ||
revtwoway.so | ||
rwarray.so | ||
time.so | ||
|
||
../.lib | ||
awkunit.awk | ||
awkunit.so | ||
|
||
Running: gawk --file ./difference-of-squares_test.awk && printf \n%s\n Tests Passed! || printf \n%s\n Tests Failed! | ||
|
||
Running 0 tests... | ||
|
||
Running 8 test cases | ||
|
||
Test 1: [difference,1] | ||
input -> [difference,1] | ||
output -> [0] | ||
result -> passed | ||
|
||
Test 2: [difference,100] | ||
input -> [difference,100] | ||
output -> [25164150] | ||
result -> passed | ||
|
||
Test 3: [difference,5] | ||
input -> [difference,5] | ||
output -> [170] | ||
result -> passed | ||
|
||
Test 4: [square_of_sum,1] | ||
input -> [square_of_sum,1] | ||
output -> [1] | ||
result -> passed | ||
|
||
Test 5: [square_of_sum,100] | ||
input -> [square_of_sum,100] | ||
output -> [25502500] | ||
result -> passed | ||
|
||
Test 6: [sum_of_squares,1] | ||
input -> [sum_of_squares,1] | ||
output -> [1] | ||
result -> passed | ||
|
||
Test 7: [sum_of_squares,100] | ||
input -> [sum_of_squares,100] | ||
output -> [338350] | ||
result -> passed | ||
|
||
Test 8: [sum_of_squares,5] | ||
input -> [sum_of_squares,5] | ||
output -> [55] | ||
result -> passed | ||
|
||
|
||
8 out of 8 tests passed! | ||
|
||
real 0m0.009s | ||
user 0m0.005s | ||
sys 0m0.004s | ||
|
||
Tests Passed! | ||
|
||
exit 0 | ||
|
||
=============================================================================== | ||
|
||
Running: misspell . | ||
|
||
real 0m0.038s | ||
user 0m0.044s | ||
sys 0m0.014s | ||
|
||
=============================================================================== | ||
|
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,20 @@ | ||
#!/usr/bin/gawk --lint --file | ||
# test-cases.awk | ||
|
||
# key: input | ||
# value: output | ||
|
||
BEGIN { | ||
cases["square_of_sum,1"]="1" | ||
cases["square_of_sum,100"]="225" | ||
cases["square_of_sum,100"]="25502500" | ||
cases["sum_of_squares,1"]="1" | ||
cases["sum_of_squares,5"]="55" | ||
cases["sum_of_squares,100"]="338350" | ||
cases["difference,1"]="0" | ||
cases["difference,5"]="170" | ||
cases["difference,100"]="25164150" | ||
|
||
# add exit here to keep it from waiting for input | ||
exit 0 | ||
} |