-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathtemperature_spec.rb
More file actions
67 lines (52 loc) · 1.56 KB
/
temperature_spec.rb
File metadata and controls
67 lines (52 loc) · 1.56 KB
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
57
58
59
60
61
62
63
64
65
66
67
# # Topics:
# * functions
# * floating-point math
#
# # Hints
#
# Remember that one degree fahrenheit is 5/9 of one degree celsius, and that the freezing point of water is 0 degrees celsius but 32 degrees fahrenheit.
#
# In integer math, there **are no fractions**. So if you are using integer literals, you will be using integer math, which means, for example...
#
# 1 / 2 => 0
#
# In floating point math, there **are** fractions. So...
#
# 1.0 / 2.0 => 0.5
#
require "temperature"
describe "temperature conversion functions" do
describe "#ftoc" do
it "converts freezing temperature" do
expect(ftoc(32)).to eq( 0 )
end
it "converts boiling temperature" do
expect(ftoc(212)).to eq( 100 )
end
it "converts body temperature" do
expect( ftoc(98.6)).to eq( 37 )
end
it "converts arbitrary temperature" do
expect( ftoc(68)).to eq( 20 )
end
end
describe "#ctof" do
it "converts freezing temperature" do
expect( ctof(0)).to eq( 32 )
end
it "converts boiling temperature" do
expect( ctof(100)).to eq( 212 )
end
it "converts arbitrary temperature" do
expect( ctof(20)).to eq( 68 )
end
it "converts body temperature" do
expect( ctof(37)).to be_within(0.1).of(98.6)
# Why do we need to use be_within?
# See http://www.ruby-forum.com/topic/169330
# and http://en.wikipedia.org/wiki/IEEE_754-2008
# and http://en.wikipedia.org/wiki/Double_precision_floating-point_format
# Also, try "puts 0.5 - 0.4 - 0.1" -- pretty crazy, right?
end
end
end