diff --git a/lib/packer.rb b/lib/packer.rb index b4f3106..e2f61ab 100644 --- a/lib/packer.rb +++ b/lib/packer.rb @@ -46,7 +46,6 @@ class Packer end def self.pack_big_int(sign, num) - num += 1 if sign == '1' num_bits = num.to_s(2) first = "1#{sign}#{num_bits[-6..]}" diff --git a/spec/04_packer_spec.rb b/spec/04_packer_spec.rb index 31a7b46..4ca1ef4 100644 --- a/spec/04_packer_spec.rb +++ b/spec/04_packer_spec.rb @@ -48,11 +48,19 @@ describe 'Packer', :packer do end it 'Should pack negative' do - expect(Packer.pack_int(-65).map { |e| e.to_s(2).rjust(8, '0') }).to eq(%w[11000001 00000001]) + expect(Packer.pack_int(-65).map { |e| e.to_s(2).rjust(8, '0') }).to eq(%w[11000000 00000001]) end it 'Should pack large numbers' do expect(Packer.pack_int(99_999_999_999_999_999)).to eq([191, 131, 255, 147, 246, 194, 215, 232, 88]) end + + it 'Should pack -128 to 1111 1111 0000 0001 (match tw traffic)' do + expect(Packer.pack_int(-128).map { |b| b.to_s(2).rjust(8, '0') }).to eq(%w[11111111 00000001]) + end + + it 'Should pack -128 to 0xFF 0x01 (match tw traffic)' do + expect(Packer.pack_int(-128)).to eq([0xFF, 0x01]) + end end end diff --git a/spec/05_unpacker_spec.rb b/spec/05_unpacker_spec.rb index 5b3f9ae..e282b52 100644 --- a/spec/05_unpacker_spec.rb +++ b/spec/05_unpacker_spec.rb @@ -44,7 +44,7 @@ describe 'Unpacker', :unpacker do end it 'Should pack and unpack and match from 0 to 63' do - (0..63).each do |i| + 64.times do |i| u = Unpacker.new(Packer.pack_int(i)) expect(u.get_int).to eq(i) end @@ -74,14 +74,14 @@ describe 'Unpacker', :unpacker do it 'Should pack and unpack and match from -3 to 3' do (-3..3).each do |i| u = Unpacker.new(Packer.pack_int(i)) - expect(u.get_int()).to eq(i) + expect(u.get_int).to eq(i) end end it 'Should pack and unpack and match from -63 to 63' do (-63..63).each do |i| u = Unpacker.new(Packer.pack_int(i)) - expect(u.get_int()).to eq(i) + expect(u.get_int).to eq(i) end end @@ -93,18 +93,23 @@ describe 'Unpacker', :unpacker do context 'Unpack multi byte integers' do it 'Should pack and unpack and match from 0 to 128' do - (0..128).each do |i| + 129.times do |i| u = Unpacker.new(Packer.pack_int(i)) expect(u.get_int).to eq(i) end end - # it 'Should pack and unpack and match from -128 to 128' do - # (-128..128).each do |i| - # u = Unpacker.new(Packer.pack_int(i)) - # expect(u.get_int()).to eq(i) - # end - # end + it 'Should unpack 0xFF 0x01 to -128 (match tw traffic)' do + u = Unpacker.new([0xFF, 0x01]) + expect(u.get_int).to eq(-128) + end + + it 'Should pack and unpack and match from -128 to 128' do + (-128..128).each do |i| + u = Unpacker.new(Packer.pack_int(i)) + expect(u.get_int).to eq(i) + end + end it 'Should unpack [128, 1] to 64' do u = Unpacker.new([128, 1])