Files @ f1f8a2421f92
Branch filter:

Location: OneEye/src/tests/testStatebag.py

Laman
updated readme
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