Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 278 lines (214 sloc) 8.078 kb
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
1 $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
796fdf6 @bhuga Added initial spec
bhuga authored
2
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
3 require 'quantity'
4 require 'quantity/systems/si'
5 require 'quantity/systems/us'
34d7135 @bhuga Implemented several basic math functions
bhuga authored
6
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
7 describe Quantity do
8
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
9 context "instantiation" do
10 it "should be instantiated from numbers" do
11 1.meter.should == 1
12 2.5.feet.should == 2.5
13 end
14
15 it "should work with alias names" do
16 2.meters.should == 2
17 end
18
19 it "should know what it measures" do
20 2.meters.unit.dimension.name.should == :length
21 2.meters.measures.name.should == :length
22 end
23
24 it "should know its units" do
25 2.meters.unit.name.should == :meter
26 2.meters.units.should == :meter
27 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
28
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
29 it "should have a string representation" do
30 2.meters.to_s.should == "2 meter"
31 (2.meters * 2.meters).to_s.should == (defined?(Rational) ? "4 meter^2" : "4.0 meter^2")
32 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
33
34 end
35
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
36 context "conversions" do
37 it "converts from one type to another" do
38 1.meter.in_centimeters.should == 100
39 10.meters.convert(:feet).should be_close 32.808399.feet, 10**-6
40 end
41
42 it "converts from one type to another when not using the reference value for that dimension" do
43 1.kilometer.in_centimeters.should == 100_000
44 end
45
46 it "fails to convert things that do not measure the same dimension" do
47 lambda { 1.picogram.in_meters }.should raise_error ArgumentError
48 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
49
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
50 it "converts derived units" do
51 Quantity.new(2,'m^2').to_feet.to_f.should be_close 21.5278208, 10**-5
52 Quantity.new(2,'m^2').convert('foot^2').to_f.should be_close 21.5278208, 10**-5
53 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
54
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
55 it "converts derived units to named units" do
56 (1.centimeter * 1.centimeter * 1.centimeter).should == 0.1.centiliter
57 (1000.mm * 1.mm * 1.mm).should == 1.ml
58 (1.mm**3).unit.name.should == 'millimeter^3'
59 (1.mm**3).measures.name.should == :volume
60 (1.centimeter * 1.centimeter).measures.name.should == :area
61 (30.meters / 1.second).measures.name.should == :speed
62 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
63
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
64 it "reduces derived units" do
65 ((1.meter / 1.second) * 1.second).should == 1.meter
66 end
67
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
68 end
69
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
70 context "math operations" do
71
72 context "equality" do
73 it "enforces exact equality" do
74 12.meter.should == 12.meters
75 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
76
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
77 it "does not intern quantities" do
78 12.meter.should_not equal 12.meters
79 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
80
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
81 it "enforces equality across a dimension" do
82 1.meter.should == 100.centimeter
83 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
84
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
85 it "does not find quantities on different dimensions to be equal" do
86 1.millimeter.should_not == 1.milligram
87 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
88
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
89 end
90
91 context "general" do
92 it "supports abs" do
93 ((-(5.seconds)).abs).should == 5.seconds
94 (-5).seconds.abs.should == 5.seconds
95 5.seconds.abs.should == 5.seconds
96 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
97
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
98 it "supports @-" do
99 (-(35.meters)).should be_close -(114.829396.feet), 10**-5
100 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
101
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
102 it "supports @+" do
103 +4.kilograms.should == 4.kilograms
104 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
105
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
106 it "supports %" do
107 (35.meters % 6).should == 5.meters
108 (35.meters % 6.feet).should be_close 0.2528.meters, 10**-5
109 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
110
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
111 it "supports modulo" do
112 4.kilograms.modulo(15.grams).should == 10.grams
113 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
114
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
115 it "supports round" do
116 15.6.meters.round.should == 16.meters
117 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
118
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
119 it "supports truncate" do
120 15.2.meters.truncate.should == 15.meters
121 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
122
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
123 it "supports ceil" do
124 15.2.meters.ceil.should == 16.meters
125 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
126
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
127 it "supports floor" do
128 (-5.5.meters).floor.should == -6.meters
129 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
130
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
131 it "supports divmod" do
132 11.meters.divmod(3).should == [3.meter,2.meter]
133 11.meters.divmod(3.meters).should == [3.meter,2.meter]
134 11.meters.divmod(-3).should == [-4.meter,-1.meter]
135 11.meters.divmod(-3.meters).should == [-4.meter,-1.meter]
136 11.meters.divmod(3.5).should == [3.meter,0.5.meter]
137 11.meters.divmod(3.5.meters).should == [3.meter,0.5.meter]
138 (-11.meters).divmod(3.5).should == [-4.meter,3.0.meter]
139 (-11.meters).divmod(3.5.meters).should == [-4.meter,3.0.meter]
140 11.5.meters.divmod(3.5).should == [3.meter,1.0.meter]
141 11.5.meters.divmod(3.5.meters).should == [3.meter,1.0.meter]
142 end
143
144 it "supports zero?" do
145 0.kilograms.zero?.should == true
146 end
147 end
148
149 context "addition and subtraction" do
150 it "adds quantities of the same thing" do
151 (12.meters + 5.meters).should == 17.meters
152 end
153
154 it "adds quantities of the same dimension" do
155 (12.meters + 15.centimeters).should == 1215.centimeters
156 end
157
158 it "adds numerics to quantities" do
159 3.meters.should == 1.meter + 2
160 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
161
dc015eb @bhuga Refactor quantity spec. No functional changes.
bhuga authored
162 it "adds quantities to numerics" do
163 6.4.meters.should == 4.4 + 2.meter
164 end
165
166 it "does not add items of different types" do
167 lambda { 12.meters + 24.picograms }.should raise_error TypeError
168 end
169
170 it "adds negative quantities" do
171 (5.meters + (-3.meters)).should == 2.meters
172 end
173
174 it "subtracts quantities of the same thing" do
175 (12.meters - 3.meters).should == 9.meters
176 end
177
178 it "subtracts quantities of the same dimension" do
179 (12.meters - 3650.centimeters).should == -2450.centimeters
180 end
181
182 it "does not add items of different types" do
183 lambda { (12.meters - 3650.picograms)}.should raise_error TypeError
184 end
185
186 it "subtracts numerics from quantities" do
187 (12.meters - 3).should == 9.meters
188 end
189
190 it "subtracts quantities from numerics" do
191 (15 - 5.meters).should == 10.meters
192 end
193 end
194
195 context "multiplication" do
196
197 it "multiplies quantities of the same unit" do
198 (2.meters * 5.meters).should == 10
199 end
200
201 it "multiplies quantities of the same dimension" do
202 (1.meter * 1.foot).should be_close Quantity.new(3.280839,:'foot^2'), 10**-5
203 end
204
205 it "uses the unit on the right when multiplying across the same dimension" do
206 (1.meter * 1.foot).unit.name.should == :'foot^2'
207 end
208
209 it "multiplies complex units" do
210 (3.meter * Quantity.new(1,:'m^2')).should == Quantity.new(3,:'m^3')
211 end
212
213 it "multiplies units of different dimensions" do
214 (2.meters * 2.kilograms).should == Quantity.new(4,:'meter*kilogram')
215 end
216 end
217
218 context "division" do
219 it "divides numerics by quantities" do
220 (6 / 2.meters).should == 3.meters
221 end
222
223 it "divides quantities by numerics" do
224 (6.meters / 2).should == 3.meters
225 end
226
227 it "divides quantities of the same unit" do
228 (6.meters / 2.meters).should == 3.meters
229 end
230
231 it "divides quantities of the same dimension" do
232 (6.meters / 2.feet).should == 3.meters
233 end
234
235 it "divides quantities of different dimensions" do
236 (1.kilogram / 1.second).unit.name.should == :'kilogram/second'
237 end
238
239 it "correctly calculates the value of a divided unit" do
240 (10.meters / 2.picograms).should be_close 5, 10**-5
241 end
242
243 end
244
245 context "exponentiation" do
246 it "raises quantities to positive powers" do
247 (2.meters**2).should be_close Quantity.new(4,:'meter^2'), 10**-5
248 end
249
250 it "raises quantities to negative powers" do
251 (2.meters**-1).unit.name.should == :'1/meter'
252 (2.meters**-1).should == 2
253 end
254
255 it "supports a cubed function" do
256 (1.centimeter * 1.centimeter * 1.centimeter).should == 1.centimeter.cubed
257 end
258
259 it "supports a squared function" do
260 (1.centimeter * 1.centimeter).should == 1.centimeter.squared
261 end
262
263 it "does not raise to fractional powers" do
264 lambda {2.meters**1.5}.should raise_error ArgumentError
265 end
266 end
267
268 context "enumerable" do
269 it "should be comparable" do
270 2.meters.should be < 3.meters
271 150.centimeters.should be > 1.meter
272 [1.meter, 1.foot, 1.inch].sort.should == [1.inch, 1.foot, 1.meter]
273 end
274 end
275
276 end
ed0ea8b @bhuga Go back to old system, no longer testing rational on quantities
bhuga authored
277 end
Something went wrong with that request. Please try again.