void test_board_save_to_file_saved_board_matches_original_when_loaded() { const char *filename = "test_board.chs"; board_t *to_save = board_init_start(); bool overwrite = true; int result; /* Save the file */ result = board_save_to_file(filename, to_save, overwrite); TEST_ASSERT_MESSAGE( result == 0, "Expected overwriting save of valid board to succeed" ); /* Reload the file into a new board_t* */ board_t *loaded_board; loaded_board = board_load_from_file(filename); TEST_ASSERT_MESSAGE( loaded_board != NULL, "Expected board load from valid file to succeed" ); /* Check that the old and new board match */ bool boards_match; boards_match = board_equal(to_save, loaded_board); TEST_ASSERT_MESSAGE( boards_match, "Expected the loaded board to match the saved one" ); board_destroy(loaded_board); board_destroy(to_save); }
/* Check that pieces are in the same place on original and copy boards */ void test_board_copy_deep_piece_values_match() { board_t *original = board_init_start(); board_t *copy = board_copy_deep(original); int rank, file; for (rank = 0; rank < BOARD_SIZE; rank++) { for (file = 0; file < BOARD_SIZE; file++) { /* Check that all original pieces can be found on the new board */ piece_t *original_piece = original->spaces[rank][file]->piece; if (original_piece) { /* Check that the copied piece matches the original */ piece_t *copied_piece = copy->spaces[rank][file]->piece; bool equal = piece_equal(original_piece, copied_piece); TEST_ASSERT_MESSAGE( equal, "Original and copied pieces dont match" ); } } } board_destroy(original); board_destroy(copy); }
void test_board_copy_deep_moves_next_is_preserved() { board_t *original = board_init_start(); board_t *copy = board_copy_deep(original); TEST_ASSERT_MESSAGE( original->moves_next == copy->moves_next, "Expected original and copy boards to have same 'next-movers'" ); board_destroy(original); board_destroy(copy); }
void test_board_copy_deep_returns_board_at_new_address() { board_t *original = board_init_start(); board_t *copy = board_copy_deep(original); TEST_ASSERT_MESSAGE( original != copy, "Expected original and copy board_t* to point to different places" ); board_destroy(original); board_destroy(copy); }
void test_board_init_start_sets_pieces() { int rank, file; board_t *board = board_init_start(); /* Check for white's pieces (ranks: 0-1) */ for (rank = 0; rank <= 1; rank++) { for (file = 0; file < BOARD_SIZE; file++) { TEST_ASSERT_MESSAGE( board->spaces[rank][file] != NULL, "Expected square_t* to be non-NULL but found NULL" ); TEST_ASSERT_MESSAGE( board->spaces[rank][file]->piece != NULL, "Expected valid piece_t*, but found NULL" ); } } /* Check for black's pieces (ranks: 6-7) */ for (rank = 6; rank < BOARD_SIZE; rank++) { for (file = 0; file < BOARD_SIZE; file++) { TEST_ASSERT_MESSAGE( board->spaces[rank][file] != NULL, "Expected square_t* to be non-NULL, but found NULL" ); TEST_ASSERT_MESSAGE( board->spaces[rank][file]->piece != NULL, "Expected valid black piece_t* found NULL" ); } } board_destroy(board); }
static PyObject * _cffi_f_board_destroy(PyObject *self, PyObject *arg0) { board_t * x0; Py_ssize_t datasize; datasize = _cffi_prepare_pointer_call_argument( _cffi_type(5), arg0, (char **)&x0); if (datasize != 0) { if (datasize < 0) return NULL; x0 = (board_t *)alloca((size_t)datasize); memset((void *)x0, 0, (size_t)datasize); if (_cffi_convert_array_from_object((char *)x0, _cffi_type(5), arg0) < 0) return NULL; } Py_BEGIN_ALLOW_THREADS _cffi_restore_errno(); { board_destroy(x0); } _cffi_save_errno(); Py_END_ALLOW_THREADS (void)self; /* unused */ Py_INCREF(Py_None); return Py_None; }
int main(int argc, char * argv[]){ Board * board; int x, y; Pos pos; UCTNode * uct_tree = 0; UCTNode * ptr; board_initialize(); uct_initialize(); board = board_new(); while(1) { board_print(board, stdout); printf("\nBlack: "); if(scanf("%d %d", &x, &y) == -1) break; board_play(board, to_pos(x, y), BLACK); board_print(board, stdout); pos = uct_search(board, WHITE); board_play(board, pos, WHITE); } //for(ptr = uct_tree->child; ptr; ptr = ptr->next) // printf("%d ", ptr->move); puts(""); board_destroy(); uct_destroy(); return 0; }
void test_board_copy_deep_squares_pointers_are_different() { board_t *original = board_init_start(); board_t *copy = board_copy_deep(original); int rank, file; for (rank = 0; rank < BOARD_SIZE; rank++) { for (file = 0; file < BOARD_SIZE; file++) { TEST_ASSERT_MESSAGE( original->spaces[rank][file] != copy->spaces[rank][file], "Expected square_t ptrs to be different after deep copy" ); } } board_destroy(original); board_destroy(copy); }
void test_board_init_makes_square_array() { board_t *board = board_init(); TEST_ASSERT_MESSAGE( board->spaces != NULL, "Expected board_init to create initialize an array of squares, but found NULL" ); board_destroy(board); }
void test_board_init_start_moves_white_first() { board_t *board = board_init_start(); TEST_ASSERT_MESSAGE( board->moves_next == WHITE, "Expected board_init to let WHITE move first, but found BLACK" ); board_destroy(board); }
void test_board_init_start_makes_square_array() { board_t *board = board_init_start(); TEST_ASSERT_MESSAGE( board != NULL, "Expected board_init_start to set square array, found NULL pointer" ); board_destroy(board); }
void test_board_init_start_returns_non_null() { board_t *board = board_init_start(); TEST_ASSERT_MESSAGE( board != NULL, "Expected board_init_start to return board_t, but found NULL" ); board_destroy(board); }
void test_board_copy_deep_piece_pointers_are_different() { board_t *original = board_init_start(); board_t *copy = board_copy_deep(original); int rank, file; for (rank = 0; rank < BOARD_SIZE; rank++) { for (file = 0; file < BOARD_SIZE; file++) { if (original->spaces[rank][file]->piece) { TEST_ASSERT_MESSAGE( original->spaces[rank][file]->piece != copy->spaces[rank][file]->piece, "Expected piece_t*'s to be different after deep copying board" ); } } } board_destroy(original); board_destroy(copy); }
void test_board_init_sets_no_pieces() { board_t *board = board_init(); int rank, file; for (rank = 0; rank < BOARD_SIZE; rank++) { for (file = 0; file < BOARD_SIZE; file++) { TEST_ASSERT_MESSAGE( board->spaces[rank][file]->piece == NULL, "Expected all squares to be empty, found non-null piece_t*" ); } } board_destroy(board); }
void test_board_init_creates_each_square() { board_t *board = board_init(); int rank, file; for (rank = 0; rank < BOARD_SIZE; rank++) { for (file = 0; file < BOARD_SIZE; file++) { TEST_ASSERT_MESSAGE( board->spaces[rank][file] != NULL, "Expected square to be square_t, but found NULL" ); } } board_destroy(board); }
void test_board_save_to_file_returns_neg_two_if_file_exists() { const char *filename = "existing_board.chs"; board_t *to_save = board_init_start(); bool overwrite = false; int result; /* Return error code if the file exists and we dont want to overwrite */ result = board_save_to_file(filename, to_save, overwrite); TEST_ASSERT_MESSAGE( result == -2, "Expected to return error when not overwriting existing file" ); board_destroy(to_save); }
void test_board_save_to_file_returns_neg_one_for_invalid_args() { const char *filename = "saved1.chs"; board_t *to_save = board_init_start(); bool overwrite = true; int result; /* Return error code when given bad filename */ result = board_save_to_file(NULL, to_save, overwrite); TEST_ASSERT_MESSAGE( result == -1, "Expected to return -1 when given bad filename" ); /* Return error code when given bad board */ result = board_save_to_file(filename, NULL, overwrite); TEST_ASSERT_MESSAGE( result == -1, "Expected to return -1 when given bad board_t" ); board_destroy(to_save); }
int board_close(board_t* board) { int ret = 0; /* * write statistics information to the end of index file. */ if ((PROT_READ | PROT_WRITE) == board->index.prot) { struct stat st; if ((-1 != fstat(board->index.fd, &st) && board->index.used_size + sizeof(stat_info_t) <= st.st_size && -1 != lseek(board->index.fd, -sizeof(stat_info_t), SEEK_END)) || -1 != lseek(board->index.fd, 0, SEEK_END)) { writen(board->index.fd, &board->stat, sizeof(stat_info_t)); } else { ret = -1; } } board_destroy(board); return ret; }
int main(int argc, char **argv) { struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_sec*1000000ULL + tv.tv_usec); if (argc >= 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) { fprintf(stderr, "Usage: %s [width] [height] [nbombs]\n", argv[0]); return 1; } int width = DEF_WIDTH; int height = DEF_HEIGHT; int nbombs = -1; for (int i = 1; i < argc; i++) { int val = atoi(argv[i]); switch (i) { case 1: width = val; break; case 2: height = val; break; case 3: nbombs = val; break; } } if (nbombs == -1) { nbombs = .123 * width * height; } if (nbombs >= width*height) { fprintf(stderr, "nbombs (=%d) more than or equal to width * height (=%d)\n", nbombs, width*height); return 1; } initscreen(); atexit(endscreen); signal(SIGINT, signalend); Board *bd=board_make(width, height, nbombs); Key key; bool quit = false; int repeat = 1; bool have_repeat_num = false; while (!quit) { board_draw(bd); if (board_win(bd)) { char *timestamp; formatTime(×tamp, time(NULL) - bd->startTime); if (!prompt_playagain("You win!", timestamp, height + 2)) { break; } board_destroy(bd); bd = board_make(width, height, nbombs); continue; } getkey(&key); switch (key.type) { case KNUM: if (have_repeat_num) { if (repeat >= (INT_MAX - key.num) / 10) { // would overflow bel(); repeat = 1; have_repeat_num = false; } else { repeat = 10 * repeat + key.num; } } else if (key.num >= 1) { repeat = key.num; have_repeat_num = true; } break; case KARROW: board_shiftcursor(bd, key.dir, repeat); repeat = 1; have_repeat_num = false; break; case KCHAR: switch (key.ch) { case 'q': prompt_quit(height + 2); break; case 'f': board_flag(bd); break; case 'r': board_destroy(bd); bd=board_make(width, height, nbombs); break; case ' ': if (!board_open(bd)) break; board_revealbombs(bd); char *timestamp; formatTime(×tamp, time(NULL) - bd->startTime); if (!prompt_playagain("BOOM!", timestamp, height + 2)) { quit = true; break; } board_destroy(bd); bd = board_make(width, height, nbombs); break; } if (have_repeat_num) bel(); repeat = 1; have_repeat_num = false; break; default: bel(); if (have_repeat_num) bel(); repeat = 1; have_repeat_num = false; break; } } board_destroy(bd); }
static void _cffi_d_board_destroy(board_t * x0) { board_destroy(x0); }
board_t* board_open(const char* file, char mode) { board_t* board = NULL; char* path = NULL; int readonly; off_t begin; size_t len; long pagesize; assert(NULL != file && ('r' == mode || 'w' == mode)); /* * prepare board_t* pointer to return. */ board = malloc(sizeof(board_t)); ERRORP(NULL == board, "Can't allocate memory for board."); memset(board, 0, sizeof(board_t)); board->data.fd = board->index.fd = -1; board->data.content = board->index.content = MAP_FAILED; board->data.flags = MAP_SHARED; board->index.flags = MAP_SHARED; pagesize = sysconf(_SC_PAGESIZE); board->data.default_win = ALIGN_UP(DEFAULT_DATA_MMAP_WIN, pagesize); board->index.default_win = ALIGN_UP(DEFAULT_INDEX_MMAP_WIN, pagesize); board->data.expand_size = DEFAULT_DATA_EXPAND_SIZE; board->index.expand_size = DEFAULT_INDEX_EXPAND_SIZE; /* * prepare file path for 'board.d'. */ len = strlen(file); assert(len > 0); path = malloc(len + 3); ERRORP(NULL == path, "Can't allocate memory for path."); strcpy(path, file); path[len] = '.'; path[len + 1] = 'd'; path[len + 2] = '\0'; /* * open 'board.d' and 'board.i' */ readonly = 'r' == mode ? 1 : 0; if (readonly) { board->data.fd = open(path, O_RDONLY); ERRORVP(-1 == board->data.fd, "Can't open %s to read", path); board->data.prot = PROT_READ; path[len + 1] = 'i'; board->index.fd = open(path, O_RDONLY); ERRORVP(-1 == board->data.fd, "Can't open %s to read", path); board->index.prot = PROT_READ; } else { board->data.fd = open(path, O_RDWR | O_CREAT, 0644); ERRORVP(-1 == board->data.fd, "Can't open %s to read and write", path); board->data.prot = PROT_READ | PROT_WRITE; path[len + 1] = 'i'; board->index.fd = open(path, O_RDWR | O_CREAT, 0644); ERRORVP(-1 == board->data.fd, "Can't open %s to read and write", path); board->index.prot = PROT_READ | PROT_WRITE; } /* * check data file header and index file header, write if neccessary. */ if (0 != check_and_write_header(board->data.fd, 'd', readonly)) goto L_error; if (0 != check_and_write_header(board->index.fd, 'i', readonly)) goto L_error; /* * check statistics information in the end of index file. */ if (0 != check_stat_info(board)) goto L_error; /* * do memory mapping for data file and index file. */ begin = 0; if (board->data.used_size > board->data.default_win) begin = board->data.used_size - board->data.default_win; if (-1 == slide_mmap_window(&board->data, begin, board->data.default_win)) goto L_error; begin = 0; if (board->index.used_size > board->index.default_win) begin = board->index.used_size - board->index.default_win; if (-1 == slide_mmap_window(&board->index, begin, board->index.default_win)) goto L_error; return board; L_error: board_destroy(board); if (NULL != path) free(path); return NULL; }