From 7ecca26f8f38a3d6f8e16c8b06a2f193898a91dc Mon Sep 17 00:00:00 2001 From: Augustin Gottlieb <33221555+aguspe@users.noreply.github.com> Date: Sat, 18 Apr 2026 13:17:31 +0200 Subject: [PATCH] [Tests] Add test cases for String#getbyte and String#setbyte Cover behavior documented in rdoc but not asserted in test/ruby/test_string.rb: * getbyte: negative index, out-of-range (positive and negative), empty string. * setbyte: return value, negative index, out-of-range (positive and negative), frozen string raises FrozenError. --- test/ruby/test_string.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index f2aade2aa1a1ec..67a19e1ae06eb9 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -998,6 +998,32 @@ def test_bytes assert_equal [65, 66, 67], res end + def test_getbyte + s = S('foo') + assert_equal(102, s.getbyte(0)) + assert_equal(111, s.getbyte(2)) + assert_equal(102, s.getbyte(-3)) + assert_nil(s.getbyte(3)) + assert_nil(s.getbyte(-4)) + assert_nil(S('').getbyte(0)) + assert_nil(S('').getbyte(-1)) + end + + def test_setbyte + s = S('xyzzy') + assert_equal(129, s.setbyte(2, 129)) + assert_equal(S("xy\x81zy").force_encoding(s.encoding), s) + + s = S('foo') + s.setbyte(-3, 98) + assert_equal(S('boo').force_encoding(s.encoding), s) + + assert_raise(IndexError) { S('foo').setbyte(3, 0) } + assert_raise(IndexError) { S('foo').setbyte(-4, 0) } + + assert_raise(FrozenError) { S('foo').freeze.setbyte(0, 0x61) } + end + def test_each_codepoint # Single byte optimization assert_equal 65, S("ABC").each_codepoint.next