eymiha_units 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/gem_package.rb +34 -0
- data/lib/units/definitions/area.rb +5 -0
- data/lib/units/definitions/length.rb +34 -0
- data/lib/units/definitions/mass.rb +16 -0
- data/lib/units/definitions/measures.rb +6 -0
- data/lib/units/definitions/time.rb +61 -0
- data/lib/units/definitions/velocity.rb +9 -0
- data/lib/units/definitions/volume.rb +27 -0
- data/lib/units/numeric.rb +88 -0
- data/lib/units/numeric_with_units.rb +635 -0
- data/lib/units/object.rb +79 -0
- data/lib/units/units.rb +229 -0
- data/lib/units/units_exception.rb +14 -0
- data/lib/units/units_hash.rb +112 -0
- data/lib/units/units_measure.rb +91 -0
- data/lib/units/units_system.rb +85 -0
- data/lib/units/units_unit.rb +60 -0
- data/lib/units.rb +4 -0
- data/rakefile.rb +2 -0
- data/test/framework.rb +7 -0
- data/test/tc_definitions.rb +49 -0
- data/test/tc_formatting.rb +41 -0
- data/test/tc_formatting_derived.rb +73 -0
- data/test/tc_measure_create.rb +59 -0
- data/test/tc_measure_derive.rb +46 -0
- data/test/tc_system_create.rb +31 -0
- data/test/tc_unit_ambiguity.rb +46 -0
- data/test/tc_unit_arithmetic.rb +41 -0
- data/test/tc_unit_create.rb +35 -0
- data/test/tc_unit_derive.rb +87 -0
- data/test/tc_unit_equality.rb +54 -0
- data/test/tc_unit_forward_reference.rb +44 -0
- data/test/tc_unit_greek.rb +163 -0
- data/test/tc_unit_hash.rb +66 -0
- data/test/tc_unit_identifiers.rb +48 -0
- data/test/tc_unit_rank.rb +53 -0
- data/test/tc_uses_1.rb +212 -0
- data/test/tc_uses_2.rb +149 -0
- metadata +118 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_formatting_derived < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_formatting_derived
|
11
|
+
|
12
|
+
Units.create :length do |m|
|
13
|
+
m.system :metric do |s|
|
14
|
+
s.unit :name => :meter, :abbrev => :m, :greek => :ten
|
15
|
+
end
|
16
|
+
end
|
17
|
+
Units.derive :volume, Units.length**3 do |m|
|
18
|
+
m.system :metric do |s|
|
19
|
+
s.unit :name => :milliliter, :abbrev => :ml, :equals => 1.cm**3
|
20
|
+
end
|
21
|
+
end
|
22
|
+
Units.create :time do |m|
|
23
|
+
m.system :base do |s|
|
24
|
+
s.unit :name => :second, :abbrev => :s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
assert((225.ml/5.cm) =~ (45.0.cm^2))
|
29
|
+
|
30
|
+
a = 225.ml
|
31
|
+
assert(a.to_s == "225 milliliters")
|
32
|
+
|
33
|
+
b = 5.cm
|
34
|
+
c = 15.cm^3
|
35
|
+
assert((a/b).to_s == "45.0 cm^2")
|
36
|
+
assert((b/a).to_s == "222.222222222222 1/m^2")
|
37
|
+
assert((a*b).to_s == "1125.0 cm^4")
|
38
|
+
ba = (b*a).to_s
|
39
|
+
assert((ba == "1.125e-05 m^4")||(ba == "1.125e-005 m^4"))
|
40
|
+
assert((a+c).to_s == "240.0 cm^3")
|
41
|
+
assert((c+a).to_s == "0.00024 m^3")
|
42
|
+
assert((a-c).to_s == "210.0 cm^3")
|
43
|
+
assert((c-a).to_s == "-0.00021 m^3")
|
44
|
+
|
45
|
+
assert(c.in_ml.to_s == "15.0 milliliters")
|
46
|
+
|
47
|
+
c = 15.cm^7
|
48
|
+
assert(c.ml.to_s == "15.0 cm ml^2")
|
49
|
+
NumericWithUnits.derived_align_type = :fractional_powers
|
50
|
+
assert(c.ml.to_s == "15.0 ml^2.33333333333333")
|
51
|
+
|
52
|
+
d = 15.ml^(7.0/3)
|
53
|
+
assert(d =~ c)
|
54
|
+
assert(c =~ d)
|
55
|
+
assert(c.ml =~ d)
|
56
|
+
assert(d.cm =~ c)
|
57
|
+
|
58
|
+
assert(c.in_ml =~ 15.ml^(7.0/3))
|
59
|
+
d = c.to_ml
|
60
|
+
assert(d.to_s == "15.0 ml^2.33333333333333")
|
61
|
+
|
62
|
+
NumericWithUnits.derived_align_type = :whole_powers
|
63
|
+
assert((225.ml/5.cm).to_s == '45.0 cm^2')
|
64
|
+
assert((225.ml/5.cm).in_ml.to_s == '45.0 ml / cm')
|
65
|
+
|
66
|
+
assert((225.ml/5.cm).align([1.cm^2]).to_s == '45.0 cm^2')
|
67
|
+
assert((225.ml/5.cm).align([1.ml,1/cm]).to_s == '45.0 ml / cm')
|
68
|
+
assert((225.ml/5.cm).align([1.m^2]).to_s == '0.0045 m^2')
|
69
|
+
assert((225.ml/5.cm).align([1.ml,1/m]).to_s == '4500.0 ml / m')
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_measure_create < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_measure_create
|
11
|
+
|
12
|
+
assert(Units.units_measures == [])
|
13
|
+
|
14
|
+
assert(Units.create(:length).kind_of?(UnitsMeasure))
|
15
|
+
assert(Units.length.kind_of?(UnitsMeasure))
|
16
|
+
assert_raise(UnitsException) { Units.time }
|
17
|
+
assert(Units.units_measures.index('length') != nil)
|
18
|
+
assert(Units.units_measures.index('time') == nil)
|
19
|
+
assert(Units.units_measures.index('distance') == nil)
|
20
|
+
|
21
|
+
assert(Units.create('time').kind_of?(UnitsMeasure))
|
22
|
+
assert(Units.size == 2)
|
23
|
+
assert(Units.time.kind_of?(UnitsMeasure))
|
24
|
+
assert(Units.units_measures.index('length') != nil)
|
25
|
+
assert(Units.units_measures.index('time') != nil)
|
26
|
+
assert(Units.units_measures.index('distance') == nil)
|
27
|
+
|
28
|
+
assert(Units.derive(:distance, 'length').kind_of?(UnitsMeasure))
|
29
|
+
assert(Units.size == 3)
|
30
|
+
assert(Units.distance.kind_of?(UnitsMeasure))
|
31
|
+
assert(Units.length == Units.distance)
|
32
|
+
assert(Units.units_measures.index('length') != nil)
|
33
|
+
assert(Units.units_measures.index('time') != nil)
|
34
|
+
assert(Units.units_measures.index('distance') != nil)
|
35
|
+
|
36
|
+
assert(Units.delete(:time).kind_of?(UnitsMeasure))
|
37
|
+
assert(Units.delete('time') == nil)
|
38
|
+
assert(Units.delete('length').kind_of?(UnitsMeasure))
|
39
|
+
assert(Units.delete(:length) == nil)
|
40
|
+
assert(Units.size == 1)
|
41
|
+
assert_raise(UnitsException) { Units.time }
|
42
|
+
assert_raise(UnitsException) { Units.length }
|
43
|
+
assert(Units.units_measures.index('length') == nil)
|
44
|
+
assert(Units.units_measures.index('time') == nil)
|
45
|
+
assert(Units.units_measures.index('distance') != nil)
|
46
|
+
|
47
|
+
assert(Units.clear == Units)
|
48
|
+
assert(Units.size == 0)
|
49
|
+
assert_raise(UnitsException) { Units.time }
|
50
|
+
assert_raise(UnitsException) { Units.length }
|
51
|
+
assert_raise(UnitsException) { Units.distance }
|
52
|
+
assert(Units.units_measures.index('length') == nil)
|
53
|
+
assert(Units.units_measures.index('time') == nil)
|
54
|
+
assert(Units.units_measures.index('distance') == nil)
|
55
|
+
assert(Units.units_measures == [])
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_measure_derive < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_measure_derive
|
11
|
+
|
12
|
+
assert((length = Units.create :length) == Units.length)
|
13
|
+
assert(length.derived == nil)
|
14
|
+
|
15
|
+
assert((distance = Units.derive 'distance', length) == Units.length)
|
16
|
+
assert(distance.derived == nil)
|
17
|
+
|
18
|
+
assert((area = Units.derive 'area', length**2) == Units.area)
|
19
|
+
assert(area.derived.size == 1)
|
20
|
+
assert(area.derived[Units.length] == 2)
|
21
|
+
|
22
|
+
assert((volume = Units.derive 'volume', area*length) == Units.volume)
|
23
|
+
assert(volume.derived.size == 1)
|
24
|
+
assert(volume.derived[Units.length] == 3)
|
25
|
+
|
26
|
+
assert((time = Units.create 'time') == Units.time)
|
27
|
+
assert(time.derived == nil)
|
28
|
+
|
29
|
+
assert((velocity = Units.derive :velocity, distance/time) ==
|
30
|
+
Units.velocity)
|
31
|
+
assert(velocity.derived.size == 2)
|
32
|
+
assert(velocity.derived[Units.distance] == 1)
|
33
|
+
assert(velocity.derived[Units.time] == -1)
|
34
|
+
|
35
|
+
assert((acceleration = Units.derive :acceleration, distance/time**2) ==
|
36
|
+
Units.acceleration)
|
37
|
+
assert(acceleration.derived.size == 2)
|
38
|
+
assert(acceleration.derived[Units.distance] == 1)
|
39
|
+
assert(acceleration.derived[Units.time] == -2)
|
40
|
+
|
41
|
+
assert(acceleration == Units.derive(:acceleration2, distance/time**2))
|
42
|
+
assert(Units.acceleration2 == Units.acceleration)
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_system_create < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_system_create
|
11
|
+
|
12
|
+
Units.create :length do |m|
|
13
|
+
m.system 'english'
|
14
|
+
m.system 'metric'
|
15
|
+
end
|
16
|
+
|
17
|
+
english = Units.length.english
|
18
|
+
metric = Units.length.metric
|
19
|
+
assert(english.kind_of?(UnitsSystem))
|
20
|
+
assert(metric.kind_of?(UnitsSystem))
|
21
|
+
assert(english.name == 'english')
|
22
|
+
assert(metric.name == 'metric')
|
23
|
+
assert(english.units_measure == Units.length)
|
24
|
+
assert(metric.units_measure == Units.length)
|
25
|
+
assert(Units.length.keys.index('english') != nil)
|
26
|
+
assert(Units.length.keys.index('metric') != nil)
|
27
|
+
assert(Units.length.keys.index('foo') == nil)
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_unit_ambiguity < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_unit_ambiguity
|
11
|
+
|
12
|
+
Units.create :volume do |m|
|
13
|
+
m.system :english do |s|
|
14
|
+
s.unit :name => :ounce, :abbrev => :oz
|
15
|
+
end
|
16
|
+
end
|
17
|
+
volume_ounce = Units.volume.english.ounce
|
18
|
+
Units.create :mass do |m|
|
19
|
+
m.system :english do |s|
|
20
|
+
s.unit :name => :ounce, :abbrev => :oz
|
21
|
+
end
|
22
|
+
end
|
23
|
+
mass_ounce = Units.mass.english.ounce
|
24
|
+
|
25
|
+
assert(volume_ounce.equals.unit == {volume_ounce => 1})
|
26
|
+
assert(mass_ounce.equals.unit == {mass_ounce => 1})
|
27
|
+
|
28
|
+
Units.create :volume do |m|
|
29
|
+
m.system :english do |s|
|
30
|
+
s.unit :name => :cup, :abbrev => :c, :equals => 16.ounces
|
31
|
+
end
|
32
|
+
end
|
33
|
+
cup = Units.volume.english.cup
|
34
|
+
assert(cup.equals.unit == {volume_ounce => 1})
|
35
|
+
|
36
|
+
Units.create :mass do |m|
|
37
|
+
m.system :english do |s|
|
38
|
+
s.unit :name => :pound, :abbrev => :lb, :equals => 16.ounces
|
39
|
+
end
|
40
|
+
end
|
41
|
+
pound = Units.mass.english.pound
|
42
|
+
assert(pound.equals.unit == {mass_ounce => 1})
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
require 'units'
|
4
|
+
|
5
|
+
class TC_unit_arithmetic < Test::Unit::TestCase
|
6
|
+
|
7
|
+
understands UnitsTest
|
8
|
+
|
9
|
+
def test_unit_arithmetic
|
10
|
+
|
11
|
+
Units.create :length do |m|
|
12
|
+
m.system :english do |s|
|
13
|
+
s.unit :name => :inch,
|
14
|
+
:plural => :inches,
|
15
|
+
:abbrev => :in
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
inch = Units.length.english.inch.equals
|
20
|
+
assert(inch.to_s == "1 inch")
|
21
|
+
assert(inch == 1.inch)
|
22
|
+
|
23
|
+
yard = inch*36
|
24
|
+
assert(yard.to_s == "36 inches")
|
25
|
+
assert(yard == 36.in)
|
26
|
+
|
27
|
+
foot = yard/3
|
28
|
+
assert(foot.to_s == "12 inches")
|
29
|
+
assert(foot == 12.in)
|
30
|
+
|
31
|
+
half_foot = foot-6
|
32
|
+
assert(half_foot.to_s == "6 inches")
|
33
|
+
assert(half_foot == 6.in)
|
34
|
+
|
35
|
+
yard_and_a_half = yard+18
|
36
|
+
assert(yard_and_a_half.to_s == "54 inches")
|
37
|
+
assert(yard_and_a_half == 54.in)
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_unit_create < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_unit_create
|
11
|
+
|
12
|
+
Units.create :length do |m|
|
13
|
+
m.system 'english' do |s|
|
14
|
+
s.unit :name => 'inch'
|
15
|
+
s.unit :name => 'foot'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
inch = Units.length.english.inch
|
20
|
+
foot = Units.length.english.foot
|
21
|
+
assert(inch.kind_of?(UnitsUnit))
|
22
|
+
assert(foot.kind_of?(UnitsUnit))
|
23
|
+
assert(inch.name == 'inch')
|
24
|
+
assert(foot.name == 'foot')
|
25
|
+
assert(inch.units_system == Units.length.english)
|
26
|
+
assert(foot.units_system == Units.length.english)
|
27
|
+
assert(inch.units_system.units_measure == Units.length)
|
28
|
+
assert(foot.units_system.units_measure == Units.length)
|
29
|
+
assert(Units.length.english.keys.index('inch') != nil)
|
30
|
+
assert(Units.length.english.keys.index('foot') != nil)
|
31
|
+
assert(Units.length.english.keys.index('foo') == nil)
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_unit_derive < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_unit_derived
|
11
|
+
|
12
|
+
length =
|
13
|
+
Units.create :length do |m|
|
14
|
+
m.system :metric do |s|
|
15
|
+
s.unit :name => :meter,
|
16
|
+
:abbrev => :m,
|
17
|
+
:greek => :ten
|
18
|
+
end
|
19
|
+
m.system :english do |s|
|
20
|
+
s.unit :name => :inch,
|
21
|
+
:plural => :inches,
|
22
|
+
:abbrev => :in
|
23
|
+
s.unit :name => :foot,
|
24
|
+
:plural => :feet,
|
25
|
+
:abbrev => :ft,
|
26
|
+
:equals => 12.inches
|
27
|
+
s.unit :name => :yard,
|
28
|
+
:abbrev => :yd,
|
29
|
+
:equals => 3.feet
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
centimeter = length.metric.centimeter
|
34
|
+
inch = length.english.inch
|
35
|
+
foot = length.english.foot
|
36
|
+
yard = length.english.yard
|
37
|
+
|
38
|
+
volume =
|
39
|
+
Units.derive :volume, length**3 do |m|
|
40
|
+
m.system :metric do |s|
|
41
|
+
s.unit :name => :milliliter,
|
42
|
+
:abbrev => :mL,
|
43
|
+
:equals => 1.cm**3
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
assert(volume.derived.size == 1)
|
48
|
+
assert(volume.derived[length] == 3)
|
49
|
+
milliliter = volume.metric.milliliter
|
50
|
+
|
51
|
+
mass =
|
52
|
+
Units.create :mass do |m|
|
53
|
+
m.system :metric do |s|
|
54
|
+
s.unit :name => :gram,
|
55
|
+
:abbrev => :g,
|
56
|
+
:greek => :ten
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
gram = mass.metric.gram
|
61
|
+
|
62
|
+
density =
|
63
|
+
Units.derive :density, mass/volume do |m|
|
64
|
+
m.system :base do |s|
|
65
|
+
s.unit :name => :foo,
|
66
|
+
:equals => 1.g/1.mL
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
assert(density.derived.size == 2)
|
71
|
+
assert(density.derived[length] == -3)
|
72
|
+
assert(density.derived[mass] == 1)
|
73
|
+
|
74
|
+
area =
|
75
|
+
Units.derive :area, length**2 do |m|
|
76
|
+
m.system :english do |s|
|
77
|
+
s.unit :name => :acre,
|
78
|
+
:equals => 220.yards * 66.feet
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
assert(area.derived.size == 1)
|
83
|
+
assert(area.derived[length] == 2)
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_unit_equality < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_unit_equality
|
11
|
+
|
12
|
+
Units.create :length do |m|
|
13
|
+
m.system :english do |s|
|
14
|
+
s.unit :name => :inch,
|
15
|
+
:plural => :inches,
|
16
|
+
:abbrev => :in
|
17
|
+
s.unit :name => :foot,
|
18
|
+
:plural => :feet,
|
19
|
+
:abbrev => [ :ft ],
|
20
|
+
:equals => 12.inches
|
21
|
+
s.unit :name => :yard,
|
22
|
+
:abbrevs => :yd,
|
23
|
+
:equals => 3.feet
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
inch = Units.length.english.inch
|
28
|
+
foot = Units.length.english.foot
|
29
|
+
yard = Units.length.english.yard
|
30
|
+
|
31
|
+
assert(inch.equals.numeric == 1)
|
32
|
+
assert(inch.equals.unit == {inch => 1})
|
33
|
+
assert(foot.equals.numeric == 12)
|
34
|
+
assert(foot.equals.unit == {inch => 1})
|
35
|
+
assert(yard.equals.numeric == 36)
|
36
|
+
assert(yard.equals.unit == {inch => 1})
|
37
|
+
|
38
|
+
assert(inch.equals.to_s == "1 inch")
|
39
|
+
assert(foot.equals.to_s == "12 inches")
|
40
|
+
assert(yard.equals.to_s == "36 inches")
|
41
|
+
|
42
|
+
a = 12.feet
|
43
|
+
assert(a.numeric == 12)
|
44
|
+
assert(a.unit == {foot => 1})
|
45
|
+
assert(a.to_s == "12 feet")
|
46
|
+
|
47
|
+
b = (2.0.ft^2)**0.5
|
48
|
+
c = 1.4142135623.ft
|
49
|
+
assert(b != c)
|
50
|
+
assert(b =~ c)
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
class TC_unit_forward_reference < Test::Unit::TestCase
|
7
|
+
|
8
|
+
understands UnitsTest
|
9
|
+
|
10
|
+
def test_unit_forward_reference
|
11
|
+
|
12
|
+
Units.create :length do |m|
|
13
|
+
m.system :english do |s|
|
14
|
+
s.unit :name => :yard,
|
15
|
+
:abbrevs => :yd,
|
16
|
+
:equals => 3.feet
|
17
|
+
s.unit :name => :foot,
|
18
|
+
:plural => :feet,
|
19
|
+
:abbrev => [ :ft ],
|
20
|
+
:equals => 12.inches
|
21
|
+
s.unit :name => :inch,
|
22
|
+
:plural => :inches,
|
23
|
+
:abbrev => :in
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
inch = Units.length.english.inch
|
28
|
+
foot = Units.length.english.foot
|
29
|
+
yard = Units.length.english.yard
|
30
|
+
|
31
|
+
assert(inch.equals.numeric == 1)
|
32
|
+
assert(inch.equals.unit == {inch => 1})
|
33
|
+
assert(foot.equals.numeric == 12)
|
34
|
+
assert(foot.equals.unit == {inch => 1})
|
35
|
+
assert(yard.equals.numeric == 36)
|
36
|
+
assert(yard.equals.unit == {inch => 1})
|
37
|
+
|
38
|
+
assert(inch.equals.to_s == "1 inch")
|
39
|
+
assert(foot.equals.to_s == "12 inches")
|
40
|
+
assert(yard.equals.to_s == "36 inches")
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/framework'
|
3
|
+
|
4
|
+
require 'units'
|
5
|
+
|
6
|
+
# note that different rubys apparently have different minimum exponent digits
|
7
|
+
|
8
|
+
class TC_unit_greek < Test::Unit::TestCase
|
9
|
+
|
10
|
+
understands UnitsTest
|
11
|
+
|
12
|
+
def test_unit_greek
|
13
|
+
|
14
|
+
Units.create :length do |m|
|
15
|
+
m.system :metric do |s|
|
16
|
+
s.unit :name => :meter,
|
17
|
+
:abbrev => :m,
|
18
|
+
:greek => :ten
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
yoctometer = Units.length.metric.yoctometer.equals
|
23
|
+
assert((yoctometer.to_s == "1.0e-24 meters")||
|
24
|
+
(yoctometer.to_s == "1.0e-024 meters"))
|
25
|
+
assert(yoctometer == 0.000000000000000000000001.meters)
|
26
|
+
|
27
|
+
zeptometer = Units.length.metric.zeptometer.equals
|
28
|
+
assert((zeptometer.to_s == "1.0e-21 meters")||
|
29
|
+
(zeptometer.to_s == "1.0e-021 meters"))
|
30
|
+
assert(zeptometer == 0.000000000000000000001.meters)
|
31
|
+
|
32
|
+
attometer = Units.length.metric.attometer.equals
|
33
|
+
assert((attometer.to_s == "1.0e-18 meters")||
|
34
|
+
(attometer.to_s == "1.0e-018 meters"))
|
35
|
+
assert(attometer == 0.000000000000000001.meters)
|
36
|
+
|
37
|
+
femtometer = Units.length.metric.femtometer.equals
|
38
|
+
assert((femtometer.to_s == "1.0e-15 meters")||
|
39
|
+
(femtometer.to_s == "1.0e-015 meters"))
|
40
|
+
assert(femtometer == 0.000000000000001.meters)
|
41
|
+
|
42
|
+
picometer = Units.length.metric.picometer.equals
|
43
|
+
assert((picometer.to_s == "1.0e-12 meters")||
|
44
|
+
(picometer.to_s == "1.0e-012 meters"))
|
45
|
+
assert(picometer == 0.000000000001.meters)
|
46
|
+
|
47
|
+
nanometer = Units.length.metric.nanometer.equals
|
48
|
+
assert((nanometer.to_s == "1.0e-09 meters")||
|
49
|
+
(nanometer.to_s == "1.0e-009 meters"))
|
50
|
+
assert(nanometer == 0.000000001.meters)
|
51
|
+
|
52
|
+
micrometer = Units.length.metric.micrometer.equals
|
53
|
+
assert((micrometer.to_s == "1.0e-06 meters")||
|
54
|
+
(micrometer.to_s == "1.0e-006 meters"))
|
55
|
+
assert(micrometer == 0.000001.meters)
|
56
|
+
|
57
|
+
millimeter = Units.length.metric.millimeter.equals
|
58
|
+
assert(millimeter.to_s == "0.001 meters")
|
59
|
+
assert(millimeter == 0.001.meters)
|
60
|
+
|
61
|
+
centimeter = Units.length.metric.centimeter.equals
|
62
|
+
assert(centimeter.to_s == "0.01 meters")
|
63
|
+
assert(centimeter == 0.01.meters)
|
64
|
+
|
65
|
+
decimeter = Units.length.metric.decimeter.equals
|
66
|
+
assert(decimeter.to_s == "0.1 meters")
|
67
|
+
assert(decimeter == 0.1.meters)
|
68
|
+
|
69
|
+
meter = Units.length.metric.meter.equals
|
70
|
+
assert(meter.to_s == "1 meter")
|
71
|
+
assert(meter == 1.meter)
|
72
|
+
|
73
|
+
decameter = Units.length.metric.decameter.equals
|
74
|
+
assert(decameter.to_s == "10.0 meters")
|
75
|
+
assert(decameter == 10.meters)
|
76
|
+
|
77
|
+
hectometer = Units.length.metric.hectometer.equals
|
78
|
+
assert(hectometer.to_s == "100.0 meters")
|
79
|
+
assert(hectometer == 100.meters)
|
80
|
+
|
81
|
+
kilometer = Units.length.metric.kilometer.equals
|
82
|
+
assert(kilometer.to_s == "1000.0 meters")
|
83
|
+
assert(kilometer == 1000.meters)
|
84
|
+
|
85
|
+
megameter = Units.length.metric.megameter.equals
|
86
|
+
assert(megameter.to_s == "1000000.0 meters")
|
87
|
+
assert(megameter == 1000000.meters)
|
88
|
+
|
89
|
+
gigameter = Units.length.metric.gigameter.equals
|
90
|
+
assert(gigameter.to_s == "1000000000.0 meters")
|
91
|
+
assert(gigameter == 1000000000.meters)
|
92
|
+
|
93
|
+
terameter = Units.length.metric.terameter.equals
|
94
|
+
assert(terameter.to_s == "1000000000000.0 meters")
|
95
|
+
assert(terameter == 1000000000000.meters)
|
96
|
+
|
97
|
+
petameter = Units.length.metric.petameter.equals
|
98
|
+
assert((petameter.to_s == "1.0e+15 meters")||
|
99
|
+
(petameter.to_s == "1.0e+015 meters"))
|
100
|
+
assert(petameter == 1000000000000000.meters)
|
101
|
+
|
102
|
+
exameter = Units.length.metric.exameter.equals
|
103
|
+
assert((exameter.to_s == "1.0e+18 meters")||
|
104
|
+
(exameter.to_s == "1.0e+018 meters"))
|
105
|
+
assert(exameter == 1000000000000000000.meters)
|
106
|
+
|
107
|
+
zettameter = Units.length.metric.zettameter.equals
|
108
|
+
assert((zettameter.to_s == "1.0e+21 meters")||
|
109
|
+
(zettameter.to_s == "1.0e+021 meters"))
|
110
|
+
assert(zettameter == 1000000000000000000000.meters)
|
111
|
+
|
112
|
+
yottameter = Units.length.metric.yottameter.equals
|
113
|
+
assert((yottameter.to_s == "1.0e+24 meters")||
|
114
|
+
(yottameter.to_s == "1.0e+024 meters"))
|
115
|
+
assert(yottameter == 1000000000000000000000000.meters)
|
116
|
+
|
117
|
+
Units.create :storage do |m|
|
118
|
+
m.system :base do |s|
|
119
|
+
s.unit :name => :byte,
|
120
|
+
:abbrev => :b,
|
121
|
+
:greek => :two
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
byte = Units.storage.base.byte.equals
|
126
|
+
assert(byte.to_s == "1 byte")
|
127
|
+
assert(byte == 1.byte)
|
128
|
+
|
129
|
+
kilobyte = Units.storage.base.kilobyte.equals
|
130
|
+
assert(kilobyte.to_s == "1024 bytes")
|
131
|
+
assert(kilobyte == 1024.bytes)
|
132
|
+
|
133
|
+
megabyte = Units.storage.base.megabyte.equals
|
134
|
+
assert(megabyte.to_s == "1048576 bytes")
|
135
|
+
assert(megabyte == 1048576.bytes)
|
136
|
+
|
137
|
+
gigabyte = Units.storage.base.gigabyte.equals
|
138
|
+
assert(gigabyte.to_s == "1073741824 bytes")
|
139
|
+
assert(gigabyte == 1073741824.bytes)
|
140
|
+
|
141
|
+
terabyte = Units.storage.base.terabyte.equals
|
142
|
+
assert(terabyte.to_s == "1099511627776 bytes")
|
143
|
+
assert(terabyte == 1099511627776.bytes)
|
144
|
+
|
145
|
+
petabyte = Units.storage.base.petabyte.equals
|
146
|
+
assert(petabyte.to_s == "1125899906842624 bytes")
|
147
|
+
assert(petabyte == 1125899906842624.bytes)
|
148
|
+
|
149
|
+
exabyte = Units.storage.base.exabyte.equals
|
150
|
+
assert(exabyte.to_s == "1152921504606846976 bytes")
|
151
|
+
assert(exabyte == 1152921504606846976.byte)
|
152
|
+
|
153
|
+
zettabyte = Units.storage.base.zettabyte.equals
|
154
|
+
assert(zettabyte.to_s == "1180591620717411303424 bytes")
|
155
|
+
assert(zettabyte == 1180591620717411303424.bytes)
|
156
|
+
|
157
|
+
yottabyte = Units.storage.base.yottabyte.equals
|
158
|
+
assert(yottabyte.to_s == "1208925819614629174706176 bytes")
|
159
|
+
assert(yottabyte == 1208925819614629174706176.bytes)
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|