commit ade02b2af7cc2149d34666c560ef839f9b804b09
parent 3f1ce423a6ecb591b45650303512340de08dbc13
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date: Thu, 22 Dec 2022 16:01:02 -0600
Refactor unit tests
Diffstat:
2 files changed, 17 insertions(+), 18 deletions(-)
diff --git a/tictactoe.c b/tictactoe.c
@@ -46,7 +46,7 @@ char play(Get_move_fn get_x_move, Get_move_fn get_o_move) {
int main(void) {
struct tb_event ev;
tb_init();
- char winner = play(get_move_minimax, get_move_from_human);
+ char winner = play(get_move_minimax, get_move_minimax);
if(winner)
print_msg("%c wins.", winner);
else
diff --git a/unit_test.c b/unit_test.c
@@ -1,3 +1,4 @@
+#include <stdbool.h>
#include <stdio.h>
#define UNIT_TEST
#define NO_PRINT_BOARD
@@ -16,21 +17,12 @@ struct test_board test_boards[] = {
{"o_wins_row", "O WINS" },
};
-typedef _Bool (*Result_checker)(char);
-_Bool is_draw(char actual_result) {
- return actual_result == 0;
-}
-_Bool is_draw_or_win(char actual_result) {
- return actual_result != 'O';
-}
-
-
void print_test_header(int test_num, char *desc, char *ex_res) {
printf("TEST %d\n", test_num);
puts( "--------");
printf("Testing %s -- expecting %s ...\n", desc, ex_res);
}
-_Bool print_test_results(int test_num, _Bool succeeded) {
+bool print_test_results(int test_num, bool succeeded) {
printf("Test %d ", test_num);
if(succeeded) {
puts("SUCCESS\n");
@@ -42,13 +34,20 @@ _Bool print_test_results(int test_num, _Bool succeeded) {
}
}
-_Bool test_ai_against(Get_move_fn get_o_move, int iters, int test_num, char *opp_desc, char *ex_res, Result_checker is_expected) {
+bool test_ai_against(Get_move_fn get_o_move, int iters, int test_num, char *opp_desc, char *ex_res, bool wins_ok) {
char desc[120];
int i;
+ int wins = 0, losses = 0;
sprintf(desc, "our allegedly perfect AI against %s %d times", opp_desc, iters);
print_test_header(test_num, desc, ex_res);
- for(i=0; i < iters && is_expected(play(get_move_minimax, get_o_move)); ++i);
- return print_test_results(test_num, i == iters);
+ for(i=0; i < iters; i++) {
+ switch(play(get_move_minimax, get_o_move)) {
+ case 'X': wins++; break;
+ case 'O': losses++; break;
+ }
+ }
+ printf("Results were: %d scratch; %d wins for X; %d wins for O\n", iters - wins - losses, wins, losses);
+ return print_test_results(test_num, wins_ok ? !losses : !(wins || losses));
}
int main() {
@@ -69,10 +68,10 @@ int main() {
printf("Result was: %s\n", actual_result);
failures += print_test_results(i, !strcmp(actual_result, b->expected_result));
}
- failures += test_ai_against(get_move_minimax, 50, ++i, "itself", "a draw for all executions", is_draw);
- failures += test_ai_against(get_random_move, 500, ++i, "a random move generator", "the AI wins or draws for all executions", is_draw_or_win);
- failures += test_ai_against(get_winning_move_else_random, 500, ++i, "a slightly dumber AI", "the AI wins or draws for all executions", is_draw_or_win);
- failures += test_ai_against(get_winning_move_else_block_else_random, 500, ++i, "a slightly smarter, dumber AI", "the AI wins or draws for all executions", is_draw_or_win);
+ failures += test_ai_against(get_move_minimax, 50, ++i, "itself", "a draw for all executions", false);
+ failures += test_ai_against(get_random_move, 500, ++i, "a random move generator", "the AI wins or draws for all executions", true);
+ failures += test_ai_against(get_winning_move_else_random, 500, ++i, "a slightly dumber AI", "the AI wins or draws for all executions", true);
+ failures += test_ai_against(get_winning_move_else_block_else_random, 500, ++i, "a slightly smarter, dumber AI", "the AI wins or draws for all executions", true);
puts("-----------------------------------------------------------");
printf("\n### SUITE %s ###\n", failures ? "FAILURE" : "SUCCESS");