繁体   English   中英

本地测试用例问题C

[英]Problems with test cases locally in C

你好,我的任务是编写测试,检查扫雷板(在选择重播选项后)是否与前一个不同。

首先,我写了主要the replay

之后我在本地测试它是否有效,每场比赛,每块棋盘都不一样。 没有问题。 所以我开始编写测试用例,现在我在这里。

TEST replay_board() {
    Game *game = create_game();
    Board *board = create_board(9, 9, 9);
    game->board = board;
    int k = 0;
    printf("\n");
    char mine_list[100];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if (game->board->tiles[i][j]->is_mine == true) {
                mine_list[k] = '1';
                printf("1");
            } else {
                mine_list[k] = '0';
                printf("0");
            }

            k++;
        }
    }
    printf("\n");
    mine_list[k] = '\0';

    replay_game(replay, game);

    char mine_list2[100];
    k = 0;

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if (game->board->tiles[i][j]->is_mine == true) {
                mine_list2[k] = '1';
                printf("1");
            } else {
                mine_list2[k] = '0';
                printf("0");
            }
            k++;
        }
    }
    printf("\n");
    mine_list2[k] = '\0';
    int same = 0;

    if (strcmp(mine_list, mine_list2) != 0) {
        same = 0;
    } else {
        same = 1;
    }
    ASSERT_EQ(same, 0);
    PASS();
    destroy_game(game);
}

在每次测试中,它都写着这头老公猪与前一头相同,但在本地我可以看到它们不一样。

我不知道比较两个字符串是否理想,但这是从结构比较它们的最简单方法

重播 function

void replay_game(char* replay, Game* game) {
    while (strcmp(replay, "yes") != 0 && strcmp(replay, "no") != 0 ) {
        scanf("%s", replay);
        if (strcmp(replay, "yes") != 0 || strcmp(replay, "no") != 0) {
            printf("invalid value\n");
        }
    }
    if (strcmp(replay, "yes") == 0) {
        destroy_board(game->board);
        game->game_state = PLAYING;
        game->board = create_board(9, 9, 9);
        game->player->score = 0;
    }
}

创建板 function

int generate_random_coordinates(int upper_range) {
    return rand() % upper_range;
}

/**
 * Generates random coordinates to row and column according to mine count value
 * Randomly sets mines to the Board pointer
 */
void set_mines_randomly(Board *board) {
    assert(board != NULL);

    int board_mine_count = 0;
    srand(time(NULL));
    while (board_mine_count != board->mine_count) {
        int random_row = generate_random_coordinates(board->row_count);
        int random_column = generate_random_coordinates(board->column_count);

        if (board->tiles[random_row][random_column]->is_mine == false) {

            board->tiles[random_row][random_column]->is_mine = true;
            board_mine_count++;
        }
    }
}

Board *create_board(int row_count, int column_count, int mine_count) {
    Board *board = (Board *) calloc(1, sizeof(Board));
    board->row_count = row_count;
    board->column_count = column_count;
    board->mine_count = mine_count;

    for (int row = 0; row < board->row_count; row++) {
        for (int column = 0; column < board->column_count; column++) {
            board->tiles[row][column] = (Tile *) calloc(1, sizeof(Tile));
            board->tiles[row][column]->tile_state = CLOSED;
            board->tiles[row][column]->is_mine = false;
        }
    }
    set_mines_randomly(board);
    set_tile_values(board);
    return board;
}

主要的

#include <stdlib.h>
#include <string.h>
#include "game.h"
#include "user_interface.h"
#include "board.h"

int main() {
    char replay[] = "yes";
    Game *game = create_game();
    Board *board = create_board(9, 9, 9);
    game->board = board;
    read_player_name(game);

    // impleting the replay function
    while(strcmp(replay, "yes") == 0) {
        play_game(game);
        printf("Chcel by si znova zahrat? (yes/no)\n");
        scanf("%3s", replay);
        printf("\n");



        // new part
        char mine_list[100];
        int k = 0;

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (game->board->tiles[i][j]->is_mine == true) {
                    mine_list[k] = '1';
                    //printf("1");
                } else {
                    mine_list[k] = '0';
                    //printf("0");
                }
                k++;
            }
        }
        mine_list[k] = '\0';

        replay_game(replay, game);

        char mine_list2[100];
        k = 0;

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (game->board->tiles[i][j]->is_mine == true) {
                    mine_list2[k] = '1';
                    //printf("1");
                } else {
                    mine_list2[k] = '0';
                    //printf("0");
                }
                k++;
            }
        }
        mine_list2[k] = '\0';        
        printf("%s\n", mine_list);
        printf("%s\n", mine_list2);

        // new part ending heree

    }
    destroy_game(game);
    exit(EXIT_SUCCESS);
}

丢失output后运行主程序

   1 2 3 4 5 6 7 8 9 
1  1 1 0 0 0 0 0 0 0 
2  X 1 0 1 1 1 0 0 0 
3  - 1 0 1 X 1 0 0 0 
4  - 1 1 1 1 1 1 1 1 
5  - X 1 0 0 0 1 X - 
6  - - 2 1 1 1 2 - - 
7  - - X - - X - - - 
8  - X - - X - - - - 
9  - - - - - - X - - 

Ľutujem tom. Riešenie je nesprávne!
Vaše skóre je: 13
Chcel by si znova zahrat? (yes/no)
yes 

玩了2分钟后

   1 2 3 4 5 6 7 8 9 
1  0 0 0 0 0 1 - 1 0 
2  0 0 0 0 1 3 X 2 0 
3  0 0 0 0 1 X X 3 1 
4  1 1 0 0 1 2 2 2 X 
5  X 1 0 0 0 0 0 1 - 
6  - 2 1 2 2 3 2 1 - 
7  - - X - X X X - - 
8  - - - - - - - 1 - 
9  - - - - - - - - - 

Ľutujem tom. Riešenie je nesprávne!
Vaše skóre je: 19
Chcel by si znova zahrat? (yes/no)

失败的测试结果:

* Suite test_board:
......
000000000000001100000010001000100000000000001000100000100010000000000000000000000
000000000000001100000010001000100000000000001000100000100010000000000000000000000
F
FAIL replay_board: same != 0 (tests/test_board.c:125)

游戏.h

typedef enum  {
    FAILED,
    PLAYING,
    SOLVED,
} GameState;

typedef struct {
    Board *board;          /* Struct of the play field */
    Player *player;        /* Struct of user who is playing the Game */
    GameState game_state;  /* Enum for status of the Game */
} Game;

* 创建游戏()*

Game *create_game() {
    Game *game = (Game *) calloc(1, sizeof(Game));
    Player *player = (Player *) calloc(1, sizeof(Player));
    game->player = player;
    game->player->score = 1;
    game->game_state = PLAYING;
    return game;
}

用 printf 更新主函数后的新输出

   1 2 3 4 5 6 7 8 9 
1  - 1 0 0 0 0 0 0 0 
2  X 1 0 0 0 0 1 1 1 
3  - 1 0 0 0 0 1 X - 
4  - 1 1 0 0 1 2 - - 
5  - X 2 1 1 1 X - X 
6  1 1 2 X 1 1 2 X X 
7  0 0 1 1 1 0 1 3 X 
8  0 0 0 0 0 0 0 1 1 
9  0 0 0 0 0 0 0 0 0 

Ľutujem asd. Riešenie je nesprávne!
Vaše skóre je: 17
Chcel by si znova zahrat? (yes/no)
yes

000000000100000000000000010000000000010000101000100011000000001000000000000000000
000000001100000000000000000000100011000000000000000010010000000000001000000010000

   1 2 3 4 5 6 7 8 9 
1  - - - - - - - - - 
2  - - - - - - - - - 
3  - - - - - - - - - 
4  - - - - - - - - - 
5  - - - - - - - - - 
6  - - - - - - - - - 
7  - - - - - - - - - 
8  - - - - - - - - - 
9  - - - - - - - - - 



这是问题所在:

char* mine_list[100];

您将mine_list定义为char *数组而不是char数组。 你的编译器应该在你使用它的任何地方给你一些警告。 将其更改为char数组:

char mine_list[100];

你检查过replay_game("yes", game); 真的改变游戏?

无论哪种方式,都有一种更简单、更正确的方法来做到这一点。 分配一个新的 board* 指针,使用memcpy将 board 的内容复制到新分配的 memory ,然后调用replay_game 假设这实际上改变了board变量,您现在可以简单地比较一个嵌套循环中的两个板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM