-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1e01_calculator.rb
56 lines (51 loc) · 945 Bytes
/
d1e01_calculator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Calculator
def self.add (a, b)
a+b
end
def self.substract(a,b)
a-b
end
def self.multiply(a,b)
a*b
end
def self.divide(a,b)
a/b
end
end
### main
def assert(expected, actual, msg="")
if expected != actual
if msg == ""
msg = "Error. Expected #{expected}, encountered #{actual}"
end
puts msg
else
puts "Ok"
end
end
test = [
[6,4,2,"+"],
[12,4,3,"*"],
[7,10,3,"-"],
[4,8,2,"/"],
[-6,-4,-2,"+"],
[-12,-4,3,"*"],
[-13,-10,3,"-"],
[-4,-8,2,"/"],
[-6.0000001,-4.0000001,-2,"+"],
[-12.0,-4.0,3.0,"*"],
[-13,-10,3,"-"],
[-4.0,-8,2,"/"]
]
test.each do |result, param1, param2, operator|
case operator
when "+"
assert(result,Calculator.add(param1,param2))
when "-"
assert(result,Calculator.substract(param1,param2))
when "*"
assert(result,Calculator.multiply(param1,param2))
when "/"
assert(result,Calculator.divide(param1,param2))
end
end