common.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. require "cjson"
  2. require "posix"
  3. -- Misc routines to assist with CJSON testing
  4. --
  5. -- Mark Pulford <[email protected]>
  6. -- Determine with a Lua table can be treated as an array.
  7. -- Explicitly returns "not an array" for very sparse arrays.
  8. -- Returns:
  9. -- -1 Not an array
  10. -- 0 Empty table
  11. -- >0 Highest index in the array
  12. function is_array(table)
  13. local max = 0
  14. local count = 0
  15. for k, v in pairs(table) do
  16. if type(k) == "number" then
  17. if k > max then max = k end
  18. count = count + 1
  19. else
  20. return -1
  21. end
  22. end
  23. if max > count * 2 then
  24. return -1
  25. end
  26. return max
  27. end
  28. function serialise_table(value, indent, depth)
  29. local spacing, spacing2, indent2
  30. if indent then
  31. spacing = "\n" .. indent
  32. spacing2 = spacing .. " "
  33. indent2 = indent .. " "
  34. else
  35. spacing, spacing2, indent2 = " ", " ", false
  36. end
  37. depth = depth + 1
  38. if depth > 50 then
  39. return "Cannot serialise any further: too many nested tables"
  40. end
  41. local max = is_array(value)
  42. local comma = false
  43. local fragment = { "{" .. spacing2 }
  44. if max > 0 then
  45. -- Serialise array
  46. for i = 1, max do
  47. if comma then
  48. table.insert(fragment, "," .. spacing2)
  49. end
  50. table.insert(fragment, serialise_value(value[i], indent2, depth))
  51. comma = true
  52. end
  53. elseif max < 0 then
  54. -- Serialise table
  55. for k, v in pairs(value) do
  56. if comma then
  57. table.insert(fragment, "," .. spacing2)
  58. end
  59. table.insert(fragment, string.format(
  60. "[%s] = %s", serialise_value(k, indent2, depth),
  61. serialise_value(v, indent2, depth))
  62. )
  63. comma = true
  64. end
  65. end
  66. table.insert(fragment, spacing .. "}")
  67. return table.concat(fragment)
  68. end
  69. function serialise_value(value, indent, depth)
  70. if indent == nil then indent = "" end
  71. if depth == nil then depth = 0 end
  72. if value == cjson.null then
  73. return "cjson.null"
  74. elseif type(value) == "string" then
  75. return string.format("%q", value)
  76. elseif type(value) == "nil" or type(value) == "number" or
  77. type(value) == "boolean" then
  78. return tostring(value)
  79. elseif type(value) == "table" then
  80. return serialise_table(value, indent, depth)
  81. else
  82. return "\"<" .. type(value) .. ">\""
  83. end
  84. end
  85. function dump_value(value)
  86. print(serialise_value(value))
  87. end
  88. function file_load(filename)
  89. local file, err = io.open(filename)
  90. if file == nil then
  91. error("Unable to read " .. filename)
  92. end
  93. local data = file:read("*a")
  94. file:close()
  95. if data == nil then
  96. error("Failed to read " .. filename)
  97. end
  98. return data
  99. end
  100. function file_save(filename, data)
  101. local file, err = io.open(filename, "w")
  102. if file == nil then
  103. error("Unable to write " .. filename)
  104. end
  105. file:write(data)
  106. file:close()
  107. end
  108. function gettimeofday()
  109. local tv_sec, tv_usec = posix.gettimeofday()
  110. return tv_sec + tv_usec / 1000000
  111. end
  112. function benchmark(tests, iter, rep)
  113. local function bench(func, iter)
  114. collectgarbage("stop")
  115. local t = gettimeofday()
  116. for i = 1, iter do
  117. func(i)
  118. end
  119. t = gettimeofday() - t
  120. collectgarbage("restart")
  121. return (iter / t)
  122. end
  123. local test_results = {}
  124. for name, func in pairs(tests) do
  125. -- k(number), v(string)
  126. -- k(string), v(function)
  127. -- k(number), v(function)
  128. if type(func) == "string" then
  129. name = func
  130. func = _G[name]
  131. end
  132. local result = {}
  133. for i = 1, rep do
  134. result[i] = bench(func, iter)
  135. end
  136. table.sort(result)
  137. test_results[name] = result[rep]
  138. end
  139. return test_results
  140. end
  141. function compare_values(val1, val2)
  142. local type1 = type(val1)
  143. local type2 = type(val2)
  144. if type1 ~= type2 then
  145. return false
  146. end
  147. -- Check for NaN
  148. if type1 == "number" and val1 ~= val1 and val2 ~= val2 then
  149. return true
  150. end
  151. if type1 ~= "table" then
  152. return val1 == val2
  153. end
  154. -- check_keys stores all the keys that must be checked in val2
  155. local check_keys = {}
  156. for k, _ in pairs(val1) do
  157. check_keys[k] = true
  158. end
  159. for k, v in pairs(val2) do
  160. if not check_keys[k] then
  161. return false
  162. end
  163. if not compare_values(val1[k], val2[k]) then
  164. return false
  165. end
  166. check_keys[k] = nil
  167. end
  168. for k, _ in pairs(check_keys) do
  169. -- Not the same if any keys from val1 were not found in val2
  170. return false
  171. end
  172. return true
  173. end
  174. function run_test(testname, func, input, should_work, output)
  175. local function status_line(name, status, value)
  176. local statusmap = { [true] = ":success", [false] = ":error" }
  177. if status ~= nil then
  178. name = name .. statusmap[status]
  179. end
  180. print(string.format("[%s] %s", name, serialise_value(value, false)))
  181. end
  182. local result = { pcall(func, unpack(input)) }
  183. local success = table.remove(result, 1)
  184. local correct = false
  185. if success == should_work and compare_values(result, output) then
  186. correct = true
  187. end
  188. local teststatus = { [true] = "PASS", [false] = "FAIL" }
  189. print("==> Test " .. testname .. ": " .. teststatus[correct])
  190. status_line("Input", nil, input)
  191. if not correct then
  192. status_line("Expected", should_work, output)
  193. end
  194. status_line("Received", success, result)
  195. print()
  196. return correct, result
  197. end
  198. function run_test_group(testgroup, tests)
  199. local function run_config(configname, func)
  200. local success, msg = pcall(func)
  201. print(string.format("==> Config %s: %s", configname, msg))
  202. print()
  203. end
  204. local function test_id(group, id)
  205. return string.format("%s [%d]", group, id)
  206. end
  207. for k, v in ipairs(tests) do
  208. if type(v) == "function" then
  209. -- Useful for changing configuration during a batch
  210. run_config(test_id(testgroup, k), v)
  211. elseif type(v) == "table" then
  212. run_test(test_id(testgroup, k), unpack(v))
  213. else
  214. error("Testgroup can only contain functions and tables")
  215. end
  216. end
  217. end
  218. -- vi:ai et sw=4 ts=4: