软件工程——第四次作业(2)

软件工程——第四次作业(2)

单元测试

作业要求:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/994

程序代码:https://git.coding.net/ss505072461/f4.git

结对编程队友:@葛美义

——————————————————————————————————————————

本次作业的程序是f4(四则运算生成),由于我实现的功能中,主要的内容是对于被输出的题目的结果计算,毕竟作为一个出题程序,自己都无法知道结果就是一个失败的程序。

我的编码及测试环境是Visual Studio 2015,运用的是环境中的单元测试功能。测试的方法是自行在网上搜索了很久学习的。

我在程序中,作为计算结果用的函数是:Result((float)a, (float)b, (float)c, (float)d, symbol[0], symbol[1], symbol[2]);

其中a、b、c、d这4个变量是随机生成的[1,9]整形数,由于有除法运算,在参数中就被转化为浮点数了,symbol数组是3个四则运算符的代表,其范围为[0,3]整形数,分别对应着加减乘除4种运算。

我在第一次测试中,参数设定是:

a = 1;b = 1;c = 1;d = 1;symbol[0] = 0;symbol[1] = 0;symbol[2] = 0;

其含义是1+1+1+1,预期结果是4,其测试目的仅仅是为了判断程序内容是否执行。

单元测试的代码如下:

using System.Threading.Tasks;

namespace f4_program.Tests
{
    [TestClass()]
    public class f4Tests
    {
        [TestMethod()]
        public void ResultTest()
        {
            int a = 1;
            int b = 1;
            int c = 1;
            int d = 1;
            int[] symbol = new int[] { 0, 0, 0 };
            Assert.IsTrue(f4.Result((float)a, (float)b, (float)c, (float)d, symbol[0], symbol[1], symbol[2]) == 4);
        }
    }
}
View Code

而测试后的结果是:

而后,我开始了对于一些相对复杂的内容计算测试:

参数设定:

a = 9;b = 8;c = 4;d = 6;symbol[0] = 3;symbol[1] =0;symbol[2] = 2;

即9/8+4*6,预期结果是25.125,开始测试计算的精确度,结果开始涉及小数。

代码:

using System.Threading.Tasks;

namespace f4_program.Tests
{
    [TestClass()]
    public class f4Tests
    {
        [TestMethod()]
        public void ResultTest()
        {
            int a = 9;
            int b = 8;
            int c = 4;
            int d = 6;
            int[] symbol = new int[] { 3, 0, 2 };
            Assert.IsTrue(f4.Result((float)a, (float)b, (float)c, (float)d, symbol[0], symbol[1], symbol[2]) == 25.125);
        }
    }
}
View Code

测试结果:

之后的几次测试:

1+1+1+1=4

9/8+4*6=25.125

6+1-3*2=1

7*6/3-2=12

1/8-7*9=-62.875

测试结果:

 

 

之所以在测试方面能够通过的原因是由于程序的内容十分简单,在功能实现上仅仅是实现了功能1的四则运算,并没有涉及到功能2的括号以及功能3的打印等问题,并且在看到微信群里的讨论后,程序的数据生成也被限制在[1,9]的整形数范围内,因此程序生成的结果也会被限制在一定的范围内,导致测试用样例选择也比较少。这是本人水平不足的问题。

其中之所以能够测试一次通过的计算代码(前方高能):

public static float Result(float a,float b,float c,float d,int sym1,int sym2,int sym3)
        {            
            //没错,因为三个运算符“只有”4*4*4=64种情况……
            if ((sym1 == 0) && (sym2 == 0) && (sym3 == 0))
                return(a + b + c + d);
            if ((sym1 == 0) && (sym2 == 0) && (sym3 == 1))
                return (a + b + c - d);
            if ((sym1 == 0) && (sym2 == 0) && (sym3 == 2))
                return (a + b + c * d);
            if ((sym1 == 0) && (sym2 == 0) && (sym3 == 3))
                return (a + b + c / d);

            if ((sym1 == 0) && (sym2 == 1) && (sym3 == 0))
                return (a + b - c + d);
            if ((sym1 == 0) && (sym2 == 1) && (sym3 == 1))
                return (a + b - c - d);
            if ((sym1 == 0) && (sym2 == 1) && (sym3 == 2))
                return (a + b - c * d);
            if ((sym1 == 0) && (sym2 == 1) && (sym3 == 3))
                return (a + b - c / d);            

            if ((sym1 == 0) && (sym2 == 2) && (sym3 == 0))
                return (a + b * c + d);
            if ((sym1 == 0) && (sym2 == 2) && (sym3 == 1))
                return (a + b * c - d);
            if ((sym1 == 0) && (sym2 == 2) && (sym3 == 2))
                return (a + b * c * d);
            if ((sym1 == 0) && (sym2 == 2) && (sym3 == 3))
                return (a + b * c / d);

            if ((sym1 == 0) && (sym2 == 3) && (sym3 == 0))
                return (a + b / c + d);
            if ((sym1 == 0) && (sym2 == 3) && (sym3 == 1))
                return (a + b / c - d);
            if ((sym1 == 0) && (sym2 == 3) && (sym3 == 2))
                return (a + b / c * d);
            if ((sym1 == 0) && (sym2 == 3) && (sym3 == 3))
                return (a + b / c / d);

            if ((sym1 == 1) && (sym2 == 0) && (sym3 == 0))
                return (a - b + c + d);
            if ((sym1 == 1) && (sym2 == 0) && (sym3 == 1))
                return (a - b + c - d);
            if ((sym1 == 1) && (sym2 == 0) && (sym3 == 2))
                return (a - b + c * d);
            if ((sym1 == 1) && (sym2 == 0) && (sym3 == 3))
                return (a - b + c / d);

            if ((sym1 == 1) && (sym2 == 1) && (sym3 == 0))
                return (a - b - c + d);
            if ((sym1 == 1) && (sym2 == 1) && (sym3 == 1))
                return (a - b - c - d);
            if ((sym1 == 1) && (sym2 == 1) && (sym3 == 2))
                return (a - b - c * d);
            if ((sym1 == 1) && (sym2 == 1) && (sym3 == 3))
                return (a - b - c / d);

            if ((sym1 == 1) && (sym2 == 2) && (sym3 == 0))
                return (a - b * c + d);
            if ((sym1 == 1) && (sym2 == 2) && (sym3 == 1))
                return (a - b * c - d);
            if ((sym1 == 1) && (sym2 == 2) && (sym3 == 2))
                return (a - b * c * d);
            if ((sym1 == 1) && (sym2 == 2) && (sym3 == 3))
                return (a - b * c / d);

            if ((sym1 == 1) && (sym2 == 3) && (sym3 == 0))
                return (a - b / c + d);
            if ((sym1 == 1) && (sym2 == 3) && (sym3 == 1))
                return (a - b / c - d);
            if ((sym1 == 1) && (sym2 == 3) && (sym3 == 2))
                return (a - b / c * d);
            if ((sym1 == 1) && (sym2 == 3) && (sym3 == 3))
                return (a - b / c / d);

            if ((sym1 == 2) && (sym2 == 0) && (sym3 == 0))
                return (a * b + c + d);
            if ((sym1 == 2) && (sym2 == 0) && (sym3 == 1))
                return (a * b + c - d);
            if ((sym1 == 2) && (sym2 == 0) && (sym3 == 2))
                return (a * b + c * d);
            if ((sym1 == 2) && (sym2 == 0) && (sym3 == 3))
                return (a * b + c / d);

            if ((sym1 == 2) && (sym2 == 1) && (sym3 == 0))
                return (a * b - c + d);
            if ((sym1 == 2) && (sym2 == 1) && (sym3 == 1))
                return (a * b - c - d);
            if ((sym1 == 2) && (sym2 == 1) && (sym3 == 2))
                return (a * b - c * d);
            if ((sym1 == 2) && (sym2 == 1) && (sym3 == 3))
                return (a * b - c / d);

            if ((sym1 == 2) && (sym2 == 2) && (sym3 == 0))
                return (a * b * c + d);
            if ((sym1 == 2) && (sym2 == 2) && (sym3 == 1))
                return (a * b * c - d);
            if ((sym1 == 2) && (sym2 == 2) && (sym3 == 2))
                return (a * b * c * d);
            if ((sym1 == 2) && (sym2 == 2) && (sym3 == 3))
                return (a * b * c / d);

            if ((sym1 == 2) && (sym2 == 3) && (sym3 == 0))
                return (a * b / c + d);
            if ((sym1 == 2) && (sym2 == 3) && (sym3 == 1))
                return (a * b / c - d);
            if ((sym1 == 2) && (sym2 == 3) && (sym3 == 2))
                return (a * b / c * d);
            if ((sym1 == 2) && (sym2 == 3) && (sym3 == 3))
                return (a * b / c / d);

            if ((sym1 == 3) && (sym2 == 0) && (sym3 == 0))
                return (a / b + c + d);
            if ((sym1 == 3) && (sym2 == 0) && (sym3 == 1))
                return (a / b + c - d);
            if ((sym1 == 3) && (sym2 == 0) && (sym3 == 2))
                return (a / b + c * d);
            if ((sym1 == 3) && (sym2 == 0) && (sym3 == 3))
                return (a / b + c / d);

            if ((sym1 == 3) && (sym2 == 1) && (sym3 == 0))
                return (a / b - c + d);
            if ((sym1 == 3) && (sym2 == 1) && (sym3 == 1))
                return (a / b - c - d);
            if ((sym1 == 3) && (sym2 == 1) && (sym3 == 2))
                return (a / b - c * d);
            if ((sym1 == 3) && (sym2 == 1) && (sym3 == 3))
                return (a / b - c / d);

            if ((sym1 == 3) && (sym2 == 2) && (sym3 == 0))
                return (a / b * c + d);
            if ((sym1 == 3) && (sym2 == 2) && (sym3 == 1))
                return (a / b * c - d);
            if ((sym1 == 3) && (sym2 == 2) && (sym3 == 2))
                return (a / b * c * d);
            if ((sym1 == 3) && (sym2 == 2) && (sym3 == 3))
                return (a / b * c / d);

            if ((sym1 == 3) && (sym2 == 3) && (sym3 == 0))
                return (a / b / c + d);
            if ((sym1 == 3) && (sym2 == 3) && (sym3 == 1))
                return (a / b / c - d);
            if ((sym1 == 3) && (sym2 == 3) && (sym3 == 2))
                return (a / b / c * d);
            if ((sym1 == 3) && (sym2 == 3) && (sym3 == 3))
                return (a / b / c / d);

            return 0;
        }
View Code

由于实现的功能比较少,因此测试用样例也显得十分地单薄……

 

posted @ 2017-10-11 11:46  打酱油的路过  阅读(114)  评论(0编辑  收藏  举报