mirror of
https://github.com/ruby/ruby.git
synced 2025-09-22 03:53:58 +02:00
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
This commit is contained in:
parent
74495cfa11
commit
0dd9c302d9
3 changed files with 94 additions and 5 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue