summaryrefslogtreecommitdiff
path: root/spec/ruby/core/bignum/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/bignum/shared')
-rw-r--r--spec/ruby/core/bignum/shared/abs.rb6
-rw-r--r--spec/ruby/core/bignum/shared/divide.rb27
-rw-r--r--spec/ruby/core/bignum/shared/equal.rb31
-rw-r--r--spec/ruby/core/bignum/shared/modulo.rb29
4 files changed, 93 insertions, 0 deletions
diff --git a/spec/ruby/core/bignum/shared/abs.rb b/spec/ruby/core/bignum/shared/abs.rb
new file mode 100644
index 0000000000..35fd85060c
--- /dev/null
+++ b/spec/ruby/core/bignum/shared/abs.rb
@@ -0,0 +1,6 @@
+describe :bignum_abs, shared: true do
+ it "returns the absolute value" do
+ bignum_value(39).send(@method).should == 9223372036854775847
+ (-bignum_value(18)).send(@method).should == 9223372036854775826
+ end
+end
diff --git a/spec/ruby/core/bignum/shared/divide.rb b/spec/ruby/core/bignum/shared/divide.rb
new file mode 100644
index 0000000000..cbde69bbb2
--- /dev/null
+++ b/spec/ruby/core/bignum/shared/divide.rb
@@ -0,0 +1,27 @@
+describe :bignum_divide, shared: true do
+ before :each do
+ @bignum = bignum_value(88)
+ end
+
+ it "returns self divided by other" do
+ @bignum.send(@method, 4).should == 2305843009213693974
+
+ @bignum.send(@method, bignum_value(2)).should be_close(1, TOLERANCE)
+
+ (-(10**50)).send(@method, -(10**40 + 1)).should == 9999999999
+ (10**50).send(@method, 10**40 + 1).should == 9999999999
+
+ (-10**50).send(@method, 10**40 + 1).should == -10000000000
+ (10**50).send(@method, -(10**40 + 1)).should == -10000000000
+ end
+
+ it "raises a ZeroDivisionError if other is zero and not a Float" do
+ lambda { @bignum.send(@method, 0) }.should raise_error(ZeroDivisionError)
+ end
+
+ it "raises a TypeError when given a non-Integer" do
+ lambda { @bignum.send(@method, mock('10')) }.should raise_error(TypeError)
+ lambda { @bignum.send(@method, "2") }.should raise_error(TypeError)
+ lambda { @bignum.send(@method, :symbol) }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/ruby/core/bignum/shared/equal.rb b/spec/ruby/core/bignum/shared/equal.rb
new file mode 100644
index 0000000000..ffe4daf4f1
--- /dev/null
+++ b/spec/ruby/core/bignum/shared/equal.rb
@@ -0,0 +1,31 @@
+describe :bignum_equal, shared: true do
+ before :each do
+ @bignum = bignum_value
+ end
+
+ it "returns true if self has the same value as the given argument" do
+ @bignum.send(@method, @bignum).should == true
+ @bignum.send(@method, @bignum.to_f).should == true
+
+ @bignum.send(@method, @bignum + 1).should == false
+ (@bignum + 1).send(@method, @bignum).should == false
+
+ @bignum.send(@method, 9).should == false
+ @bignum.send(@method, 9.01).should == false
+
+ @bignum.send(@method, bignum_value(10)).should == false
+ end
+
+ it "calls 'other == self' if the given argument is not an Integer" do
+ obj = mock('not integer')
+ obj.should_receive(:==).and_return(true)
+ @bignum.send(@method, obj).should == true
+ end
+
+ it "returns the result of 'other == self' as a boolean" do
+ obj = mock('not integer')
+ obj.should_receive(:==).exactly(2).times.and_return("woot", nil)
+ @bignum.send(@method, obj).should == true
+ @bignum.send(@method, obj).should == false
+ end
+end
diff --git a/spec/ruby/core/bignum/shared/modulo.rb b/spec/ruby/core/bignum/shared/modulo.rb
new file mode 100644
index 0000000000..9814e22f3b
--- /dev/null
+++ b/spec/ruby/core/bignum/shared/modulo.rb
@@ -0,0 +1,29 @@
+describe :bignum_modulo, shared: true do
+ before :each do
+ @bignum = bignum_value
+ end
+
+ it "returns the modulus obtained from dividing self by the given argument" do
+ @bignum.send(@method, 5).should == 3
+ @bignum.send(@method, -5).should == -2
+ @bignum.send(@method, -100).should == -92
+ @bignum.send(@method, 2.22).should be_close(0.780180180180252, TOLERANCE)
+ @bignum.send(@method, bignum_value(10)).should == 9223372036854775808
+ end
+
+ it "raises a ZeroDivisionError when the given argument is 0" do
+ lambda { @bignum.send(@method, 0) }.should raise_error(ZeroDivisionError)
+ lambda { (-@bignum).send(@method, 0) }.should raise_error(ZeroDivisionError)
+ end
+
+ it "raises a ZeroDivisionError when the given argument is 0 and a Float" do
+ lambda { @bignum.send(@method, 0.0) }.should raise_error(ZeroDivisionError)
+ lambda { -@bignum.send(@method, 0.0) }.should raise_error(ZeroDivisionError)
+ end
+
+ it "raises a TypeError when given a non-Integer" do
+ lambda { @bignum.send(@method, mock('10')) }.should raise_error(TypeError)
+ lambda { @bignum.send(@method, "10") }.should raise_error(TypeError)
+ lambda { @bignum.send(@method, :symbol) }.should raise_error(TypeError)
+ end
+end