Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated tests to do both rational and non-rational, since

fixing a rational bug was so huge.
  • Loading branch information...
commit 3723f104b9070c1b5160261007c02e1aaf66c0b0 1 parent fd597ef
@bhuga authored
View
156 spec/dimension-real.spec
@@ -0,0 +1,156 @@
+$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
+
+require 'quantity/dimension'
+
+describe Quantity::Dimension do
+
+ # We use these for testing dimension. the systems are tested in a separate spec.
+ # we'll be using these for the rest of the specs
+ before(:each) do
+ @length = Quantity::Dimension.for(:length)
+ @mass = Quantity::Dimension.for(:mass)
+ @area = Quantity::Dimension.for(:area)
+ @accel = Quantity::Dimension.for(:acceleration)
+ @force = Quantity::Dimension.for(:force)
+ @time = Quantity::Dimension.for(:time)
+ end
+
+ it "should be creatable and internd with the DSL" do
+ class Length < Quantity::Dimension ; end
+ class Area < Quantity::Dimension ; end
+ class Acceleration < Quantity::Dimension ; end
+ Length.add_dimension :length, :width
+ Area.add_dimension :'length^2'
+ Acceleration.add_dimension :'length/time^2'
+ Quantity::Dimension.add_dimension :mass
+ Quantity::Dimension.add_dimension :time
+
+ length = Quantity::Dimension.for(:length)
+ mass = Quantity::Dimension.for(:mass)
+ area = Quantity::Dimension.for(:area)
+ accel = Quantity::Dimension.for(:acceleration)
+ time = Quantity::Dimension.for(:time)
+
+ ml = Quantity::Dimension.add_dimension mass * length
+ t2 = Quantity::Dimension.add_dimension time**2
+ Quantity::Dimension.add_dimension ml / t2, :force
+ #Quantity::Dimension.add_dimension :'length*mass/time^2', :force
+ force = Quantity::Dimension.for(:'length*mass/time^2')
+ force2 = Quantity::Dimension.for(:'mass*length/time^2')
+
+ length.to_s.should == "length"
+ length.class.should == Length
+ length.name.should == :length
+ area.to_s.should == "area"
+ area.name.should == :area
+ area.class.should == Area
+ area.string_form.should == 'length^2'
+ accel.class.should == Acceleration
+ accel.to_s.should == "acceleration"
+ accel.name.should == :acceleration
+ mass.to_s.should == "mass"
+ mass.name.should == :mass
+ mass.class.should == Quantity::Dimension
+ Quantity::Dimension.for(:width).should equal(length)
+ force.name.should == :force # note normalized reordering
+ force.to_s.should == "force" # note normalized reordering
+ force.string_form.should == "length*mass/time^2" # note normalized reordering
+ force.class.should == Quantity::Dimension
+ force2.should equal(force)
+ end
+
+ it "should have a name" do
+ @length.name.should == :length
+ @force.name.should == :force
+ end
+
+ it "should track numerators and denominators" do
+ @length.numerators.first.dimension.should == :length
+ @length.numerators.first.power.should == 1
+ @force.numerators.first.dimension.should == :length
+ @force.numerators.first.power== 1
+ @force.numerators[1].dimension.should == :mass
+ @force.numerators[1].power== 1
+ @force.denominators.first.dimension.should == :time
+ @force.denominators.first.power.should == 2
+ end
+
+ it "should provide a vaguely parsable string format" do
+ component = Quantity::Dimension::DimensionComponent.new(:length,3)
+ component2 = Quantity::Dimension::DimensionComponent.new(:length,2)
+ component3 = Quantity::Dimension::DimensionComponent.new(:time,2)
+ component4 = Quantity::Dimension::DimensionComponent.new(:length,5)
+ Quantity::Dimension.string_form([component],[]).should=='length^3'
+ Quantity::Dimension.string_form([component,component2],[]).should=='length^3*length^2'
+ Quantity::Dimension.string_form([component,component2],[component3]).should=='length^3*length^2/time^2'
+ Quantity::Dimension.parse_string_form('length^3*length^2/time^2').inspect.should == [[component4],[component3]].inspect
+ end
+
+ it "should multiply dimensions" do
+ volume = @area * @length
+ volume.numerators.first.dimension.should == :length
+ volume.numerators.first.power.should == 3
+ area2 = @length * @length
+ area2.numerators.first.dimension.should == :length
+ area2.numerators.first.power.should == 2
+ area2.should equal(@area)
+ force_top = @mass * @length
+ force_top.numerators.first.dimension.should == :length
+ force_top.numerators.first.power.should == 1
+ force_top.numerators.length.should == 2
+ force_top.numerators[1].dimension.should == :mass
+ force_top.name.should == :"length*mass"
+ end
+
+ it "should divide dimensions" do
+ speed = @length / @time
+ speed.name.should == :'length/time'
+ speed.numerators.first.dimension.should == :length
+ speed.numerators.first.power.should == 1
+ speed.denominators.first.power.should == 1
+ speed.denominators.first.dimension.should == :time
+ speed2 = @length / @time
+ speed.should equal(speed2)
+ accel = speed / @time
+ accel.numerators.first.dimension.should == :length
+ accel.numerators.first.power.should == 1
+ accel.denominators.first.power.should == 2
+ accel.denominators.first.dimension.should == :time
+ accel.should equal(@accel)
+ force = @accel * @mass
+ force.numerators.first.dimension.should == :length
+ force.numerators.first.power.should == 1
+ force.numerators[1].dimension.should == :mass
+ force.numerators[1].power.should == 1
+ force.denominators.first.power.should == 2
+ force.denominators.first.dimension.should == :time
+ @force.should equal(force)
+ accel = @force / @mass
+ accel.numerators.first.dimension.should == :length
+ accel.numerators.first.power.should == 1
+ accel.denominators.first.power.should == 2
+ accel.denominators.first.dimension.should == :time
+ @accel.should equal(accel)
+ area = @length * @length
+ area.numerators.first.dimension.should == :length
+ area.numerators.first.power.should == 2
+ volume = @area * @length
+ volume.numerators.first.dimension.should == :length
+ volume.numerators.first.power.should == 3
+ area = volume / @length
+ area.numerators.first.dimension.should == :length
+ area.numerators.first.power.should == 2
+ area.should equal(@area)
+ end
+
+ it "should allow exponentiation" do
+ (@length**3).should == @length * @length * @length
+ end
+
+ # this is useful for testing
+ it "should allow resetting the world" do
+ Quantity::Dimension.__reset!
+ x = Quantity::Dimension.for(:length)
+ x.should == nil
+ end
+end
View
156 spec/dimension.spec
@@ -1,156 +1,6 @@
-$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
-require 'quantity/dimension'
+load 'dimension-real.spec'
-describe Quantity::Dimension do
-
- # We use these for testing dimension. the systems are tested in a separate spec.
- # we'll be using these for the rest of the specs
- before(:each) do
- @length = Quantity::Dimension.for(:length)
- @mass = Quantity::Dimension.for(:mass)
- @area = Quantity::Dimension.for(:area)
- @accel = Quantity::Dimension.for(:acceleration)
- @force = Quantity::Dimension.for(:force)
- @time = Quantity::Dimension.for(:time)
- end
+require 'rational'
- it "should be creatable and internd with the DSL" do
- class Length < Quantity::Dimension ; end
- class Area < Quantity::Dimension ; end
- class Acceleration < Quantity::Dimension ; end
- Length.add_dimension :length, :width
- Area.add_dimension :'length^2'
- Acceleration.add_dimension :'length/time^2'
- Quantity::Dimension.add_dimension :mass
- Quantity::Dimension.add_dimension :time
-
- length = Quantity::Dimension.for(:length)
- mass = Quantity::Dimension.for(:mass)
- area = Quantity::Dimension.for(:area)
- accel = Quantity::Dimension.for(:acceleration)
- time = Quantity::Dimension.for(:time)
-
- ml = Quantity::Dimension.add_dimension mass * length
- t2 = Quantity::Dimension.add_dimension time**2
- Quantity::Dimension.add_dimension ml / t2, :force
- #Quantity::Dimension.add_dimension :'length*mass/time^2', :force
- force = Quantity::Dimension.for(:'length*mass/time^2')
- force2 = Quantity::Dimension.for(:'mass*length/time^2')
-
- length.to_s.should == "length"
- length.class.should == Length
- length.name.should == :length
- area.to_s.should == "area"
- area.name.should == :area
- area.class.should == Quantity::Dimension::Area
- area.string_form.should == 'length^2'
- accel.class.should == Acceleration
- accel.to_s.should == "acceleration"
- accel.name.should == :acceleration
- mass.to_s.should == "mass"
- mass.name.should == :mass
- mass.class.should == Quantity::Dimension
- Quantity::Dimension.for(:width).should equal(length)
- force.name.should == :force # note normalized reordering
- force.to_s.should == "force" # note normalized reordering
- force.string_form.should == "length*mass/time^2" # note normalized reordering
- force.class.should == Quantity::Dimension
- force2.should equal(force)
- end
-
- it "should have a name" do
- @length.name.should == :length
- @force.name.should == :force
- end
-
- it "should track numerators and denominators" do
- @length.numerators.first.dimension.should == :length
- @length.numerators.first.power.should == 1
- @force.numerators.first.dimension.should == :length
- @force.numerators.first.power== 1
- @force.numerators[1].dimension.should == :mass
- @force.numerators[1].power== 1
- @force.denominators.first.dimension.should == :time
- @force.denominators.first.power.should == 2
- end
-
- it "should provide a vaguely parsable string format" do
- component = Quantity::Dimension::DimensionComponent.new(:length,3)
- component2 = Quantity::Dimension::DimensionComponent.new(:length,2)
- component3 = Quantity::Dimension::DimensionComponent.new(:time,2)
- component4 = Quantity::Dimension::DimensionComponent.new(:length,5)
- Quantity::Dimension.string_form([component],[]).should=='length^3'
- Quantity::Dimension.string_form([component,component2],[]).should=='length^3*length^2'
- Quantity::Dimension.string_form([component,component2],[component3]).should=='length^3*length^2/time^2'
- Quantity::Dimension.parse_string_form('length^3*length^2/time^2').inspect.should == [[component4],[component3]].inspect
- end
-
- it "should multiply dimensions" do
- volume = @area * @length
- volume.numerators.first.dimension.should == :length
- volume.numerators.first.power.should == 3
- area2 = @length * @length
- area2.numerators.first.dimension.should == :length
- area2.numerators.first.power.should == 2
- area2.should equal(@area)
- force_top = @mass * @length
- force_top.numerators.first.dimension.should == :length
- force_top.numerators.first.power.should == 1
- force_top.numerators.length.should == 2
- force_top.numerators[1].dimension.should == :mass
- force_top.name.should == :"length*mass"
- end
-
- it "should divide dimensions" do
- speed = @length / @time
- speed.name.should == :'length/time'
- speed.numerators.first.dimension.should == :length
- speed.numerators.first.power.should == 1
- speed.denominators.first.power.should == 1
- speed.denominators.first.dimension.should == :time
- speed2 = @length / @time
- speed.should equal(speed2)
- accel = speed / @time
- accel.numerators.first.dimension.should == :length
- accel.numerators.first.power.should == 1
- accel.denominators.first.power.should == 2
- accel.denominators.first.dimension.should == :time
- accel.should equal(@accel)
- force = @accel * @mass
- force.numerators.first.dimension.should == :length
- force.numerators.first.power.should == 1
- force.numerators[1].dimension.should == :mass
- force.numerators[1].power.should == 1
- force.denominators.first.power.should == 2
- force.denominators.first.dimension.should == :time
- @force.should equal(force)
- accel = @force / @mass
- accel.numerators.first.dimension.should == :length
- accel.numerators.first.power.should == 1
- accel.denominators.first.power.should == 2
- accel.denominators.first.dimension.should == :time
- @accel.should equal(accel)
- area = @length * @length
- area.numerators.first.dimension.should == :length
- area.numerators.first.power.should == 2
- volume = @area * @length
- volume.numerators.first.dimension.should == :length
- volume.numerators.first.power.should == 3
- area = volume / @length
- area.numerators.first.dimension.should == :length
- area.numerators.first.power.should == 2
- area.should equal(@area)
- end
-
- it "should allow exponentiation" do
- (@length**3).should == @length * @length * @length
- end
-
- # this is useful for testing
- it "should allow resetting the world" do
- Quantity::Dimension.__reset!
- x = Quantity::Dimension.for(:length)
- x.should == nil
- end
-end
+load 'dimension-real.spec'
View
164 spec/quantity-real.spec
@@ -0,0 +1,164 @@
+$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
+
+require 'quantity'
+require 'quantity/systems/si'
+require 'quantity/systems/us'
+require 'rational'
+
+describe Quantity do
+
+ it "should be instantiated from numbers" do
+ 1.meter.should == 1
+ 2.5.feet.should == 2.5
+ end
+
+ it "should work with alias names" do
+ 2.meters.should == 2
+ end
+
+ it "should know what it measures" do
+ 2.meters.unit.dimension.name.should == :length
+ 2.meters.measures.name.should == :length
+ end
+
+ it "should know its units" do
+ 2.meters.unit.name.should == :meter
+ 2.meters.units.should == :meter
+ end
+
+ it "should convert from one type to another" do
+ 1.meter.in_centimeters.should == 100
+ 50.centimeters.to_meters.should == 0.5
+ 10.meters.convert(:feet).should be_close 32.808399.feet, 10**-6
+ 1.mm.to_m.should == 0.001
+ end
+
+ it "should convert from one type to another when not using the reference" do
+ 1.kilometer.in_centimeters.should == 100_000
+ end
+
+ it "should fail to convert from disparate measurement types" do
+ lambda { 1.picogram.in_meters }.should raise_error ArgumentError
+ end
+
+ it "should enforce equality correctly" do
+ 12.meter.should == 12.meters
+ 1.meter.should == 100.centimeter
+ 1.meter.should_not == 1.centimeter
+ 1.picograms.should_not == 1.centimeter
+ 1.meter.eql?(100.centimeters).should == false
+ 1.meter.eql?(1.meter).should == true
+ end
+
+ it "should add items of the same type" do
+ 12.meters.should == 1200.centimeters
+ (12.meters + 12).should == 24.meters
+ (12.meters + 15.centimeters).should == 1215.centimeters
+ (2 + 5.meters).should == 7.meters
+ (2.5 + 5.meters).should == 7.5.meters
+ end
+
+ it "should add and subtract with normal numbers" do
+ 3.meters.should == 1.meter + 2
+ 4.meters.should == 2 + 2.meter
+ 6.4.meters.should == 4.4 + 2.meter
+ end
+
+ it "should fail to add items of different types" do
+ lambda { 12.meters + 24.picograms }.should raise_error ArgumentError
+ end
+
+ it "should subtract items of the same type" do
+ (12.meters - 3).should == 9.meters
+ (12.meters - 3650.centimeters).should == -2450.centimeters
+ lambda { (12.meters - 3650.picograms)}.should raise_error ArgumentError
+ (15 - 5.meters).should == 10.meters
+ (15.5 - 5.meters).should == 10.5.meters
+ end
+
+ it "should support basic math operations" do
+ ((-(5.seconds)).abs).should == 5.seconds
+ (-5).seconds.abs.should == 5.seconds
+ (-(35.meters)).should be_close -(114.829396.feet), 10**-5
+ (35.meters % 6).should == 5.meters
+ (35.meters % 6.feet).should be_close 0.2528.meters, 10**-5
+ 4.kilograms.modulo(15.grams).should == 10.grams
+ 15.2.meters.truncate.should == 15.meters
+ 15.6.meters.round.should == 16.meters
+ 15.2.meters.ceil.should == 16.meters
+ (-5.5.meters).floor.should == -6.meters
+ 11.meters.divmod(3).should == [3.meter,2.meter]
+ 11.meters.divmod(3.meters).should == [3.meter,2.meter]
+ 11.meters.divmod(-3).should == [-4.meter,-1.meter]
+ 11.meters.divmod(-3.meters).should == [-4.meter,-1.meter]
+ 11.meters.divmod(3.5).should == [3.meter,0.5.meter]
+ 11.meters.divmod(3.5.meters).should == [3.meter,0.5.meter]
+ (-11.meters).divmod(3.5).should == [-4.meter,3.0.meter]
+ (-11.meters).divmod(3.5.meters).should == [-4.meter,3.0.meter]
+ 11.5.meters.divmod(3.5).should == [3.meter,1.0.meter]
+ 11.5.meters.divmod(3.5.meters).should == [3.meter,1.0.meter]
+ +4.kilograms.should == 4.kilograms
+ 0.kilograms.zero?.should == true
+ end
+
+ it "should multiply any items" do
+ (2.meters * 5.meters).should == 10
+ # on second thought, this works :D
+ #lambda { (1.meters * 1.foot).unit.name }.should raise_error ArgumentError
+ (2.meters * 2.meters).unit.name.should == "meter^2"
+ (2.meters * 2.meters).unit.dimension.string_form.should == "length^2"
+ (1.meter * Quantity.new(1,'m^2')).unit.dimension.string_form.should == "length^3"
+ (1.meter * Quantity.new(1,'m^2')).units.should == "meter^3"
+ (3.meter * Quantity.new(1,'m^2')).units.should == "meter^3"
+ (3.meter * Quantity.new(1,'m^2')).should == 3
+ end
+
+ it "should raise to powers" do
+ (2.meters**2).should be_close Quantity.new(4,"meter^2"), 10**-5
+ lambda {2.meters**-1}.should raise_error ArgumentError
+ lambda {2.meters**1.5}.should raise_error ArgumentError
+ end
+
+ it "should divide any items" do
+ newton = 1.meter * 1.kilogram / 1.second**2
+ newton_p_s3 = newton / 1.second
+ newton_p_s3.unit.name.should == 'meter*kilogram/second^3'
+ (2.meters / 2.picograms).unit.dimension.string_form.should == "length/mass"
+ (2.meters / 2.picograms).units.should == "meter/picogram"
+ (10.meters / 2.picograms).should be_close 5, 10**-5
+ ((1.mm**3)/ 1.second).value.should == 1
+ end
+
+ it "should convert derived units" do
+ Quantity.new(2,'m^2').to_feet.to_f.should be_close 21.5278208, 10**-5
+ Quantity.new(2,'m^2').convert('foot^2').to_f.should be_close 21.5278208, 10**-5
+ end
+
+ it "should convert derived classes to hard classes" do
+ (1.centimeter * 1.centimeter * 1.centimeter).should == 0.1.centiliter
+ (1.centimeter * 1.centimeter * 1.centimeter).should == 1.centimeter.cubed
+ (1000.mm * 1.mm * 1.mm).should == 1.ml
+ (1.mm**3).unit.name.should == 'millimeter^3'
+ (1.mm**3).unit.dimension.name.should == :volume
+ 0.1.centiliter.should == (1.cm * 1.cm * 1.cm)
+ (1.centimeter * 1.centimeter * 1.centimeter).measures.name.should == :volume
+ (1.centimeter * 1.centimeter).measures.name.should == :area
+ (30.meters / 1.second).measures.name.should == :speed
+ end
+
+ it "should reduce derived units" do
+ ((1.meter / 1.second) * 1.second).should == 1.meter
+ end
+
+ it "should be comparable" do
+ 2.meters.should be < 3.meters
+ 150.centimeters.should be > 1.meter
+ [1.meter, 1.foot, 1.inch].sort.should == [1.inch, 1.foot, 1.meter]
+ end
+
+ it "should have a string representation" do
+ 2.meters.to_s.should == "2 meter"
+ (2.meters * 2.meters).to_s.should == (defined?(Rational) ? "4 meter^2" : "4.0 meter^2")
+ end
+
+end
View
164 spec/quantity.spec
@@ -1,164 +1,6 @@
-$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
-require 'quantity'
-require 'quantity/systems/si'
-require 'quantity/systems/us'
-require 'rational'
-
-describe Quantity do
-
- it "should be instantiated from numbers" do
- 1.meter.should == 1
- 2.5.feet.should == 2.5
- end
-
- it "should work with alias names" do
- 2.meters.should == 2
- end
-
- it "should know what it measures" do
- 2.meters.unit.dimension.name.should == :length
- 2.meters.measures.name.should == :length
- end
-
- it "should know its units" do
- 2.meters.unit.name.should == :meter
- 2.meters.units.should == :meter
- end
-
- it "should convert from one type to another" do
- 1.meter.in_centimeters.should == 100
- 50.centimeters.to_meters.should == 0.5
- 10.meters.convert(:feet).should be_close 32.808399.feet, 10**-6
- 1.mm.to_m.should == 0.001
- end
-
- it "should convert from one type to another when not using the reference" do
- 1.kilometer.in_centimeters.should == 100_000
- end
-
- it "should fail to convert from disparate measurement types" do
- lambda { 1.picogram.in_meters }.should raise_error ArgumentError
- end
-
- it "should enforce equality correctly" do
- 12.meter.should == 12.meters
- 1.meter.should == 100.centimeter
- 1.meter.should_not == 1.centimeter
- 1.picograms.should_not == 1.centimeter
- 1.meter.eql?(100.centimeters).should == false
- 1.meter.eql?(1.meter).should == true
- end
-
- it "should add items of the same type" do
- 12.meters.should == 1200.centimeters
- (12.meters + 12).should == 24.meters
- (12.meters + 15.centimeters).should == 1215.centimeters
- (2 + 5.meters).should == 7.meters
- (2.5 + 5.meters).should == 7.5.meters
- end
-
- it "should add and subtract with normal numbers" do
- 3.meters.should == 1.meter + 2
- 4.meters.should == 2 + 2.meter
- 6.4.meters.should == 4.4 + 2.meter
- end
+load 'quantity-real.spec'
- it "should fail to add items of different types" do
- lambda { 12.meters + 24.picograms }.should raise_error ArgumentError
- end
-
- it "should subtract items of the same type" do
- (12.meters - 3).should == 9.meters
- (12.meters - 3650.centimeters).should == -2450.centimeters
- lambda { (12.meters - 3650.picograms)}.should raise_error ArgumentError
- (15 - 5.meters).should == 10.meters
- (15.5 - 5.meters).should == 10.5.meters
- end
-
- it "should support basic math operations" do
- ((-(5.seconds)).abs).should == 5.seconds
- (-5).seconds.abs.should == 5.seconds
- (-(35.meters)).should be_close -(114.829396.feet), 10**-5
- (35.meters % 6).should == 5.meters
- (35.meters % 6.feet).should be_close 0.2528.meters, 10**-5
- 4.kilograms.modulo(15.grams).should == 10.grams
- 15.2.meters.truncate.should == 15.meters
- 15.6.meters.round.should == 16.meters
- 15.2.meters.ceil.should == 16.meters
- (-5.5.meters).floor.should == -6.meters
- 11.meters.divmod(3).should == [3.meter,2.meter]
- 11.meters.divmod(3.meters).should == [3.meter,2.meter]
- 11.meters.divmod(-3).should == [-4.meter,-1.meter]
- 11.meters.divmod(-3.meters).should == [-4.meter,-1.meter]
- 11.meters.divmod(3.5).should == [3.meter,0.5.meter]
- 11.meters.divmod(3.5.meters).should == [3.meter,0.5.meter]
- (-11.meters).divmod(3.5).should == [-4.meter,3.0.meter]
- (-11.meters).divmod(3.5.meters).should == [-4.meter,3.0.meter]
- 11.5.meters.divmod(3.5).should == [3.meter,1.0.meter]
- 11.5.meters.divmod(3.5.meters).should == [3.meter,1.0.meter]
- +4.kilograms.should == 4.kilograms
- 0.kilograms.zero?.should == true
- end
-
- it "should multiply any items" do
- (2.meters * 5.meters).should == 10
- # on second thought, this works :D
- #lambda { (1.meters * 1.foot).unit.name }.should raise_error ArgumentError
- (2.meters * 2.meters).unit.name.should == "meter^2"
- (2.meters * 2.meters).unit.dimension.string_form.should == "length^2"
- (1.meter * Quantity.new(1,'m^2')).unit.dimension.string_form.should == "length^3"
- (1.meter * Quantity.new(1,'m^2')).units.should == "meter^3"
- (3.meter * Quantity.new(1,'m^2')).units.should == "meter^3"
- (3.meter * Quantity.new(1,'m^2')).should == 3
- end
-
- it "should raise to powers" do
- (2.meters**2).should be_close Quantity.new(4,"meter^2"), 10**-5
- lambda {2.meters**-1}.should raise_error ArgumentError
- lambda {2.meters**1.5}.should raise_error ArgumentError
- end
-
- it "should divide any items" do
- newton = 1.meter * 1.kilogram / 1.second**2
- newton_p_s3 = newton / 1.second
- newton_p_s3.unit.name.should == 'meter*kilogram/second^3'
- (2.meters / 2.picograms).unit.dimension.string_form.should == "length/mass"
- (2.meters / 2.picograms).units.should == "meter/picogram"
- (10.meters / 2.picograms).should be_close 5, 10**-5
- ((1.mm**3)/ 1.second).value.should == 1
- end
-
- it "should convert derived units" do
- Quantity.new(2,'m^2').to_feet.to_f.should be_close 21.5278208, 10**-5
- Quantity.new(2,'m^2').convert('foot^2').to_f.should be_close 21.5278208, 10**-5
- end
-
- it "should convert derived classes to hard classes" do
- (1.centimeter * 1.centimeter * 1.centimeter).should == 0.1.centiliter
- (1.centimeter * 1.centimeter * 1.centimeter).should == 1.centimeter.cubed
- (1000.mm * 1.mm * 1.mm).should == 1.ml
- (1.mm**3).unit.name.should == 'millimeter^3'
- (1.mm**3).unit.dimension.name.should == :volume
- 0.1.centiliter.should == (1.cm * 1.cm * 1.cm)
- (1.centimeter * 1.centimeter * 1.centimeter).measures.name.should == :volume
- (1.centimeter * 1.centimeter).measures.name.should == :area
- (30.meters / 1.second).measures.name.should == :speed
- end
-
- it "should reduce derived units" do
- ((1.meter / 1.second) * 1.second).should == 1.meter
- end
-
- it "should be comparable" do
- 2.meters.should be < 3.meters
- 150.centimeters.should be > 1.meter
- [1.meter, 1.foot, 1.inch].sort.should == [1.inch, 1.foot, 1.meter]
- end
-
- it "should have a string representation" do
- 2.meters.to_s.should == "2 meter"
- (2.meters * 2.meters).to_s.should == (defined?(Rational) ? "4 meter^2" : "4.0 meter^2")
- end
+require 'rational'
-end
+load 'quantity-real.spec'
View
28 spec/systems-real.spec
@@ -0,0 +1,28 @@
+
+
+describe Quantity::Dimension do
+ it "should have the base dimensional units available" do
+ require 'quantity/dimension/base'
+ force = Quantity::Dimension.for(:force)
+ force.name.should == :force
+ force.numerators.first.dimension.should == :length
+ end
+end
+
+describe Quantity::Unit do
+ it "should have the SI system available" do
+ require 'quantity/systems/si'
+ Quantity::Unit.for(:kilometers).name.should == :kilometer
+ end
+
+ it "should have the US system available" do
+ require 'quantity/systems/us'
+ Quantity::Unit.for(:gallons).name.should == :gallon
+ end
+
+ it "should blow up the world" do
+ #Quantity::Unit.__reset!
+ #Quantity::Dimension.__reset!
+ end
+
+end
View
28 spec/systems.spec
@@ -1,28 +1,6 @@
+load 'systems-real.spec'
-describe Quantity::Dimension do
- it "should have the base dimensional units available" do
- require 'quantity/dimension/base'
- force = Quantity::Dimension.for(:force)
- force.name.should == :force
- force.numerators.first.dimension.should == :length
- end
-end
+require 'rational'
-describe Quantity::Unit do
- it "should have the SI system available" do
- require 'quantity/systems/si'
- Quantity::Unit.for(:kilometers).name.should == :kilometer
- end
-
- it "should have the US system available" do
- require 'quantity/systems/us'
- Quantity::Unit.for(:gallons).name.should == :gallon
- end
-
- it "should blow up the world" do
- #Quantity::Unit.__reset!
- #Quantity::Dimension.__reset!
- end
-
-end
+load 'systems-real.spec'
View
144 spec/unit-real.spec
@@ -0,0 +1,144 @@
+$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
+
+require 'quantity/dimension'
+require 'quantity/unit'
+require 'rational'
+
+
+describe Quantity::Unit do
+
+ before(:all) do
+ class Length < Quantity::Dimension ; end
+ class Area < Quantity::Dimension ; end
+ class Acceleration < Quantity::Dimension ; end
+ length = Length.add_dimension :length, :width
+ Area.add_dimension :'length^2'
+ Acceleration.add_dimension :'length/time^2'
+ Quantity::Dimension.add_dimension :mass
+ Quantity::Dimension.add_dimension :time
+ Quantity::Dimension.add_dimension :'mass*length/time^2', :force
+ Quantity::Dimension.add_dimension length**3, :volume
+ end
+
+ before(:each) do
+ @length = Quantity::Dimension.for(:length)
+ @mass = Quantity::Dimension.for(:mass)
+ @area = Quantity::Dimension.for(:area)
+ @accel = Quantity::Dimension.for(:acceleration)
+ @force = Quantity::Dimension.for(:force)
+ @time = Quantity::Dimension.for(:time)
+ @volume = Quantity::Dimension.for(:volume)
+
+ @meter = Quantity::Unit.for(:meter)
+ @mm = Quantity::Unit.for(:mm)
+ @inch = Quantity::Unit.for(:inch)
+ @foot = Quantity::Unit.for(:foot)
+ @second = Quantity::Unit.for(:second)
+ @gram = Quantity::Unit.for(:gram)
+ @liter = Quantity::Unit.for(:liter)
+ end
+
+ it "should respond correctly to the DSL and add units" do
+ Quantity::Unit.add_unit :meter, @length, 1000, :meters, :m
+ Quantity::Unit.add_unit :millimeter, @length, 1, :mm, :millimeters
+ Quantity::Unit.add_unit :second, @time, 1000, :seconds, :s
+ Quantity::Unit.add_unit :foot, @length, 304.8, :feet
+ Quantity::Unit.add_unit :inch, @length, 25.4, :inches
+ Quantity::Unit.add_unit :gram, @mass, 1000, :grams
+ Quantity::Unit.add_unit :nanosecond, @time, 10**-6, :nanoseconds
+ Quantity::Unit.add_unit :picogram, @mass, 10**-9, :picograms
+ Quantity::Unit.add_unit :mps, @accel, 10**12, :meterspersecond
+ Quantity::Unit.add_unit :liter, @volume, 10**6, :liters, :l
+ meters = Quantity::Unit.for :meter
+ meters.dimension.should == @length
+ meters.name.should == :meter
+ meters.value.should == 1000
+ mps = Quantity::Unit.for :mps
+ mps.name.should == :mps
+ mps.value.should == 10**12
+ end
+
+ it "should know its aliases" do
+ m1 = Quantity::Unit.for(:meter)
+ m2 = Quantity::Unit.for(:meters)
+ m3 = Quantity::Unit.for(:m)
+ m1.should equal(m2)
+ m1.should equal(m3)
+ end
+
+ it "should support complex units" do
+ sqft = Quantity::Unit.new({ :name => :sqft, :dimension => @area, :units => { @length => @foot}})
+ sqft.name.should == :sqft
+ sqft.dimension.should == @area
+ sqft.dimension.string_form.should == 'length^2'
+ sqft.string_form.should == 'foot^2'
+ sqft.value.should be_close 92903.04, 10**-5
+ area_p_sec = @area / @time
+ f2ps = Quantity::Unit.new({:name => :f2ps, :dimension => area_p_sec,
+ :units => { @length => @foot, @time => @second }})
+ f2ps.name.should == :f2ps
+ f2ps.string_form.should == 'foot^2/second'
+ f2ps.dimension.string_form.should == 'length^2/time'
+ f2ps.value.should be_close 92.90304, 10**-5
+ end
+
+# should it really, though?
+# it "should generate reference units for compound dimensions" do
+# length = Quantity::Dimension.for(:length)
+# volume = length * length * length
+# cubicmm = Quantity::Unit::Compound.reference_unit_for(volume)
+# cubicmm.value.should == 1
+# cubicmm.string_form.should == 'millimeter^3'
+# cubicmm.dimension.should == volume
+# end
+
+ it "should multiply units" do
+ sqft = @foot * @foot
+ sqft.name.should == 'foot^2'
+ #sqft.dimension.reference.should equal(Quantity::Unit.for('millimeter^2'))
+ cubeft = sqft * @foot
+ cubeft.name.should == 'foot^3'
+ s_f3 = @second * cubeft
+ s_f3.name.should == 'foot^3*second'
+ #cubeft.dimension.reference.should equal(Quantity::Unit.for('millimeter^3'))
+ end
+
+ it "should divide units" do
+ m_s = @meter / @second
+ m_s.name.should == 'meter/second'
+ accel_dim = @length / (@time**2)
+ accel = m_s / @second
+ accel.dimension.should equal(accel_dim)
+ accel.name.should == 'meter/second^2'
+ (accel * @second).should equal(m_s)
+ x = (@mm**3) / @second
+ x.value.should == 1 / 1000.0
+ lps = @liter / @second
+ lps.unit.dimension.name.should == :'length^3/time'
+ lps.unit.name.should == :'liter/second'
+ newton = @meter * @gram / @second**2
+ (newton / @second).name.should == 'meter*gram/second^3'
+ end
+
+ it "should convert complex units" do
+ foot = Quantity::Unit.for(:foot)
+ sqft = foot * foot
+ sqmt = sqft.convert(:meter)
+ sqmt.name.should == 'meter^2'
+ foot_grams_of_force = @gram * @foot / (@second**2)
+ lambda {foot_grams_of_force.convert(:'feet^2')}.should raise_error ArgumentError
+ lambda {foot_grams_of_force.convert('feet^2')}.should raise_error ArgumentError
+ foot_grams_of_force.convert('feet*picograms/nanoseconds^2').string_form.should == 'foot*picogram/nanosecond^2'
+ foot_grams_of_force.convert(:'feet*picograms/nanoseconds^2').string_form.should == 'foot*picogram/nanosecond^2'
+ foot_grams_of_force.convert(:meter).name.should == 'meter*gram/second^2'
+ end
+
+ it "should reset the world" do
+ Quantity::Unit.__reset!
+ Quantity::Dimension.__reset!
+ nil.should == Quantity::Unit.for(:meter)
+ nil.should == Quantity::Dimension.for(:time)
+ end
+
+
+end
View
144 spec/unit.spec
@@ -1,144 +1,6 @@
-$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
-require 'quantity/dimension'
-require 'quantity/unit'
-require 'rational'
-
-
-describe Quantity::Unit do
-
- before(:all) do
- class Length < Quantity::Dimension ; end
- class Area < Quantity::Dimension ; end
- class Acceleration < Quantity::Dimension ; end
- length = Length.add_dimension :length, :width
- Area.add_dimension :'length^2'
- Acceleration.add_dimension :'length/time^2'
- Quantity::Dimension.add_dimension :mass
- Quantity::Dimension.add_dimension :time
- Quantity::Dimension.add_dimension :'mass*length/time^2', :force
- Quantity::Dimension.add_dimension length**3, :volume
- end
-
- before(:each) do
- @length = Quantity::Dimension.for(:length)
- @mass = Quantity::Dimension.for(:mass)
- @area = Quantity::Dimension.for(:area)
- @accel = Quantity::Dimension.for(:acceleration)
- @force = Quantity::Dimension.for(:force)
- @time = Quantity::Dimension.for(:time)
- @volume = Quantity::Dimension.for(:volume)
-
- @meter = Quantity::Unit.for(:meter)
- @mm = Quantity::Unit.for(:mm)
- @inch = Quantity::Unit.for(:inch)
- @foot = Quantity::Unit.for(:foot)
- @second = Quantity::Unit.for(:second)
- @gram = Quantity::Unit.for(:gram)
- @liter = Quantity::Unit.for(:liter)
- end
-
- it "should respond correctly to the DSL and add units" do
- Quantity::Unit.add_unit :meter, @length, 1000, :meters, :m
- Quantity::Unit.add_unit :millimeter, @length, 1, :mm, :millimeters
- Quantity::Unit.add_unit :second, @time, 1000, :seconds, :s
- Quantity::Unit.add_unit :foot, @length, 304.8, :feet
- Quantity::Unit.add_unit :inch, @length, 25.4, :inches
- Quantity::Unit.add_unit :gram, @mass, 1000, :grams
- Quantity::Unit.add_unit :nanosecond, @time, 10**-6, :nanoseconds
- Quantity::Unit.add_unit :picogram, @mass, 10**-9, :picograms
- Quantity::Unit.add_unit :mps, @accel, 10**12, :meterspersecond
- Quantity::Unit.add_unit :liter, @volume, 10**6, :liters, :l
- meters = Quantity::Unit.for :meter
- meters.dimension.should == @length
- meters.name.should == :meter
- meters.value.should == 1000
- mps = Quantity::Unit.for :mps
- mps.name.should == :mps
- mps.value.should == 10**12
- end
-
- it "should know its aliases" do
- m1 = Quantity::Unit.for(:meter)
- m2 = Quantity::Unit.for(:meters)
- m3 = Quantity::Unit.for(:m)
- m1.should equal(m2)
- m1.should equal(m3)
- end
-
- it "should support complex units" do
- sqft = Quantity::Unit.new({ :name => :sqft, :dimension => @area, :units => { @length => @foot}})
- sqft.name.should == :sqft
- sqft.dimension.should == @area
- sqft.dimension.string_form.should == 'length^2'
- sqft.string_form.should == 'foot^2'
- sqft.value.should be_close 92903.04, 10**-5
- area_p_sec = @area / @time
- f2ps = Quantity::Unit.new({:name => :f2ps, :dimension => area_p_sec,
- :units => { @length => @foot, @time => @second }})
- f2ps.name.should == :f2ps
- f2ps.string_form.should == 'foot^2/second'
- f2ps.dimension.string_form.should == 'length^2/time'
- f2ps.value.should be_close 92.90304, 10**-5
- end
-
-# should it really, though?
-# it "should generate reference units for compound dimensions" do
-# length = Quantity::Dimension.for(:length)
-# volume = length * length * length
-# cubicmm = Quantity::Unit::Compound.reference_unit_for(volume)
-# cubicmm.value.should == 1
-# cubicmm.string_form.should == 'millimeter^3'
-# cubicmm.dimension.should == volume
-# end
-
- it "should multiply units" do
- sqft = @foot * @foot
- sqft.name.should == 'foot^2'
- #sqft.dimension.reference.should equal(Quantity::Unit.for('millimeter^2'))
- cubeft = sqft * @foot
- cubeft.name.should == 'foot^3'
- s_f3 = @second * cubeft
- s_f3.name.should == 'foot^3*second'
- #cubeft.dimension.reference.should equal(Quantity::Unit.for('millimeter^3'))
- end
-
- it "should divide units" do
- m_s = @meter / @second
- m_s.name.should == 'meter/second'
- accel_dim = @length / (@time**2)
- accel = m_s / @second
- accel.dimension.should equal(accel_dim)
- accel.name.should == 'meter/second^2'
- (accel * @second).should equal(m_s)
- x = (@mm**3) / @second
- x.value.should == 1 / 1000.0
- lps = @liter / @second
- lps.unit.dimension.name.should == :'length^3/time'
- lps.unit.name.should == :'liter/second'
- newton = @meter * @gram / @second**2
- (newton / @second).name.should == 'meter*gram/second^3'
- end
-
- it "should convert complex units" do
- foot = Quantity::Unit.for(:foot)
- sqft = foot * foot
- sqmt = sqft.convert(:meter)
- sqmt.name.should == 'meter^2'
- foot_grams_of_force = @gram * @foot / (@second**2)
- lambda {foot_grams_of_force.convert(:'feet^2')}.should raise_error ArgumentError
- lambda {foot_grams_of_force.convert('feet^2')}.should raise_error ArgumentError
- foot_grams_of_force.convert('feet*picograms/nanoseconds^2').string_form.should == 'foot*picogram/nanosecond^2'
- foot_grams_of_force.convert(:'feet*picograms/nanoseconds^2').string_form.should == 'foot*picogram/nanosecond^2'
- foot_grams_of_force.convert(:meter).name.should == 'meter*gram/second^2'
- end
-
- it "should reset the world" do
- Quantity::Unit.__reset!
- Quantity::Dimension.__reset!
- nil.should == Quantity::Unit.for(:meter)
- nil.should == Quantity::Dimension.for(:time)
- end
+load 'unit-real.spec'
+require 'rational'
-end
+load 'unit-real.spec'
Please sign in to comment.
Something went wrong with that request. Please try again.