misc.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. local function test_random()
  2. -- Try out PseudoRandom
  3. local pseudo = PseudoRandom(13)
  4. assert(pseudo:next() == 22290)
  5. assert(pseudo:next() == 13854)
  6. end
  7. unittests.register("test_random", test_random)
  8. local function test_dynamic_media(cb, player)
  9. if core.get_player_information(player:get_player_name()).protocol_version < 40 then
  10. core.log("warning", "test_dynamic_media: Client too old, skipping test.")
  11. return cb()
  12. end
  13. -- Check that the client acknowledges media transfers
  14. local path = core.get_worldpath() .. "/test_media.obj"
  15. local f = io.open(path, "w")
  16. f:write("# contents don't matter\n")
  17. f:close()
  18. local call_ok = false
  19. local ok = core.dynamic_add_media({
  20. filepath = path,
  21. to_player = player:get_player_name(),
  22. }, function(name)
  23. if not call_ok then
  24. return cb("impossible condition")
  25. end
  26. cb()
  27. end)
  28. if not ok then
  29. return cb("dynamic_add_media() returned error")
  30. end
  31. call_ok = true
  32. -- if the callback isn't called this test will just hang :shrug:
  33. end
  34. unittests.register("test_dynamic_media", test_dynamic_media, {async=true, player=true})
  35. local function test_v3f_metatable(player)
  36. assert(vector.check(player:get_pos()))
  37. end
  38. unittests.register("test_v3f_metatable", test_v3f_metatable, {player=true})
  39. local function test_v3s16_metatable(player, pos)
  40. local node = minetest.get_node(pos)
  41. local found_pos = minetest.find_node_near(pos, 0, node.name, true)
  42. assert(vector.check(found_pos))
  43. end
  44. unittests.register("test_v3s16_metatable", test_v3s16_metatable, {map=true})
  45. local function test_clear_meta(_, pos)
  46. local ref = core.get_meta(pos)
  47. for way = 1, 3 do
  48. ref:set_string("foo", "bar")
  49. assert(ref:contains("foo"))
  50. if way == 1 then
  51. ref:from_table({})
  52. elseif way == 2 then
  53. ref:from_table(nil)
  54. else
  55. ref:set_string("foo", "")
  56. end
  57. assert(#core.find_nodes_with_meta(pos, pos) == 0, "clearing failed " .. way)
  58. end
  59. end
  60. unittests.register("test_clear_meta", test_clear_meta, {map=true})
  61. local on_punch_called
  62. minetest.register_on_punchnode(function()
  63. on_punch_called = true
  64. end)
  65. unittests.register("test_punch_node", function(_, pos)
  66. minetest.place_node(pos, {name="basenodes:dirt"})
  67. on_punch_called = false
  68. minetest.punch_node(pos)
  69. minetest.remove_node(pos)
  70. -- currently failing: assert(on_punch_called)
  71. end, {map=true})
  72. local function test_compress()
  73. -- This text should be compressible, to make sure the results are... normal
  74. 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."
  75. local methods = {
  76. "deflate",
  77. "zstd",
  78. -- "noodle", -- for warning alarm test
  79. }
  80. local zstd_magic = string.char(0x28, 0xB5, 0x2F, 0xFD)
  81. for _, method in ipairs(methods) do
  82. local compressed = core.compress(text, method)
  83. assert(core.decompress(compressed, method) == text, "input/output mismatch for compression method " .. method)
  84. local has_zstd_magic = compressed:sub(1, 4) == zstd_magic
  85. if method == "zstd" then
  86. assert(has_zstd_magic, "zstd magic number not in zstd method")
  87. else
  88. assert(not has_zstd_magic, "zstd magic number in method " .. method .. " (which is not zstd)")
  89. end
  90. end
  91. end
  92. unittests.register("test_compress", test_compress)
  93. local function test_game_info()
  94. local info = minetest.get_game_info()
  95. local game_conf = Settings(info.path .. "/game.conf")
  96. assert(info.id == "devtest")
  97. assert(info.title == game_conf:get("title"))
  98. end
  99. unittests.register("test_game_info", test_game_info)
  100. local function test_mapgen_edges(cb)
  101. -- Test that the map can extend to the expected edges and no further.
  102. local min_edge, max_edge = minetest.get_mapgen_edges()
  103. local min_finished = {}
  104. local max_finished = {}
  105. local function finish()
  106. if #min_finished ~= 1 then
  107. return cb("Expected 1 block to emerge around mapgen minimum edge")
  108. end
  109. if min_finished[1] ~= (min_edge / minetest.MAP_BLOCKSIZE):floor() then
  110. return cb("Expected block within minimum edge to emerge")
  111. end
  112. if #max_finished ~= 1 then
  113. return cb("Expected 1 block to emerge around mapgen maximum edge")
  114. end
  115. if max_finished[1] ~= (max_edge / minetest.MAP_BLOCKSIZE):floor() then
  116. return cb("Expected block within maximum edge to emerge")
  117. end
  118. return cb()
  119. end
  120. local emerges_left = 2
  121. local function emerge_block(blockpos, action, blocks_left, finished)
  122. if action ~= minetest.EMERGE_CANCELLED then
  123. table.insert(finished, blockpos)
  124. end
  125. if blocks_left == 0 then
  126. emerges_left = emerges_left - 1
  127. if emerges_left == 0 then
  128. return finish()
  129. end
  130. end
  131. end
  132. minetest.emerge_area(min_edge:subtract(1), min_edge, emerge_block, min_finished)
  133. minetest.emerge_area(max_edge, max_edge:add(1), emerge_block, max_finished)
  134. end
  135. unittests.register("test_mapgen_edges", test_mapgen_edges, {map=true, async=true})
  136. local finish_test_on_mapblocks_changed
  137. minetest.register_on_mapblocks_changed(function(modified_blocks, modified_block_count)
  138. if finish_test_on_mapblocks_changed then
  139. finish_test_on_mapblocks_changed(modified_blocks, modified_block_count)
  140. finish_test_on_mapblocks_changed = nil
  141. end
  142. end)
  143. local function test_on_mapblocks_changed(cb, player, pos)
  144. local bp1 = (pos / minetest.MAP_BLOCKSIZE):floor()
  145. local bp2 = bp1:add(1)
  146. for _, bp in ipairs({bp1, bp2}) do
  147. -- Make a modification in the block.
  148. local p = bp * minetest.MAP_BLOCKSIZE
  149. minetest.load_area(p)
  150. local meta = minetest.get_meta(p)
  151. meta:set_int("test_on_mapblocks_changed", meta:get_int("test_on_mapblocks_changed") + 1)
  152. end
  153. finish_test_on_mapblocks_changed = function(modified_blocks, modified_block_count)
  154. if modified_block_count < 2 then
  155. return cb("Expected at least two mapblocks to be recorded as modified")
  156. end
  157. if not modified_blocks[minetest.hash_node_position(bp1)] or
  158. not modified_blocks[minetest.hash_node_position(bp2)] then
  159. return cb("The expected mapblocks were not recorded as modified")
  160. end
  161. cb()
  162. end
  163. end
  164. unittests.register("test_on_mapblocks_changed", test_on_mapblocks_changed, {map=true, async=true})