Skip to content

Instantly share code, notes, and snippets.

@seki
Created October 28, 2013 13:05
Show Gist options
  • Save seki/7196466 to your computer and use it in GitHub Desktop.
Save seki/7196466 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'test/unit'
class TriApp
def on_OK(t1, t2, t3)
n1 = positive_integer(t1)
n2 = positive_integer(t2)
n3 = positive_integer(t3)
return "三角形ではありません" unless triangle?(n1, n2, n3)
case [n1, n2, n3].uniq.size
when 1
"正三角形"
when 2
"二等辺三角形"
else
"三角形"
end
rescue ArgumentError, TypeError
"引数がエラーです"
end
def positive_integer(text)
n = Integer(text)
raise ArgumentError if n <=0
n
end
def triangle?(n1, n2, n3)
n1 < n2 + n3 &&
n2 < n1 + n3 &&
n3 < n1 + n2
end
end
class TestTriangle < Test::Unit::TestCase
def test_tri
app = TriApp.new
assert_equal("引数がエラーです", app.on_OK("", "", ''))
assert_equal("引数がエラーです", app.on_OK(nil, nil, ''))
assert_equal("引数がエラーです", app.on_OK("a", "b", '1'))
assert_equal("引数がエラーです", app.on_OK("1 a", "2", '1'))
assert_equal("引数がエラーです", app.on_OK("1 1", "2", '1'))
assert_equal("引数がエラーです", app.on_OK("0", "1", '1'))
assert_equal("引数がエラーです", app.on_OK("-1", "1", '1'))
assert_equal("引数がエラーです", app.on_OK("1.0", "1.2", '1.1'))
assert_equal("三角形ではありません", app.on_OK(" 1", " 2", '1 '))
assert_equal("引数がエラーです", app.on_OK(" 1", " 2", '1 '))
assert_equal("三角形", app.on_OK("2", "3", "4"))
assert_equal("三角形", app.on_OK("200000000000000000000000000000000",
"300000000000000000000000000000000",
"400000000000000000000000000000000"))
assert_equal("三角形ではありません", app.on_OK("12", "3", "4"))
assert_equal("三角形ではありません", app.on_OK("7", "3", "4"))
assert_equal("二等辺三角形", app.on_OK("2", "3", "2"))
assert_equal("二等辺三角形", app.on_OK("2", "2", "3"))
assert_equal("正三角形", app.on_OK("2", "2", "2"))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment