From d068f93d16018cc9223591f5c07abaa3a62a020f Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Wed, 9 Nov 2016 20:10:09 +0200 Subject: [PATCH] implement str2long and long2str. With a unit test. --- pysollib/pysolrandom.py | 21 ++++++++++++++++++++- tests/board_gen/ms_deals1.py | 11 ++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pysollib/pysolrandom.py b/pysollib/pysolrandom.py index 71bef164..f2f03f2d 100644 --- a/pysollib/pysolrandom.py +++ b/pysollib/pysolrandom.py @@ -246,9 +246,13 @@ PysolRandom = MTRandom # * PySol support code # ************************************************************************ +def _match_ms(s): + """match an ms based seed string.""" + return re.match(r"ms([0-9]+)\n?\Z", s) + # construct Random from seed string def constructRandom(s): - m = re.match(r"ms([0-9]+)\n?\Z", s); + m = _match_ms(s) if m: seed = long(m.group(1)) if 0 <= seed <= LCRandom31.MAX_SEED: @@ -266,6 +270,21 @@ def constructRandom(s): return LCRandom31(seed) return PysolRandom(seed) +MS_LONG_BIT = (1L << 1000) + +def random__str2long(s): + m = _match_ms(s) + if m: + return (long(m.group(1)) | MS_LONG_BIT) + else: + return long(s) + +def random__long2str(l): + if ((l & MS_LONG_BIT) != 0): + return "ms" + str(l & (~ MS_LONG_BIT)) + else: + return str(l) + # test if __name__ == '__main__': r = constructRandom('12345') diff --git a/tests/board_gen/ms_deals1.py b/tests/board_gen/ms_deals1.py index 391a76fb..0c90297c 100644 --- a/tests/board_gen/ms_deals1.py +++ b/tests/board_gen/ms_deals1.py @@ -54,7 +54,6 @@ # imports import sys, os, re, string, time, types -import random sys.path.append("./tests/lib") from TAP.Simple import plan, ok @@ -62,7 +61,7 @@ from TAP.Simple import plan, ok # So the localpaths will be overrided. sys.path.insert(0, ".") -from pysollib.pysolrandom import constructRandom, LCRandom31 +from pysollib.pysolrandom import constructRandom, LCRandom31, random__str2long, random__long2str # PySol imports @@ -557,7 +556,7 @@ class Game: def shlomif_main(args): - plan(5) + plan(6) rand = constructRandom('24') game = Game("freecell", rand, True) @@ -639,5 +638,11 @@ QH 9H 9D 5S 7S 6C 'Microsoft Deal #6E9 - extra long seed.', ); + inp = 'ms12345678' + got = random__long2str(random__str2long(inp)) + + # TEST + ok (got == inp, 'long2str ms roundtrip.') + if __name__ == "__main__": sys.exit(shlomif_main(sys.argv))