github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/tablelib/lua/tablelib.lua (about)

     1  do
     2      print(pcall(table.concat))
     3      --> ~^false\t.*value needed
     4  
     5      print(pcall(table.concat, "not a table"))
     6      --> ~^false\t.*must be a table
     7  
     8      print(pcall(table.concat, {}, 1))
     9      --> ~^false\t.*must be a string
    10  
    11      print(pcall(table.concat, {}, "--", false))
    12      --> ~^false\t.*must be an integer
    13  
    14      print(pcall(table.concat, {}, "--", 1, {}))
    15      --> ~^false\t.*must be an integer
    16  
    17      local t = {1, 2, 3}
    18  
    19      print(table.concat(t))
    20      --> =123
    21  
    22      print(table.concat(t, "--"))
    23      --> =1--2--3
    24  
    25      print(table.concat({}))
    26      --> =
    27  
    28      print(type(table.concat({})))
    29      --> =string
    30  
    31      print(table.concat({"foo"}))
    32      --> =foo
    33  
    34      print(table.concat(t, "", 2, 3))
    35      --> =23
    36  
    37      print(table.concat(t, "", 2, 2))
    38      --> =2
    39  
    40      print(table.concat(t, "", 3, 2))
    41      --> =
    42  
    43      t[-1]="hel"
    44      t[0]="lo"
    45      print(table.concat(t, "", -1, 1))
    46      --> =hello1
    47  
    48      print(pcall(table.concat, t, "", 2, 5))
    49      --> ~^false\t.*
    50  
    51      print(pcall(table.concat, {}, " ", 10, 10))
    52      --> ~false\t.*at index 10
    53  end
    54  
    55  do
    56      print(pcall(table.insert))
    57      --> ~^false\t.*2 arguments needed
    58  
    59      print(pcall(table.insert, 1, false))
    60      --> ~^false\t.*must be a table
    61  
    62      print(pcall(table.insert, {}, "hello", true))
    63      --> ~^false\t.*must be an integer
    64  
    65      local t = {1, 2, 3}
    66      table.insert(t, "foo")
    67      print(t[4])
    68      --> =foo
    69  
    70      table.insert(t, 2, 42)
    71      print(t[2], t[3], #t)
    72      --> =42	2	5
    73  
    74      print(pcall(table.insert, t, -1, 1))
    75      --> ~^false\t.*
    76  
    77      local tt = {}
    78      setmetatable(tt, {
    79          __len=function() return 3 end,
    80          __index=function() error("g", 0) end
    81      })
    82      print(pcall(table.insert, tt, 1, 12))
    83      --> =false	g
    84  
    85      local tt = {}
    86      setmetatable(tt, {
    87          __len=function() return 3 end,
    88          __index=function() return 2 end,
    89          __newindex=function() error("s", 0) end
    90      })
    91      print(pcall(table.insert, tt, 1, 12))
    92      --> =false	s
    93  
    94      print(pcall(table.insert, tt, 4, 123))
    95      --> =false	s
    96  end
    97  
    98  do
    99      print(pcall(table.move))
   100      --> ~^false\t.*4 arguments needed
   101  
   102      print(pcall(table.move, 1, 2, 3, 4))
   103      --> ~^false\t.*must be a table
   104  
   105      print(pcall(table.move, {}, true, 3, 4))
   106      --> ~^false\t.*must be an integer
   107  
   108      print(pcall(table.move, {}, 2, false, 4))
   109      --> ~^false\t.*must be an integer
   110  
   111      print(pcall(table.move, {}, 2, 3, "xxx"))
   112      --> ~^false\t.*must be an integer
   113  
   114      print(pcall(table.move, {}, 1, 2, 3, "bar"))
   115      --> ~^false\t.*must be a table
   116  
   117      local t = {1, 2, 3, 4}
   118      table.move(t, 2, 4, 3)
   119      print(table.concat(t))
   120      --> =12234
   121  
   122      table.move(t, 3, 5, 2)
   123      print(table.concat(t))
   124      --> =12344
   125  
   126      local u = {}
   127      print(table.concat(table.move(t, 1, 4, 1, u)))
   128      --> =1234
   129  
   130      -- Edge conditions
   131      print(pcall(table.move, {}, 0, 10, math.maxinteger - 9))
   132      --> ~false\t.*wrap around
   133  
   134      print(pcall(table.move, {}, -10, math.maxinteger, -20))
   135      --> ~false\t.*interval too large
   136  end
   137  
   138  do
   139      local t = table.pack(3, 2, 1, 4, 5)
   140      print(t.n, #t)
   141      --> =5	5
   142      print(table.concat(t))
   143      --> =32145
   144  end
   145  
   146  do
   147      print(pcall(table.remove))
   148      --> ~^false\t.*value needed
   149  
   150      print(pcall(table.remove, 1))
   151      --> ~^false\t.*must be a table
   152  
   153      print(pcall(table.remove, {}, true))
   154      --> ~^false\t.*must be an integer
   155  
   156      print(pcall(table.remove, {}, 2))
   157      --> ~^false\t.*out of range
   158  
   159      local t = {1, 2, 3, 4, 5}
   160  
   161      print(#t)
   162      --> =5
   163  
   164      print(table.remove(t))
   165      --> =5
   166  
   167      print(#t)
   168      --> =4
   169  
   170      print(table.remove(t))
   171      --> =4
   172  
   173      print(t[4])
   174      --> =nil
   175  
   176      print(#t)
   177      --> =3
   178  
   179      print(table.remove(t, 2))
   180      --> =2
   181  
   182      print(table.concat(t))
   183      --> =13
   184  
   185      print(table.remove({}))
   186      --> =nil
   187  
   188      print(table.remove({}, 0))
   189      --> =nil
   190  
   191      print(table.remove({}, 1))
   192      --> =nil
   193  
   194      print(table.remove(t, 3))
   195      --> =nil
   196            
   197      local tt = {}
   198      setmetatable(tt, {
   199          __len=function() return 3 end,
   200          __index=function() error("g", 0) end
   201      })
   202      print(pcall(table.remove, tt))
   203      --> =false	g
   204  
   205      local tt = {}
   206      setmetatable(tt, {
   207          __len=function() return 3 end,
   208          __index=function(n, i) return -i end,
   209          __newindex=function() error("s", 0) end
   210      })
   211      print(pcall(table.remove, tt))
   212      --> =false	s
   213  
   214      print(pcall(table.remove, tt, 2))
   215      --> =false	s
   216  end
   217  
   218  do
   219      print(pcall(table.sort))
   220      --> ~^false\t.*value needed
   221  
   222      print(pcall(table.sort, 1))
   223      --> ~^false\t.*must be a table
   224  
   225      local t = {3, 2, 4, 1, 5}
   226      table.sort(t)
   227      print(table.concat(t))
   228      --> =12345
   229  
   230      table.sort(t, function(x, y) return x > y end)
   231      print(table.concat(t))
   232      --> =54321
   233  
   234      local t = {"bar", "bat", "fur", "ball", "four"}
   235      table.sort(t)
   236      print(table.concat(t, " "))
   237      --> =ball bar bat four fur
   238  
   239      local tt = {}
   240      setmetatable(tt, {
   241          __len=function() return 3 end,
   242          __index=function() error("g", 0) end
   243      })
   244      print(pcall(table.sort, tt))
   245      --> =false	g
   246  
   247      local tt = {}
   248      setmetatable(tt, {
   249          __len=function() return 3 end,
   250          __index=function(n, i) return -i end,
   251          __newindex=function() error("s", 0) end
   252      })
   253      print(pcall(table.sort, tt))
   254      --> =false	s
   255  end
   256  
   257  do
   258      print(pcall(table.unpack))
   259      --> ~^false\t.*value needed
   260  
   261      print(pcall(table.unpack, 1))
   262      --> ~^false\t.*must be a table
   263  
   264      print(pcall(table.unpack, {}, "a"))
   265      --> ~^false\t.*must be an integer
   266  
   267      print(pcall(table.unpack, {}, 2, "a"))
   268      --> ~^false\t.*must be an integer
   269  
   270      print(table.unpack({3, 4, 1, 5}))
   271      --> =3	4	1	5
   272  
   273      print(table.unpack({1, 2, 3, 4, 5, 6}, 3, 5))
   274      --> =3	4	5
   275  
   276      print(table.unpack({3, 2, 1}, 3, 5))
   277      --> =1	nil	nil
   278  
   279      print(table.unpack({4, 3, 2}, -1, 1))
   280      --> =nil	nil	4
   281  
   282      local tt = {}
   283      setmetatable(tt, {
   284          __len=function() return 3 end,
   285          __index=function() error("g") end
   286      })
   287      print(pcall(table.unpack, tt))
   288      --> ~false\t.* g
   289  end