测试脚本如下 PS:北航一位同学编写链接:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ruangong2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExpressionOutputer; //DLL引用
using System.IO;
namespace ruangong2.Tests
{
[TestClass()]
public class Form1Tests
{
[TestMethod()]
public void test1()
//复杂式子评判
{
string exercise = "( ( ( 62'1/8 ÷ ( 9'1/7 ÷ 80'9/47 ) ) ÷ ( (-24'5/18) × (-7'26/37) ) ) + (-84) )";
string answer = "-81'43054479/499508480";
core ccc = new core();
try {
string sss = ccc.calc(exercise);
Assert.AreEqual<string>(answer, sss);
}
catch(Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test2()
//运算符错乱异常
{
string exercise = "( ( ( 62'1/8 ÷ ( 9'1/7 ÷ 80'9/47 ) ) ÷ ( (-24'5/18) × (-7'26/37) ) ) + (-84) *** )";
core ccc = new core();
string sss = "";
try
{
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (Exception e) {
//MessageBox.Show(exceptionStr,"异常");
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test3()
//括号不匹配异常
{
string exercise = "( ( ( 62'1/8 ÷ ( 9'1/7 ÷ 80'9/47 ) ) ÷ ( (-24'5/18) × (-7'26/37) ))) ) + (-84) )";
core ccc = new core();
string sss = "";
try
{
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (Exception e) {
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test31()
//除0异常
{
string exercise = "13 / (5-5)";
core ccc = new core();
string sss = "";
try
{
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (Exception e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test4()
//检查乱码异常
{
string exercise = "( ( ( 62'1/8 ÷ ( 9'1/7 ÷ 80'9/47 ) ) ÷ ( (-24'5/18) × (-7'26/37) ) ) #$%^&*不想写软工!+ (-84) )";
core ccc = new core();
string sss;
try {
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (Exception e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test5()
//基础功能测试
{
string exercise = "1 + 5 - 1 + 2";
string answer = "7";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test6()
//基础功能测试
{
string exercise = "10 - ( 6 - ( 0 × 3 ) )";
string answer = "4";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test7()
//基础功能测试--判错
{
string exercise = "10 - ( 6 - ( 0 × 3 ) )";
string answer = "5";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreNotEqual<string>(answer, sss);
}
[TestMethod()]
public void test8()
//基础功能测试
{
string exercise = "(-10) - ( ( (-1) ÷ (-10) ) ÷ (-2'1/2) )";
string answer = "-9'24/25";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test9()
//基础功能测试
{
string exercise = "2 ÷ ( ( (-4'7/9) × (-3'5/6) ) ÷ 6'1/2 )";
string answer = "702/989";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test10()
//基础功能测试
{
string exercise = "( ( 4'1/5 ÷ 9 ) × (-8) ) + (-9)";
string answer = "-12'11/15";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test11()
//测试select传入参数异常
{
core ccc = new core();
try
{
ccc.intSelect(0, 0, 0);
Assert.Fail();
}
catch (InvalidDataException e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test12()
//测试文件路径错误异常
{
core ccc = new core();
ccc.addInit("C:\\abc\\cde.txt", "C:\\abc\\cde2.txt", "C:\\abc\\cde3.txt");
try
{
ccc.generate();
Assert.Fail();
}
catch (FileNotFoundException e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test13()
//测试文件路径错误异常
{
core ccc = new core();
ccc.addInit("C:\\abc\\cde.txt", "C:\\abc\\cde2.txt", "C:\\abc\\cde3.txt");
try
{
ccc.check();
Assert.Fail();
}
catch (FileNotFoundException e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test14()
//基础功能测试
{
string exercise = "-5'46/78*59 / (-37+1/4-35-2) ÷ 5";
string answer = "872/975";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test15()
//基础功能测试
{
string exercise = "-0/6";
string answer = "0";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test16()
//基础功能测试
{
string exercise = "-5'8/2";
string answer = "-9";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test17()
//基础功能测试
{
string exercise = "-25124/125";
string answer = "-200'124/125";
core ccc = new core();
string sss = ccc.calc(exercise);
Console.WriteLine(sss);
Assert.AreEqual<string>(answer, sss);
}
[TestMethod()]
public void test18()
//非法分数异常
{
string exercise = "21'3/-2";
core ccc = new core();
string sss = "";
try
{
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (FormatException e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test19()
//非法分数异常
{
string exercise = "21'-3/2";
core ccc = new core();
string sss = "";
try
{
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (FormatException e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test20()
//非法分数异常
{
string exercise = "2222222222222222222222222222222222222222222222222/2";
core ccc = new core();
string sss = "";
try
{
sss = ccc.calc(exercise);
Assert.Fail();
}
catch (InvalidDataException e)
{
Assert.IsTrue(true);
}
}
[TestMethod()]
public void test21()
//基础功能性测试
{
core ccc = new core();
ccc.init();
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test22()
//基础功能性测试
{
core ccc = new core();
ccc.select(10,8,50000,false,true,true,true);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test23()
//基础功能性测试
{
core ccc = new core();
ccc.select(5, 32, 1000, true, false, false, true);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test24()
//基础功能性测试
{
core ccc = new core();
ccc.select(1, 2, 20, true, true, true, true);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test25()
//基础功能性测试
{
core ccc = new core();
ccc.select(3, 5, 100, true, false, false, false);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test26()
//基础功能性测试
{
core ccc = new core();
ccc.select(3, 5, 100, false, true, false, false);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test27()
//基础功能性测试
{
core ccc = new core();
ccc.select(3, 5, 100, false, false, true, false);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test28()
//基础功能性测试
{
core ccc = new core();
ccc.select(3, 5, 100, false, false, false, true);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test29()
//基础功能性测试
{
core ccc = new core();
ccc.select(3, 5, 20, false, false, false, false);
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test30()
//基础功能性测试
{
core ccc = new core();
ccc.select(3, 5, 100000, true, true, true, true);
ccc.stdExpr();
ccc.showInfo();
try
{
ccc.generate();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
[TestMethod()]
public void test32()
//极限查重测试
{
core ccc = new core();
try
{
ccc.select(1, 2, 6, false, false, false, false);
ccc.generate();
Assert.IsTrue(ccc.check());
ccc.select(1, 2, 10, false, false, false, true);
ccc.generate();
Assert.IsTrue(ccc.check());
ccc.select(1, 2, 6, false, false, false, false);
ccc.generate();
ccc.stdExpr();
Assert.IsTrue(ccc.check());
ccc.select(1, 2, 10, false, false, false, true);
ccc.generate();
ccc.stdExpr();
Assert.IsTrue(ccc.check());
}
catch (Exception e)
{
Assert.Fail();
}
}
}
}
运行测试用例之后的结果如下: