Software Testing (iv) Printprimes

Source: Internet
Author: User

Homework 3

The code for the topic is as follows:

1 /******************************************************* 2 * Finds and prints n prime integers3 * Jeff Offutt, Spring 20034      ******************************************************/ 5      Public Static voidPrintprimes (intN)6     { 7         intCurprime;//Value currently considered for primeness8         intNumprimes;//Number of primes found so far.9         BooleanIsPrime;//Is curprime prime?Ten         int[] primes =New int[Maxprimes];//The list of prime numbers. One          A         //Initialize 2 into the list of primes. -primes [0] = 2;  -Numprimes = 1;  theCurprime = 2;  -          while(Numprimes <N) -         {  -curprime++;//next number to consider ... +IsPrime =true;  -              for(inti = 0; I <= numPrimes-1; i++)  +{//For each previous prime. A                 if(Isdivisible (primes[i], curprime)) at{//Found a divisor, curprime is not prime. -IsPrime =false;  -                      Break;//Out of loop through primes. -                 }  -             }  -             if(IsPrime) in{//Save It! -Primes[numprimes] =Curprime; tonumprimes++;  +             }  -}//End while the          *         //Print all the primes out. $          for(inti = 0; I <= numPrimes-1; i++) Panax Notoginseng         {  -System.out.println ("Prime:" +Primes[i]);  the         }  +}//End Printprimes

(a): Control flow graph as follows (drawing with ProcessOn)

(b): Set Maxprimes to 4 so that t2= (n=5) will have an array out of bounds error, but t1= (n=3) has no effect.

(c): N=1 not meet Numprimes < N, it does not go through the while loop

(d): dot overlay: {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

Side overlays: {(2,3), (2,12), (3,4), (4,5), (5,6), (6,7), (6,8), (7,5), (8,9), (5,9), (9,10), (9,11), (10,11), (11,2), (12,13), (13,14), (14,15), (15,13), (13,16)}

Main path overrides: {(1,2,3,4,5,6,7), (1,2,3,4,5,6,8,9,10,11), (1,2,3,4,5,6,8,9,11), (1,2,3,4,5,9,10,11), (1,2,3,4,5,9,11), (1, 2,12,13,14,15), (1,2,12,16), (3,4,5,6,8,9,10,11,2,12,13,14,15),

(3,4,5,6,8,9,11,2,12,13,14,15), (3,4,5,6,8,9,10,11,2,12,13,16), (3,4,5,6,8,9,11,2,12,13,16), (3, 4,5,9,10,11,2,12,13,14,15), (3,4,5,9,11,2,12,13,14,15), (3,4,5,9,10,11,2,12,13,16),

(3,4,5,9,11,2,12,13,16), (6,7,5,9,10,11,2,12,13,14,15), (6,7,5,9,11,2,12,13,14,15), (6,7,5,9,10,11,2,12,13,16), (6 , 7,5,9,11,2,12,13,16), (14,15,13,16), (13,14,15,13), (5,6,7,5),

(2,3,4,5,6,8,9,10,11,2), (2,3,4,5,6,8,9,11,2), (2,3,4,5,9,10,11,2), (2,3,4,5,9,11,2)}

Design test Cases for master path overrides

To test the above program Printprimes for example, the following test program with some extracurricular self-learning things ~

The code is as follows:

1 Import Staticorg.junit.assert.*;2 3 ImportJava.io.ByteArrayOutputStream;4 ImportJava.io.PrintStream;5 ImportJava.lang.reflect.Method;6 7 ImportOrg.junit.After;8 ImportOrg.junit.AfterClass;9 ImportOrg.junit.Before;Ten ImportOrg.junit.BeforeClass; One  A  -  Public classTest { -     Privateprintprimes p; the  -PrintStream console =NULL;//output stream (character device) -Bytearrayoutputstream bytes =NULL;//a stream of characters used to cache console redirection . -  + @org. Junit.before -      Public voidSetUp ()throwsException { +p =NewPrintprimes (); Initialize  Abytes =NewBytearrayoutputstream ();//Allocate Space atconsole = System.out;//gets the handle to the System.out output stream -System.setout (NewPrintStream (bytes));//REDIRECT character streams that were originally output to console console to bytes -     } -      - @org. Junit.after -      Public voidTearDown ()throwsException { in system.setout (console); -     } to      + @org. Junit.test -      Public voidTestResult ()throwsException { theString s =NewString ("prime:2" + ' \ R ' + ' \ n ');//console wrap, where ' \ r ' + ' \ n ' is equivalent to println *s + = "prime:3" + ' \ R ' + ' \ n '; $s + = "prime:5" + ' \ R ' + ' \ n ';Panax Notoginseng          -Class pp =P.getclass (); the         //Get Method +method = Pp.getdeclaredmethod ("Printprimes",  A                 Newclass[]{int.class}); the         //The private settings can be accessed +Method.setaccessible (true); -         //Pass value, return result object $Method.invoke (P, 3); $        // comparison results  -Assertequals (S, bytes.tostring ());//The bytes.tostring () function is to convert the bytes content to a character stream -  the     } -}

The test results are as follows: Master path overwrite completed

Software Testing (iv) Printprimes

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.