fixed the implementation of Bignum#to_ber.

This commit is contained in:
blackhedd 2006-12-15 18:29:26 +00:00
parent 0bf35f8d3e
commit 5f197cc040
3 changed files with 39 additions and 4 deletions

View file

@ -72,7 +72,7 @@ end
desc "(Provisional) Run tests for SNMP"
task :test_snmp do |t|
run_test_set t, ['tests/testsnmp.rb']
run_test_set t, ['tests/testsnmp.rb', 'tests/testber.rb']
end
spec = eval(File.read("net-ldap.gemspec"))

View file

@ -409,9 +409,22 @@ end # class Fixnum
class Bignum
def to_ber
i = [self].pack('w')
i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
[2, i.length].pack("CC") + i
#i = [self].pack('w')
#i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
#[2, i.length].pack("CC") + i
# Ruby represents Bignums as two's-complement numbers so we may actually be
# good as far as representing negatives goes.
# I'm sure this implementation can be improved performance-wise if necessary.
sz = self.size
out = "\000" * sz
(sz*8).times {|bit|
if self[bit] == 1
out[bit/8] += (1 << (bit % 8))
end
}
[2, sz].pack("CC") + out.reverse
end
end

View file

@ -23,6 +23,28 @@ class TestBer < Test::Unit::TestCase
assert_equal( "\002\005\222\320\227\344\000", 5000000000.to_ber )
end
def test_ber_bignums
# Some of these values are Fixnums and some are Bignums. Different BER code.
[
5,
50,
500,
5000,
50000,
500000,
5000000,
50000000,
500000000,
1000000000,
2000000000,
3000000000,
4000000000,
5000000000
].each {|val|
assert_equal( val, val.to_ber.read_ber )
}
end
def test_ber_parsing
assert_equal( 6, "\002\001\006".read_ber( Net::LDAP::AsnSyntax ))
assert_equal( "testing", "\004\007testing".read_ber( Net::LDAP::AsnSyntax ))