strings.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. print('testing strings and string library')
  2. assert('alo' < 'alo1')
  3. assert('' < 'a')
  4. assert('alo\0alo' < 'alo\0b')
  5. assert('alo\0alo\0\0' > 'alo\0alo\0')
  6. assert('alo' < 'alo\0')
  7. assert('alo\0' > 'alo')
  8. assert('\0' < '\1')
  9. assert('\0\0' < '\0\1')
  10. assert('\1\0a\0a' <= '\1\0a\0a')
  11. assert(not ('\1\0a\0b' <= '\1\0a\0a'))
  12. assert('\0\0\0' < '\0\0\0\0')
  13. assert(not('\0\0\0\0' < '\0\0\0'))
  14. assert('\0\0\0' <= '\0\0\0\0')
  15. assert(not('\0\0\0\0' <= '\0\0\0'))
  16. assert('\0\0\0' <= '\0\0\0')
  17. assert('\0\0\0' >= '\0\0\0')
  18. assert(not ('\0\0b' < '\0\0a\0'))
  19. print('+')
  20. assert(string.sub("123456789",2,4) == "234")
  21. assert(string.sub("123456789",7) == "789")
  22. assert(string.sub("123456789",7,6) == "")
  23. assert(string.sub("123456789",7,7) == "7")
  24. assert(string.sub("123456789",0,0) == "")
  25. assert(string.sub("123456789",-10,10) == "123456789")
  26. assert(string.sub("123456789",1,9) == "123456789")
  27. assert(string.sub("123456789",-10,-20) == "")
  28. assert(string.sub("123456789",-1) == "9")
  29. assert(string.sub("123456789",-4) == "6789")
  30. assert(string.sub("123456789",-6, -4) == "456")
  31. assert(string.sub("\000123456789",3,5) == "234")
  32. assert(("\000123456789"):sub(8) == "789")
  33. print('+')
  34. assert(string.find("123456789", "345") == 3)
  35. a,b = string.find("123456789", "345")
  36. assert(string.sub("123456789", a, b) == "345")
  37. assert(string.find("1234567890123456789", "345", 3) == 3)
  38. assert(string.find("1234567890123456789", "345", 4) == 13)
  39. assert(string.find("1234567890123456789", "346", 4) == nil)
  40. assert(string.find("1234567890123456789", ".45", -9) == 13)
  41. assert(string.find("abcdefg", "\0", 5, 1) == nil)
  42. assert(string.find("", "") == 1)
  43. assert(string.find('', 'aaa', 1) == nil)
  44. assert(('alo(.)alo'):find('(.)', 1, 1) == 4)
  45. assert(string.len("") == 0)
  46. assert(string.len("\0\0\0") == 3)
  47. assert(string.len("1234567890") == 10)
  48. assert(#"" == 0)
  49. assert(#"\0\0\0" == 3)
  50. assert(#"1234567890" == 10)
  51. assert(string.byte("a") == 97)
  52. assert(string.byte("á") > 127)
  53. assert(string.byte(string.char(255)) == 255)
  54. assert(string.byte(string.char(0)) == 0)
  55. assert(string.byte("\0") == 0)
  56. assert(string.byte("\0\0alo\0x", -1) == string.byte('x'))
  57. assert(string.byte("ba", 2) == 97)
  58. assert(string.byte("\n\n", 2, -1) == 10)
  59. assert(string.byte("\n\n", 2, 2) == 10)
  60. assert(string.byte("") == nil)
  61. assert(string.byte("hi", -3) == nil)
  62. assert(string.byte("hi", 3) == nil)
  63. assert(string.byte("hi", 9, 10) == nil)
  64. assert(string.byte("hi", 2, 1) == nil)
  65. assert(string.char() == "")
  66. assert(string.char(0, 255, 0) == "\0\255\0")
  67. assert(string.char(0, string.byte("á"), 0) == "\0á\0")
  68. assert(string.char(string.byte("ál\0óu", 1, -1)) == "ál\0óu")
  69. assert(string.char(string.byte("ál\0óu", 1, 0)) == "")
  70. assert(string.char(string.byte("ál\0óu", -10, 100)) == "ál\0óu")
  71. print('+')
  72. assert(string.upper("ab\0c") == "AB\0C")
  73. assert(string.lower("\0ABCc%$") == "\0abcc%$")
  74. assert(string.rep('teste', 0) == '')
  75. assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê')
  76. assert(string.rep('', 10) == '')
  77. assert(string.reverse"" == "")
  78. assert(string.reverse"\0\1\2\3" == "\3\2\1\0")
  79. assert(string.reverse"\0001234" == "4321\0")
  80. for i=0,30 do assert(string.len(string.rep('a', i)) == i) end
  81. assert(type(tostring(nil)) == 'string')
  82. assert(type(tostring(12)) == 'string')
  83. assert(''..12 == '12' and type(12 .. '') == 'string')
  84. assert(string.find(tostring{}, 'table:'))
  85. assert(string.find(tostring(print), 'function:'))
  86. assert(tostring(1234567890123) == '1234567890123')
  87. assert(#tostring('\0') == 1)
  88. assert(tostring(true) == "true")
  89. assert(tostring(false) == "false")
  90. print('+')
  91. x = '"ílo"\n\\'
  92. -- assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
  93. -- assert(string.format('%q', "\0") == [["\000"]])
  94. --assert(string.format("\0%c\0%c%x\0", string.byte("á"), string.byte("b"), 140) ==
  95. -- "\0á\0b8c\0")
  96. assert(string.format('') == "")
  97. assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
  98. string.format("%c%c%c%c", 34, 48, 90, 100))
  99. assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
  100. assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
  101. assert(tonumber(string.format("%f", 10.3)) == 10.3)
  102. x = string.format('"%-50s"', 'a')
  103. assert(#x == 52)
  104. assert(string.sub(x, 1, 4) == '"a ')
  105. assert(string.format("-%.20s.20s", string.rep("%", 2000)) == "-"..string.rep("%", 20)..".20s")
  106. assert(string.format('"-%20s.20s"', string.rep("%", 2000)) ==
  107. string.format("%q", "-"..string.rep("%", 2000)..".20s"))
  108. -- longest number that can be formated
  109. assert(string.len(string.format('%99.99f', -1e308)) >= 100)
  110. -- assert(loadstring("return 1\n--comentário sem EOL no final")() == 1)
  111. assert(table.concat{} == "")
  112. assert(table.concat({}, 'x') == "")
  113. assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2")
  114. local a = {}; for i=1,3000 do a[i] = "xuxu" end
  115. assert(table.concat(a, "123").."123" == string.rep("xuxu123", 3000))
  116. assert(table.concat(a, "b", 20, 20) == "xuxu")
  117. assert(table.concat(a, "", 20, 21) == "xuxuxuxu")
  118. assert(table.concat(a, "", 22, 21) == "")
  119. assert(table.concat(a, "3", 2999) == "xuxu3xuxu")
  120. a = {"a","b","c"}
  121. assert(table.concat(a, ",", 1, 0) == "")
  122. assert(table.concat(a, ",", 1, 1) == "a")
  123. assert(table.concat(a, ",", 1, 2) == "a,b")
  124. assert(table.concat(a, ",", 2) == "b,c")
  125. assert(table.concat(a, ",", 3) == "c")
  126. assert(table.concat(a, ",", 4) == "")
  127. local locales = { "ptb", "ISO-8859-1", "pt_BR" }
  128. local function trylocale (w)
  129. for _, l in ipairs(locales) do
  130. if os.setlocale(l, w) then return true end
  131. end
  132. return false
  133. end
  134. if not trylocale("collate") then
  135. print("locale not supported")
  136. else
  137. assert("alo" < "álo" and "álo" < "amo")
  138. end
  139. if not trylocale("ctype") then
  140. print("locale not supported")
  141. else
  142. assert(string.gsub("áéíóú", "%a", "x") == "xxxxx")
  143. assert(string.gsub("áÁéÉ", "%l", "x") == "xÁxÉ")
  144. assert(string.gsub("áÁéÉ", "%u", "x") == "áxéx")
  145. assert(string.upper"áÁé{xuxu}ção" == "ÁÁÉ{XUXU}ÇÃO")
  146. end
  147. -- os.setlocale("C")
  148. -- assert(os.setlocale() == 'C')
  149. -- assert(os.setlocale(nil, "numeric") == 'C')
  150. print('OK')