2929#include " deepmind/lua/call.h"
3030#include " deepmind/lua/n_results_or_test_util.h"
3131#include " deepmind/lua/push_script.h"
32+ #include " deepmind/lua/table_ref.h"
3233#include " deepmind/lua/vm_test_util.h"
3334
3435namespace deepmind {
@@ -45,13 +46,19 @@ class LuaMazeGenerationTest : public lua::testing::TestWithVm {
4546 vm ()->AddCModuleToSearchers (
4647 " dmlab.system.maze_generation" , &lua::Bind<LuaMazeGeneration::Require>,
4748 {reinterpret_cast <void *>(static_cast <std::uintptr_t >(0 ))});
49+ vm ()->AddCModuleToSearchers (
50+ " dmlab.system.maze_generation1" , &lua::Bind<LuaMazeGeneration::Require>,
51+ {reinterpret_cast <void *>(static_cast <std::uintptr_t >(1 ))});
4852 LuaRandom::Register (L);
4953 vm ()->AddCModuleToSearchers (
5054 " dmlab.system.sys_random" , &lua::Bind<LuaRandom::Require>,
51- {&prbg_, reinterpret_cast <void *>(static_cast <std::uintptr_t >(0 ))});
55+ {&prbg_[0 ], reinterpret_cast <void *>(static_cast <std::uintptr_t >(0 ))});
56+ vm ()->AddCModuleToSearchers (
57+ " dmlab.system.sys_random1" , &lua::Bind<LuaRandom::Require>,
58+ {&prbg_[1 ], reinterpret_cast <void *>(static_cast <std::uintptr_t >(1 ))});
5259 }
5360
54- std::mt19937_64 prbg_;
61+ std::mt19937_64 prbg_[ 2 ] ;
5562};
5663
5764constexpr char kCreateMaze [] = R"(
@@ -468,6 +475,65 @@ TEST_F(LuaMazeGenerationTest, CreateRandomMazeNoRandom) {
468475 ASSERT_FALSE (lua::Call (L, 0 ).ok ());
469476}
470477
478+ constexpr int kRandomMazeSeedCount = 10000 ;
479+ constexpr char kCreateRandomMazes [] = R"(
480+ local seed, maps, vers = ...
481+ local sys_random = require('dmlab.system.sys_random' .. vers)
482+ local maze_generation = require('dmlab.system.maze_generation' .. vers)
483+ sys_random:seed(seed)
484+ local maze = maze_generation.randomMazeGeneration{
485+ random = sys_random,
486+ width = 17,
487+ height = 17,
488+ extraConnectionProbability = 0.0,
489+ hasDoors = false,
490+ roomCount = 4,
491+ maxRooms = 4,
492+ roomMaxSize = 5,
493+ roomMinSize = 3
494+ }
495+ local key = maze:entityLayer()
496+ local count = maps[key] or 0
497+ maps[key] = count + 1
498+ )" ;
499+
500+ TEST_F (LuaMazeGenerationTest, CreateRandomMazes) {
501+ auto maps = lua::TableRef::Create (L);
502+ lua::PushScript (L, kCreateRandomMazes , sizeof (kCreateRandomMazes ) - 1 ,
503+ " kCreateRandomMazes" );
504+ for (int i = 0 ; i < kRandomMazeSeedCount ; ++i) {
505+ LOG (INFO) << " Phase 1: step " << i + 1 << " out of "
506+ << kRandomMazeSeedCount ;
507+ lua_pushvalue (L, -1 );
508+ lua::Push (L, i);
509+ lua::Push (L, maps);
510+ lua::Push (L, " " );
511+ ASSERT_THAT (lua::Call (L, 3 ), IsOkAndHolds (0 ));
512+ }
513+ const auto mixer_seed_0_maps = maps.KeyCount ();
514+ const auto mixer_seed_0_repeat_ratio =
515+ (kRandomMazeSeedCount - mixer_seed_0_maps) /
516+ static_cast <float >(kRandomMazeSeedCount );
517+ ASSERT_GE (mixer_seed_0_repeat_ratio, 0.0 );
518+ ASSERT_LE (mixer_seed_0_repeat_ratio, 0.01 );
519+ for (int i = 0 ; i < kRandomMazeSeedCount ; ++i) {
520+ LOG (INFO) << " Phase 2: step " << i + 1 << " out of "
521+ << kRandomMazeSeedCount ;
522+ lua_pushvalue (L, -1 );
523+ lua::Push (L, i);
524+ lua::Push (L, maps);
525+ lua::Push (L, " 1" );
526+ ASSERT_THAT (lua::Call (L, 3 ), IsOkAndHolds (0 ));
527+ }
528+ const auto mixer_seed_1_maps = maps.KeyCount () - mixer_seed_0_maps;
529+ const auto mixer_seed_1_repeat_ratio =
530+ (kRandomMazeSeedCount - mixer_seed_1_maps) /
531+ static_cast <float >(kRandomMazeSeedCount );
532+ ASSERT_GE (mixer_seed_1_repeat_ratio, 0.0 );
533+ ASSERT_LE (mixer_seed_1_repeat_ratio, 0.01 );
534+ lua_pop (L, 1 );
535+ }
536+
471537constexpr char kVisitRandomPath [] = R"(
472538local sys_random = require 'dmlab.system.sys_random'
473539local maze_generation = require 'dmlab.system.maze_generation'
0 commit comments