1
0

table.lua 719 B

12345678910111213141516171819202122232425262728293031323334353637
  1. local a = {}
  2. assert(table.maxn(a) == 0)
  3. a["key"] = 1
  4. assert(table.maxn(a) == 0)
  5. table.insert(a, 10)
  6. table.insert(a, 3, 10)
  7. assert(table.maxn(a) == 3)
  8. local ok, msg = pcall(function()
  9. table.insert(a)
  10. end)
  11. assert(not ok and string.find(msg, "wrong number of arguments"))
  12. a = {}
  13. a["key0"] = "0"
  14. a["key1"] = "1"
  15. a[1] = 1
  16. a[2] = 2
  17. a[true] = "true"
  18. a[false] = "false"
  19. for k, v in pairs(a) do
  20. if k == "key0" then
  21. assert(v == "0")
  22. elseif k == "key1" then
  23. assert(v == "1")
  24. elseif k == 1 then
  25. assert(v == 1)
  26. elseif k == 2 then
  27. assert(v == 2)
  28. elseif k == true then
  29. assert(v == "true")
  30. elseif k == false then
  31. assert(v == "false")
  32. else
  33. error("unexpected key:" .. tostring(k))
  34. end
  35. end