Skip to content

Instantly share code, notes, and snippets.

@dmoney
Created May 10, 2013 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmoney/5551923 to your computer and use it in GitHub Desktop.
Save dmoney/5551923 to your computer and use it in GitHub Desktop.
Script for verifying the output of a FizzBuzz program. $ ./fizzbuzz.sh | ./fizzcheck.sh $ ./fizzcheck.sh ./fizzbuzz.sh`
#!/bin/sh
# fizzbuzz.sh - A reference FizzBuzz program for testing fizzcheck.sh
# Author: Dustin King (cathodion@gmail.com)
limit=20
i=1
while expr $i '<=' $limit >/dev/null; do
out=
if expr $i % 3 = 0 >/dev/null; then out=Fizz; fi
if expr $i % 5 = 0 >/dev/null; then out="$out"Buzz; fi
if expr X$out = X'' >/dev/null; then out=$i; fi
echo $out
i=`expr $i + 1`
done
#!/bin/sh
# fizzcheck.sh - A script for verifying the output of FizzBuzz
# Author: Dustin King (cathodion@gmail.com)
# Original idea from https://twitter.com/qntm/status/329560932269301760
CMD=${1-'cat -'}
i=1
$CMD | while read actual; do
expect=
if expr $i % 3 = 0 >/dev/null; then expect=Fizz; fi
if expr $i % 5 = 0 >/dev/null; then expect="$expect"Buzz; fi
if expr X$expect = X'' >/dev/null; then expect=$i; fi
if expr "X$actual" '!=' "X$expect" >/dev/null; then
echo $i: Expected: \"$expect\", got \"$actual\";
exit 1
fi
i=`expr $i + 1`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment