~bonbon/gmcts

gmcts/tree_test.go -rw-r--r-- 1.1 KiB
f7bd5ecabonbon move v2.0.0 changes to v2 folder 1 year, 4 months ago
                                                                                
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
package gmcts

import (
	"testing"
	"time"

	tictactoe "git.sr.ht/~bonbon/go-tic-tac-toe"
)

func TestRounds(t *testing.T) {
	rounds := treeToTest.Rounds()
	if rounds != 10000 {
		t.Errorf("Tree performed %d rounds: wanted 1", rounds)
		t.FailNow()
	}
}

func TestNodes(t *testing.T) {
	//The amount of nodes in the tree should not exceed the
	//amount of mcts rounds performed on the tree.
	rounds := treeToTest.Rounds()
	nodes := treeToTest.Nodes()
	if nodes > rounds {
		t.Errorf("Tree has %d nodes: wanted <= %d", nodes, rounds)
		t.FailNow()
	}
}

func TestDepth(t *testing.T) {
	//Because tictactoe is a simple game, the
	//tree should have looked 9 moves ahead.
	depth := treeToTest.MaxDepth()
	if depth != 9 {
		t.Errorf("Tree has depth %d: wanted 0", depth)
		t.FailNow()
	}
}

func TestSearch(t *testing.T) {
	mcts := NewMCTS(tttGame{tictactoe.NewGame()})
	tree := mcts.SpawnTree()

	timeToSearch := 1 * time.Millisecond
	t0 := time.Now()
	tree.Search(timeToSearch)
	td := time.Now().Sub(t0)

	if td < timeToSearch {
		t.Errorf("Tree was searched for %s: wanted >= %s", td, timeToSearch)
		t.FailNow()
	}
}