A Word About Testing

While the game of snake does make use of the hash and queue, it does not serve as a exhaustive test of your data structure implementations. For example, it takes a long time of playing to create a resize event that is often the hardest to get correct for both the ArrayQueue and MyHashSet. Instead, write JUnit tests to check the correctness of your program.

We have provided you a starter test for the ArrayQueue class in ArrayQueueTest.java. It is our hope that you will take this test case and make it more complete, as well as making one for your hash set. It will be hard to get a good grade without good tests.

You need to be able to debug failures (Eclipse Debugging may be a useful resource) As the assignments get harder and harder, testing will only become more essential. We hope you will start with good habits now!

What Do I Test?

Probably the hardest part of testing is figuring out a strategy to make good tests. Your end goal is to make sure you cover every possible code path and check that it is correct. You can do this by creating three types of tests.

Sanity Tests
Sanity tests are tests that check basic operations. The goal is to make sure you got a good start on the code. A sanity test might add an item to a queue and then remove it, and check that the same item was returned.
Edge Case Tests
What happens if you construct a hashtable with a load factor of -1.1? Check to make sure your code acts correctly, for example, by thowing an exception. What happens when the ArrayQueue wraps around or resizes? Check cases like this as well with separate tests.
Stress Tests
Stress tests will attempt to run through all code paths by doing long, random sequences of operations. For example, our stress test for hashset does the following operations
  • Generates a bool. If the bool is true, it adds an item to the hash set, and then make sure the hash set returns true for contains on that object. Otherwise, it removes a random item.
  • It checks equality of your set and a java.util.HashSet that has had the same operations done on it.
  • It tests that your iterator gives the same number of items as the java.util.HashSet's. Also, for each item, it makes sure contains returns true.
This gives you a good idea of the effort required for a stress test.
Quis Custodiet Ipsos Custodes?

An important question to ask is Who watches the watchmen?, or How do I know my test cases are correct?. In this assignment, there is an easy answer. For MyHashSet, HashSet is the gold standard for correctness. An implementation of MyHashSet that just called HashSet would pass all of the tests. Thus, a test for your test cases is if a HashSet with the same operations done on it acts in the same way. Likewise for ArrayQueue Java's LinkedList (it implements Queue) is a gold standard. These are especially useful for the stress tests.

Random Tips

Here are some assorted thoughts on what might need testing in ArrayQueue and MyHashSet