| |   |
| 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
| 3 | describe Infinity do |
| 4 | |
| 5 | it "should correctly compare with integers and floats" do |
| 6 | Infinity.should > 1 |
| 7 | Infinity.should > 1.0 |
| 8 | end |
| 9 | |
| 10 | it "should correctly work with ranges" do |
| 11 | (-Infinity..1).should include(-100) |
| 12 | (-Infinity..Infinity).should include(Float::MAX) |
| 13 | (0..Infinity).should include(Float::MAX) |
| 14 | end |
| 15 | |
| 16 | it "should return Infinity in arithmetics" do |
| 17 | (Infinity + Infinity).should == Infinity |
| 18 | (Infinity * Infinity).should == Infinity |
| 19 | (Infinity ** Infinity).should == Infinity |
| 20 | (Infinity / 0).should == Infinity |
| 21 | (Infinity / 0.0).should == Infinity |
| 22 | end |
| 23 | |
| 24 | it "should return NaN in arithmetics" do |
| 25 | (Infinity - Infinity).should be_nan |
| 26 | (Infinity / Infinity).should be_nan |
| 27 | (Infinity * 0).should be_nan |
| 28 | (Infinity * 0.0).should be_nan |
| 29 | end |
| 30 | |
| 31 | end |
| 32 | |
| 33 | describe NaN do |
| 34 | |
| 35 | it "should correctly compare with numerics" do |
| 36 | (NaN < 1).should be_false |
| 37 | (NaN > 1).should be_false |
| 38 | (NaN == 1).should be_false |
| 39 | end |
| 40 | |
| 41 | it "should not compare with itself" do |
| 42 | NaN.should_not == NaN |
| 43 | NaN.should be_nan |
| 44 | NaN.object_id.should == NaN.object_id |
| 45 | (NaN + 1).object_id.should_not == NaN.object_id |
| 46 | end |
| 47 | |
| 48 | it "should produce NaN in all the operations except NaN**0 == 1" do |
| 49 | (NaN + 1).should be_nan |
| 50 | (NaN + Infinity).should be_nan |
| 51 | (NaN - 1).should be_nan |
| 52 | (NaN * 0).should be_nan |
| 53 | (NaN * Infinity).should be_nan |
| 54 | (NaN ** 0).should == 1 # <-- WTF? (Ruby 1.8.6 rel. 2007-03-13) |
| 55 | (NaN ** 1).should be_nan |
| 56 | (NaN ** Infinity).should be_nan |
| 57 | (NaN / 0).should be_nan |
| 58 | (NaN / 1).should be_nan |
| 59 | (NaN / Infinity).should be_nan |
| 60 | end |
| 61 | |
| 62 | end |
| toggle raw diff |
--- /dev/null
+++ b/spec/lib/strokedb/core_ext/float_spec.rb
@@ -0,0 +1,62 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe Infinity do
+
+ it "should correctly compare with integers and floats" do
+ Infinity.should > 1
+ Infinity.should > 1.0
+ end
+
+ it "should correctly work with ranges" do
+ (-Infinity..1).should include(-100)
+ (-Infinity..Infinity).should include(Float::MAX)
+ (0..Infinity).should include(Float::MAX)
+ end
+
+ it "should return Infinity in arithmetics" do
+ (Infinity + Infinity).should == Infinity
+ (Infinity * Infinity).should == Infinity
+ (Infinity ** Infinity).should == Infinity
+ (Infinity / 0).should == Infinity
+ (Infinity / 0.0).should == Infinity
+ end
+
+ it "should return NaN in arithmetics" do
+ (Infinity - Infinity).should be_nan
+ (Infinity / Infinity).should be_nan
+ (Infinity * 0).should be_nan
+ (Infinity * 0.0).should be_nan
+ end
+
+end
+
+describe NaN do
+
+ it "should correctly compare with numerics" do
+ (NaN < 1).should be_false
+ (NaN > 1).should be_false
+ (NaN == 1).should be_false
+ end
+
+ it "should not compare with itself" do
+ NaN.should_not == NaN
+ NaN.should be_nan
+ NaN.object_id.should == NaN.object_id
+ (NaN + 1).object_id.should_not == NaN.object_id
+ end
+
+ it "should produce NaN in all the operations except NaN**0 == 1" do
+ (NaN + 1).should be_nan
+ (NaN + Infinity).should be_nan
+ (NaN - 1).should be_nan
+ (NaN * 0).should be_nan
+ (NaN * Infinity).should be_nan
+ (NaN ** 0).should == 1 # <-- WTF? (Ruby 1.8.6 rel. 2007-03-13)
+ (NaN ** 1).should be_nan
+ (NaN ** Infinity).should be_nan
+ (NaN / 0).should be_nan
+ (NaN / 1).should be_nan
+ (NaN / Infinity).should be_nan
+ end
+
+end |