Experiment Three white Box test

Source: Internet
Author: User
Tags greatest common divisor

Experimental purpose

(1) Learn white box test method

(2) Mastering the logic covering methods of statement coverage, conditional coverage, branch coverage, etc.

(3) Mastering the use of Java Code Analysis tools

Experimental content

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

? Use classes and methods (write a method for greatest common divisor), and name them according to the specification.

? in the main mode, 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.

 Packagecn.lq.com;ImportJava.io.*;ImportJava.util.*;classDemo { Public Static voidMain (String args[])throwsException {Scanner cin=NewScanner (system.in); System.out.println ("Please enter the first number:"); intA =Cin.nextint (); System.out.println ("\ n Please enter a second number:"); intb =Cin.nextint (); intc =gcd (A, b); System.out.println ("\ n Greatest Common divisor:" +c); }     Public Static intgcdintAintb) { while(true) {            if((a = a% b) = = 0)                returnb; if((b = b% a) = = 0)                returnA; }    }    }

No syntax errors

2, the application of logic coverage

? write Java code (implemented using classes and methods) according to the program flowchart given

? Write a test case for a statement overlay, a branch overlay, and the path it covers, test with JUnit

 Packagecn.lq.com;ImportJava.util.Scanner; Public classDemo {/** * @paramargs*/ Public Static  voidTfloatXfloaty) {System.out.print ("The path passed is a");if(x<4| | Y>0) {System.out.print ("B");if(y>1) {System.out.println (C); y=y+1; System.out.println ("X=" +x); System.out.println ("Y=" +y);}Else{System.out.println ("D"); System.out.println ("End judgment"); System.out.println ("X=" +x); System.out.println ("Y=" +y);}}Else{System.out.print (E);if(x>=5) {System.out.println ("F"); x=x-y; System.out.println ("X=" +x); System.out.println ("Y=" +y);}Else{System.out.println ("G"); x=x+y; System.out.println ("X=" +x); System.out.println ("Y=" +y); }}}

Test class

 package   cn.lq.com;  import  static  org.junit.assert.*;  import   Org.junit.Test;  public  class   Demotest {@Test  public  void   Test () { float  x = 2; float  y = 1 "test two numbers for x=" +x+ "y=" +y+ ""  

Statement overrides

Test Cases

Overwrite use cases

Results

< Span style= "font-family: ' Courier New ';" >x=4 y=0

aeg

x=4 y=0

< Span style= "font-family: ' Courier New ';" >x=5 y=0

aef

x=5 y=0

X=2 y=2

Adc

X=2 Y=3

Branch Overrides

< Span style= "font-family: ' Courier New ';" > test case

overwrite use case

result

< Span style= "font-family: ' Courier New ';" >x=4 y=0

aeg

x=4 y=0

< Span style= "font-family: ' Courier New ';" >x=5 y=0

aef

x=5 y=0

< Span style= "font-family: ' Courier New ';" >x=2 y=2

ADC

x=2 y=3

x=2 Y=1

Abd

x=2 Y=1

We test the statements covered by the

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.