Advertisement
Guest User

Lua __mode test

a guest
Oct 19th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. local test = function (mode, t, k, v, k2, v2)
  2.     print('', 'k == ' .. tostring(k), 'v == ' .. tostring(v))
  3.    print('', 'k2 == ' .. tostring(k2), 'v2 == ' .. tostring(v2))
  4.     collectgarbage()
  5.     for k,v in pairs(t) do print('',k,v) end
  6.     print('')
  7. end
  8. local testmt = function (mode)
  9.     local t = setmetatable({},{__mode = mode})
  10.     local k, k2, v, v2 = {},{},{},{}
  11.     t[k] = v
  12.     t[k2] = v2
  13.     t[v] = k2
  14.     t[v2] = k
  15.    
  16.     print('testing mode ' .. mode)
  17.    
  18.     table.insert(t,k); print('t[1] == k')
  19.     table.insert(t,k2); print('t[2] == k2')
  20.     table.insert(t,v); print('t[3] == v')
  21.     table.insert(t,v2); print('t[4] == v2')
  22.    
  23.     test(mode,t,k,v,k2,v2)
  24.    
  25.     k = nil
  26.     test(mode,t,k,v,k2,v2)
  27.    
  28.     v2 = nil
  29.     test(mode,t,k,v,k2,v2)
  30.    
  31.     k2 = nil
  32.     test(mode,t,k,v,k2,v2)
  33.    
  34.     v = nil
  35.     test(mode,t,k,v,k2,v2)
  36.     print('mode ' .. mode .. ' complete.')
  37.     print('')
  38. end
  39.  
  40. for _,v in ipairs{'k','v','kv'} do testmt(v) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement