quantify 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/COPYING +20 -0
- data/Examples.rb +104 -0
- data/README +7 -0
- data/lib/quantify/config.rb +379 -0
- data/lib/quantify/core_extensions.rb +63 -0
- data/lib/quantify/dimensions.rb +523 -0
- data/lib/quantify/exception.rb +21 -0
- data/lib/quantify/inflections.rb +63 -0
- data/lib/quantify/quantify.rb +37 -0
- data/lib/quantify/quantity.rb +325 -0
- data/lib/quantify/unit/base_unit.rb +518 -0
- data/lib/quantify/unit/compound_base_unit.rb +91 -0
- data/lib/quantify/unit/compound_unit.rb +321 -0
- data/lib/quantify/unit/non_si_unit.rb +20 -0
- data/lib/quantify/unit/prefix/base_prefix.rb +42 -0
- data/lib/quantify/unit/prefix/non_si_prefix.rb +10 -0
- data/lib/quantify/unit/prefix/prefix.rb +73 -0
- data/lib/quantify/unit/prefix/si_prefix.rb +10 -0
- data/lib/quantify/unit/si_unit.rb +10 -0
- data/lib/quantify/unit/unit.rb +217 -0
- data/lib/quantify.rb +26 -0
- data/spec/dimension_spec.rb +294 -0
- data/spec/quantity_spec.rb +250 -0
- data/spec/unit_spec.rb +687 -0
- metadata +103 -0
@@ -0,0 +1,250 @@
|
|
1
|
+
require 'quantify'
|
2
|
+
include Quantify
|
3
|
+
|
4
|
+
describe Quantity do
|
5
|
+
|
6
|
+
it "should create a valid instance with standard create and unit name" do
|
7
|
+
quantity = Quantity.new 10.0, 'metre'
|
8
|
+
quantity.value.should == 10
|
9
|
+
quantity.unit.symbol.should == 'm'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should create a valid instance with standard create and unit name" do
|
13
|
+
quantity = Quantity.new 5000, :kilowatt
|
14
|
+
quantity.value.should == 5000
|
15
|
+
quantity.unit.symbol.should == 'kW'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should create a valid instance with standard create and unit symbol" do
|
19
|
+
quantity = Quantity.new 5000, 'kW'
|
20
|
+
quantity.value.should == 5000
|
21
|
+
quantity.unit.name.should == 'kilowatt'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should create a valid instance with dynamic create and unit name" do
|
25
|
+
quantity = 10.metre
|
26
|
+
quantity.value.should == 10
|
27
|
+
quantity.unit.symbol.should == 'm'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should create a valid instance with dynamic create and unit symbol" do
|
31
|
+
quantity = 10.km
|
32
|
+
quantity.value.should == 10
|
33
|
+
quantity.unit.name.should == 'kilometre'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should create a valid instance with class parse method" do
|
37
|
+
quantity = Quantity.parse "10 m"
|
38
|
+
quantity.value.should == 10
|
39
|
+
quantity.unit.symbol.should == 'm'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should create a valid instance with class parse method" do
|
43
|
+
quantity = Quantity.parse "155.6789 ly"
|
44
|
+
quantity.value.should == 155.6789
|
45
|
+
quantity.unit.name.should == 'light year'
|
46
|
+
quantity.represents.should == 'length'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should create a valid instance with class parse method based on to_string method" do
|
50
|
+
quantity_1 = Quantity.new 15, :watt
|
51
|
+
quantity_2 = Quantity.parse quantity_1.to_s
|
52
|
+
quantity_2.value.should == 15
|
53
|
+
quantity_2.unit.name.should == 'watt'
|
54
|
+
quantity_2.represents.should == 'power'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should create a valid instance with class parse method and unit prefix based on to_string method" do
|
58
|
+
quantity_1 = Quantity.new 15, :watt
|
59
|
+
quantity_2 = Quantity.parse quantity_1.to_s
|
60
|
+
quantity_2.value.should == 15
|
61
|
+
quantity_2.unit.name.should == 'watt'
|
62
|
+
quantity_2.represents.should == 'power'
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should convert quantity correctly" do
|
66
|
+
1.km.to_metre.unit.symbol.should == 'm'
|
67
|
+
1.km.to_metre.to_s.should == "1000.0 m"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should convert quantity correctly" do
|
71
|
+
1.BTU.to_joule.to_s.should == "1054.804 J"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should convert quantity correctly" do
|
75
|
+
1.hour.to_second.to_s.should == "3600.0 s"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should convert quantity correctly with scaling (temperature)" do
|
79
|
+
85.degree_farenheit.to_degree_celsius.round.to_s.should == "29 °C"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should convert quantity correctly with scaling (temperature) and with decimal places" do
|
83
|
+
85.degree_farenheit.to_degree_celsius.round(2).to_s.should == "29.44 °C"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should add quantities correctly with same units" do
|
87
|
+
(5.metre + 3.metre).to_s.should == "8.0 m"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should add quantities correctly with same units" do
|
91
|
+
(125.4.kelvin + 61.3.K).to_s.should == "186.7 K"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should add quantities correctly with different units of same dimension" do
|
95
|
+
(15.foot + 5.yd).to_s.should == "30.0 ft"
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should add quantities correctly with different units of same dimension" do
|
99
|
+
(125.4.kelvin + -211.85.degree_celsius).to_s.should == "186.7 K"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should throw error when adding quantities with different dimensions" do
|
103
|
+
lambda{1.metre + 5.kg}.should raise_error
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should subtract quantities correctly with different units of same dimension" do
|
107
|
+
(125.4.kelvin - -211.85.degree_celsius).to_s.should == "64.1 K"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should subtract quantities correctly with different units of same dimension" do
|
111
|
+
(300.foot - 50.yard).round.to_s.should == "150 ft"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should subtract quantities correctly with same units" do
|
115
|
+
(300.foot - 100.ft).round.to_s.should == "200 ft"
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should throw error when subtracting quantities with different dimensions" do
|
119
|
+
lambda{1.metre - 5.kg}.should raise_error
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should successfully multiply a quantity by a scalar" do
|
123
|
+
(20.metre * 3).to_s.should == "60.0 m"
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should successfully multiply a quantity by a scalar" do
|
127
|
+
(2.kg * 50).round.to_s.should == "100 kg"
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should raise error if multiplying by string" do
|
131
|
+
lambda{20.metre * '3'}.should raise_error
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should two quantities" do
|
135
|
+
quantity = (20.metre * 1.metre)
|
136
|
+
quantity.value.should == 20
|
137
|
+
quantity.unit.measures.should == 'area'
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should successfully divide a quantity by a scalar" do
|
141
|
+
(20.metre / 5).to_s.should == "4.0 m"
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should successfully divide a quantity by a scalar" do
|
145
|
+
(2.kg / 0.5).round.to_s.should == "4 kg"
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should calculate speed from distance and time quantities" do
|
149
|
+
distance_in_km = 12.km
|
150
|
+
time_in_min = 16.5.min
|
151
|
+
distance_in_miles = distance_in_km.to_miles
|
152
|
+
time_in_hours = time_in_min.to_hours
|
153
|
+
speed = distance_in_miles / time_in_hours
|
154
|
+
speed.class.should == Quantity
|
155
|
+
speed.value.should be_close 27.1143792976291, 0.00000001
|
156
|
+
speed.to_s(:name).should == "27.1143792976291 miles per hour"
|
157
|
+
speed.to_s.should == "27.1143792976291 mi h^-1"
|
158
|
+
end
|
159
|
+
|
160
|
+
it "coerce method should handle inverted syntax" do
|
161
|
+
quantity = 1/2.ft
|
162
|
+
quantity.to_s.should == "0.5 ft^-1"
|
163
|
+
quantity.to_s(:name).should == "0.5 per foot"
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should convert temperature correctly" do
|
167
|
+
30.degree_celsius.to_K.to_s.should == "303.15 K"
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should convert temperature correctly" do
|
171
|
+
30.degree_celsius.to_degree_farenheit.round.to_s.should == "86 °F"
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should convert standard units correctly" do
|
175
|
+
27.feet.to_yards.round.to_s(:name).should == "9 yards"
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should convert standard units correctly" do
|
179
|
+
6000.BTU.to_megajoules.to_s(:name).should == "6.328824 megajoules"
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should convert standard units correctly" do
|
183
|
+
13.1.stones.to_kg.to_s(:name).should == "83.1888383 kilograms"
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should convert compound units correctly" do
|
187
|
+
speed = Quantity.new 100, (Unit.km/Unit.h)
|
188
|
+
speed.to_mi.round(2).to_s.should == "62.14 mi h^-1"
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should convert to SI unit correctly" do
|
192
|
+
100.cm.to_si.to_s.should == "1.0 m"
|
193
|
+
2.kWh.to_si.to_s.should == "7200000.0 J"
|
194
|
+
400.ha.to_si.to_s.should == "4000000.0 m^2"
|
195
|
+
35.degree_celsius.to_si.to_s.should == "308.15 K"
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should convert compound units to SI correctly" do
|
199
|
+
speed = Quantity.new 100, (Unit.mi/Unit.h)
|
200
|
+
speed.to_si.to_s(:name).should == "44.704 metres per second"
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should convert compound units to SI correctly" do
|
204
|
+
pressure = Quantity.new 100, (Unit.pound_force_per_square_inch)
|
205
|
+
pressure.to_si.round.to_s(:name).should == "689476 pascals"
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should return equivalent unit according to specification" do
|
209
|
+
(50.square_metres/10.m).to_s.should == "5.0 m"
|
210
|
+
(1.kg*20.m*2.m/4.s/5.s).to_s(:name).should == '2.0 joules'
|
211
|
+
(80.kg/2.m/4.s/5.s).to_s(:name).should == '2.0 pascals'
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should raise a quantity to a power correctly" do
|
215
|
+
unit = 50.ft ** 2
|
216
|
+
unit.to_s.should == "2500.0 ft^2"
|
217
|
+
unit = 50.ft ** 3
|
218
|
+
unit.to_s.should == "125000.0 ft^3"
|
219
|
+
unit = 50.ft ** -1
|
220
|
+
unit.to_s.should == "0.02 ft^-1"
|
221
|
+
unit = (10.m/1.s)**2
|
222
|
+
unit.to_s.should == "100.0 m^2 s^-2"
|
223
|
+
unit = (10.m/1.s)**-1
|
224
|
+
unit.to_s.should == "0.1 s m^-1"
|
225
|
+
lambda{ ((10.m/1.s)** 0.5) }.should raise_error
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should raise a quantity to a power correctly" do
|
229
|
+
(50.ft.pow! 2).to_s.should == "2500.0 ft^2"
|
230
|
+
(50.ft.pow! 3).to_s.should == "125000.0 ft^3"
|
231
|
+
(50.ft.pow! -1).to_s.should == "0.02 ft^-1"
|
232
|
+
((10.m/1.s).pow! 2).to_s.should == "100.0 m^2 s^-2"
|
233
|
+
((10.m/1.s).pow! -1).to_s.should == "0.1 s m^-1"
|
234
|
+
lambda{ ((10.m/1.s).pow! 0.5) }.should raise_error
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should parse using string method" do
|
238
|
+
"20 m".to_q.value.should == 20.0
|
239
|
+
"45.45 BTU".to_q.class.should == Quantity
|
240
|
+
"65 kilometres per hour".to_q.unit.class.should == Unit::Compound
|
241
|
+
"65 kilometre per hour".to_q.unit.class.should == Unit::Compound
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should cancel by base units of original compound unit if necessary" do
|
245
|
+
quantity = Quantity.new(20, Unit.psi).to(Unit.inches_of_mercury)
|
246
|
+
quantity.unit.base_units.size.should == 1
|
247
|
+
quantity.to_s.should == "40.720412743579 inHg"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|