---
# System prepended metadata

title: UnitTest

---

# UnitTest
```
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include "Guard.cpp"
#include "Guard.h"
// #define inputCD "TestingData//409r//test//"
// #define inputCD "TestingData//1000r//test//"
#define inputCD "TestingData//2000r//test//"
#define outputCD ""


using namespace std;

fstream inputFile;

const std::vector<std::string> split(const std::string& str, const std::string& pattern) {
    std::vector<std::string> result;
    std::string::size_type begin, end;

    end = str.find(pattern);
    begin = 0;

    while (end != std::string::npos) {
        if (end - begin != 0) {
            result.push_back(str.substr(begin, end-begin)); 
        }    
        begin = end + pattern.size();
        end = str.find(pattern, begin);
    }

    if (begin != str.length()) {
        result.push_back(str.substr(begin));
    }
    return result;        
}

int main()
{
    const string I_CD = inputCD;
    const string O_CD = outputCD;
    string filename1 = "round_";
    string filename2 = "0";
    string filetype = ".command";
    vector<pair<string, short>> Result;
    
    inputFile.open(I_CD + filename1 + filename2 + filetype, ios::in);
    while (inputFile.is_open())     //檢查檔案是否成功開啟
    {
        string str;
        while (getline(inputFile, str))
        {
            memset(command_title, 0, 20 * sizeof(char)); //sting.h only for memset
            memset(command_contxt, 0, 70 * sizeof(char));
            command_title[0] = ' ', command_contxt[0] = ' ';
            bool is_title = true;
            char inputchar;
            for (short title_size = 0, contxt_size = 0, i = 0;
                i < str.size(); i++)
            {
                inputchar = str[i];
                if (!is_title)
                {
                    command_contxt[contxt_size++] = inputchar;
                    continue;
                }
                else if (is_title && inputchar == ' ')
                {
                    is_title = false;
                    continue;
                }
                else
                    command_title[title_size++] = inputchar;
            }

            // cout << "Recieved: [" << command_title << ' ' << command_contxt << ']' << endl;
            Guard();
        }

        memset(command_title, 0, 20 * sizeof(char));
        memset(command_contxt, 0, 70 * sizeof(char));
        command_title[0] = '/';
        command_title[1] = 'a';
        command_contxt[0] = 't';
        Guard();
        Result.push_back(make_pair(filename2, tothrow));

        inputFile.close();

        int filenum = stoi(filename2) + 1;
        filename2 = to_string(filenum);
        inputFile.open(I_CD + filename1 + filename2 + filetype, ios::in);
    }

    vector<vector<short>> Answer;
    inputFile.open(I_CD + "answer.txt", ios::in);
    if (!inputFile.is_open())
    {
        cerr << "Can't open file " << "Answer.txt" << endl;
        system("pause");
    }
    else
    {
        string str;
        while (getline(inputFile, str))
        {
            vector<short> ans;
            vector<string> subs = split(str, " ");
            for (auto sub : subs)
            {
                ans.push_back((short)stoi(sub));
            }
            Answer.push_back(ans);
        }
        inputFile.close();
    }

    // system("CLS");
    cout << "\ntotal tests: " << Result.size() << "\n\n";
    int Xcnt = 0;
    for (int v = 0; v < Result.size(); v++)
    {
        cout << left << setw(4) << Result[v].first << " ";
        change_output(Result[v].second);
        if (v < Answer.size())
        {
            bool flag = false;
            for (auto ans : Answer[v])
            {
                // cout << ans << " ";
                if (RemoveID(Result[v].second) == ans)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag) Xcnt++, cout << "  X";
        }
        cout << endl;
    }
    cout << "total Xs: " << Xcnt << endl;
    cout << "\nCan't open file " << I_CD + filename1 + filename2 + filetype << endl;
    // system("pause");
}
