summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal/round_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/bigdecimal/round_spec.rb')
-rw-r--r--spec/ruby/library/bigdecimal/round_spec.rb242
1 files changed, 137 insertions, 105 deletions
diff --git a/spec/ruby/library/bigdecimal/round_spec.rb b/spec/ruby/library/bigdecimal/round_spec.rb
index 07b96acb2b..467e2c5563 100644
--- a/spec/ruby/library/bigdecimal/round_spec.rb
+++ b/spec/ruby/library/bigdecimal/round_spec.rb
@@ -62,129 +62,157 @@ describe "BigDecimal#round" do
@n2_49.round(0).should == @neg_two
end
- describe "BigDecimal::ROUND_UP" do
- it "rounds values away from zero" do
- @p1_50.round(0, BigDecimal::ROUND_UP).should == @two
- @p1_51.round(0, BigDecimal::ROUND_UP).should == @two
- @p1_49.round(0, BigDecimal::ROUND_UP).should == @two
- @n1_50.round(0, BigDecimal::ROUND_UP).should == @neg_two
- @n1_51.round(0, BigDecimal::ROUND_UP).should == @neg_two
- @n1_49.round(0, BigDecimal::ROUND_UP).should == @neg_two
-
- @p2_50.round(0, BigDecimal::ROUND_UP).should == @three
- @p2_51.round(0, BigDecimal::ROUND_UP).should == @three
- @p2_49.round(0, BigDecimal::ROUND_UP).should == @three
- @n2_50.round(0, BigDecimal::ROUND_UP).should == @neg_three
- @n2_51.round(0, BigDecimal::ROUND_UP).should == @neg_three
- @n2_49.round(0, BigDecimal::ROUND_UP).should == @neg_three
+ ["BigDecimal::ROUND_UP", ":up"].each do |way|
+ describe way do
+ it "rounds values away from zero" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @two
+ @p1_51.round(0, mode).should == @two
+ @p1_49.round(0, mode).should == @two
+ @n1_50.round(0, mode).should == @neg_two
+ @n1_51.round(0, mode).should == @neg_two
+ @n1_49.round(0, mode).should == @neg_two
+
+ @p2_50.round(0, mode).should == @three
+ @p2_51.round(0, mode).should == @three
+ @p2_49.round(0, mode).should == @three
+ @n2_50.round(0, mode).should == @neg_three
+ @n2_51.round(0, mode).should == @neg_three
+ @n2_49.round(0, mode).should == @neg_three
+ end
end
end
- describe "BigDecimal::ROUND_DOWN" do
- it "rounds values towards zero" do
- @p1_50.round(0, BigDecimal::ROUND_DOWN).should == @one
- @p1_51.round(0, BigDecimal::ROUND_DOWN).should == @one
- @p1_49.round(0, BigDecimal::ROUND_DOWN).should == @one
- @n1_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
- @n1_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
- @n1_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_one
-
- @p2_50.round(0, BigDecimal::ROUND_DOWN).should == @two
- @p2_51.round(0, BigDecimal::ROUND_DOWN).should == @two
- @p2_49.round(0, BigDecimal::ROUND_DOWN).should == @two
- @n2_50.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
- @n2_51.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
- @n2_49.round(0, BigDecimal::ROUND_DOWN).should == @neg_two
+ ["BigDecimal::ROUND_DOWN", ":down", ":truncate"].each do |way|
+ describe way do
+ it "rounds values towards zero" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @one
+ @p1_51.round(0, mode).should == @one
+ @p1_49.round(0, mode).should == @one
+ @n1_50.round(0, mode).should == @neg_one
+ @n1_51.round(0, mode).should == @neg_one
+ @n1_49.round(0, mode).should == @neg_one
+
+ @p2_50.round(0, mode).should == @two
+ @p2_51.round(0, mode).should == @two
+ @p2_49.round(0, mode).should == @two
+ @n2_50.round(0, mode).should == @neg_two
+ @n2_51.round(0, mode).should == @neg_two
+ @n2_49.round(0, mode).should == @neg_two
+ end
end
end
- describe "BigDecimal::ROUND_HALF_UP" do
- it "rounds values >= 5 up, otherwise down" do
- @p1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @two
- @p1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @two
- @p1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @one
- @n1_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
- @n1_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
- @n1_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_one
-
- @p2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @three
- @p2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @three
- @p2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @two
- @n2_50.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
- @n2_51.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_three
- @n2_49.round(0, BigDecimal::ROUND_HALF_UP).should == @neg_two
+ ["BigDecimal::ROUND_HALF_UP", ":half_up", ":default"].each do |way|
+ describe way do
+ it "rounds values >= 5 up, otherwise down" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @two
+ @p1_51.round(0, mode).should == @two
+ @p1_49.round(0, mode).should == @one
+ @n1_50.round(0, mode).should == @neg_two
+ @n1_51.round(0, mode).should == @neg_two
+ @n1_49.round(0, mode).should == @neg_one
+
+ @p2_50.round(0, mode).should == @three
+ @p2_51.round(0, mode).should == @three
+ @p2_49.round(0, mode).should == @two
+ @n2_50.round(0, mode).should == @neg_three
+ @n2_51.round(0, mode).should == @neg_three
+ @n2_49.round(0, mode).should == @neg_two
+ end
end
end
- describe "BigDecimal::ROUND_HALF_DOWN" do
- it "rounds values > 5 up, otherwise down" do
- @p1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
- @p1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
- @p1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @one
- @n1_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
- @n1_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
- @n1_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_one
-
- @p2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
- @p2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @three
- @p2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @two
- @n2_50.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
- @n2_51.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_three
- @n2_49.round(0, BigDecimal::ROUND_HALF_DOWN).should == @neg_two
+ ["BigDecimal::ROUND_HALF_DOWN", ":half_down"].each do |way|
+ describe way do
+ it "rounds values > 5 up, otherwise down" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @one
+ @p1_51.round(0, mode).should == @two
+ @p1_49.round(0, mode).should == @one
+ @n1_50.round(0, mode).should == @neg_one
+ @n1_51.round(0, mode).should == @neg_two
+ @n1_49.round(0, mode).should == @neg_one
+
+ @p2_50.round(0, mode).should == @two
+ @p2_51.round(0, mode).should == @three
+ @p2_49.round(0, mode).should == @two
+ @n2_50.round(0, mode).should == @neg_two
+ @n2_51.round(0, mode).should == @neg_three
+ @n2_49.round(0, mode).should == @neg_two
+ end
end
end
- describe "BigDecimal::ROUND_CEILING" do
- it "rounds values towards +infinity" do
- @p1_50.round(0, BigDecimal::ROUND_CEILING).should == @two
- @p1_51.round(0, BigDecimal::ROUND_CEILING).should == @two
- @p1_49.round(0, BigDecimal::ROUND_CEILING).should == @two
- @n1_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
- @n1_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
- @n1_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_one
-
- @p2_50.round(0, BigDecimal::ROUND_CEILING).should == @three
- @p2_51.round(0, BigDecimal::ROUND_CEILING).should == @three
- @p2_49.round(0, BigDecimal::ROUND_CEILING).should == @three
- @n2_50.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
- @n2_51.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
- @n2_49.round(0, BigDecimal::ROUND_CEILING).should == @neg_two
+ ["BigDecimal::ROUND_CEILING", ":ceiling", ":ceil"].each do |way|
+ describe way do
+ it "rounds values towards +infinity" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @two
+ @p1_51.round(0, mode).should == @two
+ @p1_49.round(0, mode).should == @two
+ @n1_50.round(0, mode).should == @neg_one
+ @n1_51.round(0, mode).should == @neg_one
+ @n1_49.round(0, mode).should == @neg_one
+
+ @p2_50.round(0, mode).should == @three
+ @p2_51.round(0, mode).should == @three
+ @p2_49.round(0, mode).should == @three
+ @n2_50.round(0, mode).should == @neg_two
+ @n2_51.round(0, mode).should == @neg_two
+ @n2_49.round(0, mode).should == @neg_two
+ end
end
end
- describe "BigDecimal::ROUND_FLOOR" do
- it "rounds values towards -infinity" do
- @p1_50.round(0, BigDecimal::ROUND_FLOOR).should == @one
- @p1_51.round(0, BigDecimal::ROUND_FLOOR).should == @one
- @p1_49.round(0, BigDecimal::ROUND_FLOOR).should == @one
- @n1_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
- @n1_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
- @n1_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_two
-
- @p2_50.round(0, BigDecimal::ROUND_FLOOR).should == @two
- @p2_51.round(0, BigDecimal::ROUND_FLOOR).should == @two
- @p2_49.round(0, BigDecimal::ROUND_FLOOR).should == @two
- @n2_50.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
- @n2_51.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
- @n2_49.round(0, BigDecimal::ROUND_FLOOR).should == @neg_three
+ ["BigDecimal::ROUND_FLOOR", ":floor"].each do |way|
+ describe way do
+ it "rounds values towards -infinity" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @one
+ @p1_51.round(0, mode).should == @one
+ @p1_49.round(0, mode).should == @one
+ @n1_50.round(0, mode).should == @neg_two
+ @n1_51.round(0, mode).should == @neg_two
+ @n1_49.round(0, mode).should == @neg_two
+
+ @p2_50.round(0, mode).should == @two
+ @p2_51.round(0, mode).should == @two
+ @p2_49.round(0, mode).should == @two
+ @n2_50.round(0, mode).should == @neg_three
+ @n2_51.round(0, mode).should == @neg_three
+ @n2_49.round(0, mode).should == @neg_three
+ end
end
end
- describe "BigDecimal::ROUND_HALF_EVEN" do
- it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do
- @p1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
- @p1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
- @p1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @one
- @n1_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
- @n1_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
- @n1_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_one
-
- @p2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
- @p2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @three
- @p2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @two
- @n2_50.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
- @n2_51.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_three
- @n2_49.round(0, BigDecimal::ROUND_HALF_EVEN).should == @neg_two
+ ["BigDecimal::ROUND_HALF_EVEN", ":half_even", ":banker"].each do |way|
+ describe way do
+ it "rounds values > 5 up, < 5 down and == 5 towards even neighbor" do
+ mode = eval(way)
+
+ @p1_50.round(0, mode).should == @two
+ @p1_51.round(0, mode).should == @two
+ @p1_49.round(0, mode).should == @one
+ @n1_50.round(0, mode).should == @neg_two
+ @n1_51.round(0, mode).should == @neg_two
+ @n1_49.round(0, mode).should == @neg_one
+
+ @p2_50.round(0, mode).should == @two
+ @p2_51.round(0, mode).should == @three
+ @p2_49.round(0, mode).should == @two
+ @n2_50.round(0, mode).should == @neg_two
+ @n2_51.round(0, mode).should == @neg_three
+ @n2_49.round(0, mode).should == @neg_two
+ end
end
end
@@ -199,4 +227,8 @@ describe "BigDecimal#round" do
lambda { BigDecimal('Infinity').round(2) }.should_not raise_error(FloatDomainError)
lambda { BigDecimal('-Infinity').round(2) }.should_not raise_error(FloatDomainError)
end
+
+ it "raise for a non-existent round mode" do
+ lambda { @p1_50.round(0, :nonsense) }.should raise_error(ArgumentError, "invalid rounding mode")
+ end
end