common.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "ERROR: 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("collect")
  115. local t = gettimeofday()
  116. for i = 1, iter do
  117. func(i)
  118. end
  119. t = gettimeofday() - t
  120. return (iter / t)
  121. end
  122. local test_results = {}
  123. for name, func in pairs(tests) do
  124. -- k(number), v(string)
  125. -- k(string), v(function)
  126. -- k(number), v(function)
  127. if type(func) == "string" then
  128. name = func
  129. func = _G[name]
  130. end
  131. local result = {}
  132. for i = 1, rep do
  133. result[i] = bench(func, iter)
  134. end
  135. table.sort(result)
  136. test_results[name] = result[rep]
  137. end
  138. return test_results
  139. end
  140. function compare_values(val1, val2)
  141. local type1 = type(val1)
  142. local type2 = type(val2)
  143. if type1 ~= type2 then
  144. return false
  145. end
  146. -- Check for NaN
  147. if type1 == "number" and val1 ~= val1 and val2 ~= val2 then
  148. return true
  149. end
  150. if type1 ~= "table" then
  151. return val1 == val2
  152. end
  153. -- check_keys stores all the keys that must be checked in val2
  154. local check_keys = {}
  155. for k, _ in pairs(val1) do
  156. check_keys[k] = true
  157. end
  158. for k, v in pairs(val2) do
  159. if not check_keys[k] then
  160. return false
  161. end
  162. if not compare_values(val1[k], val2[k]) then
  163. return false
  164. end
  165. check_keys[k] = nil
  166. end
  167. for k, _ in pairs(check_keys) do
  168. -- Not the same if any keys from val1 were not found in val2
  169. return false
  170. end
  171. return true
  172. end
  173. function run_test(testname, func, input, should_work, output)
  174. local function status_line(name, status, value)
  175. local statusmap = { [true] = ":success", [false] = ":error" }
  176. if status ~= nil then
  177. name = name .. statusmap[status]
  178. end
  179. print(string.format("[%s] %s", name, serialise_value(value, false)))
  180. end
  181. local result = { pcall(func, unpack(input)) }
  182. local success = table.remove(result, 1)
  183. local correct = false
  184. if success == should_work and compare_values(result, output) then
  185. correct = true
  186. end
  187. local teststatus = { [true] = "PASS", [false] = "FAIL" }
  188. print("==> Test " .. testname .. ": " .. teststatus[correct])
  189. status_line("Input", nil, input)
  190. if not correct then
  191. status_line("Expected", should_work, output)
  192. end
  193. status_line("Received", success, result)
  194. print()
  195. return correct, result
  196. end
  197. function run_test_group(testgroup, tests)
  198. for k, v in ipairs(tests) do
  199. if type(v) == "function" then
  200. -- Useful for changing configuration during a batch
  201. msg = v()
  202. print(string.format("==> Config %s [%d]: %s", testgroup, k, msg))
  203. print()
  204. elseif type(v) == "table" then
  205. run_test(testgroup .. " [" .. k .. "]", unpack(v))
  206. else
  207. error("Testgroup can only contain functions and tables")
  208. end
  209. end
  210. end
  211. -- vi:ai et sw=4 ts=4: