diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-12-01 13:08:20 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-12-01 13:08:20 +0000 |
commit | 0dd9c302d9374f52abb09662b554e4ee95011444 (patch) | |
tree | 58a26d5d86ba98caa360b4d319e5092f6a5ba2b1 /test | |
parent | 74495cfa11743a9cd1b27da81968cfbc00538bc4 (diff) |
Supports `buffer` and `offset` in `Array#pack`
* pack.c (pack_pack): Supports `buffer` and `offset` in `Array#pack`.
[Feature #12754] [ruby-dev:49798]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56957 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_pack.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index c5af6f2152..415dc78e60 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -812,4 +812,38 @@ EXPECTED assert_raise_with_message(ArgumentError, /too few/) {ary.pack("AA")} end; end + + def test_pack_with_buffer + buf = String.new(capacity: 100) + + assert_raise_with_message(ArgumentError, /without buffer/) { + [0xDEAD_BEEF].pack('N', offset: 10) + } + assert_raise_with_message(ArgumentError, /too small/) { + [0xDEAD_BEEF].pack('N', buffer: buf, offset: 200) + } + assert_raise_with_message(RuntimeError, /frozen/) { + [0xDEAD_BEEF].pack('N', buffer: 'foo'.freeze) + } + assert_raise_with_message(TypeError, /into Integer/) { + [0xDEAD_BEEF].pack('N', buffer: buf, offset: '10') + } + assert_raise_with_message(TypeError, /must be String/) { + [0xDEAD_BEEF].pack('N', buffer: Object.new) + } + + addr = [buf].pack('p') + + [0xDEAD_BEEF].pack('N', buffer: buf) + assert_equal "\xDE\xAD\xBE\xEF", buf + + [0xBABE_F00D].pack('N', buffer: buf, offset: 4) + assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D", buf + assert_equal addr, [buf].pack('p') + + [0xBAAD_FACE].pack('N', buffer: buf, offset: 10) + assert_equal "\xDE\xAD\xBE\xEF\xBA\xBE\xF0\x0D\0\0\xBA\xAD\xFA\xCE", buf + + assert_equal addr, [buf].pack('p') + end end |