summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-05-17 06:01:47 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-05-17 06:01:47 +0000
commit932e916b9e340d3ae52bac2eb57567208dc21d4f (patch)
treec1ba22f01a708157a7ceef0725942ebce1f61ef3 /test/ruby
parent1fbf1f755218d96447cf1db8441b554aa95d655d (diff)
numeric.c: Numeric#positive? and Numeric#negative?
* numeric.c (num_positive_p, num_negative_p): add methods Numeric#positive? and Numeric#negative?. [ruby-core:69173] [Feature #11151] * numeric.c (flo_positive_p, flo_negative_p): specialiazed functions for Float. * complex.c (Init_Complex): Complex do not have positive? and negative? methods git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50522 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_fixnum.rb17
-rw-r--r--test/ruby/test_float.rb24
-rw-r--r--test/ruby/test_numeric.rb24
-rw-r--r--test/ruby/test_rational.rb10
4 files changed, 75 insertions, 0 deletions
diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb
index 8b2cf2e256..e10371d3e3 100644
--- a/test/ruby/test_fixnum.rb
+++ b/test/ruby/test_fixnum.rb
@@ -312,4 +312,21 @@ class TestFixnum < Test::Unit::TestCase
assert_equal(1, 5.remainder(4))
assert_predicate(4.remainder(Float::NAN), :nan?)
end
+
+ def test_zero_p
+ assert_predicate(0, :zero?)
+ assert_not_predicate(1, :zero?)
+ end
+
+ def test_positive_p
+ assert_predicate(1, :positive?)
+ assert_not_predicate(0, :positive?)
+ assert_not_predicate(-1, :positive?)
+ end
+
+ def test_negative_p
+ assert_predicate(-1, :negative?)
+ assert_not_predicate(0, :negative?)
+ assert_not_predicate(1, :negative?)
+ end
end
diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb
index eab4d12a29..f1e01fc249 100644
--- a/test/ruby/test_float.rb
+++ b/test/ruby/test_float.rb
@@ -356,6 +356,30 @@ class TestFloat < Test::Unit::TestCase
assert_not_predicate(1.0, :zero?)
end
+ def test_positive_p
+ assert_predicate(+1.0, :positive?)
+ assert_not_predicate(+0.0, :positive?)
+ assert_not_predicate(-0.0, :positive?)
+ assert_not_predicate(-1.0, :positive?)
+ assert_predicate(+(0.0.next_float), :positive?)
+ assert_not_predicate(-(0.0.next_float), :positive?)
+ assert_predicate(Float::INFINITY, :positive?)
+ assert_not_predicate(-Float::INFINITY, :positive?)
+ assert_not_predicate(Float::NAN, :positive?)
+ end
+
+ def test_negative_p
+ assert_predicate(-1.0, :negative?)
+ assert_not_predicate(-0.0, :negative?)
+ assert_not_predicate(+0.0, :negative?)
+ assert_not_predicate(+1.0, :negative?)
+ assert_predicate(-(0.0.next_float), :negative?)
+ assert_not_predicate(+(0.0.next_float), :negative?)
+ assert_predicate(-Float::INFINITY, :negative?)
+ assert_not_predicate(Float::INFINITY, :negative?)
+ assert_not_predicate(Float::NAN, :negative?)
+ end
+
def test_infinite_p
inf = Float::INFINITY
assert_equal(1, inf.infinite?)
diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb
index 5a7a4e3765..f539ccf731 100644
--- a/test/ruby/test_numeric.rb
+++ b/test/ruby/test_numeric.rb
@@ -146,6 +146,30 @@ class TestNumeric < Test::Unit::TestCase
assert_predicate(a, :zero?)
end
+ def test_positive_p
+ a = Class.new(Numeric) do
+ def >(x); true; end
+ end.new
+ assert_predicate(a, :positive?)
+
+ a = Class.new(Numeric) do
+ def >(x); false; end
+ end.new
+ assert_not_predicate(a, :positive?)
+ end
+
+ def test_negative_p
+ a = Class.new(Numeric) do
+ def <(x); true; end
+ end.new
+ assert_predicate(a, :negative?)
+
+ a = Class.new(Numeric) do
+ def <(x); false; end
+ end.new
+ assert_not_predicate(a, :negative?)
+ end
+
def test_to_int
a = Class.new(Numeric) do
def to_i; :ok; end
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index ae4a2c6dd6..1f32a49273 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -921,6 +921,16 @@ class Rational_Test < Test::Unit::TestCase
assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) }
end
+ def test_positive_p
+ assert_predicate(1/2r, :positive?)
+ assert_not_predicate(-1/2r, :positive?)
+ end
+
+ def test_negative_p
+ assert_predicate(-1/2r, :negative?)
+ assert_not_predicate(1/2r, :negative?)
+ end
+
def test_known_bug
end