Skip to content

Commit 7ecca26

Browse files
aguspenobu
authored andcommitted
[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.
1 parent f393180 commit 7ecca26

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

test/ruby/test_string.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,32 @@ def test_bytes
998998
assert_equal [65, 66, 67], res
999999
end
10001000

1001+
def test_getbyte
1002+
s = S('foo')
1003+
assert_equal(102, s.getbyte(0))
1004+
assert_equal(111, s.getbyte(2))
1005+
assert_equal(102, s.getbyte(-3))
1006+
assert_nil(s.getbyte(3))
1007+
assert_nil(s.getbyte(-4))
1008+
assert_nil(S('').getbyte(0))
1009+
assert_nil(S('').getbyte(-1))
1010+
end
1011+
1012+
def test_setbyte
1013+
s = S('xyzzy')
1014+
assert_equal(129, s.setbyte(2, 129))
1015+
assert_equal(S("xy\x81zy").force_encoding(s.encoding), s)
1016+
1017+
s = S('foo')
1018+
s.setbyte(-3, 98)
1019+
assert_equal(S('boo').force_encoding(s.encoding), s)
1020+
1021+
assert_raise(IndexError) { S('foo').setbyte(3, 0) }
1022+
assert_raise(IndexError) { S('foo').setbyte(-4, 0) }
1023+
1024+
assert_raise(FrozenError) { S('foo').freeze.setbyte(0, 0x61) }
1025+
end
1026+
10011027
def test_each_codepoint
10021028
# Single byte optimization
10031029
assert_equal 65, S("ABC").each_codepoint.next

0 commit comments

Comments
 (0)