Lab 02: JUnit Tests and Debugging

Prerequisites to running the code.

Instructions: https://fa22.datastructur.es/materials/lab/lab02/

Solution: https://github.com/tomthestrom/cs61b/tree/master/lab02

This lab was fairly straightforward - as the title says - it was aimed at practicing the GUI of the debugger.

The files provided had some bugs and the tasks involved finding these bugs by running pre-written unit tests while using the debugger and in Part C, writing our own unit tests to accomplish the same.

Part A: IntList Iteration

In this part, we will be debugging the addConstant method in IntListExercises.java. This method is intended to take in an IntList and mutatively add a constant to each element of the list.

/* Expected Behavior */

IntList lst = IntList.of(1, 2, 3);

addConstant(lst, 1);
System.out.println(lst.toString());
// Output: 2 -> 3 -> 4

addConstant(lst, 4);
System.out.println(lst.toString());
// Output: 6 -> 7 -> 8

Original code - addConstant:

Solution - Fixed addConstant:

Part B: Nested Helper Methods and Refactoring for Debugging

Instructions: https://fa22.datastructur.es/materials/lab/lab02/#part-b-nested-helper-methods-and-refactoring-for-debugging

Problem - setToZeroIfMaxFEL method is buggy, fix it.

The bug was actually in the firstDigitEqualsLastDigit method

Original code - setToZeroIfMaxFEL, firstDigitEqualsLastDigit

Solution - fixed firstDigitEqualsLastDigit

Part C: Tricky IntLists

Instructions: https://fa22.datastructur.es/materials/lab/lab02/#part-c-tricky-intlists

Problem: the squarePrimes method is buggy. It’s your job to find and fix the bug! In order to do this, we recommend that you:

  1. Create JUnit Test(s) that test the squarePrimes method over a variety of different inputs. Make sure to test that it makes updates to the passed-in IntList correctly and returns the correct boolean value.

  2. Once you’ve created a JUnit Test on which squarePrimes fails, you’ve made progress! Woohoo! Now use the Java Debugger to step through the problem and isolate the bug.

  3. Finally, write a fix to the bug. For this particular bug, the fix is not many lines of code. Finding the bug is much more difficult than fixing it!

Added JUnit testcases for the squarePrimes:

Original code - squarePrimes

Solution - fixed squarePrimes

Last updated