test/bits_test.rb in bindata-1.8.3 vs test/bits_test.rb in bindata-2.0.0

- old
+ new

@@ -3,62 +3,70 @@ require File.expand_path(File.join(File.dirname(__FILE__), "common")) module AllBitfields def test_has_a_sensible_value_of_zero - all_objects do |obj, nbits| - obj.must_equal 0 + all_classes do |bit_class| + bit_class.new.must_equal 0 end end def test_avoids_underflow - all_objects do |obj, nbits| + all_classes do |bit_class| + obj = bit_class.new + obj.assign(min_value - 1) obj.must_equal min_value end end def test_avoids_overflow - all_objects do |obj, nbits| + all_classes do |bit_class| + obj = bit_class.new + obj.assign(max_value + 1) obj.must_equal max_value end end def test_assign_values - all_objects do |obj, nbits| + all_classes do |bit_class| some_values_within_range.each do |val| + obj = bit_class.new obj.assign(val) + obj.must_equal val end end end def test_assign_values_from_other_bit_objects - all_objects do |obj, nbits| + all_classes do |bit_class| some_values_within_range.each do |val| - obj.assign(obj.new(val)) + obj = bit_class.new + obj.assign(bit_class.new(val)) + obj.must_equal val end end end def test_symmetrically_read_and_write - all_objects do |obj, nbits| + all_classes do |bit_class| some_values_within_range.each do |val| + obj = bit_class.new obj.assign(val) - other = obj.new - other.read(obj.to_binary_s) - other.must_equal obj + + obj.value_read_from_written.must_equal obj end end end - def all_objects(&block) - @bits.each do |obj, nbits| + def all_classes(&block) + @bits.each_pair do |bit_class, nbits| @nbits = nbits - yield obj, nbits + yield bit_class end end def min_value if @signed @@ -88,33 +96,25 @@ (min_value .. max_value).include?(val) end end def generate_bit_classes_to_test(endian, signed) - bits = [] + bits = {} if signed base = "Sbit" start = 2 else base = "Bit" start = 1 end - (start .. 64).each do |nbits| + (start .. 50).each do |nbits| name = "#{base}#{nbits}" name << "le" if endian == :little - obj = BinData.const_get(name).new - bits << [obj, nbits] + bit_class = BinData.const_get(name) + bits[bit_class] = nbits end - - (start .. 64).each do |nbits| - name = "#{base}" - name << "Le" if endian == :little - obj = BinData.const_get(name).new(:nbits => nbits) - bits << [obj, nbits] - end - bits end describe "Unsigned big endian bitfields" do include AllBitfields @@ -123,16 +123,15 @@ @signed = false @bits = generate_bit_classes_to_test(:big, @signed) end it "read big endian values" do - all_objects do |obj, nbits| + @bits.each_pair do |bit_class, nbits| nbytes = (nbits + 7) / 8 str = [0b1000_0000].pack("C") + "\000" * (nbytes - 1) - obj.read(str) - obj.must_equal 1 << (nbits - 1) + bit_class.read(str).must_equal 1 << (nbits - 1) end end end describe "Signed big endian bitfields" do @@ -142,16 +141,15 @@ @signed = true @bits = generate_bit_classes_to_test(:big, @signed) end it "read big endian values" do - all_objects do |obj, nbits| + @bits.each_pair do |bit_class, nbits| nbytes = (nbits + 7) / 8 str = [0b0100_0000].pack("C") + "\000" * (nbytes - 1) - obj.read(str) - obj.must_equal 1 << (nbits - 2) + bit_class.read(str).must_equal 1 << (nbits - 2) end end end describe "Unsigned little endian bitfields" do @@ -161,16 +159,15 @@ @signed = false @bits = generate_bit_classes_to_test(:little, @signed) end it "read little endian values" do - all_objects do |obj, nbits| + @bits.each_pair do |bit_class, nbits| nbytes = (nbits + 7) / 8 str = [0b0000_0001].pack("C") + "\000" * (nbytes - 1) - obj.read(str) - obj.must_equal 1 + bit_class.read(str).must_equal 1 end end end describe "Signed little endian bitfields" do @@ -180,15 +177,14 @@ @signed = true @bits = generate_bit_classes_to_test(:little, @signed) end it "read little endian values" do - all_objects do |obj, nbits| + @bits.each_pair do |bit_class, nbits| nbytes = (nbits + 7) / 8 str = [0b0000_0001].pack("C") + "\000" * (nbytes - 1) - obj.read(str) - obj.must_equal 1 + bit_class.read(str).must_equal 1 end end end describe "Bits of size 1" do