Appendix

Sample Extreme Testing Application

1. check4Prime.java

To compile:

 &> javac check4Prime.java

To run:

 $> java -cp check4Prime 5

Right . . . 5 is a prime number!

 $> java -cp check4Prime 10

Sorry . . . 10 is NOT a prime number!

 $> java -cp check4Prime A
 Usage: check4Prime x
        - where 0<=x<=1000

Source code:

  //check4Prime.java//
  Imports
  import java.lang.*;

  public class check4Prime {

    static final int max = 1000;   // Set upper bounds.
    static final int min = 0;      // Set lower bounds
    static int input =0;           // Initialize input variable

  public static void main (String [] args) {

    //Initialize class object to work with
    check4Prime check = new check4Prime();

    try{
    //Check arguments and assign value to input variable
      check.checkArgs(args);

    //Check for Exception and display help
    }catch (Exception e){
      System.out.println("Usage: check4Prime x");
      System.out.println("-- where 0<=x<=1000");
   System.exit(1);
   }
   //Check if input is a prime number
   if (check.primeCheck(input))
      System.out.println("Right... " + input + " is a prime number!");
   else
      System.out.println("Sorry... " + input + " is NOT a prime number!");

 } //End main

 //Calculates prime numbers and compares it to the input
 public boolean primeCheck (int num){

    double sqroot = Math.sqrt(max);// Find square root of n

 //Initialize array to hold prime numbers
 boolean primeBucket [] = new boolean [max+1];

 //Initialize all elements to true, then set non-primes to false
 for (int i=2; i<=max; i++){
   primeBucket[i]=true;
 }

 //Do all multiples of 2 first
 int j=2;
 for (int i=j+j; i<=max; i=i+j){    //start with 2j as 2 is prime
   primeBucket[i]=false;                   //set all multiples to false
 }
 for (j=3; j<=sqroot; j=j+2){       // do up to sqrt of n
   if (primeBucket[j]==true){              // only do if j is a prime
    for (int i=j+j; i<=max; i=i+j){	// start with 2j as j is prime
     primeBucket[i]=false;                 // set all multiples to false
    }
   }
 }
 //Check input against prime array
    if (primeBucket[num] == true) {
    return true;
    }else{
    return false;
    }

 }//end primeCheck()
 //Method to validate input
 public void checkArgs(String [] args) throws Exception{
    //Check arguments for correct number of parameters
    if (args.length != 1) {
       throw new Exception();
    }else{
     //Get integer from character
     Integernum = Integer.valueOf(args[0]);
     input = num.intValue();

    //If less than zero
    if (input < 0)          //If less than lower bounds
      throw new Exception();
    else if (input > max)   //If greater than upper bounds
      throw new Exception();
     }
   }
 }//End check4Prime

2. check4PrimeTest.java

Requires the JUnit api, junit.jar

To compile:

 $> javac -classpath .:junit.jar check4PrimeTest.java

To run:

 $> java -cp .:junit.jar check4PrimeTest

Examples:

Starting test . . .

  . . . . . .

Time: 0.01

OK (7 tests)

Test finished . . .

Source code:

   //check4PrimeTest.java
   //Imports
   import junit.framework.*;

   public class check4PrimeTest extends TestCase{

     //Initialize a class to work with.
     private check4Prime check4prime = new check4Prime();

     //constructor
     public check4PrimeTest (String name){
       super(name);
     }
   //Main entry point
   public static void main(String[] args) {
      System.out.println("Starting test...");
      junit.textui.TestRunner.run(suite());
      System.out.println("Test finished...");
   } // end main()
   //Test case 1
   public void testCheckPrime_true(){
      assertTrue(check4prime.primeCheck(3));
   }

   //Test cases 2,3
   public void testCheckPrime_false(){
      assertFalse(check4prime.primeCheck(0));
      assertFalse(check4prime.primeCheck(1000));
   }

   //Test case 7
   public void testCheck4Prime_checkArgs_char_input(){
      try {
       String [] args= new String[1];
       args[0]="r";
       check4prime.checkArgs(args);
       fail("Should raise an Exception.");
      } catch (Exception success){
        //successfull test
      }
   } //end testCheck4Prime_checkArgs_char_input()

   //Test case 5
   public void testCheck4Prime_checkArgs_above_upper_bound(){
      try {
       String [] args= new String[1];
       args[0]="10001";
       check4prime.checkArgs(args);
       fail("Should raise an Exception.");
      } catch (Exception success){
        //successfull test
	   }
   } // end testCheck4Prime_checkArgs_upper_bound()

   //Test case 4
   public void testCheck4Prime_checkArgs_neg_input(){
      try {
       String [] args= new String[1];
       args[0]="-1";
       check4prime.checkArgs(args);
       fail("Should raise an Exception.");
      } catch (Exception success){
        //successfull test
      }
   }// end testCheck4Prime_checkArgs_neg_input()

   //Test case 6
   public void testCheck4Prime_checkArgs_2_inputs(){
      try { 
       String [] args= new String[2];
       args[0]="5"; 
       args[1]="99"; 
       check4prime.checkArgs(args); 
       fail("Should raise an Exception.");
      } catch (Exception success){ 
        //successfull test
      }
   } // end testCheck4Prime_checkArgs_2_inputs
   //Test case 8
	public void testCheck4Prime_checkArgs_0_inputs(){
      try {
       String [] args= new String[0];
       check4prime.checkArgs(args);
       fail("Should raise an Exception.");
      } catch (Exception success){
       //successfull test
      }
   } // end testCheck4Prime_checkArgs_0_inputs

   //JUnit required method.
   public static Test suite() {
      TestSuite suite = new TestSuite(check4PrimeTest.class);
      return suite;
   }//end suite()
 } //end check4PrimeTest
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.126.56