"Software Test" experiment three white box test

Source: Internet
Author: User
Tags greatest common divisor

1. computes the greatest common divisor of an integer x and an integer y

Use the class and method implementation (define a class, define a greatest common divisor method in the Class), and name it according to the specification. In the main method, get the two integers entered by the user, call the previously written method, and output their greatest common divisor. Use FindBugs to find out if a bug exists in the program.

import Java.util.Scanner;

Public class Demo {

Public void Compare (int A,int b) {

int i = a>b?a:b;

for (; i>0;i--) {

if (a%i==0&&b%i==0) {

System. out. println ("A, B greatest common divisor" +i);

break;

}

}

return;

}

Public Static void Main (string[] arg) {

Demo demo = New demo ();

System. out. println ("Please enter A:");

Scanner NUM1 = new Scanner (System. in);

int a=num1.nextint ();

System. out. println ("Please enter B:");

Scanner num2 = new Scanner (System. in);

int b=num2.nextint ();

Demo.compare (A, b);

}

}

2. Application of Logic overlay

According to the program flowchart given, write out the statement overlay, branch coverage of the test case, and the path it covers.

Statement overrides:

X Y Path
3 2 Abc
5 0 Aef
4 0 Aeg

Branch Coverage:

X Y Path
3 2 Abc
3 1 Abd
5 0 Aef
4 0 Aeg

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

Public class Demo {

Public void Demo (int x,int y) {

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

if (y>1) {

y=y+1;

System. out. println ("x=" +x+ ", y=" +y);

}

Else {

System. out. println ("x=" +x+ ", y=" +y);

}

}

Else {

if (x>=5) {

X=x-y;

System. out. println ("x=" +x+ ", y=" +y);

}

X=x+y;

System. out. println ("x=" +x+ ", y=" +y);

}

}

}

Test Case:

Import static org.junit.assert.*;

Import Org.junit.Test;

public class Demotest {

public static void Main (string[] args) {

Demo Demo =new demo ();

Demo.demo (3,2);

Demo.demo (3,1);

Demo.demo (5,0);

Demo.demo (4,0);

}

}

Software test experiment three white box test

Related Article

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.