Files
@ dc1ed8ff56ef
Branch filter:
Location: OneEye/src/tests/testStatebag.py
dc1ed8ff56ef
2.6 KiB
text/x-python
cleaned up and documented shutdown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import random
import os.path
from unittest import TestCase
import config as cfg
from util import BLACK as B,WHITE as W,EMPTY as _
from go.engine import SpecGo,Engine
import go.engine
from statebag import BoardState,StateBag,updateDiff
from .util import simpleLoadSgf,listBoards,listStates
class TestBoardState(TestCase):
def testBasic(self):
s1=BoardState([
[_,_,_],
[_,_,_],
[_,_,_]
])
s2=BoardState([
[_,_,_],
[_,B,_],
[_,_,_]
])
g=SpecGo(3)
go.engine.eng=Engine(g)
s2.tryConnect(s1)
self.assertIs(s2.getPrev(), s1)
self.assertEqual(s2.getWeight(), 1)
def test2ply(self):
s1=BoardState([
[_,_,_],
[_,_,_],
[_,_,_]
])
s2=BoardState([
[_,_,_],
[_,B,_],
[_,_,_]
])
s3=BoardState([
[_,W,_],
[_,B,B],
[_,_,_]
])
g=SpecGo(3)
go.engine.eng=Engine(g)
s2.tryConnect(s1)
s3.tryConnect(s2)
self.assertIs(s3.getPrev(), s2)
self.assertEqual(s3.getWeight(), 1)
def testUpdateDiff(self):
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)
for (i,j,k) in [(1,2,3),(10,20,30),(90,100,110),(20,70,120)]:
s1=states[i]
s2=states[j]
s3=states[k]
diff1=s2-s1
diff2=s3-s2
with self.subTest(file=f,ijk=(i,j,k)):
self.assertEqual(s3-s1,updateDiff(diff1,diff2))
class TestStateBag(TestCase):
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))
boards=listBoards(moves)
for k in range(1,3):
bag=StateBag()
i=0
for b in boards:
i+=1
if i%(2*k-1)>=k: # keep k, skip k-1
continue
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
|