package analysis;

/** A driver program to run the Loop-based analysis exercises.
 * Usage: java TestAnalysis exercise n
 */
public class TestAnalysis {

  private static int MAX_EXERCISE = 8;
    private static int NUMBER_REPETITIONS = 10000;
  public static void printUsage() {

    System.out.println("Usage: java TestAnalysis exercise n");
    System.out.println("  exercise - an integer between 1 and + " + 
                       MAX_EXERCISE + "(inclusive)");
    System.out.println("  n        - a non-negative integer");

  }

    public static int proc (int n)
    {
        return n;
    }
    
  public static void main(String[] args) throws Exception {

    if (args.length != 2) {
      printUsage();
      System.exit(1);
    }

    int ex=0,n=0,res=0;


    try {
      ex = Integer.parseInt(args[0]);
      n = Integer.parseInt(args[1]);
    }
    catch (NumberFormatException e) {
      printUsage();
      System.exit(1);
    }

    if (ex>MAX_EXERCISE || ex <= 0 || n<0) {
      printUsage();
      System.exit(1);
    }

    int i, j;

    System.out.println ("Running Exercise " + ex + " with n="+n);
    System.out.println ("   run repeated " + NUMBER_REPETITIONS 
                        + " squared times");

    // first time doubly-nested loop and assignment
    final long baseStartTime = System.currentTimeMillis();
    final long baseEndTime;
    for (i = 0; i < NUMBER_REPETITIONS; i++)
        for (j = 0; j < NUMBER_REPETITIONS; j++)
            res = proc(j);
    baseEndTime = System.currentTimeMillis();
    
    final long baseDuration = baseEndTime - baseStartTime;

    // now test desired method
    final long startTime = System.currentTimeMillis();
    switch(ex) {
        case 1: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop1.run(n); 
            break;
        case 2: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop2.run(n); 
            break;
        case 3: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop3.run(n); 
            break;
        case 4: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop4.run(n); 
            break;
        case 5: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop5.run(n); 
            break;
        case 6: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop6.run(n); 
            break;
        case 7: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop7.run(n); 
            break;
        case 8: 
            for (i = 0; i < NUMBER_REPETITIONS; i++)
                for (j = 0; j < NUMBER_REPETITIONS; j++)
                    res = Loop8.run(n); 
            break;
        default: printUsage();
        }
    final long endTime = System.currentTimeMillis();
    final long duration = endTime - startTime;

    System.out.printf("result returned:  %6d\n", res);

    long methodTime = duration - baseDuration;
   if (methodTime < 0)
       methodTime = 0;

   System.out.printf("looping time:     %6d\n", baseDuration);
   System.out.printf("method execution: %6d\n", duration);
   System.out.printf("execution time:   %6d\n", methodTime);
  }
}

