Testing

As you may have noticied, there are innumerable things that can go wrong when writing code. We can break the things that need to be tested into different catagories in order to make testing an easier process.

Testing For Functional Inputs

In many cases, your functions will not work with certain inputs. The primary example is how will your function behave if it is passed a null pointer? Throwing a null pointer exception is fine, but sometimes you can get other, more undefined behavior. There are many ways this can go wrong though, so always check to make sure your preconditions are met.

public static void main(String[] args) { 
  //Test open cash
  openCashRegister(null);
}

public static void openCashRegister(CashRegister cash) {
  if(cash == null) {
      return;
  }
...
}

Checking Class Invarients

You should always try and answer the question "can I feed this method something that will cause the class invarients to be violated?" If I pass in a negative number of sides for shape, will it accept it? If I give my cash register a negative amount of money, what will happen?

public static void main(String[] args) { 
  //Test shape
  Shape s = new Shape(-1);
}

public Shape(int numSides) {
   if(numSides < 1) {
      numSides = 1;
   }
...
}

Testing General Cases

This may be obvious, but you should always test your program with several different full fledged edge cases. These should be very similar to what your program will encounter when actually running. If you don't know what those will look like, try generating a few using the other parts of your code. If that's not possible, give it your best case, but try and make them as difficult or as complicated as possible.
public static void main(String[] args) {
   //Testing quicksort
   
   double[] testOne = { .4, 5, 10, 5.3, 8 15, 22 };
   double[] testTwo = { 4, 15, 8, 16, 23, 32};
   
   quickSort(testOne);
   quickSort(testTwo);

}

Testing Edge Cases

It is also important to test extremely atypical cases. What happens when things are towards the end of your array, or when you have very large numbers? Will your program continue to function with empty inputs?

public static void main(String[] args) {
   //Testing quicksort
   
   double[] testOne = { 1, 2, 3, 4, 5 };
   double[] testTwo = { 5, 4, 3, 2, 1 };
   double[] testThree = { 0, 0, 0, 0, 0 };
   
   quickSort(testOne);
   quickSort(testTwo);
   quickSort(testThree);

}