This lab must be completed individually
The goal of this lab is to demonstrate how we can abstract “things” in the world (such as geometric objects) into program contructs. We also hope that you will get a lot more practice with using pointers, passing pointers to functions and using pointers along with structs. Y
You will continue to use the test-driven development (TDD) process to develop high quality and well-tested code in a systematic manner. When you run into problems with your code, remember to use the skills we have been learning such as code tracing and drawing pointer diagrams, to understand the dynamics of your program and how it is interacting with memory.
Go to github and find a repo for lab03 assigned to your GitHub id.
Log on to your CSIL account.
Change into your ~/cs16
directory
Clone your empty lab03 repo into your ~/cs16
directory.
In your empty repo, do git checkout -b main
to establish that you are on the main
branch as your default branch.
The starter code is in this repo:
The URL for cloning this repo is this: git@github.com:ucsb-cs16-w23/STARTER-lab03.git
Previous labs contain instruction for the process of:
starter
remote for this repostarter
remote into your own repo.Please do those steps now, and then do a git push origin main
to populate your own repo with the starter code.
If you need help with these steps:
Once you’ve populated your repo, typing the ls
command should show you the following files in your current directory
$ ls
Makefile pointsApproxEqualTest.cpp
README.md shapeFuncs.cpp
areaOfBoxTest.cpp shapeFuncs.h
boxesApproxEqualTest.cpp shapes.h
distanceBetweenTest.cpp tddFuncs.cpp
initBoxTest.cpp tddFuncs.h
initPointTest.cpp utility.cpp
pointToStringTest.cpp utility.h
$
Here is a brief description of each of the files and expected implementation.
Note that some of the files are organized in pairs of matching .h
/.cpp
files:
shapeFuncs.h
/shapeFuncs.cpp
tddFuncs.h
/tddFuncs.cpp
utililty.h
/utility.cpp
.h
file has function declarations (also known as function prototypes) and the .cpp
file has the function definitions..h
files and .cpp
files that are in pairs like this..h
or a .cpp
file.The shapes.h
file is different; it isn’t paired with any .cpp
file. That’s because it contains struct
declarations used in the other files.
Finally, there are also a variety of .cpp
files that have the suffix Test
in their names. Each of these contains a main
function, and code that runs our
tests. These are the same tests that run on Gradescope.
Here’s an overview of all of the files and their purpose:
Here are some further notes about the files that end in Test.cpp
:
distanceBetweenTest.cpp
contains test code to test your implementation of the distanceBetween()
function in shapeFuncs.cpp
.*Test.cpp
file tests contains a main function, which means that each test file along with its dependent code is meant to be compiled into a separate executable.Makefile
makes sure that this is in fact the case.The rationale behind this approach is that each function in shapeFuncs.cpp
can be developed and tested independently as much as possible.
Here is a list of your tasks for this lab. You will see that we are working on one function at a time, seeing the tests fail, then editing code to see the tests pass.
distanceBetween
workingmake
and see the given code being compiled../distanceBetweenTest
and see it fail.distanceBetween
function in shapeFuncs.cpp
to replace with correct code../distanceBetweenTest
and see it pass.git status
to see which files have been changed and need to be added.git add filename
for each filename
that you have changedgit add shapeFuncs.cpp
git commit -m "distanceBetween function is now passing tests"
(the exact comment is up to you; this is a suggestion)git push origin main
pointsApproxEqual
functionThere is no code to commit for this step; this is just a step for you to read some code and understand it.
initPoint
working./initPointTest
and see it fail.initPointTest.cpp
, figure out what the initPoint
function is supposed to do.initPoint
function in shapeFuncs.cpp
to replace the stub with correct code../initPointTest
and see it pass.git status
to see which files have been changed and need to be added.git add filename
(replacing filename
as needed)git commit -m "distanceBetween function is now passing tests"
(the exact comment is up to you; this is a suggestion)git push origin main
Now, as preparation for homework and exam questions, reason about why your code works.
initPoint
function returns when it is called for the very first time by the test code.x
and y
of the struct object p1
in initPointTest.cpp
as well as the relationship between p1
and the formal parameter p
of the function initPoint
.xVal
and yVal
in memory and indicate whether or not they are colocated in memory with any other variables (such as x
and y
).boxesApproxEqual
working./boxesApproxEqualTest
and see it fail.boxesApproxEqual
function in shapeFuncs.cpp
to replace the stub with correct code. As you do, consider adding an approxEqual function that takes two double values into utility.h
and utility.cpp
, as this will make your coding job easier, and keep you code “DRYer”.pointsApproxEqual
function in your boxesApproxEqual
solution. Remember that the &&
operator is the symbol for “logical and” in C++../boxesApproxEqualTest
and see it pass.initBox
working./initBoxTest
and see it failinitBox
function in shapeFuncs.cpp
to replace with correct code. As you do, remember that you use ->
to access members of a struct through a pointer, but simply .
to access members of a struct directly. You may need both in your answer../initBoxTest
and see it passareaOfBox
workingboxToString
tests set up./pointToStringTest
and see it pass; we’ll use that as a basis to develop boxToString
.pointToStringTest.cpp
to boxToStringTest.cpp
(the Unix command is cp
) and write code for tests for the boxToString
function.
shapeFuncs.cpp
at the boxToString
function stub for an example of the format you need for boxToString’s return values.pointToString
has.Makefile
so that boxToString
runs. Just follow the model—adding code in the Makefile
for boxToStringTest
everywhere you see code for pointToStringTest
make
boxToString
working./boxToStringTest
and see the tests failboxToString
in shapeFuncs.cpp
./boxToStringTest
passEven if you now got 100 on Gradescope, please now check your work before submitting.
When you are finished, you should be able to type make clean
and then make tests
and see the following output:
-bash-4.2$ make clean
/bin/rm -f distanceBetweenTest initPointTest pointsApproxEqualTest boxesApproxEqualTest initBoxTest areaOfBoxTest pointToStringTest *.o
-bash-4.2$ make tests
g++ -Wall -Wno-uninitialized -c -o distanceBetweenTest.o distanceBetweenTest.cpp
g++ -Wall -Wno-uninitialized -c -o tddFuncs.o tddFuncs.cpp
g++ -Wall -Wno-uninitialized -c -o utility.o utility.cpp
g++ -Wall -Wno-uninitialized -c -o shapeFuncs.o shapeFuncs.cpp
g++ -Wall -Wno-uninitialized distanceBetweenTest.o tddFuncs.o utility.o shapeFuncs.o -o distanceBetweenTest
g++ -Wall -Wno-uninitialized -c -o initPointTest.o initPointTest.cpp
g++ -Wall -Wno-uninitialized initPointTest.o tddFuncs.o utility.o shapeFuncs.o -o initPointTest
g++ -Wall -Wno-uninitialized -c -o pointsApproxEqualTest.o pointsApproxEqualTest.cpp
g++ -Wall -Wno-uninitialized pointsApproxEqualTest.o tddFuncs.o utility.o shapeFuncs.o -o pointsApproxEqualTest
g++ -Wall -Wno-uninitialized -c -o boxesApproxEqualTest.o boxesApproxEqualTest.cpp
g++ -Wall -Wno-uninitialized boxesApproxEqualTest.o tddFuncs.o utility.o shapeFuncs.o -o boxesApproxEqualTest
g++ -Wall -Wno-uninitialized -c -o initBoxTest.o initBoxTest.cpp
g++ -Wall -Wno-uninitialized initBoxTest.o tddFuncs.o utility.o shapeFuncs.o -o initBoxTest
g++ -Wall -Wno-uninitialized -c -o areaOfBoxTest.o areaOfBoxTest.cpp
g++ -Wall -Wno-uninitialized areaOfBoxTest.o tddFuncs.o utility.o shapeFuncs.o -o areaOfBoxTest
g++ -Wall -Wno-uninitialized -c -o pointToStringTest.o pointToStringTest.cpp
g++ -Wall -Wno-uninitialized pointToStringTest.o tddFuncs.o utility.o shapeFuncs.o -o pointToStringTest
./distanceBetweenTest
PASSED: distanceBetween(p1,p2)
PASSED: distanceBetween(p2,p1)
PASSED: distanceBetween(p3,p4)
PASSED: distanceBetween(p4,p5)
PASSED: distanceBetween(p5,p3)
./initPointTest
PASSED: pointsApproxEqual(p1,p1Expected)
PASSED: pointsApproxEqual(p2,p2Expected)
PASSED: pointsApproxEqual(p3,p3Expected)
PASSED: pointsApproxEqual(p4,p4Expected)
./pointsApproxEqualTest
PASSED: pointsApproxEqual(p1,p1)
PASSED: pointsApproxEqual(p1,p2)
PASSED: assertFalse(pointsApproxEqual(p2,p1)
./boxesApproxEqualTest
PASSED: boxesApproxEqual(b0,b0)
PASSED: boxesApproxEqual(b1,b0)
PASSED: boxesApproxEqual(b0,b1)
PASSED: boxesApproxEqual(b0,b2)
PASSED: boxesApproxEqual(b0,b3)
PASSED: boxesApproxEqual(b0,b4)
PASSED: boxesApproxEqual(b5,b6)
PASSED: boxesApproxEqual(b6,b5)
./initBoxTest
PASSED: boxesApproxEqual(b1,b1Expected)
PASSED: boxesApproxEqual(b2,b2Expected)
PASSED: boxesApproxEqual(b1,b2)
./areaOfBoxTest
PASSED: areaOfBox(r)
PASSED: areaOfBox(s)
PASSED: areaOfBox(t)
PASSED: areaOfBox(u)
./pointToStringTest
PASSED: pointToString(p1)
PASSED: pointToString(p2)
PASSED: pointToString(p2,1)
PASSED: pointToString(p2,4)
PASSED: pointToString(p2,5)
-bash-4.2$
Plus, some output at the end with the output of your boxToStringTest
./boxToStringTest
PASSED: boxToString(b1,1)
PASSED: boxToString(b1,2)
PASSED: boxToString(b1,3)
PASSED: boxToString(b1,4)
PASSED: boxToString(b1,5)
PASSED: boxToString(b1,6)
-bash-4.2$
At that point, if you haven’t already, submit on Gradescope
Please check that you have followed these style guidelines:
Your submission should be on-time. If you miss the deadline, you are subject to getting a zero. The instructor may or may not extend the deadline for late submissions at their discretion.