Add auto test

This commit is contained in:
2023-09-28 09:29:22 +08:00
parent 9d63717601
commit 896e621557
3 changed files with 163 additions and 0 deletions

View File

@ -6,12 +6,35 @@
#include "client.h"
#include "server.h"
/**
* @brief The implementation of function CheckResult
* @details Only for debug. It checks the map_status and orginal map to see if
* the result of Gauss-Jordan Elimination is correct.
*/
void CheckResult() {
#define ERROR_IN_RESULT 0
using namespace Client;
using namespace Server;
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) {
if ('0' <= visible_map[i][j] && visible_map[i][j] <= '8')
assert(map_status[i][j] == 2);
if (visible_map[i][j] == '?') assert(map_status[i][j] != 2);
if (map_status[i][j] == 2)
assert('0' <= visible_map[i][j] && visible_map[i][j] <= '8');
if (map_status[i][j] == 0) assert(visible_map[i][j] == '?');
if (map_status[i][j] == 1) assert(origin_map[i][j] == '.');
if (map_status[i][j] == -1) assert(origin_map[i][j] == 'X');
}
#undef ERROR_IN_RESULT
}
/**
* @brief The implementation of function Execute
* @details Use it only when trying advanced task. Do NOT modify it before
* discussing with TA.
*/
void Execute(int row, int column) {
CheckResult();
std::string str;
VisitBlock(row, column);
if (game_state != 0) {

139
src/autotest.py Executable file
View File

@ -0,0 +1,139 @@
#!/usr/bin/python3
import sys
import os
import random
built_in_test_cases = [
"""10 10
........X.
..........
..........
......X...
..........
.X.......X
..........
X........X
..........
X....XX...
0 0""",
"""10 10
.....X...X
.........X
X..X......
X.X.......
..X.......
..........
..........
..........
..........
X.........
0 0""",
"""10 10
.XX.......
...X...XX.
..........
...X......
.X......X.
..X....X..
XX....X...
..X...X...
..X.X.X.X.
....X.....
0 9""",
"""10 10
.......X..
X.....X...
X...X.....
...X......
..........
.X.X.X....
..X.X..X..
.X.X.....X
....X.....
.....X....
4 9""",
"""20 20
X...........X.......
..XX.............X.X
.X......X.........X.
....X.X...X...X.....
....X.X.........X...
X.XX...........X.X..
...X.............XX.
...XX...X..X..X.X...
...X.X...X...X......
.............XX.X...
.X............X.....
.X..X.........X....X
X.X.X..X...X..X.....
.X....X.X......X....
...X.........X..X...
...X.X...X..........
..X.XX.X......XXXX..
.X.X...............X
XXX..X....X.XX..X...
.X...X.XX........X.X
0 10"""
]
mine_rate=0.15
total_round=0
win_round=0
for i in range(0,10):
for data in built_in_test_cases:
fn=open("tmp/test.in","w")
fn.write(data)
fn.close()
os.system("build/src/client < tmp/test.in > tmp/test.out")
fn=open("tmp/test.out","r")
lines=fn.readlines()
fn.close()
print(lines)
total_round+=1
if lines[0]=='YOU WIN!\n':
win_round+=1
print("win rate: "+str(win_round/total_round),win_round,total_round)
input("Press Enter to continue...")
while True:
# randomly generate n,m in [2,30]
n=random.randint(2,30)
m=random.randint(2,30)
# print(n,m)
# randomly generate mine_rate in [0,1]
#mine_rate=random.random()
# generate a random map
map=[]
for i in range(n):
map.append([])
for j in range(m):
if random.random()<mine_rate:
map[i].append('X')
else:
map[i].append('.')
# generate a random start point with no mine
sx=random.randint(0,n-1)
sy=random.randint(0,m-1)
while map[sx][sy]=='X':
sx=random.randint(0,n-1)
sy=random.randint(0,m-1)
# output the map
fn=open("tmp/test.in","w")
fn.write(str(n)+" "+str(m)+"\n")
for i in range(n):
for j in range(m):
fn.write(map[i][j])
fn.write("\n")
fn.write(str(sx)+" "+str(sy))
fn.close()
# run the program
os.system("build/src/client < tmp/test.in > tmp/test.out")
# read the output
fn=open("tmp/test.out","r")
lines=fn.readlines()
fn.close()
# check the output
print(lines)
total_round+=1
if lines[0]=='YOU WIN!\n':
win_round+=1
print("win rate: "+str(win_round/total_round),win_round,total_round)

View File

@ -290,6 +290,7 @@ void InterpretResult(std::vector<std::vector<double> > equations) {
assert(vid >= 0);
assert(vid < variaID_to_position.size());
std::pair<int, int> pos = variaID_to_position[vid];
if (map_status[pos.first][pos.second] != 0) continue;
if (sol == 0) {
map_status[pos.first][pos.second] = 1;
no_mine_block_to_be_clicked.push(pos);