1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Add API for restoring PseudoRandom and PcgRandom state (#14123)

This commit is contained in:
sfence 2024-01-16 23:20:52 +01:00 committed by GitHub
parent 8093044f07
commit ceaa7e2fb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 155 additions and 29 deletions

View file

@ -27,6 +27,7 @@ read_globals = {
"Settings",
"check",
"PseudoRandom",
"PcgRandom",
string = {fields = {"split", "trim"}},
table = {fields = {"copy", "getn", "indexof", "insert_all"}},

View file

@ -1,15 +1,37 @@
local function test_random()
local function test_pseudo_random()
-- We have comprehensive unit tests in C++, this is just to make sure the API code isn't messing up
local pr = PseudoRandom(13)
assert(pr:next() == 22290)
assert(pr:next() == 13854)
local gen1 = PseudoRandom(13)
assert(gen1:next() == 22290)
assert(gen1:next() == 13854)
local pr2 = PseudoRandom(-101)
assert(pr2:next(0, 100) == 35)
local gen2 = PseudoRandom(gen1:get_state())
for n = 0, 16 do
assert(gen1:next() == gen2:next())
end
local pr3 = PseudoRandom(-101)
assert(pr3:next(0, 100) == 35)
-- unusual case that is normally disallowed:
assert(pr2:next(10000, 42767) == 12485)
assert(pr3:next(10000, 42767) == 12485)
end
unittests.register("test_random", test_random)
unittests.register("test_pseudo_random", test_pseudo_random)
local function test_pcg_random()
-- We have comprehensive unit tests in C++, this is just to make sure the API code isn't messing up
local gen1 = PcgRandom(55)
for n = 0, 16 do
gen1:next()
end
local gen2 = PcgRandom(26)
gen2:set_state(gen1:get_state())
for n = 16, 32 do
assert(gen1:next() == gen2:next())
end
end
unittests.register("test_pcg_random", test_pcg_random)
local function test_dynamic_media(cb, player)
if core.get_player_information(player:get_player_name()).protocol_version < 40 then