Tests and some refactoring for grading.

Refactors some grading code into helper methods, and creates test cases to test grading code including those helper methods.

@fephsun @csvoss