aboutsummaryrefslogtreecommitdiffstats
path: root/src/chess_tester.cpp
blob: a8ec0b5d71dc70e1a691266109767d2b8e991263 (plain)
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
117
118
119
#include "chess_tester.hpp"

using namespace ymhChessAI;

chess_tester::chess_tester() : ts_begin(false) {
	srand(time(NULL));

	test_id = rand() % 0xffffffff;
	test_id_test = test_id;
}

void chess_tester::chess_begin_test_suite() {
	if(ts_begin)
		fprintf(stderr, "Error: already started test suite\n");
	exit(1);

	ts_begin = true;
}

int chess_tester::chess_begin_test(std::string test_name) {
	if(!ts_begin) {
		fprintf(stderr, "Error: the test suite hasn't been started yet\n");
		exit(1);
	}

	if(!is_in_arr(test_name)) {
		fprintf(stderr, "Error: the test name does not exist\n");
		exit(1);
	}

	current_test = test_name;

	test_id = rand() % 0xffffffff;
	test_id_test = test_id;

	return test_id;
}

void chess_tester::chess_end_test(int test_id, bool passed) {
	if(test_id != test_id_test) {
		fprintf(stderr, "Error: the test from before has not been ended\n");
		exit(1);
	}

	tested_pieces tmp;
	tmp.piece_name = current_test;

	if(passed) {
		tmp.num_passed = 1;
		tmp.num_failed = 0;
	} else {
		tmp.num_passed = 0;
		tmp.num_failed = 1;
	}

	if(test_piece.empty() || !is_in_vec(tmp)) {
		test_piece.push_back(tmp);
	} else {
		for(unsigned i = 0; i < test_piece.size(); ++i) {
			if(test_piece[i] == tmp) {
				if(passed)
					test_piece[i].num_passed++;
				else
					test_piece[i].num_failed++;
			}
		}
	}
}

void chess_tester::chess_end_test_suite() {
	std::sort(test_piece.begin(), test_piece.end());

	int pieces_passed = 0;
	int pieces_partial = 0;
	int pieces_fail = 0;

	fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----",
			"------", "--------");
	fprintf(stderr, "| %10s | %5s | %6s | %8s |\n", "Piece  ", "Total", "Passed",
			"Passed %");
	fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----",
			"------", "--------");
	for(unsigned i = 0; i < test_piece.size(); ++i) {
		fprintf(stderr, "| %10s | %5d | %6d | %7.3f%% |\n",
				test_piece[i].piece_name.c_str(), test_piece[i].num_passed +
				test_piece[i].num_failed, test_piece[i].num_passed,
				100.0 * (double)test_piece[i].num_passed /
				(double)(test_piece[i].num_passed + test_piece[i].num_failed));

		if(test_piece[i].num_passed == 0)
			pieces_fail++;
		else if(test_piece[i].num_failed == 0)
			pieces_passed++;
		else
			pieces_partial++;
	}
	fprintf(stderr, "+-%10s-+-%5s-+-%6s-+-%8s-+\n", "----------", "-----",
			"------", "--------");

	fprintf(stderr, "\nTotal pieces tested: %lu\n", test_piece.size());
	fprintf(stderr, "Passed: %d\n", pieces_passed);
	fprintf(stderr, "Partially working: %d\n", pieces_partial);
	fprintf(stderr, "Failed: %d\n", pieces_fail);
}

bool chess_tester::is_in_arr(const std::string& test_piece) const {
	for(unsigned i = 0; i < CHESS_TEST_SIZE; ++i)
		if(chess_test_pieces[i] == test_piece)
			return true;

	return false;
}

bool chess_tester::is_in_vec(const tested_pieces& piece) const {
	for(unsigned i = 0; i < test_piece.size(); ++i)
		if(test_piece[i] == piece)
			return true;
	return false;
}