| 1 | import shutil |
| 2 | import threading |
| 3 | import unittest |
| 4 | from unittest import mock |
| 5 | |
| 6 | import chess |
| 7 | import chess.engine |
| 8 | |
| 9 | from schach import annotate, energy |
| 10 | |
| 11 | |
| 12 | class ProfileTest(unittest.TestCase): |
| 13 | def test_defaults(self): |
| 14 | self.assertEqual(energy.profile_values({}, "saver"), {"threads": 2, "depth": 18, "time": 10}) |
| 15 | normal = energy.profile_values({}, "normal") |
| 16 | self.assertEqual((normal["depth"], normal["time"]), (30, 30)) |
| 17 | |
| 18 | def test_threads_half_the_cpus_at_most_eight(self): |
| 19 | for cpus, threads in ((None, 1), (1, 1), (2, 1), (8, 4), (12, 6), (32, 8)): |
| 20 | with mock.patch("os.cpu_count", return_value=cpus): |
| 21 | self.assertEqual(energy.default_threads(), threads, cpus) |
| 22 | |
| 23 | def test_stored_values_override_and_are_clamped(self): |
| 24 | settings = {"analysis": {"saver": {"depth": 5, "time": 999, "threads": 3}}} |
| 25 | values = energy.profile_values(settings, "saver") |
| 26 | self.assertEqual(values["depth"], energy.MIN_DEPTH) # Hinweis braucht Tiefe 18 |
| 27 | self.assertEqual(values["time"], energy.MAX_TIME) |
| 28 | self.assertEqual(values["threads"], min(3, __import__("os").cpu_count())) |
| 29 | self.assertEqual(energy.profile_values(settings, "normal"), energy.defaults("normal")) |
| 30 | |
| 31 | def test_broken_settings_fall_back(self): |
| 32 | for broken in ({"analysis": None}, {"analysis": {"saver": "x"}}, {"analysis": {"saver": {"depth": "tief"}}}): |
| 33 | self.assertEqual(energy.profile_values(broken, "saver"), energy.defaults("saver")) |
| 34 | |
| 35 | |
| 36 | class ModeTest(unittest.TestCase): |
| 37 | def profile(self, mode, battery, saver): |
| 38 | return energy.active_profile({"analysis": {"mode": mode}}, battery=battery, saver=saver)[0] |
| 39 | |
| 40 | def test_auto(self): |
| 41 | self.assertEqual(self.profile("auto", False, False), "normal") |
| 42 | self.assertEqual(self.profile("auto", True, False), "saver") |
| 43 | self.assertEqual(self.profile("auto", False, True), "saver") # Systemprofil „Energiesparen“ |
| 44 | self.assertEqual(energy.active_profile({}, battery=True, saver=False)[0], "saver") # auto ist Standard |
| 45 | |
| 46 | def test_always_and_never_ignore_the_battery(self): |
| 47 | self.assertEqual(self.profile("always", False, False), "saver") |
| 48 | self.assertEqual(self.profile("never", True, True), "normal") |
| 49 | |
| 50 | def test_without_psutil_counts_as_plugged_in(self): |
| 51 | with mock.patch.dict("sys.modules", {"psutil": None}): |
| 52 | self.assertIsNone(energy.on_battery()) |
| 53 | |
| 54 | def test_think_settings(self): |
| 55 | threads, limit = energy.think_settings({"analysis": {"mode": "always"}}) |
| 56 | self.assertEqual((threads, limit.depth, limit.time), (2, 18, 10)) |
| 57 | |
| 58 | |
| 59 | @unittest.skipUnless(shutil.which("stockfish"), "stockfish nicht installiert") |
| 60 | class ThinkLimitTest(unittest.TestCase): |
| 61 | def test_think_job_obeys_limit_and_threads(self): |
| 62 | engine = chess.engine.SimpleEngine.popen_uci("stockfish") |
| 63 | try: |
| 64 | snapshots = [] |
| 65 | job = annotate.think_job(chess.Board(), annotate.Thinking(), snapshots.append, |
| 66 | limit=chess.engine.Limit(depth=10), threads=1) |
| 67 | worker = threading.Thread(target=lambda: snapshots.append(job(engine))) |
| 68 | worker.start() |
| 69 | worker.join(30) |
| 70 | self.assertFalse(worker.is_alive()) |
| 71 | self.assertEqual(max(s["depth"] for s in snapshots), 10) |
| 72 | self.assertEqual(engine.protocol.config.get("Threads"), 1) |
| 73 | finally: |
| 74 | engine.quit() |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
| 78 | unittest.main() |