strings.lua 18 KB

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