diff --git a/src/tests/testStatebag.py b/src/tests/testStatebag.py --- a/src/tests/testStatebag.py +++ b/src/tests/testStatebag.py @@ -1,3 +1,4 @@ +import random import os.path from unittest import TestCase @@ -6,7 +7,7 @@ from util import BLACK as B,WHITE as W,E from go.engine import SpecGo,Engine import go.engine from statebag import BoardState,StateBag,updateDiff -from .util import simpleLoadSgf,listStates +from .util import simpleLoadSgf,listBoards,listStates class TestBoardState(TestCase): @@ -69,21 +70,47 @@ class TestBoardState(TestCase): class TestStateBag(TestCase): - def testReal(self): + def testSkips(self): go.engine.eng=Engine() files=["O-Takao-20110106.sgf","Sakai-Iyama-20110110.sgf"] for f in files: moves=simpleLoadSgf(os.path.join(cfg.srcDir,"tests/data",f)) - states=listStates(moves) + boards=listBoards(moves) for k in range(1,3): bag=StateBag() i=0 - for s_ in states: + for b in boards: i+=1 if i%(2*k-1)>=k: # keep k, skip k-1 continue - s=bag.pushState(s_) + s=bag.pushState(b) if len(bag._states)>1: self.assertIs(s.getPrev(), bag._states[-2]) + + def testNoise(self): + random.seed(361) + go.engine.eng=Engine() + files=["O-Takao-20110106.sgf","Sakai-Iyama-20110110.sgf"] + + for f in files: + moves=simpleLoadSgf(os.path.join(cfg.srcDir,"tests/data",f)) + boards=listBoards(moves) + + bag=StateBag() + for b in boards: + s=bag.pushState(b) + if len(bag._states)>1: + # correct state skipping the erroneous one, connected to the previous correct one + self.assertIs(s.getPrev(), bag._states[key]) + + if random.random()<0.9: + key=-2 + continue + for i in range(random.randrange(1,10)): + r=random.randrange(19) + c=random.randrange(19) + b[r][c]=(b[r][c]+random.choice((2,3)))%3-1 # random transformation [-1,1]->[-1,1] + bag.pushState(b) + key=-3