Skip to content

Instantly share code, notes, and snippets.

@lazyatom
Forked from technoweenie/gist:213780
Created October 20, 2009 08:25
Show Gist options
  • Save lazyatom/214107 to your computer and use it in GitHub Desktop.
Save lazyatom/214107 to your computer and use it in GitHub Desktop.
# how do you test algorithms?
class Pythagoream < Struct.new(:a, :b)
class BadArgument < ArgumentError; end
def result
raise BadArgument if a < 0 || b < 0
a**2 + b**2
end
end
# I would probably test a few different aspects of this:
# 1. some simple cases, only caring about the known result
# 2. some edge cases (i.e. when an argument is zero)
# 3. error cases (i.e. when input is invalid)
context "Pythagoream" do
should "return sum of squares" do
assert_equal 8, Pythagoream.new(2, 2).result
assert_equal 18, Pythagoream.new(3, 3).result
assert_equal 25, Pythagoream.new(3, 4).result
assert_equal 32, Pythagoream.new(4, 4).result
end
should "return square of one argument when the other is zero" do
assert_equal 9, Pythagoream.new(3, 0).result
assert_equal 9, Pythagoream.new(0, 3).result
end
should "return zero when both arguments are zero" do
assert_equal 0, Pythagoream.new(0, 0).result
end
should "raise an exception if the first parameter is negative" do
assert_raise(Pythagoream::BadArgument) do
Pythagoream.new(-1, 4).result
end
end
should "raise an exception if the second parameter is negative" do
assert_raise(Pythagoream::BadArgument) do
Pythagoream.new(3, -1).result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment