def test_board(self): with get_temps(2) as (cfg, pos): # basic board cfg.write(test_config) cfg.close() pos.write(basic_pos) pos.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertEqual(ret, 0) self.assertIn("bestmove: ", out.getvalue()) self.assertEqual(len(err.getvalue()), 0) # not a board or move list pos.seek(0) pos.truncate(0) pos.write("no board or moves") pos.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertIn("does not appear to be a board", out.getvalue())
def test_movelist(self): with get_temps(2) as (cfg, movelist): cfg.write(test_config) cfg.close() movelist.write(basic_movelist) movelist.close() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, movelist.name]) self.assertEqual(ret, 0) self.assertIn("bestmove: ", out.getvalue()) self.assertIn(movelist_4g, out.getvalue()) self.assertEqual(len(err.getvalue()), 0) with save_stdio() as (out, err): out = StringIO.StringIO() err = StringIO.StringIO() sys.stdout, sys.stderr = out, err ret = analyze.main(["--config", cfg.name, movelist.name, "2s"]) self.assertEqual(ret, 0) self.assertIn("bestmove: ", out.getvalue()) self.assertIn(movelist_2s, out.getvalue()) self.assertEqual(len(err.getvalue()), 0)
def test_movelist(self): with get_temps(2) as (cfg, movelist): cfg.write(test_config) cfg.close() movelist.write(basic_movelist.encode("utf-8")) movelist.close() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, movelist.name]) stdout, stderr = out.getvalue(), err.getvalue() self.assertEqual(ret, 0) self.assertIn("bestmove: ", stdout) self.assertIn(movelist_4g, stdout) print(stderr) self.assertEqual(len(stderr), 0) with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, movelist.name, "2s"]) stdout, stderr = out.getvalue(), err.getvalue() self.assertEqual(ret, 0) self.assertIn("bestmove: ", stdout) self.assertIn(movelist_2s, stdout) self.assertEqual(len(stderr), 0)
def test_movechecks(self): with get_temps(2) as (cfg, pos): cfg.write(test_config) cfg.close() pos.write(illegal_moves.encode("utf-8")) pos.flush() # with strict checks with save_stdio() as (out, err): ret = analyze.main( ["--config", cfg.name, pos.name, "--strict-checks"]) stdout = out.getvalue() self.assertGreater(ret, 0) self.assertIn("Enabling full legality checking on moves", stdout) self.assertIn("Illegal move found", stdout) # without strict checks with save_stdio() as (out, err): ret = analyze.main( ["--config", cfg.name, pos.name, "--skip-checks"]) stdout = out.getvalue() self.assertEqual(ret, 0) self.assertNotIn("Illegal move found", stdout) # illegal setup pos.seek(0) pos.truncate(0) pos.write(illegal_setup.encode("utf-8")) pos.flush() with save_stdio() as (out, err): ret = analyze.main( ["--config", cfg.name, pos.name, "--strict-setup"]) stdout = out.getvalue() self.assertGreater(ret, 0) self.assertIn("Enabling full legality checking on setup", stdout) self.assertIn("Tried to place a piece outside", stdout) with save_stdio() as (out, err): ret = analyze.main( ["--config", cfg.name, pos.name, "--allow-setup"]) stdout = out.getvalue() self.assertEqual(ret, 0) self.assertIn("Disabling full legality checking on setup", stdout)
def test_movechecks(self): with get_temps(2) as (cfg, pos): cfg.write(test_config) cfg.close() pos.write(illegal_moves) pos.flush() # with strict checks with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name, "--strict-checks"]) self.assertGreater(ret, 0) self.assertIn("Enabling full legality checking on moves", out.getvalue()) self.assertIn("Illegal move found", out.getvalue()) # without strict checks with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name, "--skip-checks"]) self.assertEqual(ret, 0) self.assertNotIn("Illegal move found", out.getvalue()) # illegal setup pos.seek(0) pos.truncate(0) pos.write(illegal_setup) pos.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name, "--strict-setup"]) self.assertGreater(ret, 0) self.assertIn("Enabling full legality checking on setup", out.getvalue()) self.assertIn("Tried to place a piece outside", out.getvalue()) with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name, "--allow-setup"]) self.assertEqual(ret, 0) self.assertIn("Disabling full legality checking on setup", out.getvalue())
def test_board(self): with get_temps(2) as (cfg, pos): cfg.write(test_config) cfg.close() pos.write(basic_pos) pos.close() with save_stdio(): out = StringIO.StringIO() err = StringIO.StringIO() sys.stdout, sys.stderr = out, err ret = analyze.main(["--config", cfg.name, pos.name]) self.assertEqual(ret, 0) self.assertIn("bestmove: ", out.getvalue()) self.assertEqual(len(err.getvalue()), 0)
def test_config(self): # missing config file with save_stdio() as (out, err): ret = analyze.main(["--config", "nonexistantfilename", "posfile"]) self.assertGreater(ret, 0) with get_temps(2) as (cfg, pos): # bad log level pos.write(basic_pos) pos.close() cfg.write(badlog_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertIn("Bad log level \"Level UNKNOWN_LEVEL\", use ", out.getvalue()) self.assertGreater(ret, 0) # good log level cfg.seek(0) cfg.truncate(0) cfg.write(goodlog_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertEqual(ret, 0) self.assertIn("DEBUG:analyze.aei:", err.getvalue()) # bad bot name cfg.seek(0) cfg.truncate(0) cfg.write(badbot_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertGreater(ret, 0) self.assertIn("configuration for bot_simplydone", out.getvalue()) with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name, "--bot", "bot_simple"]) self.assertEqual(ret, 0) # missing bot command cfg.seek(0) cfg.truncate(0) cfg.write(nocmd_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertGreater(ret, 0) self.assertIn("No engine command line found", out.getvalue()) # bad bot command cfg.seek(0) cfg.truncate(0) cfg.write(badcmd_cfg) cfg.flush() default_start_time = aei.START_TIME aei.START_TIME = 0.1 try: with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) finally: aei.START_TIME = default_start_time self.assertGreater(ret, 0) self.assertIn("Bot probably did not start", out.getvalue()) # bot options cfg.seek(0) cfg.truncate(0) cfg.write(botoptions_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertEqual(ret, 0) self.assertIn("Warning: Received unrecognized option, nonoption", out.getvalue()) self.assertIn("Warning: Received unrecognized option, another", out.getvalue()) self.assertIn("Warning: Received unrecognized option, afteroption", out.getvalue()) self.assertIn("Warning: Received unrecognized option, aftertwo", out.getvalue()) # monkey patch aei._ProcCom to force fast communication timeouts real_ProcCom = aei._ProcCom try: aei._ProcCom = FastTimeoutCom # ensure default is search position enabled and works cfg.seek(0) cfg.truncate(0) cfg.write(delaymove_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertEqual(ret, 0) self.assertIn("bestmove:", out.getvalue()) # disable search position cfg.seek(0) cfg.truncate(0) cfg.write(nosearch_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) self.assertEqual(ret, 0) self.assertNotIn("bestmove:", out.getvalue()) finally: aei._ProcCom = real_ProcCom
def test_config(self): # missing config file with save_stdio() as (out, err): ret = analyze.main(["--config", "nonexistantfilename", "posfile"]) self.assertGreater(ret, 0) with get_temps(2) as (cfg, pos): # bad log level pos.write(basic_pos.encode("utf-8")) pos.close() cfg.write(badlog_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() self.assertIn("Bad log level \"Level UNKNOWN_LEVEL\", use ", stdout) self.assertGreater(ret, 0) # good log level cfg.seek(0) cfg.truncate(0) cfg.write(goodlog_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stderr = err.getvalue() self.assertEqual(ret, 0) self.assertIn("DEBUG:analyze.aei:", stderr) # bad bot name cfg.seek(0) cfg.truncate(0) cfg.write(badbot_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() self.assertGreater(ret, 0) self.assertIn("configuration for bot_simplydone", stdout) with save_stdio() as (out, err): ret = analyze.main( ["--config", cfg.name, pos.name, "--bot", "bot_simple"]) self.assertEqual(ret, 0) # missing bot command cfg.seek(0) cfg.truncate(0) cfg.write(nocmd_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() self.assertGreater(ret, 0) self.assertIn("No engine command line found", stdout) # bad bot command cfg.seek(0) cfg.truncate(0) cfg.write(badcmd_cfg) cfg.flush() default_start_time = aei.START_TIME aei.START_TIME = 0.1 try: with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() finally: aei.START_TIME = default_start_time self.assertGreater(ret, 0) self.assertIn("Bot probably did not start", stdout) # bot options cfg.seek(0) cfg.truncate(0) cfg.write(botoptions_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() self.assertEqual(ret, 0) self.assertIn("Warning: Received unrecognized option, nonoption", stdout) self.assertIn("Warning: Received unrecognized option, another", stdout) self.assertIn("Warning: Received unrecognized option, afteroption", stdout) self.assertIn("Warning: Received unrecognized option, aftertwo", stdout) # monkey patch aei._ProcCom to force fast communication timeouts real_ProcCom = aei._ProcCom try: aei._ProcCom = FastTimeoutCom # ensure default is search position enabled and works cfg.seek(0) cfg.truncate(0) cfg.write(delaymove_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() self.assertEqual(ret, 0) self.assertIn("bestmove:", stdout) # disable search position cfg.seek(0) cfg.truncate(0) cfg.write(nosearch_cfg) cfg.flush() with save_stdio() as (out, err): ret = analyze.main(["--config", cfg.name, pos.name]) stdout = out.getvalue() self.assertEqual(ret, 0) self.assertNotIn("bestmove:", stdout) finally: aei._ProcCom = real_ProcCom