zoukankan      html  css  js  c++  java
  • 20180925-4 单元测试

    此作业的要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2146

    单元测试:

    单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。

    本次四则运算的实现使用了python语言,对于python的单元测试,我参考了下述链接的资料https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143191629979802b566644aa84656b50cd484ec4a7838000

    为了编写单元测试,只需要引入Python自带的unittest模块。

    单元测试代码

    对生成算式、将算式转为逆波兰表达式、计算算式结果3个函数设计如下单元测试:

     1 # -*- coding: utf-8 -*-
     2 import unittest
     3 from f4 import *
     4 
     5 
     6 class F4Test(unittest.TestCase):
     7     def test_f4(self):
     8         pass
     9 
    10     def test01_create_equation(self):           # 测试顺序按函数名字字典顺序进行
    11         print("create_equation函数单元测试开始:")
    12         self.assertIsNotNone(create_equation())
    13         print("OK")
    14         print("create_equation函数单元测试结束。
    ")
    15 
    16     def test02_reverse_polish(self):
    17         eq = []
    18         print("reverse_polish函数单元测试开始:")
    19         equation = input("输入一个四则运算(括号请使用英文版的括号):")
    20         _eq_ans = input("输入正确的逆波兰表达式:")
    21         list(equation)          # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型
    22         for temp in equation:
    23             if '0' <= temp <= '9':
    24                 eq.append(int(temp))
    25             else:
    26                 eq.append(temp)
    27         re_equation = reverse_polish(eq)
    28         str_equation = "".join('%s' % id for id in re_equation)
    29         self.assertEqual(_eq_ans, str_equation)
    30         print("OK")
    31         print("reverse_polish函数单元测试结束。
    ")
    32 
    33     def test03_calculate(self):
    34         eq = []
    35         print("calculate函数单元测试开始:")
    36         equation = input("输入一个可计算的逆波兰表达式:")
    37         _eq_ans = input("输入该表达式的正确结果:")
    38         list(equation)  # 输入的表达式是str类型,该函数处理的是含有整型和字符型的list类型
    39         for temp in equation:
    40             if '0' <= temp <= '9':
    41                 eq.append(int(temp))
    42             else:
    43                 eq.append(temp)
    44         result = calculate(eq)
    45         self.assertEqual(float(_eq_ans), result)
    46         print("OK")
    47         print("calculate函数单元测试结束。
    ")
    48 
    49 
    50 if __name__ == "__main__":
    51     unittest.main()
    f4Test

    测试1(表达式不带括号):

    测试2(表达式包含括号):

     

    测试结果均正确,欢迎使用更多测试用例进行测试。

    被测试的源码(f4.py)和单元测试的代码的链接为:https://git.coding.net/Ruidxr/f4.git

    create_equation函数:

     1 def create_equation():
     2     eq = []
     3 
     4     for i in range(3):
     5         eq.append(random.randint(0, 10))
     6         eq.append(operator[random.randint(0, 3)])
     7     eq.append(random.randint(0, 10))
     8     p = random.randint(1, 5)
     9     if p is 1:
    10         eq.insert(0, "(")
    11         eq.insert(4, ")")
    12     elif p is 2:
    13         eq.insert(0, "(")
    14         eq.insert(6, ")")
    15     elif p is 3:
    16         eq.insert(2, "(")
    17         eq.insert(6, ")")
    18     elif p is 4:
    19         eq.insert(2, "(")
    20         eq.append(")")
    21     elif p is 5:
    22         eq.insert(4, "(")
    23         eq.append(")")
    24     return eq
    create_equation

     reverse_polish函数:

     1 def reverse_polish(equation):
     2     result = []
     3     c = []
     4     slist = [i for i in equation]
     5 
     6     for item in slist:
     7         if item in range(0, 100):
     8             result.append(item)
     9         elif not c and item in cal.keys():
    10             c.append(item)
    11             continue
    12         elif c and item in cal.keys():
    13             for x in range(c.__len__()):
    14                 z = c[-1]
    15                 temp = cal[z] if z in cal else cal1[z]
    16                 if temp >= cal[item]:
    17                     result.append(c.pop())
    18                 else:
    19                     c.append(item)
    20                     break
    21             if not c:
    22                 c.append(item)
    23         elif item is ")":
    24             for x in range(c.__len__()):
    25                 if c[-1] == "(":
    26                     c.pop()
    27                     break
    28                 else:
    29                     result.append(c.pop())
    30         elif item is "(":
    31             c.append(item)
    32         # print(result,c)
    33     for x in range(c.__len__()):
    34         result.append(c.pop())
    35     return result
    reverse_polish

    calculate函数:

     1 def calculate(re_equation):
     2     stack = PyStack()
     3     sumEnd = 0
     4 
     5     if len(re_equation) is 0:
     6         return sumEnd
     7     for i in re_equation:
     8         if i in range(0, 100):
     9             stack.push(float(i))
    10         elif '+' is i:
    11             a = stack.pop()
    12             b = stack.pop()
    13             stack.push(b + a)
    14         elif '-' is i:
    15             a = stack.pop()
    16             b = stack.pop()
    17             stack.push(b - a)
    18         elif '*' is i:
    19             a = stack.pop()
    20             b = stack.pop()
    21             stack.push(b * a)
    22         elif '÷' is i:
    23             a = stack.pop()
    24             b = stack.pop()
    25             if a is 0:
    26                 return False             #print('%d/%d分子不能为0' % (b, a))
    27             else:
    28                 stack.push(b / a)
    29     return stack.pop()
    calculate
  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    lambda表达式
    VIM--保存和退出等命令
  • 原文地址:https://www.cnblogs.com/ruidxr/p/9745879.html
Copyright © 2011-2022 走看看