結對編程2——單元測試

碼市地址:https://git.coding.net/cjz113/jiedui2.githtml

結對成員:201421123111  201421123113c++

題目描述:git

  上一週你們爲四則運算程序設計了2-3個新功能,本次在隔了一週以後,咱們按部就班地進階。本次目標:編程

  1. 把計算模塊提取出來,單首創建一個類。
  2. 針對提取出來的計算類的接口函數作單元測試。
  3. 經過單元測試代碼,測試加法是否能正確工做;
  4. 經過單元測試代碼,測試加減乘除功能。
  5. 經過單元測試代碼,測試計算類對於各類參數的支持:

題目要求:函數

  1. 結對編程實現上述功能,一樣的,在程序正式開發以前,請先預估下PSP每一個環節的消耗時間(分鐘),並在過程當中統計實際耗時(分鐘),最後提交PSP表格。依然注意,這個主要是給大家本身看的,沒必要造假數據。
  2. 繼續兩人結對協做,把編碼規範、領航員和駕駛員角色互換作到位。
  3. 單元測試: 有單元測試保證,有代碼覆蓋率。

提取計算類:工具

類定義單元測試

class cla
{
public:
    string integer(int op,int x,int y);
    string fraction(int op,int a,int b,int c,int d);
    static int maxNumber(int k, int l);
};    

整數:學習

string integer(int op, int x, int y)
{
    char Operator[] = { '+', '-', '*', '/' };

    string result;

    int element, denominator;        //定義答案分子、分母
    int maxNum;        //定義公約數
    char answer[10];        //答案

    if (Operator[op] == Operator[0])
    {
        int n;
        n = x + y;
        result = sprintf_s(answer, "%d", n);
    }
    else
        if (Operator[op] == Operator[1])
        {
            int n;
            n = x - y;
            result = sprintf_s(answer, "%d", n);
        }
        else
            if (Operator[op] == Operator[2])
            {
                int n;
                n = x*y;
                result = sprintf_s(answer, "%d", n);
            }
            else
                if (Operator[op] == Operator[3])
                {
                    element = x;
                    denominator = y;
                    maxNum = cla::maxNumber(element, denominator);
                    element = element / maxNum;
                    denominator = denominator / maxNum;
                    if (denominator != 1)
                    {
                        result = sprintf_s(answer, "%d/%d", element, denominator);
                    }
                    else
                    {
                        result = sprintf_s(answer, "%d", element);
                    }
                }
                else
                    if (Operator[op] != Operator[0] && Operator[op] != Operator[1] && Operator[op] != Operator[2] && Operator[op] != Operator[3])
                    {
                        result = "error";
                    }
    return result;
}

分數:測試

string fraction(int op, int a, int b, int c, int d)
{


    char Operator[] = { '+', '-', '*', '/' };

    string result;

    int element, denominator;        //定義答案分子、分母
    int maxNum;        //定義公約數
    char answer[10];        //答案

    if (Operator[op] == Operator[0])
    {
        element = (b*c) + (a*d);
        denominator = a*c;
        maxNum = cla::maxNumber(element, denominator);
        element = element / maxNum;
        denominator = denominator / maxNum;
        if (denominator != 1)
        {
            result = sprintf_s(answer, "%d/%d", element, denominator);
        }
        else
        {
            result = sprintf_s(answer, "%d", element);
        }
    }
    else
        if (Operator[op] == Operator[1])
        {
            element = (b*c) - (a*d);
            denominator = a*c;
            maxNum = cla::maxNumber(element, denominator);
            element = element / maxNum;
            denominator = denominator / maxNum;
            if (denominator != 1)
            {
                result = sprintf_s(answer, "%d/%d", element, denominator);
            }
            else
            {
                result = sprintf_s(answer, "%d", element);
            }
        }
        else
            if (Operator[op] == Operator[2])
            {
                element = b*d;
                denominator = a*c;
                maxNum = cla::maxNumber(element, denominator);
                element = element / maxNum;
                denominator = denominator / maxNum;
                if (denominator != 1)
                {
                    result = sprintf_s(answer, "%d/%d", element, denominator);
                }
                else
                {
                    result = sprintf_s(answer, "%d", element);
                }
            }
            else
                if (Operator[op] == Operator[3])
                {
                    element = b*c;
                    denominator = a*d;
                    maxNum = cla::maxNumber(element, denominator);
                    element = element / maxNum;
                    denominator = denominator / maxNum;
                    if (denominator != 1)
                    {
                        result = sprintf_s(answer, "%d/%d", element, denominator);
                    }
                    else
                    {
                        result = sprintf_s(answer, "%d", element);
                    }
                }
                else
                    if (Operator[op] != Operator[0] && Operator[op] != Operator[1] && Operator[op] != Operator[2] && Operator[op] != Operator[3])
                    {
                        result = "error";
                    }
    return result;
}

取最大公約數:ui

static int maxNumber(int k, int l)            
{
    while (k%l != 0)
    {
        int t = k%l;
        k = l;
        l = t;
    }
    return l;
}

測試用例:

整數計算測試用例:

TEST_METHOD(integerTest)
        {
            //TrivialCalculator test;
            string actual = test->integer(0, 1, 2);
            string expected = "3";
            Assert::AreEqual(expected, actual);

            actual = test->integer(0, 2, 1);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->integer(1, 1, 2);
            expected = "-1";
            Assert::AreEqual(expected, actual);

            actual = test->integer(1, 2, 1);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->integer(2, 1, 2);
            expected = "2";
            Assert::AreEqual(expected, actual);

            actual = test->integer(2, 2, 1);
            expected = "1";
            Assert::AreNotEqual(expected, actual);

            actual = test->integer(3, 1, 2);
            expected = "1 / 2";
            Assert::AreEqual(expected, actual);

            actual = test->integer(3, 2, 1);
            expected = "2";
            Assert::AreNotEqual(expected, actual);
        }

分數計算測試用例:

TEST_METHOD(fractionTest)
        {
            string actual = test->fraction(0, 1, 2, 1, 2);
            string expected = "1";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(0, 1, 2, 1, 2);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->fraction(1, 1, 2, 1, 2);
            expected = "0";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(1, 1, 2, 1, 2);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->fraction(2, 1, 2, 1, 2);
            expected = "1 / 4";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(2, 1, 2, 1, 2);
            expected = "1";
            Assert::AreNotEqual(expected, actual);

            actual = test->fraction(3, 1, 2, 1, 2);
            expected = "1";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(3, 1, 2, 1, 2);
            expected = "2";
            Assert::AreNotEqual(expected, actual);
        }

最大公約數測試用例:

        TEST_METHOD(maxNumberTest)
        {
            int actual = test->maxNumber(6, 3);
            int expected = 3;
            Assert::AreEqual(expected, actual);

            actual = test->maxNumber(4, 2);
            expected = 3;
            Assert::AreNotEqual(expected, actual);
        }

結果:

咱們試了不少次,也問過同窗,查了百度,覺得是插件問題,也在vs20十、vs20十二、vs201三、vs2015都試過,上最終仍是沒能成功測試

小結與感覺:

自學能力真的過重要了,而我明顯這方面還很弱,c++的測試之前老師沒講,咱們須要本身查資料本身學習,看着不少同窗慢慢的都掌握了,測試也在有條不紊的進行,而咱們還不知所措,無奈只能向同窗求助,按照同窗給的教程連接http://www.cnblogs.com/xiehongfeng100/p/4438076.html一步步進行操做,一開始沒有注意到要下載Unit Test Generator工具,一直沒辦法測試成功,耗了很長時間,後來想起來取下載時,出現了這種狀況:

那就先去下載.NET Framework吧,卻又出現了這種狀況:

。。。

本身下的軟件不行,那就用一下同窗的軟件吧,按照同窗耐心指導的步驟作下來,在測試的時候,仍是出現了:

這樣幾個錯誤,找了很久一直不知道該如何修改。。。

單元測試就這麼失敗了,通過此次做業,我更加深入的體會到本身自主解決問題的能力仍是很是差的,急需提升啊

結對照片:

PSP:

相關文章
相關標籤/搜索