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