20160922-四则运算单元测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20160922-四则运算单元测试相关的知识,希望对你有一定的参考价值。
对结对项目四则运算进行单元测试,使用工具是vs2015中的MS Test。
代码及单元测试代码版本控制:
HTTPS:https://git.coding.net/li_yuhuan/CalculateTest.git
SSH:[email protected]:li_yuhuan/CalculateTest.git
GIT:git://git.coding.net/li_yuhuan/CalculateTest.git
单元测试:
1.判断字符串是否是数字
输入1:
"sd13d"
期待输出1:
false
输入2:
123.456
期待输出2:
true
(1)对应单元代码片段
static public bool IsNumberic(string str) { Regex reg = new System.Text.RegularExpressions.Regex(@"^[-\\+]?(([1-9]{1}[1-9]*)|([0]))(\\.\\d+)?$"); return reg.IsMatch(str); }
(2)对应测试用例代码
[TestMethod()] public void IsNumbericTest() { string input; bool expected; bool actual; //test1 input = "sd13d"; expected = false; actual = Program.IsNumberic(input); Assert.AreEqual(expected, actual); //test2 input = "123.456"; expected = true; actual = Program.IsNumberic(input); Assert.AreEqual(expected, actual); }
2.判断是否为运算符
输入1:
"2"
期待输出1:
false;
输入2:
"+"
期待输出2:
true;
(1)对应单元代码片段
static public bool isOperateors(string input) { if (input == "+" || input == "-" || input == "*" || input == "/" || input == "(" || input == ")" || input == "#") { return true; } else { return false; } }
(2)对应测试用例代码
[TestMethod()] public void isOperateorsTest() { string input; bool expected; bool actual; //test1 input = "2"; expected = false; actual = Program.isOperateors(input); Assert.AreEqual(expected, actual); //test2 input = "+"; expected = true; actual = Program.isOperateors(input); Assert.AreEqual(expected, actual); }
3.根据运算符优先级表返回运算符的优先级
输入:
3,2
期待输出:
1
(1)对应单元代码片段
public static int[,] m_priorityTable = new int[,] { /*行为入栈运算符,列为栈顶运算符,2表示等于号,1表示大于号, 0表示小于号,-1表示错误的匹配*/ //////* ‘+‘,‘-‘,‘*‘,‘/‘,‘(‘,‘)‘,‘#‘*/ /*‘+‘*/{ 1, 1, 0, 0, 0, 1, 1}, /*‘-‘*/{ 1, 1, 0, 0, 0, 1, 1}, /*‘*‘*/{ 1, 1, 1, 1, 0, 1, 1}, /*‘/‘*/{ 1, 1, 1, 1, 0, 1, 1}, /*‘(‘*/{ 0, 0, 0, 0, 0, 2, -1}, /*‘)‘*/{ 0, 0, 0, 0, -1, 1, 1}, /*‘#‘*/{ 0, 0, 0, 0, 0, -1, 2}, };
public static int IsPriority(int stackTop, int inputOperator) { return m_priorityTable[stackTop, inputOperator]; }
(2)对应测试用例代码
[TestMethod()] public void IsPriorityTest() { int input1; int input2; int expected; int actual; //test1 input1 = 3; input2 = 2; expected = 1; actual = Program.IsPriority(input1, input2); Assert.AreEqual(expected, actual); }
4.判断一个字符串是否为正整数
输入1:
"-1"
期待输出1:
false
输入2:
"3"
期待输出2:
true
(1)对应单元代码片段
static public bool IsPositiveInt(string str) { Regex reg1 = new Regex(@"^[1-9]\\d*$"); return reg1.IsMatch(str); }
(2)对应测试用例代码
[TestMethod()] public void IsPositiveIntTest() { string input; bool expected; bool actual; //test1 input = "-1"; expected = false; actual = Program.IsPositiveInt(input); Assert.AreEqual(expected, actual); //test1 input = "3"; expected = true; actual = Program.IsPositiveInt(input); Assert.AreEqual(expected, actual); }
5.将表达式分割后存入队列
输入:
"(0/2)+4+4"
期待输出:
{ "(", "0", "/", "2", ")", "+", "4", "+", "4" }
(1)对应单元代码片段、
static public Queue<string> SplitExpress(string express) { express += "#"; Queue<string> q = new Queue<string>(); string tempNum = string.Empty; char[] arryExpress = express.ToArray<char>(); int i = 0; int j = 0; while (j < express.Length) { if (isOperateors(arryExpress[j].ToString())) { if (i != j) { tempNum = express.Substring(i, j - i); q.Enqueue(tempNum); q.Enqueue(arryExpress[j].ToString()); i = j + 1; } else { q.Enqueue(arryExpress[j].ToString()); i++; } } j++; } //q.Enqueue("#"); return q; }
(2)对应测试用例代码
public void SplitExpressTest() { string input; Queue<string> expected; Queue<string> actual; //test input = "(0/2)+4+4"; expected = new Queue<string>(); expected.Enqueue("("); expected.Enqueue("0"); expected.Enqueue("/"); expected.Enqueue("2"); expected.Enqueue(")"); expected.Enqueue("+"); expected.Enqueue("4"); expected.Enqueue("+"); expected.Enqueue("4"); expected.Enqueue("#"); actual = Program.SplitExpress(input); CollectionAssert.AreEqual(expected, actual); }
6.将中缀表达式转换为后缀续表达式
输入:
{ "0", "-", "(", "7", "/", "2", ")", "+", "4", "#" }
期待输出:
{ "0", "7", "2", "/", "-", "4", "+" }
(1)对应单元代码片段
static public List<string> InorderToPostorder(Queue<string> q) { List<string> posterOrder = new List<string>(); Stack<string> inOrder = new Stack<string>(); inOrder.Push("#"); int count = q.Count; for (int i = 0; i < count; i++) { string item = q.Dequeue(); if (isOperateors(item)) { string m = inOrder.First(); int n = IsPriority(m_dicOperators[inOrder.First()],m_dicOperators[item]); while (1 == n) { string temp = inOrder.Pop(); if (temp != "(" && temp != ")") { posterOrder.Add(temp); } n = IsPriority(m_dicOperators[inOrder.First()], m_dicOperators[item]); } if (2 == n) { inOrder.Pop(); } else if (n != -1) { inOrder.Push(item); } else { return null; } } else { posterOrder.Add(item); } } return inOrder.Count == 0 ? posterOrder : null; }
(2)对应测试用例代码
[TestMethod()] public void InorderToPostorderTest() { Queue<string> input = new Queue<string>(); List<string> expected; List<string> actual; //test input.Enqueue("0"); input.Enqueue("-"); input.Enqueue("("); input.Enqueue("7"); input.Enqueue("/"); input.Enqueue("2"); input.Enqueue(")"); input.Enqueue("+"); input.Enqueue("4"); input.Enqueue("#"); expected = new List<string>() { "0", "7", "2", "/", "-", "4", "+" }; actual = Program.InorderToPostorder(input); CollectionAssert.AreEqual(expected, actual); }
7.计算后序表达式的值
输入:
{ "5", "2", "/", "3", "*", "8", "-" }
期待输出:
-0.5
(1)对应单元代码片段
static public bool IsResult(List<string> PostorderExpress, out double result) { if (PostorderExpress != null) { try { PostorderExpress.Add("#"); string[] tempArry = PostorderExpress.ToArray(); int length = tempArry.Length; int i = 0; while (tempArry[i] != "#") { if (isOperateors(tempArry[i])) { tempArry[i - 2] = Arithmetic(tempArry[i - 2], tempArry[i - 1], tempArry[i]); for (int j = i; j < length; j++) { if (j + 1 < length) { tempArry[j - 1] = tempArry[j + 1]; } } length -= 2; i -= 2; } i++; } result = double.Parse(tempArry[0]); return true; } catch (Exception e) { result = 0; return false; } } else { result = 0; return false; } }
(2)对应测试用例代码:
[TestMethod()] public void IsResultTest() { List<string> input; double expected; double actual; //test input = new List<string>() { "5", "2", "/", "3", "*", "8", "-" }; expected = -0.5; Program.IsResult(input, out actual); Assert.AreEqual(expected, actual); }
运行测试后效果截图:
结对项目拍照留念:
以上是关于20160922-四则运算单元测试的主要内容,如果未能解决你的问题,请参考以下文章