Experiment Three white Box test

Source: Internet
Author: User

1 . Computes the greatest common divisor of integer x and integer y. (not allowed in the way the classroom is used)

L Use classes and methods (define a class, define a greatest common divisor method in the Class), and name it according to the specification.

L GET the two integers entered by the user in the main method, call the previously written method, and output their greatest common divisor.

L use FindBugs to find out if there are any bugs in the program.

1. Ask for greatest common divisor:

Package cn.wang.cd;

import Java.util.Scanner;

Public class Test1 {

/**
* Beg Greatest common divisor
* 2016/04/01 Wang Pengxin
* x is the first number
* Y is a second number
* @param args
*/

Public Static int gcd (int m,int N) {

Define a method to find greatest common divisor

while (true) {

if ((m = m% n) = = 0)

return N;

if ((n = n m) = = 0)

return m;

}

}

Public Static void Main (string[] args) {

Scanner cin=New Scanner (System. in);

System. out. println ("Please enter the first number:");

int a=cin.nextint ();

System. out. println ("Please enter a second number:");

int b=cin.nextint ();

int c = gcd(A, b); Calling methods

System. out. println ("The greatest common divisor of two numbers is:" + C);

}

}

Bug Lookup:

No bug has occurred.

Summary: Fingbug is a static code detection tool that checks the correctness of the program by analyzing or checking the syntax, structure, process, and connection of the source program, before the dynamic test.

2, the application of logic coverage

L write out the test cases covered by the statement, the branch coverage, and the path it covers, according to the program flowchart given.

L Add-on: According to the program flowchart, write out the code (define a class and method to implement), generate unit tests with JUnit, and test using the test cases previously designed.

(1) Statement overlay:

Test path: ABC AEG AEF

Test Case:

X=1 x=5 x=4

y=2 y=0 y=0

x=2 y=3 expected results for x=2, y=4

Branch Coverage:

Test path: ABC Abd AEG AEF

Test Case:

X=1 x=1 x=5 x=4

y=2 Y=1 y=0 y=0

(2)

Method:

/**

*

* 2016/04/06 Wang Pengxin

* @param args

*/

Package cn.wang.cd;

import Java.util.Scanner;

Public class TEST3 {

Public void Run () {

Scanner cin=New Scanner (System. in);

System. out. println ("Please enter the value of x:");

int x=cin.nextint ();

System. out. println ("Please enter the value of y:");

int y=cin.nextint ();

if (X<4 | | y>0) {

if (y>1) {

y=y+1;

System. out. println (the value of "X" is: "+x +" \ n "+" Y is: "+y);

}

Else {

System. out. println (the value of "X" is: "+x +" \ n "+" Y is: "+y);

}

}

Else {

if (x>=5) {

x = XY;

System. out. println (the value of "X" is: "+x+" \ n "+" Y is: "+y);

}

Else {

x = X+y;

System. out. println (the value of "X" is: "+x+" \ n "+" Y is: "+y);

}

}

}

}

Main function:

Package cn.wang.cd;

import Java.util.Scanner;

Public class Test2 {

Public Static void Main (string[] args) {

TODO auto-generated Method stub

Test3 test3 = new Test3 ();

Test3.run ();

}

}

Unit tests:

Package cn.wang.cd;

Import static org.junit.assert.*;

Public class Test {

@org. junit.test

Public void Test () {

Test3 test3 = new Test3 ();

Test3.run ();

}

}

Operation result;

Unit tests:

Summary: The strength of the branch overlay is stronger than the statement overlay, and the statement overlay cannot cover all branches.

Experiment Three white Box test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.