From e5319dc9856298f38aa9cdc6ed55e39ad0e8e070 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 18 Oct 2021 16:23:54 +0200 Subject: pack.c: add an offset argument to unpack and unpack1 [Feature #18254] This is useful to avoid repeteadly copying strings when parsing binary formats --- test/ruby/test_pack.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'test') diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index c9ca9c3dfd..af45adb2b2 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -869,4 +869,30 @@ EXPECTED assert_equal "hogefuga", "aG9nZWZ1Z2E=".unpack1("m") assert_equal "01000001", "A".unpack1("B*") end + + def test_unpack1_offset + assert_equal 65, "ZA".unpack1("C", offset: 1) + assert_equal "01000001", "YZA".unpack1("B*", offset: 2) + assert_nil "abc".unpack1("C", offset: 3) + assert_raise_with_message(ArgumentError, /offset can't be negative/) { + "a".unpack1("C", offset: -1) + } + assert_raise_with_message(ArgumentError, /offset outside of string/) { + "a".unpack1("C", offset: 2) + } + assert_nil "a".unpack1("C", offset: 1) + end + + def test_unpack_offset + assert_equal [65], "ZA".unpack("C", offset: 1) + assert_equal ["01000001"], "YZA".unpack("B*", offset: 2) + assert_equal [nil, nil, nil], "abc".unpack("CCC", offset: 3) + assert_raise_with_message(ArgumentError, /offset can't be negative/) { + "a".unpack("C", offset: -1) + } + assert_raise_with_message(ArgumentError, /offset outside of string/) { + "a".unpack("C", offset: 2) + } + assert_equal [nil], "a".unpack("C", offset: 1) + end end -- cgit v1.2.3