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