Lab 03: Timing Tests and Randomized Comparison Tests
Prerequisites to running the code.
Instructions: https://fa22.datastructur.es/materials/lab/lab03/
Solution: https://github.com/tomthestrom/cs61b/tree/master/lab03
The lab deals with writing timing and randomized tests, comparing their outputs and finding and fixing a tiny bug.
Timing Tests for List61B #
Timing the construction of an AList with a bad resize strategy
As discussed in lecture, a multiplicative resizing strategy will result in fast add operations / good performance, whereas an additive resizing strategy will result in slow add operations / bad performance. In this part of the lab, we’ll show how we can demonstrate this empirically.
In the timingtest package, we’ve provided the AList class created in lecture with the bad resizing strategy below:
public void addLast(Item x) {
if (size == items.length) {
resize(size + 1);
}
items[size] = x;
size = size + 1;
}Your goal for this part of the lab is to write code that tabulates the amount of time needed to create a AList of various sizes using the addLast method above. The output of this timing test will look something like this:
Solution:
class TimeAList
Output - of the timing test - AList with a bad resize strategy:
Timing the construction of an AList with a good resize strategy
Now modify the AList class so that the resize strategy is multiplicative instead of additive and rerun timeAListConstruction. Your AList objects should now be constructed nearly instantly, even for N = 128,000, and each add operation should only take a fraction of a microsecond.
Solution:
AList - addLast - modified resize to be multiplicative:
Output - of the timing test - AList with a good resize strategy:
Timing the getLast method of SLList #
Suppose we want to compute the time per operation for getLast for an SLList and want to know how this runtime depends on N. To do this, we need to follow the procedure below:
Create an
SLList.Add N items to the
SLList.Start the timer.
Perform M getLast operations on the
SLList.Check the timer. This gives the total time to complete all M operations.
Solution:
class TimeSLList
Output of the timing test - SLList getLast() - 1000 times for various sizes of SLList:
Obviously not a great output for retrieving the last item - this implementation of SLList goes over all the items to reach the last one.
Randomized Comparison Tests
Simple Comparison Test #
The goal of the rest of this lab is to find the bug in BuggyAList - a resizing AList. For our comparison, we're using a non-resizing, simple version of AList, called AListNoResizing that has an underlying array with a hardcoded size of 1000 .
Write a test called testThreeAddThreeRemove in TestBuggyAList that adds the same value to both the correct and buggy AList implementations, then checks that the results of three subsequent removeLast calls are the same.
Solution:
testThreeAddThreeRemove
The test passed - it's not strong enough to identify the bug in BuggyAList
Randomized Function Calls #
Next we were encouraged to write a randomized test and use the debugger to find the bug, I came up with the following test suite:
Solution:
Fixing the bug
Actually the bug lied an incorrect argument to the resize method
Original:
Solution:
Reasoning:
size != items.length, items is the underlying array, whose length only changes on resize, while size is an attribute that represents the number of elements in the array.
Last updated