table_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package lua
  2. import (
  3. "testing"
  4. )
  5. func TestTableNewLTable(t *testing.T) {
  6. tbl := newLTable(-1, -2)
  7. errorIfNotEqual(t, 0, cap(tbl.array))
  8. tbl = newLTable(10, 9)
  9. errorIfNotEqual(t, 10, cap(tbl.array))
  10. }
  11. func TestTableLen(t *testing.T) {
  12. tbl := newLTable(0, 0)
  13. tbl.RawSetInt(10, LNil)
  14. tbl.RawSetInt(9, LNumber(10))
  15. tbl.RawSetInt(8, LNil)
  16. tbl.RawSetInt(7, LNumber(10))
  17. errorIfNotEqual(t, 9, tbl.Len())
  18. tbl = newLTable(0, 0)
  19. tbl.Append(LTrue)
  20. tbl.Append(LTrue)
  21. tbl.Append(LTrue)
  22. errorIfNotEqual(t, 3, tbl.Len())
  23. }
  24. func TestTableLenType(t *testing.T) {
  25. L := NewState(Options{})
  26. err := L.DoString(`
  27. mt = {
  28. __index = mt,
  29. __len = function (self)
  30. return {hello = "world"}
  31. end
  32. }
  33. v = {}
  34. v.__index = v
  35. setmetatable(v, mt)
  36. assert(#v ~= 0, "#v should return a table reference in this case")
  37. print(#v)
  38. `)
  39. if err != nil {
  40. t.Error(err)
  41. }
  42. }
  43. func TestTableAppend(t *testing.T) {
  44. tbl := newLTable(0, 0)
  45. tbl.RawSetInt(1, LNumber(1))
  46. tbl.RawSetInt(2, LNumber(2))
  47. tbl.RawSetInt(3, LNumber(3))
  48. errorIfNotEqual(t, 3, tbl.Len())
  49. tbl.RawSetInt(1, LNil)
  50. tbl.RawSetInt(2, LNil)
  51. errorIfNotEqual(t, 3, tbl.Len())
  52. tbl.Append(LNumber(4))
  53. errorIfNotEqual(t, 4, tbl.Len())
  54. tbl.RawSetInt(3, LNil)
  55. tbl.RawSetInt(4, LNil)
  56. errorIfNotEqual(t, 0, tbl.Len())
  57. tbl.Append(LNumber(5))
  58. errorIfNotEqual(t, 1, tbl.Len())
  59. }
  60. func TestTableInsert(t *testing.T) {
  61. tbl := newLTable(0, 0)
  62. tbl.Append(LTrue)
  63. tbl.Append(LTrue)
  64. tbl.Append(LTrue)
  65. tbl.Insert(5, LFalse)
  66. errorIfNotEqual(t, LFalse, tbl.RawGetInt(5))
  67. errorIfNotEqual(t, 5, tbl.Len())
  68. tbl.Insert(-10, LFalse)
  69. errorIfNotEqual(t, LFalse, tbl.RawGet(LNumber(-10)))
  70. errorIfNotEqual(t, 5, tbl.Len())
  71. tbl = newLTable(0, 0)
  72. tbl.Append(LNumber(1))
  73. tbl.Append(LNumber(2))
  74. tbl.Append(LNumber(3))
  75. tbl.Insert(1, LNumber(10))
  76. errorIfNotEqual(t, LNumber(10), tbl.RawGetInt(1))
  77. errorIfNotEqual(t, LNumber(1), tbl.RawGetInt(2))
  78. errorIfNotEqual(t, LNumber(2), tbl.RawGetInt(3))
  79. errorIfNotEqual(t, LNumber(3), tbl.RawGetInt(4))
  80. errorIfNotEqual(t, 4, tbl.Len())
  81. tbl = newLTable(0, 0)
  82. tbl.Insert(5, LNumber(10))
  83. errorIfNotEqual(t, LNumber(10), tbl.RawGetInt(5))
  84. }
  85. func TestTableMaxN(t *testing.T) {
  86. tbl := newLTable(0, 0)
  87. tbl.Append(LTrue)
  88. tbl.Append(LTrue)
  89. tbl.Append(LTrue)
  90. errorIfNotEqual(t, 3, tbl.MaxN())
  91. tbl = newLTable(0, 0)
  92. errorIfNotEqual(t, 0, tbl.MaxN())
  93. tbl = newLTable(10, 0)
  94. errorIfNotEqual(t, 0, tbl.MaxN())
  95. }
  96. func TestTableRemove(t *testing.T) {
  97. tbl := newLTable(0, 0)
  98. errorIfNotEqual(t, LNil, tbl.Remove(10))
  99. tbl.Append(LTrue)
  100. errorIfNotEqual(t, LNil, tbl.Remove(10))
  101. tbl.Append(LFalse)
  102. tbl.Append(LTrue)
  103. errorIfNotEqual(t, LFalse, tbl.Remove(2))
  104. errorIfNotEqual(t, 2, tbl.MaxN())
  105. tbl.Append(LFalse)
  106. errorIfNotEqual(t, LFalse, tbl.Remove(-1))
  107. errorIfNotEqual(t, 2, tbl.MaxN())
  108. }
  109. func TestTableRawSetInt(t *testing.T) {
  110. tbl := newLTable(0, 0)
  111. tbl.RawSetInt(MaxArrayIndex+1, LTrue)
  112. errorIfNotEqual(t, 0, tbl.MaxN())
  113. errorIfNotEqual(t, LTrue, tbl.RawGet(LNumber(MaxArrayIndex+1)))
  114. tbl.RawSetInt(1, LTrue)
  115. tbl.RawSetInt(3, LTrue)
  116. errorIfNotEqual(t, 3, tbl.MaxN())
  117. errorIfNotEqual(t, LTrue, tbl.RawGetInt(1))
  118. errorIfNotEqual(t, LNil, tbl.RawGetInt(2))
  119. errorIfNotEqual(t, LTrue, tbl.RawGetInt(3))
  120. tbl.RawSetInt(2, LTrue)
  121. errorIfNotEqual(t, LTrue, tbl.RawGetInt(1))
  122. errorIfNotEqual(t, LTrue, tbl.RawGetInt(2))
  123. errorIfNotEqual(t, LTrue, tbl.RawGetInt(3))
  124. }
  125. func TestTableRawSetH(t *testing.T) {
  126. tbl := newLTable(0, 0)
  127. tbl.RawSetH(LString("key"), LTrue)
  128. tbl.RawSetH(LString("key"), LNil)
  129. _, found := tbl.dict[LString("key")]
  130. errorIfNotEqual(t, false, found)
  131. tbl.RawSetH(LTrue, LTrue)
  132. tbl.RawSetH(LTrue, LNil)
  133. _, foundb := tbl.dict[LTrue]
  134. errorIfNotEqual(t, false, foundb)
  135. }
  136. func TestTableRawGetH(t *testing.T) {
  137. tbl := newLTable(0, 0)
  138. errorIfNotEqual(t, LNil, tbl.RawGetH(LNumber(1)))
  139. errorIfNotEqual(t, LNil, tbl.RawGetH(LString("key0")))
  140. tbl.RawSetH(LString("key0"), LTrue)
  141. tbl.RawSetH(LString("key1"), LFalse)
  142. tbl.RawSetH(LNumber(1), LTrue)
  143. errorIfNotEqual(t, LTrue, tbl.RawGetH(LString("key0")))
  144. errorIfNotEqual(t, LTrue, tbl.RawGetH(LNumber(1)))
  145. errorIfNotEqual(t, LNil, tbl.RawGetH(LString("notexist")))
  146. errorIfNotEqual(t, LNil, tbl.RawGetH(LTrue))
  147. }
  148. func TestTableForEach(t *testing.T) {
  149. tbl := newLTable(0, 0)
  150. tbl.Append(LNumber(1))
  151. tbl.Append(LNumber(2))
  152. tbl.Append(LNumber(3))
  153. tbl.Append(LNil)
  154. tbl.Append(LNumber(5))
  155. tbl.RawSetH(LString("a"), LString("a"))
  156. tbl.RawSetH(LString("b"), LString("b"))
  157. tbl.RawSetH(LString("c"), LString("c"))
  158. tbl.RawSetH(LTrue, LString("true"))
  159. tbl.RawSetH(LFalse, LString("false"))
  160. tbl.ForEach(func(key, value LValue) {
  161. switch k := key.(type) {
  162. case LBool:
  163. switch bool(k) {
  164. case true:
  165. errorIfNotEqual(t, LString("true"), value)
  166. case false:
  167. errorIfNotEqual(t, LString("false"), value)
  168. default:
  169. t.Fail()
  170. }
  171. case LNumber:
  172. switch int(k) {
  173. case 1:
  174. errorIfNotEqual(t, LNumber(1), value)
  175. case 2:
  176. errorIfNotEqual(t, LNumber(2), value)
  177. case 3:
  178. errorIfNotEqual(t, LNumber(3), value)
  179. case 4:
  180. errorIfNotEqual(t, LNumber(5), value)
  181. default:
  182. t.Fail()
  183. }
  184. case LString:
  185. switch string(k) {
  186. case "a":
  187. errorIfNotEqual(t, LString("a"), value)
  188. case "b":
  189. errorIfNotEqual(t, LString("b"), value)
  190. case "c":
  191. errorIfNotEqual(t, LString("c"), value)
  192. default:
  193. t.Fail()
  194. }
  195. }
  196. })
  197. }