Four arithmetic operations-unit test and four arithmetic unit test
Arithmetic Operations-unit test
Li Zhiqiang 201421123028 Lian Yonggang 201421123014 Lin dialect 201421123023
Coding address: https://git.coding.net/lizhiqiang0x01/sizeyunsuan-TestExample.git
I. Question requirements
1. Pass the unit test code to test whether addition works correctly;
2. Pass the unit test code to test the addition, subtraction, multiplication, division, and Division functions.
3. Pass the unit test code to test the computing class's support for various parameters:
A. The input is incorrect, for example, "1 + + 2 ",
B. When the value range is-1000 .. 1000, transfer "10000 + 32768 ",
C. Or "248/0?
D. How can I tell the function caller "You are wrong "? Define the returned string as "-1" to represent?
E. What if the calculation result is "-1?
4. Improve the program through incremental modification to handle various errors.
Ii. experiment step a. Requirement Analysis
The unit test code is used to test whether the program addition, subtraction, multiplication, division, and Division functions work correctly. Whether the format error, numeric overflow, and division by zero exception can be detected.
B. design the test framework and simulate the test data.
- Test addition, subtraction, multiplication, division
TEST_METHOD (TestAdd) // Test addition {Test. checkInput ("1/2 + 4"); Assert: AreEqual (test11, Test. add ();} TEST_METHOD (TestAdd1) {Test. checkInput ("3 + 1/3"); Assert: AreEqual (test12, Test. add ();} TEST_METHOD (TestAdd2) {Test. checkInput ("2 + 4"); Assert: AreEqual (test13, Test. add ();} TEST_METHOD (TestAdd3) {Test. checkInput ("1/2 + 1/4"); Assert: AreEqual (test14, Test. add ();} TEST_METHOD (TestSub) // Test subtraction {Test. checkInput ("1/2-1/3"); Assert: AreEqual (test21, Test. sub ();} TEST_METHOD (TestSub1) {Test. checkInput ("1/3-2"); Assert: AreEqual (test22, Test. sub ();} TEST_METHOD (TestSub2) {Test. checkInput ("2-1/3"); Assert: AreEqual (test23, Test. sub ();} TEST_METHOD (TestSub3) {Test. checkInput ("5-3"); Assert: AreEqual (test24, Test. sub ();} TEST_METHOD (TestMul) // Test multiplication {Test. checkInput ("1 × 2"); Assert: AreEqual (test31, Test. mul ();} TEST_METHOD (TestMul1) {Test. checkInput ("1/2 × 2"); Assert: AreEqual (test32, Test. mul ();} TEST_METHOD (TestMul2) {Test. checkInput ("2 ×1/3"); Assert: AreEqual (test33, Test. mul ();} TEST_METHOD (TestMul3) {Test. checkInput ("1/3 × 1/2"); Assert: AreEqual (test34, Test. mul ();} TEST_METHOD (TestDiv) // Test the Division {Test. checkInput ("1/2 limit 2"); Assert: AreEqual (test41, Test. div ();} TEST_METHOD (TestDiv1) {Test. checkInput ("4 digit 1/2"); Assert: AreEqual (test42, Test. div ();} TEST_METHOD (TestDiv2) {Test. checkInput ("1/2 limit 2/3"); Assert: AreEqual (test43, Test. div ();} TEST_METHOD (TestDiv3) {Test. checkInput ("1 limit 2"); Assert: AreEqual (test44, Test. div ());}
- Test the format error, numeric overflow, and zero division exception Function
TEST_METHOD (TestCheckFormat1) // Test format error {Test. checkInput ("2--2"); Assert: AreEqual (test5, Test. g_szErrIn);} TEST_METHOD (TestCheckFormat2) {Test. checkInput ("2 + partition 2"); Assert: AreEqual (test5, Test. g_szErrIn);} TEST_METHOD (TestCheckFormat3) {Test. checkInput ("2 + × 2"); Assert: AreEqual (test5, Test. g_szErrIn);} TEST_METHOD (TestCheckFormat4) {Test. checkInput ("2 + ×"); Assert: AreEqual (test5, Test. g_szErrIn);} TEST_METHOD (TestCheckFormat5) {Test. checkInput ("2 +/2"); Assert: AreEqual (test5, Test. g_szErrIn);} TEST_METHOD (TestCheckNum61) // Test the invalid value {Test. checkInput ("1 limit 0"); Test. div (); Assert: AreEqual (test6, Test. g_szErrNum);} TEST_METHOD (TestCheckNum62) {Test. checkInput ("1/0"); Test. div (); Assert: AreEqual (test6, Test. g_szErrNum);} TEST_METHOD (TestCheckNum63) {Test. checkInput ("1/2 + 2/0"); Test. add (); Assert: AreEqual (test6, Test. g_szErrNum);} TEST_METHOD (TestCheckBey71) // the Test time value overflows {Test. checkInput ("-2000 + 2"); Test. add (); Assert: AreEqual (test7, Test. g_szErrBey);} TEST_METHOD (TestCheckBey72) {Test. checkInput ("-2000"); Test. add (); Assert: AreEqual (test7, Test. g_szErrBey);} TEST_METHOD (TestCheckBey73) {Test. checkInput ("-200 + 2/3000"); Test. add (); Assert: AreEqual (test7, Test. g_szErrBey );}View Code
- Unit Test addition Function
String Calc: add () {size_t iPos = g_szInput.find ('+'); g_szX = limit (0, iPos); g_szY = g_szInput.substr (iPos + 1, g_szInput.length () -1-iPos); size_t iposner = g_szX.find ('/'); g_iFirNer = stoi (g_szX.substr (0, iposner); if (iposder ==-1) {g_iFirDer = 1 ;} else {g_iFirDer = stoi (g_szX.substr (iPosInX + 1, g_szInput.length ()-1-iPosInX);} size_t iPosInY = g_szY.find ('/'); g_iSecNer = stoi (g_szY.substr (0, iPosInY); if (iPosInY =-1) {g_iSecDer = 1;} else {g_iSecDer = stoi (g_szY.substr (iPosInY + 1, convert ()-1-second);} simplify (g_iFirNer, g_iFirDer, g_iSecNer, g_iSecDer); Convert = else * g_iSecDer + g_iFirDer * g_iSecNer; g_iRusDer = g_iFirDer * g_iSecDer; init (g_iRusNer, g_iRusDer); // score = ""; g_szRusNer = ""; if (g_iRusDer = 1) {g_szRusNer + = to_string (g_iRusNer ); return g_szRusNer;} else {g_szRusDer + = to_string (g_iRusDer); g_szRusNer + = to_string (g_iRusNer); return g_szRusNer + "/" + g_szRusDer ;}
}
C. Test Results
Iii. PSP
| PSP2.1 |
Personal Software Process Stages |
Estimated time (h) |
Actual time (h) |
| Planning |
Plan |
1 |
1.5 |
| · Estimate |
Estimated time required for this task |
10 |
15 |
| Development |
Development |
8 |
10 |
| · Analysis |
Demand Analysis (including learning new technologies) |
0.5 |
1 |
| · Design Spec |
Generate design document |
2 |
2 |
| · Design Review |
Design Review |
0.3 |
0.5 |
| · Coding Standard |
Code Specification |
0.5 |
0.4 |
| · Design |
Specific design |
1.5 |
1.3 |
| · Coding |
Code |
8 |
7 |
| · Code Review |
Code Review |
0.5 |
1 |
| · Test |
Test (self-test, code modification, and modification submission) |
0.2 |
0.5 |
| Reporting |
Report |
2 |
2 |
| · |
Test report |
0.5 |
0.5 |
| · |
Computing workload |
0.5 |
1 |
| · |
And propose a process improvement plan |
2 |
4
|
Iv. Summary
A piece of bread first:Teammates used c ++ to write their previous jobs and use c ++ for unit testing.
Put the meat on:We first split each functional module and tested multiple groups of corresponding modules to achieve the purpose of unit testing. During the split process, we solved the previous variable chaos. In this unit test, we made great improvements in code specifications. We used the Hungary naming method to name the variable, for example, g_iFirNer g_iFirDer g_szRusDer. In the structure of Attribute + Type + description, it is easy to understand the meaning of this variable, which significantly accelerates the team's speed in the Process of alternate code writing, in the subsequent programming path, we will pay more attention to code specifications.
Then there is another piece of bread.: After this unit test, we have a deeper understanding of software engineering. Originally, releasing a software requires many processes to be gradually improved and requires tacit team cooperation.