Skip to content

Commit b290379

Browse files
committed
tests/lapi: fix string_byte_test
The patch fixes the following issues: - `string.byte()` returns the internal numeric codes of the characters, not a single number. - `string.char()` returns `nil` when values `i` or `j` are outside the acceptable range (less than zero and greater than the length of the string). It is not documented. Follows up #124
1 parent 509ef25 commit b290379

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

tests/lapi/string_byte_test.lua

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--[[
1+
--[=[
22
SPDX-License-Identifier: ISC
33
Copyright (c) 2023-2025, Sergey Bronnikov.
44
@@ -7,9 +7,9 @@ https://www.lua.org/manual/5.3/manual.html#6.4
77
88
string.byte gets confused with some out-of-range negative indices,
99
https://www.lua.org/bugs.html#5.1.3-9
10-
]]
1110
12-
-- Synopsis: string.byte(s [, i [, j]])
11+
Synopsis: string.byte(s [, i [, j]])
12+
]=]
1313

1414
local luzer = require("luzer")
1515
local test_lib = require("lib")
@@ -18,17 +18,15 @@ local function TestOneInput(buf, _size)
1818
local fdp = luzer.FuzzedDataProvider(buf)
1919
os.setlocale(test_lib.random_locale(fdp), "all")
2020
local str = fdp:consume_string(test_lib.MAX_STR_LEN)
21-
local i = fdp:consume_integer(0, test_lib.MAX_INT)
22-
local j = fdp:consume_integer(0, test_lib.MAX_INT)
21+
local i = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT)
22+
local j = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT)
2323
-- `string.byte()` is the same as `str:byte()`.
2424
assert(string.byte(str, i, j) == str:byte(i, j))
25-
local char_code = string.byte(str, i, j)
26-
if char_code then
27-
assert(type(char_code) == "number")
28-
local byte = string.char(char_code)
29-
assert(byte)
30-
assert(byte == str)
31-
end
25+
-- Note, `string.byte()` returns `nil` when values `i` or `j`
26+
-- are outside the acceptable range (less than zero and
27+
-- greater than the length of the string). It is undocumented.
28+
local bytes = string.char(string.byte(str, i, j))
29+
assert(bytes == string.sub(str, i, j))
3230
end
3331

3432
local args = {

0 commit comments

Comments
 (0)