spec/format_spec.rb in asciipack-0.1.3 vs spec/format_spec.rb in asciipack-0.2.0

- old
+ new

@@ -62,43 +62,45 @@ it "float 64" do format 1/3.0, T.float64, 17 format 1.0, T.float64, 17 format 0.1, T.float64, 17 format -0.1, T.float64, 17 - expect(AsciiPack.pack(-0.1)).to eq(T.float64 + 'bfb999999999999a') - expect(AsciiPack.pack(1.0)).to eq(T.float64 + '3ff0000000000000') - expect(AsciiPack.pack(1.0000000000000002)).to eq(T.float64 + '3ff0000000000001') - expect(AsciiPack.pack(1.0000000000000004)).to eq(T.float64 + '3ff0000000000002') - expect(AsciiPack.pack(1/3.0)).to eq(T.float64 + '3fd5555555555555') - expect(AsciiPack.pack(Float::NAN)).to eq(T.float64 + '7fffffffffffffff') - expect(AsciiPack.pack(1 / 0.0)).to eq(T.float64 + '7ff0000000000000') - expect(AsciiPack.pack(-1 / 0.0)).to eq(T.float64 + 'fff0000000000000') + check_each_other(-0.1, T.float64 + 'bfb999999999999a') + check_each_other(1.0, T.float64 + '3ff0000000000000') + check_each_other(1.0000000000000002, T.float64 + '3ff0000000000001') + check_each_other(1.0000000000000004, T.float64 + '3ff0000000000002') + check_each_other(1/3.0, T.float64 + '3fd5555555555555') + expect(AsciiPack.pack(Float::NAN)).to eq(T.float64 + '7ff8000000000000') + check_each_other(1 / 0.0, T.float64 + '7ff0000000000000') + check_each_other(-1 / 0.0, T.float64 + 'fff0000000000000') end - it "fixbin" do - format "", T.fixbin_0, 1 - format " ", T.fixbin_1, 2 - format "あ", T.fixbin_1, 2 - format "漢字", T.fixbin_2, 3 - format " " * 0xe, T.fixbin_E, 15 - format " " * 0xf, T.fixbin_F, 16 + it "fixstr" do + format "", T.fixstr_0, 1 + format " ", T.fixstr_1, 2 + format "あ", T.fixstr_3, 4 + format "漢字", T.fixstr_6, 7 + format " " * 0xe, T.fixstr_E, 15 + format " " * 0xf, T.fixstr_F, 16 end - it "bin 8" do - format "a" * 0x10, T.bin8, 3 + 0x10 - format "a" * 0xff, T.bin8, 3 + 0xff + it "str 8" do + format "a" * 0x10, T.str8, 3 + 0x10 + format "a" * 0xff, T.str8, 3 + 0xff end - it "bin 16" do - format "a" * 0x100, T.bin16, 5 + 0x100 - format "a" * 0xffff, T.bin16, 5 + 0xffff + it "str 16" do + 100.times { + format "a" * 0x100, T.str16, 5 + 0x100 + format "a" * 0xffff, T.str16, 5 + 0xffff + } end - it "bin 32" do - format "a" * 0x10000, T.bin32, 9 + 0x10000 + it "str 32" do + format "a" * 0x10000, T.str32, 9 + 0x10000 # FIXME too late - # format "a" * 0xffffffff, T.bin32, 9 + 0xffffffff + # format "a" * 0xffffffff, T.str32, 9 + 0xffffffff end it "map 4" do format_map 0, T.map4 format_map 0xf, T.map4 @@ -139,10 +141,19 @@ format_array 0x10000, T.array32 # FIXME too late # format_array 0xffffffff, T.array32 end + it "recursive array" do + array = [0] + 1000.times { + array = [array] + } + ap = AsciiPack.pack(array) + expect(AsciiPack.unpack(ap)).to eq(array) + end + it "nil" do format nil, T.nil, 1 end it "false" do @@ -156,9 +167,14 @@ def format (object, first, length) ap = AsciiPack.pack(object) expect(ap[0]).to eq(first) expect(ap.length).to eq(length) + expect(AsciiPack.unpack(ap)).to eq(object) +end + +def check_each_other (object, ap) + expect(AsciiPack.pack(object)).to eq(ap) expect(AsciiPack.unpack(ap)).to eq(object) end def format_map (count, first) map = {}