diff --git a/lib/packer.rb b/lib/packer.rb index 18a894b..47e05f2 100644 --- a/lib/packer.rb +++ b/lib/packer.rb @@ -158,65 +158,3 @@ def todo_make_this_rspec_test end # todo_make_this_rspec_test - -def todo_also_rspec_unpacker - # u = Unpacker.new([0x41, 0x41, 0x00, 0x42, 0x42, 0x00]) - # p u.get_string() == "AA" - # p u.get_string() == "BB" - # p u.get_string() == nil - - # u = Unpacker.new([0x01, 0x02, 0x41, 0x42]) - # p u.get_int() == 1 - # p u.get_int() == 2 - # p u.get_int() == -1 - # p u.get_int() == -2 - - # (-3..3).each do |i| - # u = Unpacker.new(Packer.pack_int(i)) - # p u.get_int() == i - # end - - # (-63..63).each do |i| - # u = Unpacker.new(Packer.pack_int(i)) - # p u.get_int() == i - # end - - # u = Unpacker.new([128, 1]) - # p u.get_int() == 64 - - # u = Unpacker.new([128, 1, 128, 1]) - # p u.get_int == 64 - # p u.get_int == 64 - # p u.get_int() == nil - - # (-128..128).each do |i| - # u = Unpacker.new(Packer.pack_int(i)) - # p u.get_int() == i - # end - - # u = Unpacker.new(['00000001'.to_i(2)]) - # p u.get_int == 1 - - # u = Unpacker.new(['10000000'.to_i(2), '00000001'.to_i(2)]) - # p u.get_int == 64 - - # should be 64 - # u = Unpacker.new(['10000000'.to_i(2), '00000001'.to_i(2)]) - # p u.get_int() - - # should be -64 - # u = Unpacker.new(['11000000'.to_i(2), '00000000'.to_i(2)]) - # p u.get_int() - - # u = Unpacker.new(['01000000'.to_i(2)]) - # p u.get_int() - - # p Packer.pack_int(-1).first.to_s(2) - - # num = '01000000'.to_i(2) - # p num == 64 - # u = Unpacker.new([num]) - # p u.get_int() -end - -todo_also_rspec_unpacker diff --git a/spec/04_unpacker_spec.rb b/spec/04_unpacker_spec.rb new file mode 100644 index 0000000..06e6312 --- /dev/null +++ b/spec/04_unpacker_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require_relative '../lib/packer' + +describe 'Unpacker', :unpacker do + context 'Unpack strings' do + it 'Should unpack multiple strings' do + u = Unpacker.new([0x41, 0x41, 0x00, 0x42, 0x42, 0x00]) + expect(u.get_string).to eq('AA') + expect(u.get_string).to eq('BB') + expect(u.get_string).to eq(nil) + end + end + + context 'Unpack single byte integers' do + it 'Should unpack positive integers' do + u = Unpacker.new([0x01, 0x02]) + expect(u.get_int).to eq(1) + expect(u.get_int).to eq(2) + end + + it 'Should unpack negative integers' do + u = Unpacker.new([0x41, 0x42]) + expect(u.get_int).to eq(-1) + expect(u.get_int).to eq(-2) + end + + it 'Should unpack positive and negative integers' do + u = Unpacker.new([0x01, 0x02, 0x41, 0x42]) + expect(u.get_int).to eq(1) + expect(u.get_int).to eq(2) + expect(u.get_int).to eq(-1) + expect(u.get_int).to eq(-2) + end + + it 'Should pack and unpack and match from 0 to 63' do + (0..63).each 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 -3 to 3' do + # (-3..3).each 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 -63 to 63' do + # (-63..63).each do |i| + # u = Unpacker.new(Packer.pack_int(i)) + # expect(u.get_int()).to eq(i) + # end + # end + + it 'Should unpack 0000 0001 to 1' do + u = Unpacker.new(['00000001'.to_i(2)]) + expect(u.get_int).to eq(1) + end + end + + context 'Unpack multi byte integers' do + it 'Should pack and unpack and match from 0 to 128' do + (0..128).each 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 [128, 1] to 64' do + u = Unpacker.new([128, 1]) + expect(u.get_int).to eq(64) + end + + it 'Should unpack [128, 1, 128, 1] to [64, 64]' do + u = Unpacker.new([128, 1, 128, 1]) + expect(u.get_int).to eq(64) + expect(u.get_int).to eq(64) + end + + it 'Should unpack 1000 0000 0000 0001 to 64' do + u = Unpacker.new(['10000000'.to_i(2), '00000001'.to_i(2)]) + expect(u.get_int).to eq(64) + end + + it 'Should unpack 1100 0001 0000 0001 to -65' do + u = Unpacker.new(['11000001'.to_i(2), '00000001'.to_i(2)]) + expect(u.get_int).to eq(-65) + end + end +end