软件工程——数独 测试1

会有一股神秘感。 提交于 2020-01-16 16:14:19

命令解析模块测试

 

 

测试代码

#include "pch.h"
#include "share.h"
int **sudoku = nullptr;
int num_of_sudoku = 0;//第几个数独表
int upper_limit_of_sudoku = 0;
HANDLE h_sudoku_txt = INVALID_HANDLE_VALUE;
HANDLE h_sudoku_problem_txt = INVALID_HANDLE_VALUE;

void closeTxt()
{
    if (h_sudoku_txt != INVALID_HANDLE_VALUE)
        CloseHandle(h_sudoku_txt);
    if (h_sudoku_problem_txt != INVALID_HANDLE_VALUE)
        CloseHandle(h_sudoku_problem_txt);
    h_sudoku_problem_txt = INVALID_HANDLE_VALUE;
    h_sudoku_txt = INVALID_HANDLE_VALUE;
}

void mallocSudoku()
{
    if (sudoku == nullptr)
    {
        sudoku = new int* [10];
        for (int i = 0; i < 10; i++)
        {
            sudoku[i] = new int[10];
        }
    }
}

void freeSudoku()
{
    if (sudoku != nullptr)
    {
        for (int i = 0; i < 10; i++)
        {
            delete[] sudoku[i];
        }
        delete[] sudoku;
        sudoku = nullptr;
    }
}

namespace TestInterpretor
{
    TEST(interpret_Test, MoreArguments)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-s");
        lstrcpy(argv[2], L"-c");
        lstrcpy(argv[3], L"200");

        EXPECT_EQ(interpret(4, argv), -1);

        lstrcpy(argv[4], L"-c");
        EXPECT_EQ(interpret(5, argv), -1);
    }

    TEST(interpret_Test, LessArguments)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-s");

        EXPECT_EQ(interpret(2, argv), -1);
    }

    TEST(interpret_Test, WrongArguments)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-t");
        lstrcpy(argv[2], L"200");

        EXPECT_EQ(interpret(3, argv), -2);

        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"一");
        lstrcpy(argv[2], L"200");

        EXPECT_EQ(interpret(3, argv), -2);
    }


    TEST(createSudokuTxt_Test, CreateSudokuTxt2)
    {
        if (h_sudoku_txt != INVALID_HANDLE_VALUE)
        {
            CloseHandle(h_sudoku_txt);
            h_sudoku_txt = INVALID_HANDLE_VALUE;
        }
        EXPECT_EQ(createSudokuTxt(), 0);
        EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);
    }

    TEST(createSudokuTxt_Test, CreateSudokuTxt)
    {
        if (h_sudoku_txt != INVALID_HANDLE_VALUE)
        {
            CloseHandle(h_sudoku_txt);
            h_sudoku_txt = INVALID_HANDLE_VALUE;
        }
        EXPECT_EQ(createSudokuTxt(), 0);
        EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);
    }


    TEST(interpretMakingSudoku_Test, NumberArgvIncludeNonNumberChar)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-c");
        lstrcpy(argv[2], L"200.");

        EXPECT_EQ(interpret(3, argv), -2);

        lstrcpy(argv[2], L"2a0");
        EXPECT_EQ(interpret(3, argv), -2);

        lstrcpy(argv[2], L"2二0");
        EXPECT_EQ(interpret(3, argv), -2);

        lstrcpy(argv[2], L"-1");
        EXPECT_EQ(interpret(3, argv), -2);

        lstrcpy(argv[2], L"+2");
        EXPECT_EQ(interpret(3, argv), -2);
    }


    TEST(interpretMakingSudoku_Test, NumberLessThanOne)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-c");
        lstrcpy(argv[2], L"0");

        EXPECT_EQ(interpret(3, argv), -3);
    }

    TEST(interpretMakingSudoku_Test, NumberGreaterThan1E6)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-c");
        lstrcpy(argv[2], L"1000001");

        EXPECT_EQ(interpret(3, argv), -3);

        lstrcpy(argv[2], L"1000002");
        EXPECT_EQ(interpret(3, argv), -3);
        lstrcpy(argv[2], L"2000000");
        EXPECT_EQ(interpret(3, argv), -3);
    }

    //TEST(interpretMakingSudoku_Test, InterpretMakingSudoku)
    //{
    //    mallocSudoku();
    //    wchar_t* argv[10];
    //    for (int i = 0; i < 4; i++)
    //        argv[i] = new wchar_t[100];
    //    lstrcpy(argv[0], L"sudoku.exe");
    //    lstrcpy(argv[1], L"-c");
    //    lstrcpy(argv[2], L"1000");

    //    /*closeTxt();
    //    EXPECT_EQ(interpret(3, argv), 0);
    //    EXPECT_EQ(1000000, upper_limit_of_sudoku);
    //    EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);*/


    //    //closeTxt();

    //    //lstrcpy(argv[2], L"999");
    //    //EXPECT_EQ(interpret(3, argv), 0);
    //    //EXPECT_EQ(999999, upper_limit_of_sudoku);
    //    //EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);

    //    //closeTxt();

    //    //lstrcpy(argv[2], L"999");
    //    //EXPECT_EQ(interpret(3, argv), 0);
    //    //EXPECT_EQ(999998, upper_limit_of_sudoku);
    //    //EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);

    //    //closeTxt();

    //    //lstrcpy(argv[2], L"1");
    //    //EXPECT_EQ(interpret(3, argv), 0);
    //    //EXPECT_EQ(1, upper_limit_of_sudoku);
    //    //EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);

    //    //closeTxt();

    //    //lstrcpy(argv[2], L"2");
    //    //EXPECT_EQ(interpret(3, argv), 0);
    //    //EXPECT_EQ(2, upper_limit_of_sudoku);
    //    //EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);


    //    //closeTxt();

    //    //lstrcpy(argv[2], L"817");
    //    //EXPECT_EQ(interpret(3, argv), 0);
    //    //EXPECT_EQ(817, upper_limit_of_sudoku);
    //    //EXPECT_NE(h_sudoku_txt, INVALID_HANDLE_VALUE);

    //    freeSudoku();
    //}


    TEST(interpretSolvingSudoku_Test, NoSuchFile)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-s");

        closeTxt();

        EXPECT_EQ(interpretSolvingSudoku(argv[2]), -5);
        lstrcpy(argv[2], L"E:\\");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), -5);

        lstrcpy(argv[2], L"E:\\helloworld");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), -5);

        lstrcpy(argv[2], L"D:\\");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), -5);

        lstrcpy(argv[2], L"D:\\helloworld");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), -5);
    }

    TEST(interpretSolvingSudoku_Test, ExistSudokuProblemTxt)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-s");
        lstrcpy(argv[2], L"E:\\requires.txt");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), 0);
        EXPECT_NE(h_sudoku_problem_txt, INVALID_HANDLE_VALUE);

        lstrcpy(argv[2], L"D:\\requires.txt");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), 0);
        EXPECT_NE(h_sudoku_problem_txt, INVALID_HANDLE_VALUE);


        lstrcpy(argv[2], L"D:\\迅雷下载\\requires.txt");
        closeTxt();
        EXPECT_EQ(interpretSolvingSudoku(argv[2]), 0);
        EXPECT_NE(h_sudoku_problem_txt, INVALID_HANDLE_VALUE);

    }

    TEST(interpretTest, ExistSudokuProblemTxt)
    {
        wchar_t* argv[10];
        for (int i = 0; i < 4; i++)
            argv[i] = new wchar_t[100];
        lstrcpy(argv[0], L"sudoku.exe");
        lstrcpy(argv[1], L"-s");
        lstrcpy(argv[2], L"E:\\requires.txt");
        closeTxt();
        EXPECT_EQ(interpret(3, argv), 0);
        EXPECT_NE(h_sudoku_problem_txt, INVALID_HANDLE_VALUE);

        lstrcpy(argv[2], L"D:\\requires.txt");
        closeTxt();
        EXPECT_EQ(interpret(3, argv), 0);

        EXPECT_NE(h_sudoku_problem_txt, INVALID_HANDLE_VALUE);


        lstrcpy(argv[2], L"D:\\迅雷下载\\requires.txt");
        closeTxt();
        EXPECT_EQ(interpret(3, argv), 0);

        EXPECT_NE(h_sudoku_problem_txt, INVALID_HANDLE_VALUE);
    }
}
View Code

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!