1
0

misc.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. core.register_mapgen_script(core.get_modpath(core.get_current_modname()) ..
  2. DIR_DELIM .. "inside_mapgen_env.lua")
  3. local function test_pseudo_random()
  4. -- We have comprehensive unit tests in C++, this is just to make sure the API code isn't messing up
  5. local gen1 = PseudoRandom(13)
  6. assert(gen1:next() == 22290)
  7. assert(gen1:next() == 13854)
  8. local gen2 = PseudoRandom(gen1:get_state())
  9. for n = 0, 16 do
  10. assert(gen1:next() == gen2:next())
  11. end
  12. local pr3 = PseudoRandom(-101)
  13. assert(pr3:next(0, 100) == 35)
  14. -- unusual case that is normally disallowed:
  15. assert(pr3:next(10000, 42767) == 12485)
  16. end
  17. unittests.register("test_pseudo_random", test_pseudo_random)
  18. local function test_pcg_random()
  19. -- We have comprehensive unit tests in C++, this is just to make sure the API code isn't messing up
  20. local gen1 = PcgRandom(55)
  21. for n = 0, 16 do
  22. gen1:next()
  23. end
  24. local gen2 = PcgRandom(26)
  25. gen2:set_state(gen1:get_state())
  26. for n = 16, 32 do
  27. assert(gen1:next() == gen2:next())
  28. end
  29. end
  30. unittests.register("test_pcg_random", test_pcg_random)
  31. local function test_dynamic_media(cb, player)
  32. if core.get_player_information(player:get_player_name()).protocol_version < 40 then
  33. core.log("warning", "test_dynamic_media: Client too old, skipping test.")
  34. return cb()
  35. end
  36. -- Check that the client acknowledges media transfers
  37. local path = core.get_worldpath() .. "/test_media.obj"
  38. local f = io.open(path, "w")
  39. f:write("# contents don't matter\n")
  40. f:close()
  41. local call_ok = false
  42. local ok = core.dynamic_add_media({
  43. filepath = path,
  44. to_player = player:get_player_name(),
  45. }, function(name)
  46. if not call_ok then
  47. return cb("impossible condition")
  48. end
  49. cb()
  50. end)
  51. if not ok then
  52. return cb("dynamic_add_media() returned error")
  53. end
  54. call_ok = true
  55. -- if the callback isn't called this test will just hang :shrug:
  56. end
  57. unittests.register("test_dynamic_media", test_dynamic_media, {async=true, player=true})
  58. local function test_v3f_metatable(player)
  59. assert(vector.check(player:get_pos()))
  60. end
  61. unittests.register("test_v3f_metatable", test_v3f_metatable, {player=true})
  62. local function test_v3s16_metatable(player, pos)
  63. local node = core.get_node(pos)
  64. local found_pos = core.find_node_near(pos, 0, node.name, true)
  65. assert(vector.check(found_pos))
  66. end
  67. unittests.register("test_v3s16_metatable", test_v3s16_metatable, {map=true})
  68. local function test_clear_meta(_, pos)
  69. local ref = core.get_meta(pos)
  70. for way = 1, 3 do
  71. ref:set_string("foo", "bar")
  72. assert(ref:contains("foo"))
  73. if way == 1 then
  74. ref:from_table({})
  75. elseif way == 2 then
  76. ref:from_table(nil)
  77. else
  78. ref:set_string("foo", "")
  79. end
  80. assert(#core.find_nodes_with_meta(pos, pos) == 0, "clearing failed " .. way)
  81. end
  82. end
  83. unittests.register("test_clear_meta", test_clear_meta, {map=true})
  84. local on_punch_called, on_place_called
  85. core.register_on_placenode(function()
  86. on_place_called = true
  87. end)
  88. core.register_on_punchnode(function()
  89. on_punch_called = true
  90. end)
  91. local function test_node_callbacks(_, pos)
  92. on_place_called = false
  93. on_punch_called = false
  94. core.place_node(pos, {name="basenodes:dirt"})
  95. assert(on_place_called, "on_place not called")
  96. core.punch_node(pos)
  97. assert(on_punch_called, "on_punch not called")
  98. core.remove_node(pos)
  99. end
  100. unittests.register("test_node_callbacks", test_node_callbacks, {map=true})
  101. local function test_hashing()
  102. local input = "hello\000world"
  103. assert(core.sha1(input) == "f85b420f1e43ebf88649dfcab302b898d889606c")
  104. assert(core.sha256(input) == "b206899bc103669c8e7b36de29d73f95b46795b508aa87d612b2ce84bfb29df2")
  105. end
  106. unittests.register("test_hashing", test_hashing)
  107. local function test_compress()
  108. -- This text should be compressible, to make sure the results are... normal
  109. local text = "The\000 icey canoe couldn't move very well on the\128 lake. The\000 ice was too stiff and the icey canoe's paddles simply wouldn't punch through."
  110. local methods = {
  111. "deflate",
  112. "zstd",
  113. -- "noodle", -- for warning alarm test
  114. }
  115. local zstd_magic = string.char(0x28, 0xB5, 0x2F, 0xFD)
  116. for _, method in ipairs(methods) do
  117. local compressed = core.compress(text, method)
  118. assert(core.decompress(compressed, method) == text, "input/output mismatch for compression method " .. method)
  119. local has_zstd_magic = compressed:sub(1, 4) == zstd_magic
  120. if method == "zstd" then
  121. assert(has_zstd_magic, "zstd magic number not in zstd method")
  122. else
  123. assert(not has_zstd_magic, "zstd magic number in method " .. method .. " (which is not zstd)")
  124. end
  125. end
  126. end
  127. unittests.register("test_compress", test_compress)
  128. local function test_urlencode()
  129. -- checks that API code handles null bytes
  130. assert(core.urlencode("foo\000bar!") == "foo%00bar%21")
  131. end
  132. unittests.register("test_urlencode", test_urlencode)
  133. local function test_parse_json()
  134. local raw = "{\"how\\u0000weird\":\n\"yes\\u0000really\",\"n\":-1234567891011,\"z\":null}"
  135. do
  136. local data = core.parse_json(raw)
  137. assert(data["how\000weird"] == "yes\000really")
  138. assert(data.n == -1234567891011)
  139. assert(data.z == nil)
  140. end
  141. do
  142. local null = {}
  143. local data = core.parse_json(raw, null)
  144. assert(data.z == null)
  145. end
  146. do
  147. local data, err = core.parse_json('"ceci n\'est pas un json', nil, true)
  148. assert(data == nil)
  149. assert(type(err) == "string")
  150. end
  151. end
  152. unittests.register("test_parse_json", test_parse_json)
  153. local function test_write_json()
  154. -- deeply nested structures should be preserved
  155. local leaf = 42
  156. local data = leaf
  157. for i = 1, 1000 do
  158. data = {data}
  159. end
  160. local roundtripped = core.parse_json(core.write_json(data))
  161. for i = 1, 1000 do
  162. roundtripped = roundtripped[1]
  163. end
  164. assert(roundtripped == 42)
  165. end
  166. unittests.register("test_write_json", test_write_json)
  167. local function test_game_info()
  168. local info = core.get_game_info()
  169. local game_conf = Settings(info.path .. "/game.conf")
  170. assert(info.id == "devtest")
  171. assert(info.title == game_conf:get("title"))
  172. end
  173. unittests.register("test_game_info", test_game_info)
  174. local function test_mapgen_edges(cb)
  175. -- Test that the map can extend to the expected edges and no further.
  176. local min_edge, max_edge = core.get_mapgen_edges()
  177. local min_finished = {}
  178. local max_finished = {}
  179. local function finish()
  180. if #min_finished ~= 1 then
  181. return cb("Expected 1 block to emerge around mapgen minimum edge")
  182. end
  183. if min_finished[1] ~= (min_edge / core.MAP_BLOCKSIZE):floor() then
  184. return cb("Expected block within minimum edge to emerge")
  185. end
  186. if #max_finished ~= 1 then
  187. return cb("Expected 1 block to emerge around mapgen maximum edge")
  188. end
  189. if max_finished[1] ~= (max_edge / core.MAP_BLOCKSIZE):floor() then
  190. return cb("Expected block within maximum edge to emerge")
  191. end
  192. return cb()
  193. end
  194. local emerges_left = 2
  195. local function emerge_block(blockpos, action, blocks_left, finished)
  196. if action ~= core.EMERGE_CANCELLED then
  197. table.insert(finished, blockpos)
  198. end
  199. if blocks_left == 0 then
  200. emerges_left = emerges_left - 1
  201. if emerges_left == 0 then
  202. return finish()
  203. end
  204. end
  205. end
  206. core.emerge_area(min_edge:subtract(1), min_edge, emerge_block, min_finished)
  207. core.emerge_area(max_edge, max_edge:add(1), emerge_block, max_finished)
  208. end
  209. unittests.register("test_mapgen_edges", test_mapgen_edges, {map=true, async=true})
  210. local finish_test_on_mapblocks_changed
  211. core.register_on_mapblocks_changed(function(modified_blocks, modified_block_count)
  212. if finish_test_on_mapblocks_changed then
  213. finish_test_on_mapblocks_changed(modified_blocks, modified_block_count)
  214. finish_test_on_mapblocks_changed = nil
  215. end
  216. end)
  217. local function test_on_mapblocks_changed(cb, player, pos)
  218. local bp1 = (pos / core.MAP_BLOCKSIZE):floor()
  219. local bp2 = bp1:add(1)
  220. for _, bp in ipairs({bp1, bp2}) do
  221. -- Make a modification in the block.
  222. local p = bp * core.MAP_BLOCKSIZE
  223. core.load_area(p)
  224. local meta = core.get_meta(p)
  225. meta:set_int("test_on_mapblocks_changed", meta:get_int("test_on_mapblocks_changed") + 1)
  226. end
  227. finish_test_on_mapblocks_changed = function(modified_blocks, modified_block_count)
  228. if modified_block_count < 2 then
  229. return cb("Expected at least two mapblocks to be recorded as modified")
  230. end
  231. if not modified_blocks[core.hash_node_position(bp1)] or
  232. not modified_blocks[core.hash_node_position(bp2)] then
  233. return cb("The expected mapblocks were not recorded as modified")
  234. end
  235. cb()
  236. end
  237. end
  238. unittests.register("test_on_mapblocks_changed", test_on_mapblocks_changed, {map=true, async=true})
  239. local function test_gennotify_api()
  240. local DECO_ID = 123
  241. local UD_ID = "unittests:dummy"
  242. -- the engine doesn't check if the id is actually valid, maybe it should
  243. core.set_gen_notify({decoration=true}, {DECO_ID})
  244. core.set_gen_notify({custom=true}, nil, {UD_ID})
  245. local flags, deco, custom = core.get_gen_notify()
  246. local function ff(flag)
  247. return (" " .. flags .. " "):match("[ ,]" .. flag .. "[ ,]") ~= nil
  248. end
  249. assert(ff("decoration"), "'decoration' flag missing")
  250. assert(ff("custom"), "'custom' flag missing")
  251. assert(table.indexof(deco, DECO_ID) > 0)
  252. assert(table.indexof(custom, UD_ID) > 0)
  253. core.set_gen_notify({decoration=false, custom=false})
  254. flags, deco, custom = core.get_gen_notify()
  255. assert(not ff("decoration") and not ff("custom"))
  256. assert(#deco == 0, "deco ids not empty")
  257. assert(#custom == 0, "custom ids not empty")
  258. end
  259. unittests.register("test_gennotify_api", test_gennotify_api)
  260. -- <=> inside_mapgen_env.lua
  261. local function test_mapgen_env(cb)
  262. -- emerge threads start delayed so this can take a second
  263. local res = core.ipc_get("unittests:mg")
  264. if res == nil then
  265. return core.after(0, test_mapgen_env, cb)
  266. end
  267. -- handle error status
  268. if res[1] then
  269. cb()
  270. else
  271. cb(res[2])
  272. end
  273. end
  274. unittests.register("test_mapgen_env", test_mapgen_env, {async=true})
  275. local function test_ipc_vector_preserve(cb)
  276. -- the IPC also uses register_portable_metatable
  277. core.ipc_set("unittests:v", vector.new(4, 0, 4))
  278. local v = core.ipc_get("unittests:v")
  279. assert(type(v) == "table")
  280. assert(vector.check(v))
  281. end
  282. unittests.register("test_ipc_vector_preserve", test_ipc_vector_preserve)
  283. local function test_ipc_poll(cb)
  284. core.ipc_set("unittests:flag", nil)
  285. assert(core.ipc_poll("unittests:flag", 1) == false)
  286. -- Note that unlike the async result callback - which has to wait for the
  287. -- next server step - the IPC is instant
  288. local t0 = core.get_us_time()
  289. core.handle_async(function()
  290. core.ipc_set("unittests:flag", true)
  291. end, function() end)
  292. assert(core.ipc_poll("unittests:flag", 1000) == true, "Wait failed (or slow machine?)")
  293. print("delta: " .. (core.get_us_time() - t0) .. "us")
  294. end
  295. unittests.register("test_ipc_poll", test_ipc_poll)