diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-07-09 12:33:01 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-07-09 12:33:01 -0300 |
| commit | 7c519dfbd0c68b952f0849e01deaa3750e1f8153 (patch) | |
| tree | dde3ddbba310877db725df37a0d9f2cbe4e2a8f9 /testes/strings.lua | |
| parent | f59e6a93c0ad38a27a420e51abf8f13d962446b5 (diff) | |
| download | lua-7c519dfbd0c68b952f0849e01deaa3750e1f8153.tar.gz lua-7c519dfbd0c68b952f0849e01deaa3750e1f8153.tar.bz2 lua-7c519dfbd0c68b952f0849e01deaa3750e1f8153.zip | |
Added manual and tests for version 5.4-w2
Diffstat (limited to 'testes/strings.lua')
| -rw-r--r-- | testes/strings.lua | 382 |
1 files changed, 382 insertions, 0 deletions
diff --git a/testes/strings.lua b/testes/strings.lua new file mode 100644 index 00000000..dd720b65 --- /dev/null +++ b/testes/strings.lua | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | -- $Id: strings.lua,v 1.89 2018/06/19 12:25:15 roberto Exp $ | ||
| 2 | -- See Copyright Notice in file all.lua | ||
| 3 | |||
| 4 | print('testing strings and string library') | ||
| 5 | |||
| 6 | local maxi, mini = math.maxinteger, math.mininteger | ||
| 7 | |||
| 8 | |||
| 9 | local function checkerror (msg, f, ...) | ||
| 10 | local s, err = pcall(f, ...) | ||
| 11 | assert(not s and string.find(err, msg)) | ||
| 12 | end | ||
| 13 | |||
| 14 | |||
| 15 | -- testing string comparisons | ||
| 16 | assert('alo' < 'alo1') | ||
| 17 | assert('' < 'a') | ||
| 18 | assert('alo\0alo' < 'alo\0b') | ||
| 19 | assert('alo\0alo\0\0' > 'alo\0alo\0') | ||
| 20 | assert('alo' < 'alo\0') | ||
| 21 | assert('alo\0' > 'alo') | ||
| 22 | assert('\0' < '\1') | ||
| 23 | assert('\0\0' < '\0\1') | ||
| 24 | assert('\1\0a\0a' <= '\1\0a\0a') | ||
| 25 | assert(not ('\1\0a\0b' <= '\1\0a\0a')) | ||
| 26 | assert('\0\0\0' < '\0\0\0\0') | ||
| 27 | assert(not('\0\0\0\0' < '\0\0\0')) | ||
| 28 | assert('\0\0\0' <= '\0\0\0\0') | ||
| 29 | assert(not('\0\0\0\0' <= '\0\0\0')) | ||
| 30 | assert('\0\0\0' <= '\0\0\0') | ||
| 31 | assert('\0\0\0' >= '\0\0\0') | ||
| 32 | assert(not ('\0\0b' < '\0\0a\0')) | ||
| 33 | |||
| 34 | -- testing string.sub | ||
| 35 | assert(string.sub("123456789",2,4) == "234") | ||
| 36 | assert(string.sub("123456789",7) == "789") | ||
| 37 | assert(string.sub("123456789",7,6) == "") | ||
| 38 | assert(string.sub("123456789",7,7) == "7") | ||
| 39 | assert(string.sub("123456789",0,0) == "") | ||
| 40 | assert(string.sub("123456789",-10,10) == "123456789") | ||
| 41 | assert(string.sub("123456789",1,9) == "123456789") | ||
| 42 | assert(string.sub("123456789",-10,-20) == "") | ||
| 43 | assert(string.sub("123456789",-1) == "9") | ||
| 44 | assert(string.sub("123456789",-4) == "6789") | ||
| 45 | assert(string.sub("123456789",-6, -4) == "456") | ||
| 46 | assert(string.sub("123456789", mini, -4) == "123456") | ||
| 47 | assert(string.sub("123456789", mini, maxi) == "123456789") | ||
| 48 | assert(string.sub("123456789", mini, mini) == "") | ||
| 49 | assert(string.sub("\000123456789",3,5) == "234") | ||
| 50 | assert(("\000123456789"):sub(8) == "789") | ||
| 51 | |||
| 52 | -- testing string.find | ||
| 53 | assert(string.find("123456789", "345") == 3) | ||
| 54 | a,b = string.find("123456789", "345") | ||
| 55 | assert(string.sub("123456789", a, b) == "345") | ||
| 56 | assert(string.find("1234567890123456789", "345", 3) == 3) | ||
| 57 | assert(string.find("1234567890123456789", "345", 4) == 13) | ||
| 58 | assert(string.find("1234567890123456789", "346", 4) == nil) | ||
| 59 | assert(string.find("1234567890123456789", ".45", -9) == 13) | ||
| 60 | assert(string.find("abcdefg", "\0", 5, 1) == nil) | ||
| 61 | assert(string.find("", "") == 1) | ||
| 62 | assert(string.find("", "", 1) == 1) | ||
| 63 | assert(not string.find("", "", 2)) | ||
| 64 | assert(string.find('', 'aaa', 1) == nil) | ||
| 65 | assert(('alo(.)alo'):find('(.)', 1, 1) == 4) | ||
| 66 | |||
| 67 | assert(string.len("") == 0) | ||
| 68 | assert(string.len("\0\0\0") == 3) | ||
| 69 | assert(string.len("1234567890") == 10) | ||
| 70 | |||
| 71 | assert(#"" == 0) | ||
| 72 | assert(#"\0\0\0" == 3) | ||
| 73 | assert(#"1234567890" == 10) | ||
| 74 | |||
| 75 | -- testing string.byte/string.char | ||
| 76 | assert(string.byte("a") == 97) | ||
| 77 | assert(string.byte("\xe4") > 127) | ||
| 78 | assert(string.byte(string.char(255)) == 255) | ||
| 79 | assert(string.byte(string.char(0)) == 0) | ||
| 80 | assert(string.byte("\0") == 0) | ||
| 81 | assert(string.byte("\0\0alo\0x", -1) == string.byte('x')) | ||
| 82 | assert(string.byte("ba", 2) == 97) | ||
| 83 | assert(string.byte("\n\n", 2, -1) == 10) | ||
| 84 | assert(string.byte("\n\n", 2, 2) == 10) | ||
| 85 | assert(string.byte("") == nil) | ||
| 86 | assert(string.byte("hi", -3) == nil) | ||
| 87 | assert(string.byte("hi", 3) == nil) | ||
| 88 | assert(string.byte("hi", 9, 10) == nil) | ||
| 89 | assert(string.byte("hi", 2, 1) == nil) | ||
| 90 | assert(string.char() == "") | ||
| 91 | assert(string.char(0, 255, 0) == "\0\255\0") | ||
| 92 | assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0") | ||
| 93 | assert(string.char(string.byte("\xe4l\0óu", 1, -1)) == "\xe4l\0óu") | ||
| 94 | assert(string.char(string.byte("\xe4l\0óu", 1, 0)) == "") | ||
| 95 | assert(string.char(string.byte("\xe4l\0óu", -10, 100)) == "\xe4l\0óu") | ||
| 96 | |||
| 97 | assert(string.upper("ab\0c") == "AB\0C") | ||
| 98 | assert(string.lower("\0ABCc%$") == "\0abcc%$") | ||
| 99 | assert(string.rep('teste', 0) == '') | ||
| 100 | assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê') | ||
| 101 | assert(string.rep('', 10) == '') | ||
| 102 | |||
| 103 | if string.packsize("i") == 4 then | ||
| 104 | -- result length would be 2^31 (int overflow) | ||
| 105 | checkerror("too large", string.rep, 'aa', (1 << 30)) | ||
| 106 | checkerror("too large", string.rep, 'a', (1 << 30), ',') | ||
| 107 | end | ||
| 108 | |||
| 109 | -- repetitions with separator | ||
| 110 | assert(string.rep('teste', 0, 'xuxu') == '') | ||
| 111 | assert(string.rep('teste', 1, 'xuxu') == 'teste') | ||
| 112 | assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1') | ||
| 113 | assert(string.rep('', 10, '.') == string.rep('.', 9)) | ||
| 114 | assert(not pcall(string.rep, "aa", maxi // 2 + 10)) | ||
| 115 | assert(not pcall(string.rep, "", maxi // 2 + 10, "aa")) | ||
| 116 | |||
| 117 | assert(string.reverse"" == "") | ||
| 118 | assert(string.reverse"\0\1\2\3" == "\3\2\1\0") | ||
| 119 | assert(string.reverse"\0001234" == "4321\0") | ||
| 120 | |||
| 121 | for i=0,30 do assert(string.len(string.rep('a', i)) == i) end | ||
| 122 | |||
| 123 | assert(type(tostring(nil)) == 'string') | ||
| 124 | assert(type(tostring(12)) == 'string') | ||
| 125 | assert(string.find(tostring{}, 'table:')) | ||
| 126 | assert(string.find(tostring(print), 'function:')) | ||
| 127 | assert(#tostring('\0') == 1) | ||
| 128 | assert(tostring(true) == "true") | ||
| 129 | assert(tostring(false) == "false") | ||
| 130 | assert(tostring(-1203) == "-1203") | ||
| 131 | assert(tostring(1203.125) == "1203.125") | ||
| 132 | assert(tostring(-0.5) == "-0.5") | ||
| 133 | assert(tostring(-32767) == "-32767") | ||
| 134 | if math.tointeger(2147483647) then -- no overflow? (32 bits) | ||
| 135 | assert(tostring(-2147483647) == "-2147483647") | ||
| 136 | end | ||
| 137 | if math.tointeger(4611686018427387904) then -- no overflow? (64 bits) | ||
| 138 | assert(tostring(4611686018427387904) == "4611686018427387904") | ||
| 139 | assert(tostring(-4611686018427387904) == "-4611686018427387904") | ||
| 140 | end | ||
| 141 | |||
| 142 | if tostring(0.0) == "0.0" then -- "standard" coercion float->string | ||
| 143 | assert('' .. 12 == '12' and 12.0 .. '' == '12.0') | ||
| 144 | assert(tostring(-1203 + 0.0) == "-1203.0") | ||
| 145 | else -- compatible coercion | ||
| 146 | assert(tostring(0.0) == "0") | ||
| 147 | assert('' .. 12 == '12' and 12.0 .. '' == '12') | ||
| 148 | assert(tostring(-1203 + 0.0) == "-1203") | ||
| 149 | end | ||
| 150 | |||
| 151 | |||
| 152 | x = '"ílo"\n\\' | ||
| 153 | assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') | ||
| 154 | assert(string.format('%q', "\0") == [["\0"]]) | ||
| 155 | assert(load(string.format('return %q', x))() == x) | ||
| 156 | x = "\0\1\0023\5\0009" | ||
| 157 | assert(load(string.format('return %q', x))() == x) | ||
| 158 | assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) == | ||
| 159 | "\0\xe4\0b8c\0") | ||
| 160 | assert(string.format('') == "") | ||
| 161 | assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == | ||
| 162 | string.format("%c%c%c%c", 34, 48, 90, 100)) | ||
| 163 | assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be') | ||
| 164 | assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") | ||
| 165 | assert(tonumber(string.format("%f", 10.3)) == 10.3) | ||
| 166 | x = string.format('"%-50s"', 'a') | ||
| 167 | assert(#x == 52) | ||
| 168 | assert(string.sub(x, 1, 4) == '"a ') | ||
| 169 | |||
| 170 | assert(string.format("-%.20s.20s", string.rep("%", 2000)) == | ||
| 171 | "-"..string.rep("%", 20)..".20s") | ||
| 172 | assert(string.format('"-%20s.20s"', string.rep("%", 2000)) == | ||
| 173 | string.format("%q", "-"..string.rep("%", 2000)..".20s")) | ||
| 174 | |||
| 175 | do | ||
| 176 | local function checkQ (v) | ||
| 177 | local s = string.format("%q", v) | ||
| 178 | local nv = load("return " .. s)() | ||
| 179 | assert(v == nv and math.type(v) == math.type(nv)) | ||
| 180 | end | ||
| 181 | checkQ("\0\0\1\255\u{234}") | ||
| 182 | checkQ(math.maxinteger) | ||
| 183 | checkQ(math.mininteger) | ||
| 184 | checkQ(math.pi) | ||
| 185 | checkQ(0.1) | ||
| 186 | checkQ(true) | ||
| 187 | checkQ(nil) | ||
| 188 | checkQ(false) | ||
| 189 | checkQ(math.huge) | ||
| 190 | checkQ(-math.huge) | ||
| 191 | assert(string.format("%q", 0/0) == "(0/0)") -- NaN | ||
| 192 | checkerror("no literal", string.format, "%q", {}) | ||
| 193 | end | ||
| 194 | |||
| 195 | assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") | ||
| 196 | checkerror("contains zeros", string.format, "%10s", "\0") | ||
| 197 | |||
| 198 | -- format x tostring | ||
| 199 | assert(string.format("%s %s", nil, true) == "nil true") | ||
| 200 | assert(string.format("%s %.4s", false, true) == "false true") | ||
| 201 | assert(string.format("%.3s %.3s", false, true) == "fal tru") | ||
| 202 | local m = setmetatable({}, {__tostring = function () return "hello" end, | ||
| 203 | __name = "hi"}) | ||
| 204 | assert(string.format("%s %.10s", m, m) == "hello hello") | ||
| 205 | getmetatable(m).__tostring = nil -- will use '__name' from now on | ||
| 206 | assert(string.format("%.4s", m) == "hi: ") | ||
| 207 | |||
| 208 | getmetatable(m).__tostring = function () return {} end | ||
| 209 | checkerror("'__tostring' must return a string", tostring, m) | ||
| 210 | |||
| 211 | |||
| 212 | assert(string.format("%x", 0.0) == "0") | ||
| 213 | assert(string.format("%02x", 0.0) == "00") | ||
| 214 | assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF") | ||
| 215 | assert(string.format("%+08d", 31501) == "+0031501") | ||
| 216 | assert(string.format("%+08d", -30927) == "-0030927") | ||
| 217 | |||
| 218 | |||
| 219 | do -- longest number that can be formatted | ||
| 220 | local i = 1 | ||
| 221 | local j = 10000 | ||
| 222 | while i + 1 < j do -- binary search for maximum finite float | ||
| 223 | local m = (i + j) // 2 | ||
| 224 | if 10^m < math.huge then i = m else j = m end | ||
| 225 | end | ||
| 226 | assert(10^i < math.huge and 10^j == math.huge) | ||
| 227 | local s = string.format('%.99f', -(10^i)) | ||
| 228 | assert(string.len(s) >= i + 101) | ||
| 229 | assert(tonumber(s) == -(10^i)) | ||
| 230 | end | ||
| 231 | |||
| 232 | |||
| 233 | -- testing large numbers for format | ||
| 234 | do -- assume at least 32 bits | ||
| 235 | local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits | ||
| 236 | assert(string.sub(string.format("%8x", -1), -8) == "ffffffff") | ||
| 237 | assert(string.format("%x", max) == "7fffffff") | ||
| 238 | assert(string.sub(string.format("%x", min), -8) == "80000000") | ||
| 239 | assert(string.format("%d", max) == "2147483647") | ||
| 240 | assert(string.format("%d", min) == "-2147483648") | ||
| 241 | assert(string.format("%u", 0xffffffff) == "4294967295") | ||
| 242 | assert(string.format("%o", 0xABCD) == "125715") | ||
| 243 | |||
| 244 | max, min = 0x7fffffffffffffff, -0x8000000000000000 | ||
| 245 | if max > 2.0^53 then -- only for 64 bits | ||
| 246 | assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff") | ||
| 247 | assert(string.format("0x%8X", 0x8f000003) == "0x8F000003") | ||
| 248 | assert(string.format("%d", 2^53) == "9007199254740992") | ||
| 249 | assert(string.format("%i", -2^53) == "-9007199254740992") | ||
| 250 | assert(string.format("%x", max) == "7fffffffffffffff") | ||
| 251 | assert(string.format("%x", min) == "8000000000000000") | ||
| 252 | assert(string.format("%d", max) == "9223372036854775807") | ||
| 253 | assert(string.format("%d", min) == "-9223372036854775808") | ||
| 254 | assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615") | ||
| 255 | assert(tostring(1234567890123) == '1234567890123') | ||
| 256 | end | ||
| 257 | end | ||
| 258 | |||
| 259 | |||
| 260 | do print("testing 'format %a %A'") | ||
| 261 | local function matchhexa (n) | ||
| 262 | local s = string.format("%a", n) | ||
| 263 | -- result matches ISO C requirements | ||
| 264 | assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$")) | ||
| 265 | assert(tonumber(s) == n) -- and has full precision | ||
| 266 | s = string.format("%A", n) | ||
| 267 | assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$")) | ||
| 268 | assert(tonumber(s) == n) | ||
| 269 | end | ||
| 270 | for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30, | ||
| 271 | -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do | ||
| 272 | matchhexa(n) | ||
| 273 | end | ||
| 274 | |||
| 275 | assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) | ||
| 276 | assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) | ||
| 277 | |||
| 278 | if not _port then -- test inf, -inf, NaN, and -0.0 | ||
| 279 | assert(string.find(string.format("%a", 1/0), "^inf")) | ||
| 280 | assert(string.find(string.format("%A", -1/0), "^%-INF")) | ||
| 281 | assert(string.find(string.format("%a", 0/0), "^%-?nan")) | ||
| 282 | assert(string.find(string.format("%a", -0.0), "^%-0x0")) | ||
| 283 | end | ||
| 284 | |||
| 285 | if not pcall(string.format, "%.3a", 0) then | ||
| 286 | (Message or print)("\n >>> modifiers for format '%a' not available <<<\n") | ||
| 287 | else | ||
| 288 | assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) | ||
| 289 | assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) | ||
| 290 | end | ||
| 291 | end | ||
| 292 | |||
| 293 | |||
| 294 | -- errors in format | ||
| 295 | |||
| 296 | local function check (fmt, msg) | ||
| 297 | checkerror(msg, string.format, fmt, 10) | ||
| 298 | end | ||
| 299 | |||
| 300 | local aux = string.rep('0', 600) | ||
| 301 | check("%100.3d", "too long") | ||
| 302 | check("%1"..aux..".3d", "too long") | ||
| 303 | check("%1.100d", "too long") | ||
| 304 | check("%10.1"..aux.."004d", "too long") | ||
| 305 | check("%t", "invalid option") | ||
| 306 | check("%"..aux.."d", "repeated flags") | ||
| 307 | check("%d %d", "no value") | ||
| 308 | |||
| 309 | |||
| 310 | assert(load("return 1\n--comment without ending EOL")() == 1) | ||
| 311 | |||
| 312 | |||
| 313 | checkerror("table expected", table.concat, 3) | ||
| 314 | assert(table.concat{} == "") | ||
| 315 | assert(table.concat({}, 'x') == "") | ||
| 316 | assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2") | ||
| 317 | local a = {}; for i=1,300 do a[i] = "xuxu" end | ||
| 318 | assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300)) | ||
| 319 | assert(table.concat(a, "b", 20, 20) == "xuxu") | ||
| 320 | assert(table.concat(a, "", 20, 21) == "xuxuxuxu") | ||
| 321 | assert(table.concat(a, "x", 22, 21) == "") | ||
| 322 | assert(table.concat(a, "3", 299) == "xuxu3xuxu") | ||
| 323 | assert(table.concat({}, "x", maxi, maxi - 1) == "") | ||
| 324 | assert(table.concat({}, "x", mini + 1, mini) == "") | ||
| 325 | assert(table.concat({}, "x", maxi, mini) == "") | ||
| 326 | assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo") | ||
| 327 | assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi) | ||
| 328 | == "y-alo") | ||
| 329 | |||
| 330 | assert(not pcall(table.concat, {"a", "b", {}})) | ||
| 331 | |||
| 332 | a = {"a","b","c"} | ||
| 333 | assert(table.concat(a, ",", 1, 0) == "") | ||
| 334 | assert(table.concat(a, ",", 1, 1) == "a") | ||
| 335 | assert(table.concat(a, ",", 1, 2) == "a,b") | ||
| 336 | assert(table.concat(a, ",", 2) == "b,c") | ||
| 337 | assert(table.concat(a, ",", 3) == "c") | ||
| 338 | assert(table.concat(a, ",", 4) == "") | ||
| 339 | |||
| 340 | if not _port then | ||
| 341 | |||
| 342 | local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" } | ||
| 343 | local function trylocale (w) | ||
| 344 | for i = 1, #locales do | ||
| 345 | if os.setlocale(locales[i], w) then | ||
| 346 | print(string.format("'%s' locale set to '%s'", w, locales[i])) | ||
| 347 | return locales[i] | ||
| 348 | end | ||
| 349 | end | ||
| 350 | print(string.format("'%s' locale not found", w)) | ||
| 351 | return false | ||
| 352 | end | ||
| 353 | |||
| 354 | if trylocale("collate") then | ||
| 355 | assert("alo" < "álo" and "álo" < "amo") | ||
| 356 | end | ||
| 357 | |||
| 358 | if trylocale("ctype") then | ||
| 359 | assert(string.gsub("áéíóú", "%a", "x") == "xxxxx") | ||
| 360 | assert(string.gsub("áÁéÉ", "%l", "x") == "xÁxÉ") | ||
| 361 | assert(string.gsub("áÁéÉ", "%u", "x") == "áxéx") | ||
| 362 | assert(string.upper"áÁé{xuxu}ção" == "ÁÁÉ{XUXU}ÇÃO") | ||
| 363 | end | ||
| 364 | |||
| 365 | os.setlocale("C") | ||
| 366 | assert(os.setlocale() == 'C') | ||
| 367 | assert(os.setlocale(nil, "numeric") == 'C') | ||
| 368 | |||
| 369 | end | ||
| 370 | |||
| 371 | |||
| 372 | -- bug in Lua 5.3.2 | ||
| 373 | -- 'gmatch' iterator does not work across coroutines | ||
| 374 | do | ||
| 375 | local f = string.gmatch("1 2 3 4 5", "%d+") | ||
| 376 | assert(f() == "1") | ||
| 377 | co = coroutine.wrap(f) | ||
| 378 | assert(co() == "2") | ||
| 379 | end | ||
| 380 | |||
| 381 | print('OK') | ||
| 382 | |||
