strings.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. -- $Id: testes/strings.lua $
  2. -- See Copyright Notice in file all.lua
  3. -- ISO Latin encoding
  4. print('testing strings and string library')
  5. local maxi <const> = math.maxinteger
  6. local mini <const> = math.mininteger
  7. local function checkerror (msg, f, ...)
  8. local s, err = pcall(f, ...)
  9. assert(not s and string.find(err, msg))
  10. end
  11. -- testing string comparisons
  12. assert('alo' < 'alo1')
  13. assert('' < 'a')
  14. assert('alo\0alo' < 'alo\0b')
  15. assert('alo\0alo\0\0' > 'alo\0alo\0')
  16. assert('alo' < 'alo\0')
  17. assert('alo\0' > 'alo')
  18. assert('\0' < '\1')
  19. assert('\0\0' < '\0\1')
  20. assert('\1\0a\0a' <= '\1\0a\0a')
  21. assert(not ('\1\0a\0b' <= '\1\0a\0a'))
  22. assert('\0\0\0' < '\0\0\0\0')
  23. assert(not('\0\0\0\0' < '\0\0\0'))
  24. assert('\0\0\0' <= '\0\0\0\0')
  25. assert(not('\0\0\0\0' <= '\0\0\0'))
  26. assert('\0\0\0' <= '\0\0\0')
  27. assert('\0\0\0' >= '\0\0\0')
  28. assert(not ('\0\0b' < '\0\0a\0'))
  29. -- testing string.sub
  30. assert(string.sub("123456789",2,4) == "234")
  31. assert(string.sub("123456789",7) == "789")
  32. assert(string.sub("123456789",7,6) == "")
  33. assert(string.sub("123456789",7,7) == "7")
  34. assert(string.sub("123456789",0,0) == "")
  35. assert(string.sub("123456789",-10,10) == "123456789")
  36. assert(string.sub("123456789",1,9) == "123456789")
  37. assert(string.sub("123456789",-10,-20) == "")
  38. assert(string.sub("123456789",-1) == "9")
  39. assert(string.sub("123456789",-4) == "6789")
  40. assert(string.sub("123456789",-6, -4) == "456")
  41. assert(string.sub("123456789", mini, -4) == "123456")
  42. assert(string.sub("123456789", mini, maxi) == "123456789")
  43. assert(string.sub("123456789", mini, mini) == "")
  44. assert(string.sub("\000123456789",3,5) == "234")
  45. assert(("\000123456789"):sub(8) == "789")
  46. -- testing string.find
  47. assert(string.find("123456789", "345") == 3)
  48. local a,b = string.find("123456789", "345")
  49. assert(string.sub("123456789", a, b) == "345")
  50. assert(string.find("1234567890123456789", "345", 3) == 3)
  51. assert(string.find("1234567890123456789", "345", 4) == 13)
  52. assert(not string.find("1234567890123456789", "346", 4))
  53. assert(string.find("1234567890123456789", ".45", -9) == 13)
  54. assert(not string.find("abcdefg", "\0", 5, 1))
  55. assert(string.find("", "") == 1)
  56. assert(string.find("", "", 1) == 1)
  57. assert(not string.find("", "", 2))
  58. assert(not string.find('', 'aaa', 1))
  59. assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
  60. assert(string.len("") == 0)
  61. assert(string.len("\0\0\0") == 3)
  62. assert(string.len("1234567890") == 10)
  63. assert(#"" == 0)
  64. assert(#"\0\0\0" == 3)
  65. assert(#"1234567890" == 10)
  66. -- testing string.byte/string.char
  67. assert(string.byte("a") == 97)
  68. assert(string.byte("\xe4") > 127)
  69. assert(string.byte(string.char(255)) == 255)
  70. assert(string.byte(string.char(0)) == 0)
  71. assert(string.byte("\0") == 0)
  72. assert(string.byte("\0\0alo\0x", -1) == string.byte('x'))
  73. assert(string.byte("ba", 2) == 97)
  74. assert(string.byte("\n\n", 2, -1) == 10)
  75. assert(string.byte("\n\n", 2, 2) == 10)
  76. assert(string.byte("") == nil)
  77. assert(string.byte("hi", -3) == nil)
  78. assert(string.byte("hi", 3) == nil)
  79. assert(string.byte("hi", 9, 10) == nil)
  80. assert(string.byte("hi", 2, 1) == nil)
  81. assert(string.char() == "")
  82. assert(string.char(0, 255, 0) == "\0\255\0")
  83. assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0")
  84. assert(string.char(string.byte("\xe4l\0óu", 1, -1)) == "\xe4l\0óu")
  85. assert(string.char(string.byte("\xe4l\0óu", 1, 0)) == "")
  86. assert(string.char(string.byte("\xe4l\0óu", -10, 100)) == "\xe4l\0óu")
  87. checkerror("out of range", string.char, 256)
  88. checkerror("out of range", string.char, -1)
  89. checkerror("out of range", string.char, math.maxinteger)
  90. checkerror("out of range", string.char, math.mininteger)
  91. assert(string.upper("ab\0c") == "AB\0C")
  92. assert(string.lower("\0ABCc%$") == "\0abcc%$")
  93. assert(string.rep('teste', 0) == '')
  94. assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê')
  95. assert(string.rep('', 10) == '')
  96. if string.packsize("i") == 4 then
  97. -- result length would be 2^31 (int overflow)
  98. checkerror("too large", string.rep, 'aa', (1 << 30))
  99. checkerror("too large", string.rep, 'a', (1 << 30), ',')
  100. end
  101. -- repetitions with separator
  102. assert(string.rep('teste', 0, 'xuxu') == '')
  103. assert(string.rep('teste', 1, 'xuxu') == 'teste')
  104. assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1')
  105. assert(string.rep('', 10, '.') == string.rep('.', 9))
  106. assert(not pcall(string.rep, "aa", maxi // 2 + 10))
  107. assert(not pcall(string.rep, "", maxi // 2 + 10, "aa"))
  108. assert(string.reverse"" == "")
  109. assert(string.reverse"\0\1\2\3" == "\3\2\1\0")
  110. assert(string.reverse"\0001234" == "4321\0")
  111. for i=0,30 do assert(string.len(string.rep('a', i)) == i) end
  112. assert(type(tostring(nil)) == 'string')
  113. assert(type(tostring(12)) == 'string')
  114. assert(string.find(tostring{}, 'table:'))
  115. assert(string.find(tostring(print), 'function:'))
  116. assert(#tostring('\0') == 1)
  117. assert(tostring(true) == "true")
  118. assert(tostring(false) == "false")
  119. assert(tostring(-1203) == "-1203")
  120. assert(tostring(1203.125) == "1203.125")
  121. assert(tostring(-0.5) == "-0.5")
  122. assert(tostring(-32767) == "-32767")
  123. if math.tointeger(2147483647) then -- no overflow? (32 bits)
  124. assert(tostring(-2147483647) == "-2147483647")
  125. end
  126. if math.tointeger(4611686018427387904) then -- no overflow? (64 bits)
  127. assert(tostring(4611686018427387904) == "4611686018427387904")
  128. assert(tostring(-4611686018427387904) == "-4611686018427387904")
  129. end
  130. if tostring(0.0) == "0.0" then -- "standard" coercion float->string
  131. assert('' .. 12 == '12' and 12.0 .. '' == '12.0')
  132. assert(tostring(-1203 + 0.0) == "-1203.0")
  133. else -- compatible coercion
  134. assert(tostring(0.0) == "0")
  135. assert('' .. 12 == '12' and 12.0 .. '' == '12')
  136. assert(tostring(-1203 + 0.0) == "-1203")
  137. end
  138. do -- tests for '%p' format
  139. -- not much to test, as C does not specify what '%p' does.
  140. -- ("The value of the pointer is converted to a sequence of printing
  141. -- characters, in an implementation-defined manner.")
  142. local null = "(null)" -- nulls are formatted by Lua
  143. assert(string.format("%p", 4) == null)
  144. assert(string.format("%p", true) == null)
  145. assert(string.format("%p", nil) == null)
  146. assert(string.format("%p", {}) ~= null)
  147. assert(string.format("%p", print) ~= null)
  148. assert(string.format("%p", coroutine.running()) ~= null)
  149. assert(string.format("%p", io.stdin) ~= null)
  150. assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
  151. assert(string.format("%p", print) == string.format("%p", print))
  152. assert(string.format("%p", print) ~= string.format("%p", assert))
  153. assert(#string.format("%90p", {}) == 90)
  154. assert(#string.format("%-60p", {}) == 60)
  155. assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null)
  156. assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null))
  157. do
  158. local t1 = {}; local t2 = {}
  159. assert(string.format("%p", t1) ~= string.format("%p", t2))
  160. end
  161. do -- short strings are internalized
  162. local s1 = string.rep("a", 10)
  163. local s2 = string.rep("aa", 5)
  164. assert(string.format("%p", s1) == string.format("%p", s2))
  165. end
  166. do -- long strings aren't internalized
  167. local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
  168. assert(string.format("%p", s1) ~= string.format("%p", s2))
  169. end
  170. end
  171. local x = '"ílo"\n\\'
  172. assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
  173. assert(string.format('%q', "\0") == [["\0"]])
  174. assert(load(string.format('return %q', x))() == x)
  175. x = "\0\1\0023\5\0009"
  176. assert(load(string.format('return %q', x))() == x)
  177. assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) ==
  178. "\0\xe4\0b8c\0")
  179. assert(string.format('') == "")
  180. assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
  181. string.format("%1c%-c%-1c%c", 34, 48, 90, 100))
  182. assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
  183. assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
  184. assert(tonumber(string.format("%f", 10.3)) == 10.3)
  185. assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"')
  186. assert(string.format("-%.20s.20s", string.rep("%", 2000)) ==
  187. "-"..string.rep("%", 20)..".20s")
  188. assert(string.format('"-%20s.20s"', string.rep("%", 2000)) ==
  189. string.format("%q", "-"..string.rep("%", 2000)..".20s"))
  190. do
  191. local function checkQ (v)
  192. local s = string.format("%q", v)
  193. local nv = load("return " .. s)()
  194. assert(v == nv and math.type(v) == math.type(nv))
  195. end
  196. checkQ("\0\0\1\255\u{234}")
  197. checkQ(math.maxinteger)
  198. checkQ(math.mininteger)
  199. checkQ(math.pi)
  200. checkQ(0.1)
  201. checkQ(true)
  202. checkQ(nil)
  203. checkQ(false)
  204. checkQ(math.huge)
  205. checkQ(-math.huge)
  206. assert(string.format("%q", 0/0) == "(0/0)") -- NaN
  207. checkerror("no literal", string.format, "%q", {})
  208. end
  209. assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0")
  210. checkerror("contains zeros", string.format, "%10s", "\0")
  211. -- format x tostring
  212. assert(string.format("%s %s", nil, true) == "nil true")
  213. assert(string.format("%s %.4s", false, true) == "false true")
  214. assert(string.format("%.3s %.3s", false, true) == "fal tru")
  215. local m = setmetatable({}, {__tostring = function () return "hello" end,
  216. __name = "hi"})
  217. assert(string.format("%s %.10s", m, m) == "hello hello")
  218. getmetatable(m).__tostring = nil -- will use '__name' from now on
  219. assert(string.format("%.4s", m) == "hi: ")
  220. getmetatable(m).__tostring = function () return {} end
  221. checkerror("'__tostring' must return a string", tostring, m)
  222. assert(string.format("%x", 0.0) == "0")
  223. assert(string.format("%02x", 0.0) == "00")
  224. assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF")
  225. assert(string.format("%+08d", 31501) == "+0031501")
  226. assert(string.format("%+08d", -30927) == "-0030927")
  227. do -- longest number that can be formatted
  228. local i = 1
  229. local j = 10000
  230. while i + 1 < j do -- binary search for maximum finite float
  231. local m = (i + j) // 2
  232. if 10^m < math.huge then i = m else j = m end
  233. end
  234. assert(10^i < math.huge and 10^j == math.huge)
  235. local s = string.format('%.99f', -(10^i))
  236. assert(string.len(s) >= i + 101)
  237. assert(tonumber(s) == -(10^i))
  238. -- limit for floats
  239. assert(10^38 < math.huge)
  240. local s = string.format('%.99f', -(10^38))
  241. assert(string.len(s) >= 38 + 101)
  242. assert(tonumber(s) == -(10^38))
  243. end
  244. -- testing large numbers for format
  245. do -- assume at least 32 bits
  246. local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits
  247. assert(string.sub(string.format("%8x", -1), -8) == "ffffffff")
  248. assert(string.format("%x", max) == "7fffffff")
  249. assert(string.sub(string.format("%x", min), -8) == "80000000")
  250. assert(string.format("%d", max) == "2147483647")
  251. assert(string.format("%d", min) == "-2147483648")
  252. assert(string.format("%u", 0xffffffff) == "4294967295")
  253. assert(string.format("%o", 0xABCD) == "125715")
  254. max, min = 0x7fffffffffffffff, -0x8000000000000000
  255. if max > 2.0^53 then -- only for 64 bits
  256. assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff")
  257. assert(string.format("0x%8X", 0x8f000003) == "0x8F000003")
  258. assert(string.format("%d", 2^53) == "9007199254740992")
  259. assert(string.format("%i", -2^53) == "-9007199254740992")
  260. assert(string.format("%x", max) == "7fffffffffffffff")
  261. assert(string.format("%x", min) == "8000000000000000")
  262. assert(string.format("%d", max) == "9223372036854775807")
  263. assert(string.format("%d", min) == "-9223372036854775808")
  264. assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615")
  265. assert(tostring(1234567890123) == '1234567890123')
  266. end
  267. end
  268. do print("testing 'format %a %A'")
  269. local function matchhexa (n)
  270. local s = string.format("%a", n)
  271. -- result matches ISO C requirements
  272. assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$"))
  273. assert(tonumber(s) == n) -- and has full precision
  274. s = string.format("%A", n)
  275. assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$"))
  276. assert(tonumber(s) == n)
  277. end
  278. for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30,
  279. -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do
  280. matchhexa(n)
  281. end
  282. assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$"))
  283. assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$"))
  284. if not _port then -- test inf, -inf, NaN, and -0.0
  285. assert(string.find(string.format("%a", 1/0), "^inf"))
  286. assert(string.find(string.format("%A", -1/0), "^%-INF"))
  287. assert(string.find(string.format("%a", 0/0), "^%-?nan"))
  288. assert(string.find(string.format("%a", -0.0), "^%-0x0"))
  289. end
  290. if not pcall(string.format, "%.3a", 0) then
  291. (Message or print)("\n >>> modifiers for format '%a' not available <<<\n")
  292. else
  293. assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$"))
  294. assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$"))
  295. end
  296. end
  297. -- testing some flags (all these results are required by ISO C)
  298. assert(string.format("%#12o", 10) == " 012")
  299. assert(string.format("%#10x", 100) == " 0x64")
  300. assert(string.format("%#-17X", 100) == "0X64 ")
  301. assert(string.format("%013i", -100) == "-000000000100")
  302. assert(string.format("%2.5d", -100) == "-00100")
  303. assert(string.format("%.u", 0) == "")
  304. assert(string.format("%+#014.0f", 100) == "+000000000100.")
  305. assert(string.format("%-16c", 97) == "a ")
  306. assert(string.format("%+.3G", 1.5) == "+1.5")
  307. assert(string.format("%.0s", "alo") == "")
  308. assert(string.format("%.s", "alo") == "")
  309. -- ISO C89 says that "The exponent always contains at least two digits",
  310. -- but unlike ISO C99 it does not ensure that it contains "only as many
  311. -- more digits as necessary".
  312. assert(string.match(string.format("% 1.0E", 100), "^ 1E%+0+2$"))
  313. assert(string.match(string.format("% .1g", 2^10), "^ 1e%+0+3$"))
  314. -- errors in format
  315. local function check (fmt, msg)
  316. checkerror(msg, string.format, fmt, 10)
  317. end
  318. local aux = string.rep('0', 600)
  319. check("%100.3d", "invalid conversion")
  320. check("%1"..aux..".3d", "too long")
  321. check("%1.100d", "invalid conversion")
  322. check("%10.1"..aux.."004d", "too long")
  323. check("%t", "invalid conversion")
  324. check("%"..aux.."d", "too long")
  325. check("%d %d", "no value")
  326. check("%010c", "invalid conversion")
  327. check("%.10c", "invalid conversion")
  328. check("%0.34s", "invalid conversion")
  329. check("%#i", "invalid conversion")
  330. check("%3.1p", "invalid conversion")
  331. check("%0.s", "invalid conversion")
  332. check("%10q", "cannot have modifiers")
  333. check("%F", "invalid conversion") -- useless and not in C89
  334. assert(load("return 1\n--comment without ending EOL")() == 1)
  335. checkerror("table expected", table.concat, 3)
  336. checkerror("at index " .. maxi, table.concat, {}, " ", maxi, maxi)
  337. -- '%' escapes following minus signal
  338. checkerror("at index %" .. mini, table.concat, {}, " ", mini, mini)
  339. assert(table.concat{} == "")
  340. assert(table.concat({}, 'x') == "")
  341. assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2")
  342. local a = {}; for i=1,300 do a[i] = "xuxu" end
  343. assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300))
  344. assert(table.concat(a, "b", 20, 20) == "xuxu")
  345. assert(table.concat(a, "", 20, 21) == "xuxuxuxu")
  346. assert(table.concat(a, "x", 22, 21) == "")
  347. assert(table.concat(a, "3", 299) == "xuxu3xuxu")
  348. assert(table.concat({}, "x", maxi, maxi - 1) == "")
  349. assert(table.concat({}, "x", mini + 1, mini) == "")
  350. assert(table.concat({}, "x", maxi, mini) == "")
  351. assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo")
  352. assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi)
  353. == "y-alo")
  354. assert(not pcall(table.concat, {"a", "b", {}}))
  355. a = {"a","b","c"}
  356. assert(table.concat(a, ",", 1, 0) == "")
  357. assert(table.concat(a, ",", 1, 1) == "a")
  358. assert(table.concat(a, ",", 1, 2) == "a,b")
  359. assert(table.concat(a, ",", 2) == "b,c")
  360. assert(table.concat(a, ",", 3) == "c")
  361. assert(table.concat(a, ",", 4) == "")
  362. if not _port then
  363. local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" }
  364. local function trylocale (w)
  365. for i = 1, #locales do
  366. if os.setlocale(locales[i], w) then
  367. print(string.format("'%s' locale set to '%s'", w, locales[i]))
  368. return locales[i]
  369. end
  370. end
  371. print(string.format("'%s' locale not found", w))
  372. return false
  373. end
  374. if trylocale("collate") then
  375. assert("alo" < "álo" and "álo" < "amo")
  376. end
  377. if trylocale("ctype") then
  378. assert(string.gsub("áéíóú", "%a", "x") == "xxxxx")
  379. assert(string.gsub("áÁéÉ", "%l", "x") == "xÁxÉ")
  380. assert(string.gsub("áÁéÉ", "%u", "x") == "áxéx")
  381. assert(string.upper"áÁé{xuxu}ção" == "ÁÁÉ{XUXU}ÇÃO")
  382. end
  383. os.setlocale("C")
  384. assert(os.setlocale() == 'C')
  385. assert(os.setlocale(nil, "numeric") == 'C')
  386. end
  387. -- bug in Lua 5.3.2
  388. -- 'gmatch' iterator does not work across coroutines
  389. do
  390. local f = string.gmatch("1 2 3 4 5", "%d+")
  391. assert(f() == "1")
  392. local co = coroutine.wrap(f)
  393. assert(co() == "2")
  394. end
  395. if T==nil then
  396. (Message or print)
  397. ("\n >>> testC not active: skipping 'pushfstring' tests <<<\n")
  398. else
  399. print"testing 'pushfstring'"
  400. -- formats %U, %f, %I already tested elsewhere
  401. local blen = 200 -- internal buffer length in 'luaO_pushfstring'
  402. local function callpfs (op, fmt, n)
  403. local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}
  404. -- stack has code, 'fmt', 'n', and result from operation
  405. assert(#x == 4) -- make sure nothing else was left in the stack
  406. return x[4]
  407. end
  408. local function testpfs (op, fmt, n)
  409. assert(callpfs(op, fmt, n) == string.format(fmt, n))
  410. end
  411. testpfs("I", "", 0)
  412. testpfs("I", string.rep("a", blen - 1), 0)
  413. testpfs("I", string.rep("a", blen), 0)
  414. testpfs("I", string.rep("a", blen + 1), 0)
  415. local str = string.rep("ab", blen) .. "%d" .. string.rep("d", blen / 2)
  416. testpfs("I", str, 2^14)
  417. testpfs("I", str, -2^15)
  418. str = "%d" .. string.rep("cd", blen)
  419. testpfs("I", str, 2^14)
  420. testpfs("I", str, -2^15)
  421. str = string.rep("c", blen - 2) .. "%d"
  422. testpfs("I", str, 2^14)
  423. testpfs("I", str, -2^15)
  424. for l = 12, 14 do
  425. local str1 = string.rep("a", l)
  426. for i = 0, 500, 13 do
  427. for j = 0, 500, 13 do
  428. str = string.rep("a", i) .. "%s" .. string.rep("d", j)
  429. testpfs("S", str, str1)
  430. testpfs("S", str, str)
  431. end
  432. end
  433. end
  434. str = "abc %c def"
  435. testpfs("I", str, string.byte("A"))
  436. testpfs("I", str, 255)
  437. str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen)
  438. testpfs("P", str, {})
  439. str = string.rep("%%", 3 * blen) .. "%p" .. string.rep("%%", 2 * blen)
  440. testpfs("P", str, {})
  441. end
  442. print('OK')