ruby-units 0.1.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/LICENSE +20 -0
- data/README +85 -0
- data/lib/ruby_units.rb +596 -0
- data/lib/units.rb +226 -0
- data/test/test_ruby_units.rb +384 -0
- metadata +50 -0
data/lib/units.rb
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class Unit < Numeric
|
4
|
+
UNIT_DEFINITIONS = {
|
5
|
+
# prefixes
|
6
|
+
'<googol>' => [%w{googol}, 1e100, :prefix],
|
7
|
+
'<kibi>' => [%w{Ki Kibi kibi}, 2**10, :prefix],
|
8
|
+
'<mebi>' => [%w{Mi Mebi mebi}, 2**20, :prefix],
|
9
|
+
'<gibi>' => [%w{Gi Gibi gibi}, 2**30, :prefix],
|
10
|
+
'<tebi>' => [%w{Ti Tebi tebi}, 2**40, :prefix],
|
11
|
+
'<pebi>' => [%w{Pi Pebi pebi}, 2**50, :prefix],
|
12
|
+
'<exi>' => [%w{Ei Exi exi}, 2**60, :prefix],
|
13
|
+
'<zebi>' => [%w{Zi Zebi zebi}, 2**70, :prefix],
|
14
|
+
'<yebi>' => [%w{Yi Yebi yebi}, 2**80, :prefix],
|
15
|
+
'<yotta>' => [%w{Y Yotta yotta}, 1e24, :prefix],
|
16
|
+
'<zetta>' => [%w{Z Zetta zetta}, 1e21, :prefix],
|
17
|
+
'<exa>' => [%w{E Exa exa}, 1e18, :prefix],
|
18
|
+
'<peta>' => [%w{P Peta peta}, 1e15, :prefix],
|
19
|
+
'<tera>' => [%w{T Tera tera}, 1e12, :prefix],
|
20
|
+
'<giga>' => [%w{G Giga giga}, 1e9, :prefix],
|
21
|
+
'<mega>' => [%w{M Mega mega}, 1e6, :prefix],
|
22
|
+
'<kilo>' => [%w{k kilo}, 1e3, :prefix],
|
23
|
+
'<hecto>' => [%w{h Hecto hecto}, 1e2, :prefix],
|
24
|
+
'<deca>' => [%w{da Deca deca deka}, 1e1, :prefix],
|
25
|
+
'<deci>' => [%w{d Deci deci}, 1e-1, :prefix],
|
26
|
+
'<centi>' => [%w{c Centi centi}, 1e-2, :prefix],
|
27
|
+
'<milli>' => [%w{m Milli milli}, 1e-3, :prefix],
|
28
|
+
'<micro>' => [%w{u Micro micro}, 1e-6, :prefix],
|
29
|
+
'<nano>' => [%w{n Nano nano}, 1e-9, :prefix],
|
30
|
+
'<pico>' => [%w{p Pico pico}, 1e-12, :prefix],
|
31
|
+
'<femto>' => [%w{f Femto femto}, 1e-15, :prefix],
|
32
|
+
'<atto>' => [%w{a Atto atto}, 1e-18, :prefix],
|
33
|
+
'<zepto>' => [%w{z Zepto zepto}, 1e-21, :prefix],
|
34
|
+
'<yocto>' => [%w{y Yocto yocto}, 1e-24, :prefix],
|
35
|
+
'<1>' => [%w{1},1,:prefix],
|
36
|
+
|
37
|
+
# length units
|
38
|
+
'<meter>' => [%w{m meter meters metre metres}, 1.0, :length, %w{<meter>} ],
|
39
|
+
'<inch>' => [%w{in inch inches "}, 0.0254, :length, %w{<meter>}],
|
40
|
+
'<foot>' => [%w{ft foot feet '}, 0.3048, :length, %w{<meter>}],
|
41
|
+
'<yard>' => [%w{yd yard yards}, 0.9144, :length, %w{<meter>}],
|
42
|
+
'<mile>' => [%w{mi mile miles "}, 1609.344, :length, %w{<meter>}],
|
43
|
+
'<naut-mile>' => [%w{nmi}, 1852, :length, %w{<meter>}],
|
44
|
+
'<league>'=> [%w{league leagues}, 4828, :length, %w{<meter>}],
|
45
|
+
'<furlong>'=> [%w{furlong furlongs}, 201.2, :length, %w{<meter>}],
|
46
|
+
'<rod>' => [%w{rd rod rods}, 5.029, :length, %w{<meter>}],
|
47
|
+
'<mil>' => [%w{mil mils}, 0.0000254, :length, %w{<meter>}],
|
48
|
+
'<angstrom>' =>[%w{ang angstrom angstroms}, 1e-10, :length, %w{<meter>}],
|
49
|
+
'<fathom>' => [%w{fathom fathoms}, 1.829, :length, %w{<meter>}],
|
50
|
+
'<pica>' => [%w{pica picas}, 0.004217, :length, %w{<meter>}],
|
51
|
+
'<point>' => [%w{pt point points}, 0.0003514, :length, %w{<meter>}],
|
52
|
+
'<redshift>' => [%w{z red-shift}, 1.302773e26, :length, %w{<meter>}],
|
53
|
+
'<AU>' => [%w{AU astronomical-unit}, 149597900000, :length, %w{<meter>}],
|
54
|
+
'<light-second>'=>[%w{ls light-second}, 299792500, :length, %w{<meter>}],
|
55
|
+
'<light-minute>'=>[%w{lmin light-minute}, 17987550000, :length, %w{<meter>}],
|
56
|
+
'<light-year>' => [%w{ly light-year}, 9460528000000000, :length, %w{<meter>}],
|
57
|
+
'<parsec>' => [%w{pc parsec parsecs}, 30856780000000000, :length, %w{<meter>}],
|
58
|
+
|
59
|
+
#mass
|
60
|
+
'<kilogram>' => [%w{kg kilogram kilograms}, 1.0, :mass, %w{<kilogram>}],
|
61
|
+
'<AMU>' => [%w{u AMU amu}, 6.0221415e26, :mass, %w{<kilogram>}],
|
62
|
+
'<dalton>' => [%w{Da Dalton Daltons dalton daltons}, 6.0221415e26, :mass, %w{<kilogram>}],
|
63
|
+
'<slug>' => [%w{slug slugs}, 14.5939029, :mass, %w{<kilogram>}],
|
64
|
+
'<short-ton>' => [%w{t ton}, 907.18474, :mass, %w{<kilogram>}],
|
65
|
+
'<metric-ton>'=>[%w{tonne}, 1000, :mass, %w{<kilogram>}],
|
66
|
+
'<carat>' => [%w{ct carat carats}, 0.0002, :mass, %w{<kilogram>}],
|
67
|
+
'<pound-mass>' => [%w{lbs lb pound pounds #}, 0.45359237, :mass, %w{<kilogram>}],
|
68
|
+
'<ounce>' => [%w{oz ounce ounces}, 0.0283495231, :mass, %w{<kilogram>}],
|
69
|
+
'<gram>' => [%w{g gram grams gramme grammes},1e-3,:mass, %w{<kilogram>}],
|
70
|
+
|
71
|
+
#area
|
72
|
+
'<hectare>'=>[%w{hectare}, 10000, :area, %w{<meter> <meter>}],
|
73
|
+
'<acre>'=>[%w(acre acres), 4046.85642, :area, %w{<meter> <meter>}],
|
74
|
+
|
75
|
+
#volume
|
76
|
+
'<liter>' => [%w{l L liter liters litre litres}, 0.001, :volume, %w{<meter> <meter> <meter>}],
|
77
|
+
'<gallon>'=> [%w{gal gallon gallons}, 0.0037854118, :volume, %w{<meter> <meter> <meter>}],
|
78
|
+
'<quart>'=> [%w{qt quart quarts}, 0.00094635295, :volume, %w{<meter> <meter> <meter>}],
|
79
|
+
'<pint>'=> [%w{pt pint pints}, 0.000473176475, :volume, %w{<meter> <meter> <meter>}],
|
80
|
+
'<cup>'=> [%w{cu cup cups}, 0.000236588238, :volume, %w{<meter> <meter> <meter>}],
|
81
|
+
'<fluid-ounce>'=> [%w{floz fluid-ounce}, 2.95735297e-5, :volume, %w{<meter> <meter> <meter>}],
|
82
|
+
'<tablespoon>'=> [%w{tbs tablespoon tablespoons}, 1.47867648e-5, :volume, %w{<meter> <meter> <meter>}],
|
83
|
+
'<teaspoon>'=> [%w{tsp teaspoon teaspoons}, 4.92892161e-6, :volume, %w{<meter> <meter> <meter>}],
|
84
|
+
|
85
|
+
#speed
|
86
|
+
'<kph>' => [%w{kph}, 0.277777778, :speed, %w{<meter>}, %w{<second>}],
|
87
|
+
'<mph>' => [%w{mph}, 0.44704, :speed, %w{<meter>}, %w{<second>}],
|
88
|
+
'<knot>' => [%w{kn knot knots}, 0.514444444, :speed, %w{<meter>}, %w{<second>}],
|
89
|
+
'<fps>' => [%w{fps}, 0.3048, :speed, %w{<meter>}, %w{<second>}],
|
90
|
+
|
91
|
+
#temperature
|
92
|
+
'<kelvin>' => [%w{degK kelvin Kelvin}, 1.0, :temperature, %w{<celcius>}],
|
93
|
+
'<celcius>' => [%w{degC celcius Celcius}, 1.0, :temperature, %w{<celcius>}],
|
94
|
+
'<farenheit>' => [%w{degF farenheit Farenheit}, 1.8, :temperature, %w{<celcius>}],
|
95
|
+
'<rankine>' => [%w{degR rankine Rankine}, 1.8, :temperature, %w{<celcius>}],
|
96
|
+
|
97
|
+
#time
|
98
|
+
'<second>'=> [%w{s sec second seconds}, 1.0, :time, %w{<second>}],
|
99
|
+
'<minute>'=> [%w{min minute minutes}, 60.0, :time, %w{<second>}],
|
100
|
+
'<hour>'=> [%w{h hour hours}, 3600.0, :time, %w{<second>}],
|
101
|
+
'<day>'=> [%w{d day days}, 3600*24, :time, %w{<second>}],
|
102
|
+
'<week>'=> [%w{wk week weeks}, 7*3600*24, :time, %w{<second>}],
|
103
|
+
'<fortnight>'=> [%w{fortnight}, 1209600, :time, %W{<second>}],
|
104
|
+
'<year>'=> [%w{y yr year years annum}, 31556926, :time, %w{<second>}],
|
105
|
+
'<decade>'=>[%w{decade decades}, 315569260, :time, %w{<second>}],
|
106
|
+
'<century>'=>[%w{century centuries}, 3155692600, :time, %w{<second>}],
|
107
|
+
|
108
|
+
#pressure
|
109
|
+
'<pascal>' => [%w{Pa pascal Pascal}, 1.0, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
110
|
+
'<bar>' => [%w{bar bars}, 100000, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
111
|
+
'<mmHg>' => [%w{mmHg}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
112
|
+
'<torr>' => [%w{Torr}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
113
|
+
'<bar>' => [%w{bar}, 100000,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
114
|
+
'<atm>' => [%w{atm atmosphere atmospheres}, 101325,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
115
|
+
'<psi>' => [%w{psi}, 6894.76,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
116
|
+
'<cmh2o>' => [%w{cmH2O}, 98.0638,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
|
117
|
+
|
118
|
+
#viscosity
|
119
|
+
'<poise>' => [%w{P poise}, 0.1, :viscosity, %w{<kilogram>},%w{<meter> <second>} ],
|
120
|
+
'<stokes>' => [%w{St stokes}, 1e-4, :viscosity, %w{<meter> <meter>}, %w{<second>}],
|
121
|
+
|
122
|
+
#substance
|
123
|
+
'<mole>' => [%w{mol mole}, 1.0, :substance, %w{<mole>}],
|
124
|
+
|
125
|
+
#concentration
|
126
|
+
'<molar>' => [%w{M molar}, 1000, :concentration, %w{<mole>}, %w{<meter> <meter> <meter>}],
|
127
|
+
|
128
|
+
#activity
|
129
|
+
'<katal>' => [%w{kat katal Katal}, 1.0, :activity, %w{<mole>}, %w{<second>}],
|
130
|
+
'<unit>' => [%w{U enzUnit}, 16.667e-16, :activity, %w{<mole>}, %w{<second>}],
|
131
|
+
|
132
|
+
#capacitance
|
133
|
+
'<farad>' => [%w{F farad Farad}, 1.0, :capacitance, %w{<farad>}],
|
134
|
+
|
135
|
+
#charge
|
136
|
+
'<coulomb>' => [%w{C coulomb Coulomb}, 1.0, :charge, %w{<ampere> <second>}],
|
137
|
+
|
138
|
+
#current
|
139
|
+
'<ampere>' => [%w{A Ampere ampere amp amps}, 1.0, :current, %w{<ampere>}],
|
140
|
+
|
141
|
+
#conductance
|
142
|
+
'<siemens>' => [%w{S Siemens siemens}, 1.0, :resistance, %w{<second> <second> <second> <ampere> <ampere>}, %w{<kilogram> <meter> <meter>}],
|
143
|
+
|
144
|
+
#inductance
|
145
|
+
'<henry>' => [%w{H Henry henry}, 1.0, :inductance, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere> <ampere>}],
|
146
|
+
|
147
|
+
#potential
|
148
|
+
'<volt>' => [%w{V Volt volt volts}, 1.0, :potential, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <second> <ampere>}],
|
149
|
+
|
150
|
+
#resistance
|
151
|
+
'<ohm>' => [%w{Ohm ohm}, 1.0, :resistance, %w{<meter> <meter> <kilogram>},%w{<second> <second> <second> <ampere> <ampere>}],
|
152
|
+
|
153
|
+
#magnetism
|
154
|
+
'<weber>' => [%w{Wb weber webers}, 1.0, :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}],
|
155
|
+
'<tesla>' => [%w{T tesla teslas}, 1.0, :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}],
|
156
|
+
'<gauss>' => [%w{G gauss}, 1e-4, :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}],
|
157
|
+
'<maxwell>' => [%w{Mx maxwell maxwells}, 1e-8, :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}],
|
158
|
+
'<oersted>' => [%w{Oe oersted oersteds}, 250.0/Math::PI, :magnetism, %w{<ampere>}, %w{<meter>}],
|
159
|
+
|
160
|
+
#energy
|
161
|
+
'<joule>' => [%w{J joule Joule joules}, 1.0, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
162
|
+
'<erg>' => [%w{erg ergs}, 1e-7, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
163
|
+
'<btu>' => [%w{BTU btu BTUs}, 1055.056, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
164
|
+
'<calorie>' => [%w{cal calorie calories}, 4.18400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
165
|
+
'<Calorie>' => [%w{Cal Calorie Calories}, 4184.00, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
166
|
+
'<therm-US>' => [%w{th therm therms Therm}, 105480400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
|
167
|
+
|
168
|
+
#force
|
169
|
+
'<newton>' => [%w{N Newton newton}, 1.0, :force, %w{<kilogram> <meter>}, %w{<second>}],
|
170
|
+
'<dyne>' => [%w{dyn dyne}, 1e-5, :force, %w{<kilogram> <meter>}, %w{<second>}],
|
171
|
+
'<pound-force>' => [%w{lbf pound-force}, 4.448222, :force, %w{<kilogram> <meter>}, %w{<second>}],
|
172
|
+
|
173
|
+
#frequency
|
174
|
+
'<hertz>' => [%w{Hz hertz Hertz}, 1.0, :frequency, %w{<1>}, %{<second>}],
|
175
|
+
|
176
|
+
#angle
|
177
|
+
'<radian>' =>[%w{rad radian radians}, 1.0, :angle, %w{<radian>}],
|
178
|
+
'<degree>' =>[%w{deg degree degrees}, Math::PI / 180.0, :angle, %w{<radian>}],
|
179
|
+
'<grad>' =>[%w{grad gradian grads}, Math::PI / 200.0, :angle, %w{<radian>}],
|
180
|
+
'<steradian>' => [%w{sr steradian steradians}, 1.0, :solid_angle, %w{<steradian>}],
|
181
|
+
|
182
|
+
#rotation
|
183
|
+
'<rotation>' => [%w{rotation}, 2.0*Math::PI, :rotation, %w{<radian>}],
|
184
|
+
'<rpm>' =>[%w{rpm}, 2.0*Math::PI / 60.0, :rotation, %w{<radian>}, %w{<second>}],
|
185
|
+
|
186
|
+
#memory
|
187
|
+
'<byte>' =>[%w{B byte}, 1.0, :memory, %w{<byte>}],
|
188
|
+
'<bit>' =>[%w{b bit}, 0.125, :memory, %w{<byte>}],
|
189
|
+
|
190
|
+
#money
|
191
|
+
'<dollar>'=>[['$','dollar','USD'], 1.0, :currency, %w{<dollar>}],
|
192
|
+
'<cents>' =>[%w{cents}, 0.01, :currency, %w{<dollar>}],
|
193
|
+
|
194
|
+
#luminosity
|
195
|
+
'<candela>' => [%w{cd candela}, 1.0, :luminosity, %w{<candela>}],
|
196
|
+
'<lumen>' => [%w{lm lumen}, 1.0, :luminous_power, %w{<candela> <steradian>}],
|
197
|
+
'<lux>' =>[%w{lux}, 1.0, :illuminance, %w{<candela> <steradian>}, %w{<meter> <meter>}],
|
198
|
+
|
199
|
+
#power
|
200
|
+
'<watt>' => [%w{W watt watts}, 1.0, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}],
|
201
|
+
'<horsepower>' => [%w{hp horsepower}, 745.699872, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}],
|
202
|
+
|
203
|
+
#radition
|
204
|
+
'<gray>' => [%w{Gy gray grays}, 1.0, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
|
205
|
+
'<roentgen>' => [%w{R roentgen}, 0.009330, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
|
206
|
+
'<sievert>' => [%w{Sv sievert sieverts}, 1.0, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
|
207
|
+
'<becquerel>' => [%w{Bq bequerel bequerels}, 1.0, :radiation, %w{<1>},%w{<second>}],
|
208
|
+
'<curie>' => [%w{Ci curie curies}, 3.7e10, :radiation, %w{<1>},%w{<second>}],
|
209
|
+
'<cpm>' => [%w{cpm}, 1.0/60.0, :radiation, %w{<1>},%w{<second>}],
|
210
|
+
'<dpm>' => [%w{dpm}, 1.0/60.0, :radiation, %w{<1>},%w{<second>}],
|
211
|
+
|
212
|
+
#other
|
213
|
+
'<each>' => [%w{each}, 1.0, :counting, %w{<each>}],
|
214
|
+
'<count>' => [%w{count}, 1.0, :counting, %w{<each>}],
|
215
|
+
'<base-pair>' => [%w{bp}, 1.0, :counting, %w{<each>}],
|
216
|
+
'<nucleotide>' => [%w{nt}, 1.0, :counting, %w{<each>}],
|
217
|
+
'<molecule>' => [%w{molecule molecules}, 1.0, :counting, %w{<each>}],
|
218
|
+
'<dozen>' => [%w{dz doz dozen},12.0,:prefix],
|
219
|
+
'<percent>'=> [%w{% percent}, 0.01, :prefix_only, %w{<centi>}],
|
220
|
+
'<ppm>' => [%w{ppm},1e-6,:prefix_only, %w{<micro>}],
|
221
|
+
'<ppt>' => [%w{ppt},1e-9,:prefix_only, %w{<nano>}],
|
222
|
+
'<gross>' => [%w{gr gross},144.0, :prefix_only, %w{<dozen> <dozen>}]
|
223
|
+
|
224
|
+
|
225
|
+
} # doc
|
226
|
+
end
|
@@ -0,0 +1,384 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'ruby_units'
|
3
|
+
|
4
|
+
class Unit < Numeric
|
5
|
+
@@USER_DEFINITIONS = {'<inchworm>' => [%w{inworm inchworm}, 0.0254, :length, %w{<meter>} ]}
|
6
|
+
Unit.setup
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestRubyUnit < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_unary_minus
|
12
|
+
unit1 = Unit.new("1 mm")
|
13
|
+
unit2 = Unit.new("-1 mm")
|
14
|
+
assert_equal unit1, -unit2
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_unary_plus
|
18
|
+
unit1 = Unit.new("1 mm")
|
19
|
+
assert_equal unit1, +unit1
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_create_unit_only
|
23
|
+
unit1 = Unit.new("m")
|
24
|
+
assert_equal ['<meter>'], unit1.numerator
|
25
|
+
assert_equal 1.0, unit1.quantity
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_to_base
|
29
|
+
unit1 = Unit.new("100 cm")
|
30
|
+
assert_equal 1.0, unit1.to_base.quantity
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_to_unit
|
34
|
+
unit1 = "1 mm".to_unit
|
35
|
+
assert Unit === unit1
|
36
|
+
unit2 = Unit("1 mm")
|
37
|
+
assert Unit === unit1
|
38
|
+
assert unit1 == unit2
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_create_unitless
|
42
|
+
unit1 = Unit.new("1")
|
43
|
+
assert_equal 1,unit1.to_f
|
44
|
+
assert_equal ['<1>'],unit1.numerator
|
45
|
+
assert_equal ['<1>'],unit1.denominator
|
46
|
+
unit1 = Unit.new("1.5")
|
47
|
+
assert_equal 1.5,unit1.to_f
|
48
|
+
assert_equal ['<1>'],unit1.numerator
|
49
|
+
assert_equal ['<1>'],unit1.denominator
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_create_simple
|
53
|
+
unit1 = Unit.new("1 m")
|
54
|
+
assert_equal 1,unit1.to_f
|
55
|
+
assert_equal ['<meter>'], unit1.numerator
|
56
|
+
assert_equal ['<1>'],unit1.denominator
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_create_compound
|
60
|
+
unit1 = Unit.new("1 N*m")
|
61
|
+
assert_equal 1,unit1.to_f
|
62
|
+
assert_equal ['<newton>','<meter>'],unit1.numerator
|
63
|
+
assert_equal ['<1>'],unit1.denominator
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_create_with_denominator
|
67
|
+
unit1 = Unit.new("1 m/s")
|
68
|
+
assert_equal 1, unit1.to_f
|
69
|
+
assert_equal ['<meter>'],unit1.numerator
|
70
|
+
assert_equal ['<second>'],unit1.denominator
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_create_with_powers
|
74
|
+
unit1 = Unit.new("1 m^2/s^2")
|
75
|
+
assert_equal 1, unit1.to_f
|
76
|
+
assert_equal ['<meter>','<meter>'],unit1.numerator
|
77
|
+
assert_equal ['<second>','<second>'],unit1.denominator
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_create_with_zero_power
|
81
|
+
unit1 = Unit.new("1 m^0")
|
82
|
+
assert_equal 1,unit1.to_f
|
83
|
+
assert_equal ['<1>'],unit1.numerator
|
84
|
+
assert_equal ['<1>'],unit1.denominator
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_create_with_negative_powers
|
88
|
+
unit1 = Unit.new("1 m^2 s^-2")
|
89
|
+
assert_equal 1, unit1.to_f
|
90
|
+
assert_equal ['<meter>','<meter>'],unit1.numerator
|
91
|
+
assert_equal ['<second>','<second>'],unit1.denominator
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_convert
|
95
|
+
unit1 = Unit.new("1 attoparsec/microfortnight")
|
96
|
+
assert_nothing_raised {
|
97
|
+
unit2 = unit1 >> "in/s"
|
98
|
+
assert_equal ['<inch>'],unit2.numerator
|
99
|
+
assert_equal ['<second>'],unit2.denominator
|
100
|
+
assert_in_delta 1.0043269330917,unit2.to_f,0.00001
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_convert_to
|
105
|
+
unit1 = Unit.new("1 mm")
|
106
|
+
unit2 = Unit.new("1 ft")
|
107
|
+
assert_nothing_raised {
|
108
|
+
unit3 = unit1 >> unit2
|
109
|
+
assert_equal ['<foot>'], unit3.numerator
|
110
|
+
assert_equal ['<1>'],unit3.denominator
|
111
|
+
unit3 = unit1 >> "ft"
|
112
|
+
assert_equal ['<foot>'], unit3.numerator
|
113
|
+
assert_equal ['<1>'],unit3.denominator
|
114
|
+
}
|
115
|
+
assert_raises(ArgumentError) { unit3= unit1 >> 5.0}
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_matched_units
|
119
|
+
unit1 = Unit.new("1 m*kg/s")
|
120
|
+
unit2 = Unit.new("1 in*pound/min")
|
121
|
+
assert unit1 =~ unit2
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_matched_units_using_string
|
125
|
+
unit1 = Unit.new("1 m*kg/s")
|
126
|
+
assert unit1 =~ "in*pound/min"
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_unmatched_units
|
130
|
+
unit1 = Unit.new("1 m*kg/s")
|
131
|
+
unit2 = Unit.new("1 mm")
|
132
|
+
assert unit1 !~ unit2
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_comparison_like_units
|
136
|
+
unit1 = Unit.new("1 in")
|
137
|
+
unit2 = Unit.new("1 mm")
|
138
|
+
assert_nothing_raised {
|
139
|
+
assert unit1 > unit2
|
140
|
+
}
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_comparison_unlike_units
|
144
|
+
unit1 = Unit.new("1 kg")
|
145
|
+
unit2 = Unit.new("1 mm")
|
146
|
+
assert_raise(ArgumentError) { unit1 > unit2 }
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_add_like_units
|
150
|
+
unit1 = Unit.new("1 mm")
|
151
|
+
unit2 = Unit.new("2 mm")
|
152
|
+
assert_nothing_raised {
|
153
|
+
assert_equal 3.0, (unit1+unit2).to_f
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_add_similar_units
|
158
|
+
unit1 = Unit.new("1 cm")
|
159
|
+
unit2 = Unit.new("1 in")
|
160
|
+
assert_nothing_raised {
|
161
|
+
unit3 = unit1 + unit2
|
162
|
+
assert_in_delta 3.54, unit3.to_f, 0.01
|
163
|
+
}
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_subtract_similar_units
|
167
|
+
unit1 = Unit.new("1 cm")
|
168
|
+
unit2 = Unit.new("1 in")
|
169
|
+
assert_nothing_raised {
|
170
|
+
unit3 = unit1 - unit2
|
171
|
+
assert_in_delta -1.54, unit3.to_f, 0.01
|
172
|
+
}
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_add_unlike_units
|
176
|
+
unit1 = Unit.new("1 mm")
|
177
|
+
unit2 = Unit.new("2 ml")
|
178
|
+
assert_raise(ArgumentError) {(unit1+unit2).to_f}
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_signature #"1 m s deg K kg A mol cd byte rad
|
182
|
+
unit1 = Unit.new("1 m*s*degK*kg*A*mol*cd*byte*rad*dollar")
|
183
|
+
assert_equal unit1.signature, (0..9).inject(0) {|product, n| product + 20**n}
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_subtract_like_units
|
187
|
+
unit1 = Unit.new("1 mm")
|
188
|
+
unit2 = Unit.new("2 mm")
|
189
|
+
assert_nothing_raised {
|
190
|
+
assert_equal -1, (unit1-unit2).to_f
|
191
|
+
}
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_subtract_unlike_units
|
195
|
+
unit1 = Unit.new("1 mm")
|
196
|
+
unit2 = Unit.new("2 ml")
|
197
|
+
assert_raise(ArgumentError) {(unit1-unit2).to_f}
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_multiply
|
201
|
+
unit1 = Unit.new("1 M")
|
202
|
+
unit2 = Unit.new("200 g/mole")
|
203
|
+
assert_nothing_raised {
|
204
|
+
unit3 = unit1 * unit2
|
205
|
+
assert_equal Unit.new("200 M*g/mol"), unit3
|
206
|
+
}
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_divide
|
210
|
+
unit1 = Unit.new("200 M*g/mol")
|
211
|
+
unit2 = Unit.new("200 g/mole")
|
212
|
+
assert_nothing_raised {
|
213
|
+
unit3 = unit1 / unit2
|
214
|
+
assert_equal Unit.new("1 M"), unit3
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_inverse
|
219
|
+
unit1 = Unit.new("1 m")
|
220
|
+
unit2 = Unit.new("1 1/m")
|
221
|
+
assert_equal unit2, unit1.inverse
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_exponentiate_positive
|
225
|
+
unit1 = Unit.new("1 mm")
|
226
|
+
unit2 = Unit.new("1 mm^2")
|
227
|
+
assert_nothing_raised {
|
228
|
+
assert_equal unit2, unit1**2
|
229
|
+
}
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_exponentiate_float
|
233
|
+
unit1 = Unit.new("1 mm")
|
234
|
+
assert_raise(ArgumentError) {unit1**2.5}
|
235
|
+
end
|
236
|
+
|
237
|
+
def test_exponentiate_negative
|
238
|
+
unit1 = Unit.new("1 m")
|
239
|
+
unit2 = Unit.new("1 m^-2")
|
240
|
+
assert_nothing_raised {
|
241
|
+
assert_equal unit2, unit1**-2
|
242
|
+
}
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_exponentiate_zero
|
246
|
+
unit1 = Unit.new("10 mm")
|
247
|
+
unit2 = Unit.new("1")
|
248
|
+
assert_nothing_raised {
|
249
|
+
assert_equal unit2, unit1**0
|
250
|
+
}
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_abs
|
254
|
+
unit1 = Unit.new("-1 mm")
|
255
|
+
assert_equal 1, unit1.abs
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_ceil
|
259
|
+
unit1 = Unit.new("1.1 mm")
|
260
|
+
unit2 = Unit.new("2 mm")
|
261
|
+
assert_equal unit2, unit1.ceil
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_floor
|
265
|
+
unit1 = Unit.new("1.1 mm")
|
266
|
+
unit2 = Unit.new("1 mm")
|
267
|
+
assert_equal unit2, unit1.floor
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_to_int
|
271
|
+
unit1 = Unit.new("1.1 mm")
|
272
|
+
unit2 = Unit.new("1 mm")
|
273
|
+
assert_equal unit2, unit1.to_int
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_truncate
|
277
|
+
unit1 = Unit.new("1.1 mm")
|
278
|
+
unit2 = Unit.new("1 mm")
|
279
|
+
assert_equal unit2, unit1.truncate
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_round
|
283
|
+
unit1 = Unit.new("1.1 mm")
|
284
|
+
unit2 = Unit.new("1 mm")
|
285
|
+
assert_equal unit2, unit1.round
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_zero?
|
289
|
+
unit1 = Unit.new("0")
|
290
|
+
assert unit1.zero?
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_nonzero?
|
294
|
+
unit1 = Unit.new("0")
|
295
|
+
unit2 = Unit.new("1 mm")
|
296
|
+
assert_nil unit1.nonzero?
|
297
|
+
assert_equal unit2, unit2.nonzero?
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_equality
|
301
|
+
unit1 = Unit.new("1 cm")
|
302
|
+
unit2 = Unit.new("10 mm")
|
303
|
+
assert unit1 == unit2
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_temperature_conversions
|
307
|
+
unit1 = Unit.new("37 degC")
|
308
|
+
unit2 = unit1 >> "degF"
|
309
|
+
assert_in_delta 98.6, unit2.to_f, 0.1
|
310
|
+
unit2 = unit1 >> "degK"
|
311
|
+
assert_in_delta 310.15, unit2.to_f, 0.01
|
312
|
+
unit2 = unit1 >> "degR"
|
313
|
+
assert_in_delta 558.27, unit2.to_f, 0.01
|
314
|
+
unit3 = Unit.new("1 J/degC")
|
315
|
+
assert_in_delta 1, (unit3 >> "J/degK").to_f, 0.01
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_feet
|
319
|
+
unit1 = Unit.new("6'6\"")
|
320
|
+
assert_in_delta 6.5, unit1.to_f, 0.01
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_pounds
|
324
|
+
unit1 = Unit.new("8 pounds, 8 ounces")
|
325
|
+
assert_in_delta 8.5, unit1.to_f, 0.01
|
326
|
+
end
|
327
|
+
|
328
|
+
# these units are 'ambiguous' and could be mis-parsed
|
329
|
+
def test_parse_tricky_units
|
330
|
+
unit1 = Unit.new('1 mm') #sometimes parsed as 'm*m'
|
331
|
+
assert_equal ['<milli>','<meter>'], unit1.numerator
|
332
|
+
unit2 = Unit.new('1 cd') # could be a 'centi-day' instead of a candela
|
333
|
+
assert_equal ['<candela>'], unit2.numerator
|
334
|
+
unit3 = Unit.new('1 min') # could be a 'milli-inch' instead of a minute
|
335
|
+
assert_equal ['<minute>'], unit3.numerator
|
336
|
+
unit4 = Unit.new('1 ft') # could be a 'femto-ton' instead of a foot
|
337
|
+
assert_equal ['<foot>'], unit4.numerator
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_to_s
|
341
|
+
unit1 = Unit.new("1")
|
342
|
+
assert_equal "1", unit1.to_s
|
343
|
+
unit2 = Unit.new("mm")
|
344
|
+
assert_equal "1 mm", unit2.to_s
|
345
|
+
assert_equal "0.04 in", unit2.to_s("%0.2f in")
|
346
|
+
assert_equal "0.1 cm", unit2.to_s("cm")
|
347
|
+
unit3 = Unit.new("1 mm")
|
348
|
+
assert_equal "1 mm", unit3.to_s
|
349
|
+
assert_equal "0.04 in", unit3.to_s("%0.2f in")
|
350
|
+
unit4 = Unit.new("1 mm^2")
|
351
|
+
assert_equal "1 mm^2", unit4.to_s
|
352
|
+
unit5 = Unit.new("1 mm^2 s^-2")
|
353
|
+
assert_equal "1 mm^2/s^2", unit5.to_s
|
354
|
+
unit6= Unit.new("1 kg*m/s")
|
355
|
+
assert_equal "1 kg*m/s", unit6.to_s
|
356
|
+
unit7= Unit.new("1 1/m")
|
357
|
+
assert_equal "1 1/m", unit7.to_s
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_to_feet_inches
|
362
|
+
unit1 = Unit.new("6'5\"")
|
363
|
+
assert_equal "6'5\"", unit1.to_s(:ft)
|
364
|
+
assert_raises(ArgumentError) {
|
365
|
+
unit1 = Unit.new("1 kg")
|
366
|
+
unit1.to_s(:ft)
|
367
|
+
}
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_to_lbs_oz
|
371
|
+
unit1 = Unit.new("8 lbs 8 oz")
|
372
|
+
assert_equal "8 lbs, 8 oz", unit1.to_s(:lbs)
|
373
|
+
assert_raises(ArgumentError) {
|
374
|
+
unit1 = Unit.new("1 m")
|
375
|
+
unit1.to_s(:lbs)
|
376
|
+
}
|
377
|
+
end
|
378
|
+
|
379
|
+
def test_add_units
|
380
|
+
a = Unit.new("1 inchworm")
|
381
|
+
assert_equal "1 inworm", a.to_s
|
382
|
+
end
|
383
|
+
|
384
|
+
end
|