summaryrefslogtreecommitdiff
path: root/spec/rubyspec/core/rational
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubyspec/core/rational')
-rw-r--r--spec/rubyspec/core/rational/abs_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/ceil_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/coerce_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/comparison_spec.rb21
-rw-r--r--spec/rubyspec/core/rational/denominator_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/div_spec.rb17
-rw-r--r--spec/rubyspec/core/rational/divide_spec.rb17
-rw-r--r--spec/rubyspec/core/rational/divmod_spec.rb13
-rw-r--r--spec/rubyspec/core/rational/equal_value_spec.rb17
-rw-r--r--spec/rubyspec/core/rational/exponent_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/fdiv_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/floor_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/hash_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/inspect_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/integer_spec.rb9
-rw-r--r--spec/rubyspec/core/rational/magnitude_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/marshal_dump_spec.rb11
-rw-r--r--spec/rubyspec/core/rational/minus_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/modulo_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/multiply_spec.rb17
-rw-r--r--spec/rubyspec/core/rational/numerator_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/plus_spec.rb16
-rw-r--r--spec/rubyspec/core/rational/quo_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/rational_spec.rb7
-rw-r--r--spec/rubyspec/core/rational/rationalize_spec.rb36
-rw-r--r--spec/rubyspec/core/rational/remainder_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/round_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/to_f_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/to_i_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/to_r_spec.rb10
-rw-r--r--spec/rubyspec/core/rational/to_s_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/truncate_spec.rb5
-rw-r--r--spec/rubyspec/core/rational/zero_spec.rb13
33 files changed, 304 insertions, 0 deletions
diff --git a/spec/rubyspec/core/rational/abs_spec.rb b/spec/rubyspec/core/rational/abs_spec.rb
new file mode 100644
index 0000000000..b04cbd2020
--- /dev/null
+++ b/spec/rubyspec/core/rational/abs_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/abs', __FILE__)
+
+describe "Rational#abs" do
+ it_behaves_like(:rational_abs, :abs)
+end
diff --git a/spec/rubyspec/core/rational/ceil_spec.rb b/spec/rubyspec/core/rational/ceil_spec.rb
new file mode 100644
index 0000000000..e7aca07d33
--- /dev/null
+++ b/spec/rubyspec/core/rational/ceil_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/ceil', __FILE__)
+
+describe "Rational#ceil" do
+ it_behaves_like(:rational_ceil, :ceil)
+end
diff --git a/spec/rubyspec/core/rational/coerce_spec.rb b/spec/rubyspec/core/rational/coerce_spec.rb
new file mode 100644
index 0000000000..2cfe1db415
--- /dev/null
+++ b/spec/rubyspec/core/rational/coerce_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/coerce', __FILE__)
+
+describe "Rational#coerce" do
+ it_behaves_like(:rational_coerce, :coerce)
+end
diff --git a/spec/rubyspec/core/rational/comparison_spec.rb b/spec/rubyspec/core/rational/comparison_spec.rb
new file mode 100644
index 0000000000..47e3ab043d
--- /dev/null
+++ b/spec/rubyspec/core/rational/comparison_spec.rb
@@ -0,0 +1,21 @@
+require File.expand_path('../../../shared/rational/comparison', __FILE__)
+
+describe "Rational#<=> when passed a Rational object" do
+ it_behaves_like(:rational_cmp_rat, :<=>)
+end
+
+describe "Rational#<=> when passed a Integer object" do
+ it_behaves_like(:rational_cmp_int, :<=>)
+end
+
+describe "Rational#<=> when passed a Float object" do
+ it_behaves_like(:rational_cmp_float, :<=>)
+end
+
+describe "Rational#<=> when passed an Object that responds to #coerce" do
+ it_behaves_like(:rational_cmp_coerce, :<=>)
+end
+
+describe "Rational#<=> when passed a non-Numeric Object that doesn't respond to #coerce" do
+ it_behaves_like(:rational_cmp_other, :<=>)
+end
diff --git a/spec/rubyspec/core/rational/denominator_spec.rb b/spec/rubyspec/core/rational/denominator_spec.rb
new file mode 100644
index 0000000000..ac2795ded1
--- /dev/null
+++ b/spec/rubyspec/core/rational/denominator_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/denominator', __FILE__)
+
+describe "Rational#denominator" do
+ it_behaves_like(:rational_denominator, :denominator)
+end
diff --git a/spec/rubyspec/core/rational/div_spec.rb b/spec/rubyspec/core/rational/div_spec.rb
new file mode 100644
index 0000000000..1fd304b646
--- /dev/null
+++ b/spec/rubyspec/core/rational/div_spec.rb
@@ -0,0 +1,17 @@
+require File.expand_path('../../../shared/rational/div', __FILE__)
+
+describe "Rational#div" do
+ it_behaves_like(:rational_div, :div)
+end
+
+describe "Rational#div passed a Rational" do
+ it_behaves_like(:rational_div_rat, :div)
+end
+
+describe "Rational#div passed an Integer" do
+ it_behaves_like(:rational_div_int, :div)
+end
+
+describe "Rational#div passed a Float" do
+ it_behaves_like(:rational_div_float, :div)
+end
diff --git a/spec/rubyspec/core/rational/divide_spec.rb b/spec/rubyspec/core/rational/divide_spec.rb
new file mode 100644
index 0000000000..75e447878c
--- /dev/null
+++ b/spec/rubyspec/core/rational/divide_spec.rb
@@ -0,0 +1,17 @@
+require File.expand_path('../../../shared/rational/divide', __FILE__)
+
+describe "Rational#/" do
+ it_behaves_like(:rational_divide, :/)
+end
+
+describe "Rational#/ when passed an Integer" do
+ it_behaves_like(:rational_divide_int, :/)
+end
+
+describe "Rational#/ when passed a Rational" do
+ it_behaves_like(:rational_divide_rat, :/)
+end
+
+describe "Rational#/ when passed a Float" do
+ it_behaves_like(:rational_divide_float, :/)
+end
diff --git a/spec/rubyspec/core/rational/divmod_spec.rb b/spec/rubyspec/core/rational/divmod_spec.rb
new file mode 100644
index 0000000000..ee03c9498a
--- /dev/null
+++ b/spec/rubyspec/core/rational/divmod_spec.rb
@@ -0,0 +1,13 @@
+require File.expand_path('../../../shared/rational/divmod', __FILE__)
+
+describe "Rational#divmod when passed a Rational" do
+ it_behaves_like(:rational_divmod_rat, :divmod)
+end
+
+describe "Rational#divmod when passed an Integer" do
+ it_behaves_like(:rational_divmod_int, :divmod)
+end
+
+describe "Rational#divmod when passed a Float" do
+ it_behaves_like(:rational_divmod_float, :divmod)
+end
diff --git a/spec/rubyspec/core/rational/equal_value_spec.rb b/spec/rubyspec/core/rational/equal_value_spec.rb
new file mode 100644
index 0000000000..708eccf6d1
--- /dev/null
+++ b/spec/rubyspec/core/rational/equal_value_spec.rb
@@ -0,0 +1,17 @@
+require File.expand_path('../../../shared/rational/equal_value', __FILE__)
+
+describe "Rational#==" do
+ it_behaves_like(:rational_equal_value, :==)
+end
+
+describe "Rational#== when passed a Rational" do
+ it_behaves_like(:rational_equal_value_rat, :==)
+end
+
+describe "Rational#== when passed a Float" do
+ it_behaves_like(:rational_equal_value_float, :==)
+end
+
+describe "Rational#== when passed an Integer" do
+ it_behaves_like(:rational_equal_value_int, :==)
+end
diff --git a/spec/rubyspec/core/rational/exponent_spec.rb b/spec/rubyspec/core/rational/exponent_spec.rb
new file mode 100644
index 0000000000..e4810d1b41
--- /dev/null
+++ b/spec/rubyspec/core/rational/exponent_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/exponent', __FILE__)
+
+describe "Rational#**" do
+ it_behaves_like(:rational_exponent, :**)
+end
diff --git a/spec/rubyspec/core/rational/fdiv_spec.rb b/spec/rubyspec/core/rational/fdiv_spec.rb
new file mode 100644
index 0000000000..15b4ed0619
--- /dev/null
+++ b/spec/rubyspec/core/rational/fdiv_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/fdiv', __FILE__)
+
+describe "Rational#fdiv" do
+ it_behaves_like(:rational_fdiv, :fdiv)
+end
diff --git a/spec/rubyspec/core/rational/floor_spec.rb b/spec/rubyspec/core/rational/floor_spec.rb
new file mode 100644
index 0000000000..485abcf58f
--- /dev/null
+++ b/spec/rubyspec/core/rational/floor_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/floor', __FILE__)
+
+describe "Rational#floor" do
+ it_behaves_like(:rational_floor, :floor)
+end
diff --git a/spec/rubyspec/core/rational/hash_spec.rb b/spec/rubyspec/core/rational/hash_spec.rb
new file mode 100644
index 0000000000..a17572d57e
--- /dev/null
+++ b/spec/rubyspec/core/rational/hash_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/hash', __FILE__)
+
+describe "Rational#hash" do
+ it_behaves_like(:rational_hash, :hash)
+end
diff --git a/spec/rubyspec/core/rational/inspect_spec.rb b/spec/rubyspec/core/rational/inspect_spec.rb
new file mode 100644
index 0000000000..452cf9c5ae
--- /dev/null
+++ b/spec/rubyspec/core/rational/inspect_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/inspect', __FILE__)
+
+describe "Rational#inspect" do
+ it_behaves_like(:rational_inspect, :inspect)
+end
diff --git a/spec/rubyspec/core/rational/integer_spec.rb b/spec/rubyspec/core/rational/integer_spec.rb
new file mode 100644
index 0000000000..451e42ee14
--- /dev/null
+++ b/spec/rubyspec/core/rational/integer_spec.rb
@@ -0,0 +1,9 @@
+describe "Rational#integer?" do
+ it "returns false for a rational with a numerator and no denominator" do
+ Rational(20).integer?.should be_false
+ end
+
+ it "returns false for a rational with a numerator and a denominator" do
+ Rational(20,3).integer?.should be_false
+ end
+end
diff --git a/spec/rubyspec/core/rational/magnitude_spec.rb b/spec/rubyspec/core/rational/magnitude_spec.rb
new file mode 100644
index 0000000000..288b9a3fa3
--- /dev/null
+++ b/spec/rubyspec/core/rational/magnitude_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/abs', __FILE__)
+
+describe "Rational#abs" do
+ it_behaves_like(:rational_abs, :magnitude)
+end
diff --git a/spec/rubyspec/core/rational/marshal_dump_spec.rb b/spec/rubyspec/core/rational/marshal_dump_spec.rb
new file mode 100644
index 0000000000..46e9267ebd
--- /dev/null
+++ b/spec/rubyspec/core/rational/marshal_dump_spec.rb
@@ -0,0 +1,11 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Rational#marshal_dump" do
+ it "is a private method" do
+ Rational.should have_private_instance_method(:marshal_dump, false)
+ end
+
+ it "dumps numerator and denominator" do
+ Rational(1, 2).send(:marshal_dump).should == [1, 2]
+ end
+end
diff --git a/spec/rubyspec/core/rational/minus_spec.rb b/spec/rubyspec/core/rational/minus_spec.rb
new file mode 100644
index 0000000000..9e28cb1c4f
--- /dev/null
+++ b/spec/rubyspec/core/rational/minus_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/minus', __FILE__)
+
+describe "Rational#-" do
+ it_behaves_like(:rational_minus, :-)
+end
diff --git a/spec/rubyspec/core/rational/modulo_spec.rb b/spec/rubyspec/core/rational/modulo_spec.rb
new file mode 100644
index 0000000000..81e7c13797
--- /dev/null
+++ b/spec/rubyspec/core/rational/modulo_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/modulo', __FILE__)
+
+describe "Rational#%" do
+ it_behaves_like(:rational_modulo, :%)
+end
diff --git a/spec/rubyspec/core/rational/multiply_spec.rb b/spec/rubyspec/core/rational/multiply_spec.rb
new file mode 100644
index 0000000000..c45491fde3
--- /dev/null
+++ b/spec/rubyspec/core/rational/multiply_spec.rb
@@ -0,0 +1,17 @@
+require File.expand_path('../../../shared/rational/multiply', __FILE__)
+
+describe "Rational#*" do
+ it_behaves_like(:rational_multiply, :*)
+end
+
+describe "Rational#* passed a Rational" do
+ it_behaves_like(:rational_multiply_rat, :*)
+end
+
+describe "Rational#* passed a Float" do
+ it_behaves_like(:rational_multiply_float, :*)
+end
+
+describe "Rational#* passed an Integer" do
+ it_behaves_like(:rational_multiply_int, :*)
+end
diff --git a/spec/rubyspec/core/rational/numerator_spec.rb b/spec/rubyspec/core/rational/numerator_spec.rb
new file mode 100644
index 0000000000..ac6591291d
--- /dev/null
+++ b/spec/rubyspec/core/rational/numerator_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/numerator', __FILE__)
+
+describe "Rational#numerator" do
+ it_behaves_like(:rational_numerator, :numerator)
+end
diff --git a/spec/rubyspec/core/rational/plus_spec.rb b/spec/rubyspec/core/rational/plus_spec.rb
new file mode 100644
index 0000000000..b82c44bbad
--- /dev/null
+++ b/spec/rubyspec/core/rational/plus_spec.rb
@@ -0,0 +1,16 @@
+require File.expand_path('../../../shared/rational/plus', __FILE__)
+
+describe "Rational#+" do
+ it_behaves_like(:rational_plus, :+)
+end
+
+describe "Rational#+ with a Rational" do
+ it_behaves_like(:rational_plus_rat, :+)
+end
+describe "Rational#+ with a Float" do
+ it_behaves_like(:rational_plus_float, :+)
+end
+
+describe "Rational#+ with an Integer" do
+ it_behaves_like(:rational_plus_int, :+)
+end
diff --git a/spec/rubyspec/core/rational/quo_spec.rb b/spec/rubyspec/core/rational/quo_spec.rb
new file mode 100644
index 0000000000..f4d04d5f6f
--- /dev/null
+++ b/spec/rubyspec/core/rational/quo_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/divide', __FILE__)
+
+describe "Rational#quo" do
+ it_behaves_like(:rational_divide, :quo)
+end
diff --git a/spec/rubyspec/core/rational/rational_spec.rb b/spec/rubyspec/core/rational/rational_spec.rb
new file mode 100644
index 0000000000..fc3108c11b
--- /dev/null
+++ b/spec/rubyspec/core/rational/rational_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Rational" do
+ it "includes Comparable" do
+ Rational.include?(Comparable).should == true
+ end
+end
diff --git a/spec/rubyspec/core/rational/rationalize_spec.rb b/spec/rubyspec/core/rational/rationalize_spec.rb
new file mode 100644
index 0000000000..053e245cff
--- /dev/null
+++ b/spec/rubyspec/core/rational/rationalize_spec.rb
@@ -0,0 +1,36 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe "Rational#rationalize" do
+ it "returns self with no argument" do
+ Rational(12,3).rationalize.should == Rational(12,3)
+ Rational(-45,7).rationalize.should == Rational(-45,7)
+ end
+
+ # FIXME: These specs need reviewing by somebody familiar with the
+ # algorithm used by #rationalize
+ it "simplifies self to the degree specified by a Rational argument" do
+ r = Rational(5404319552844595,18014398509481984)
+ r.rationalize(Rational(1,10)).should == Rational(1,3)
+ r.rationalize(Rational(-1,10)).should == Rational(1,3)
+
+ r = Rational(-5404319552844595,18014398509481984)
+ r.rationalize(Rational(1,10)).should == Rational(-1,3)
+ r.rationalize(Rational(-1,10)).should == Rational(-1,3)
+
+ end
+
+ it "simplifies self to the degree specified by a Float argument" do
+ r = Rational(5404319552844595,18014398509481984)
+ r.rationalize(0.05).should == Rational(1,3)
+ r.rationalize(0.001).should == Rational(3, 10)
+
+ r = Rational(-5404319552844595,18014398509481984)
+ r.rationalize(0.05).should == Rational(-1,3)
+ r.rationalize(0.001).should == Rational(-3,10)
+ end
+
+ it "raises ArgumentError when passed more than one argument" do
+ lambda { Rational(1,1).rationalize(0.1, 0.1) }.should raise_error(ArgumentError)
+ lambda { Rational(1,1).rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError)
+ end
+end
diff --git a/spec/rubyspec/core/rational/remainder_spec.rb b/spec/rubyspec/core/rational/remainder_spec.rb
new file mode 100644
index 0000000000..1de276548f
--- /dev/null
+++ b/spec/rubyspec/core/rational/remainder_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/remainder', __FILE__)
+
+describe "Rational#remainder" do
+ it_behaves_like(:rational_remainder, :remainder)
+end
diff --git a/spec/rubyspec/core/rational/round_spec.rb b/spec/rubyspec/core/rational/round_spec.rb
new file mode 100644
index 0000000000..2b432f3234
--- /dev/null
+++ b/spec/rubyspec/core/rational/round_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/round', __FILE__)
+
+describe "Rational#round" do
+ it_behaves_like(:rational_round, :round)
+end
diff --git a/spec/rubyspec/core/rational/to_f_spec.rb b/spec/rubyspec/core/rational/to_f_spec.rb
new file mode 100644
index 0000000000..92ff2444b4
--- /dev/null
+++ b/spec/rubyspec/core/rational/to_f_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/to_f', __FILE__)
+
+describe "Rational#to_f" do
+ it_behaves_like(:rational_to_f, :to_f)
+end
diff --git a/spec/rubyspec/core/rational/to_i_spec.rb b/spec/rubyspec/core/rational/to_i_spec.rb
new file mode 100644
index 0000000000..fda6b06e96
--- /dev/null
+++ b/spec/rubyspec/core/rational/to_i_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/to_i', __FILE__)
+
+describe "Rational#to_i" do
+ it_behaves_like(:rational_to_i, :to_i)
+end
diff --git a/spec/rubyspec/core/rational/to_r_spec.rb b/spec/rubyspec/core/rational/to_r_spec.rb
new file mode 100644
index 0000000000..22c8b4532c
--- /dev/null
+++ b/spec/rubyspec/core/rational/to_r_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path('../../../shared/rational/to_r', __FILE__)
+
+describe "Rational#to_r" do
+ it_behaves_like(:rational_to_r, :to_r)
+
+ it "raises TypeError trying to convert BasicObject" do
+ obj = BasicObject.new
+ lambda { Rational(obj) }.should raise_error(TypeError)
+ end
+end
diff --git a/spec/rubyspec/core/rational/to_s_spec.rb b/spec/rubyspec/core/rational/to_s_spec.rb
new file mode 100644
index 0000000000..7d1702b8fa
--- /dev/null
+++ b/spec/rubyspec/core/rational/to_s_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/to_s', __FILE__)
+
+describe "Rational#to_s" do
+ it_behaves_like(:rational_to_s, :to_s)
+end
diff --git a/spec/rubyspec/core/rational/truncate_spec.rb b/spec/rubyspec/core/rational/truncate_spec.rb
new file mode 100644
index 0000000000..000db73357
--- /dev/null
+++ b/spec/rubyspec/core/rational/truncate_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path('../../../shared/rational/truncate', __FILE__)
+
+describe "Rational#truncate" do
+ it_behaves_like(:rational_truncate, :truncate)
+end
diff --git a/spec/rubyspec/core/rational/zero_spec.rb b/spec/rubyspec/core/rational/zero_spec.rb
new file mode 100644
index 0000000000..e6dd751922
--- /dev/null
+++ b/spec/rubyspec/core/rational/zero_spec.rb
@@ -0,0 +1,13 @@
+describe "Rational#zero?" do
+ it "returns true if the numerator is 0" do
+ Rational(0,26).zero?.should be_true
+ end
+
+ it "returns true if the numerator is 0.0" do
+ Rational(0.0,26).zero?.should be_true
+ end
+
+ it "returns false if the numerator isn't 0" do
+ Rational(26).zero?.should be_false
+ end
+end