Skip to content

Commit 75242d4

Browse files
committed
at last, do BN comparison == vs eql? properly - just like MRI version
1 parent acbfa26 commit 75242d4

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/main/java/org/jruby/ext/openssl/BN.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ static BN newInstance(final Ruby runtime, BigInteger value) {
8989
return new BN(runtime, value != null ? value : BigInteger.ZERO);
9090
}
9191

92-
//static BN newInstance(final Ruby runtime, long value) {
93-
// return new BN(runtime, BigInteger.valueOf(value));
94-
//}
95-
9692
public static void createBN(final Ruby runtime, final RubyModule OpenSSL) {
9793
final RubyClass OpenSSLError = OpenSSL.getClass("OpenSSLError");
9894
OpenSSL.defineClassUnder("BNError", OpenSSLError, OpenSSLError.getAllocator());
@@ -164,7 +160,7 @@ public IRubyObject initialize(final ThreadContext context,
164160
}
165161

166162
@Override
167-
public synchronized IRubyObject initialize_copy(final IRubyObject that) {
163+
public IRubyObject initialize_copy(final IRubyObject that) {
168164
super.initialize_copy(that);
169165
if ( this != that ) this.value = ((BN) that).value;
170166
return this;
@@ -342,8 +338,18 @@ public IRubyObject ucmp(final ThreadContext context, IRubyObject other) {
342338
return context.runtime.newFixnum( value.abs().compareTo( asBigInteger(other).abs() ) );
343339
}
344340

345-
@JRubyMethod(name={"eql?", "==", "==="})
346-
public RubyBoolean eql_p(final ThreadContext context, IRubyObject other) {
341+
@JRubyMethod(name = "eql?")
342+
public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
343+
return context.runtime.newBoolean(eql(other));
344+
}
345+
346+
@Override
347+
public boolean eql(IRubyObject other) {
348+
return equals(other);
349+
}
350+
351+
@JRubyMethod(name = "==")
352+
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
347353
return context.runtime.newBoolean( value.equals( asBigInteger(other) ) );
348354
}
349355

src/test/ruby/test_bn.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ def test_to_bn
5252
assert_equal bn, 1234567890123456789012345678901234567890.to_bn
5353
end
5454

55+
def test_comparison
56+
e1 = OpenSSL::BN.new(999.to_s(16), 16)
57+
e3 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
58+
assert_equal(false, e1 == nil)
59+
assert_equal(false, e1 == -999)
60+
assert_equal(true, e1 == 999)
61+
assert_equal(true, e1 == 999.to_bn)
62+
assert_equal(false, e1.eql?(nil))
63+
assert_equal(false, e1.eql?(999))
64+
assert_equal(true, e1.eql?(999.to_bn))
65+
assert_equal(e1.hash, 999.to_bn.hash)
66+
assert_not_equal(e1.hash, e3.hash)
67+
assert_equal(0, e1.cmp(999))
68+
assert_equal(1, e1.cmp(-999))
69+
assert_equal(0, e1.ucmp(999))
70+
assert_equal(0, e1.ucmp(-999))
71+
assert_instance_of(String, e1.hash.to_s)
72+
end
73+
5574
def test_to_java
5675
assert_equal java.lang.Integer.new(42), OpenSSL::BN.new('42').to_java(:int)
5776
assert_equal java.math.BigInteger.valueOf(24), OpenSSL::BN.new('24').to_java

0 commit comments

Comments
 (0)