Unpacker should be fully working now

Covered by good test coverage.
Parts of the tests are commented out because they depend on the
currently borken packer to work.
This commit is contained in:
ChillerDragon 2022-11-06 13:18:26 +01:00
parent cafbd79fe9
commit 11a7e9629d
2 changed files with 99 additions and 62 deletions

View file

@ -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

99
spec/04_unpacker_spec.rb Normal file
View file

@ -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