This assignment gives you practice at writing maintainable, testable code and developing a carefully written, automated test suite. It assumes you have set up your computer to run Docker as discussed in assignment 1. For this assignment, you will write your own code and construct a Google test suite for that code. You will use this to set up a "pipeline" in the next assignment.
To enable Google Test in your Ubuntu image, you need to execute the
commands that are in the Dockerfile
from assignment 1: Start
your Ubuntu image, open the list of applications, and run Software
Updater. This will look for updates and attempt to install them. You
can do the same thing from the command prompt by opening a Terminal
window and typing
sudo apt install -y g++ cmake git libgtest-dev cd /usr/src/gtest sudo cmake CMakeLists.txt sudo make sudo cp lib/*.a /usr/lib(Note: if the last step fails, try
sudo cp *.a /usr/lib
. If
that fails, ask for help.)
Once you have Google Test set up, cd
to the directory
containing your cube
code (cd ~/cube
)
and type
make ./cube_testto confirm the tests build and pass.
Using the cube
code as an example, write your own program that
classifies triangles by the angles entered by a user. All data is entered
as floats. The triangles are classified as right, equilateral, isosceles,
acute, obtuse, or not a triangle. The 'not a triangle' classification
applies whenever the angles do not add to 180. For this and all other
rules, use 10-4 as the tolerance; that is, all matches must be
within one part of ten thousand. For legal triangles, the 'right'
classification applies whenever one angle is within the tolerance of 90
degrees, and the equilateral classification applies if the angles are
(nearly the) same, and the isosceles classification applies if it is not a
right triangle and two angles are (nearly) the same. Acute and obtuse apply
only when none of the other categories apply and (for acute) all angles are
(significantly) less than 90 or (for obtuse) one angle is (significantly)
greater than 90. To understand why tolerances are necessary with
floating-point values,
see ../../samples/crazy-math.cpp
.
A naive implementation would be a single main
that prompts for
the input and writes the results. That is not very testable: it requires
you to do various back-flips to capture standard output from test code. A
far simpler mechanism is to write functions (and classes in some cases,
though probably not for this assignment) that compute the results and
returns them as a reference parameter of function result. You can then
write main
to call these functions and display the results. The
wins of this method:
Break your solution into multiple functions that support
testing. Your main
should be so simple that the only testing
needed is to ensure it handles a single common case. You can do that
testing at the command prompt (by hand) rather than attempting to automate
it through GoogleTest.
The goal of this assignment is to get some experience at writing your own
code and tests using the Google framework, not to make you an expert with
Google Test. However, there is one common mistake that should be addressed:
comparing to true
or false
is always a bad
idea. Use EXPECT_TRUE
and EXPECT_FALSE
instead
of EXPECT_EQ(true, xyz)
. For example:
EXPECT_TRUE(is_prime(23)); EXPECT_FALSE(is_prime(100));For more detail on Google Test, see this article. It is a quick read and explains the Google Test framework in more detail for those who are looking for more information.
cube_test.cpp
" (say) in a project that classifies triangles.
enum StopLightStatus { STOP, YIELD, GO };
.cpp
) file with an associated .h
file. A separate file for each function does not scale to real projects,
and it makes grading your solutions much harder. You will have
a separate file for tests.
make
can be used for multi-directory projects,
but it's not trivial to implement. Besides, this project is too small to
make that effort valuable.
1e-4
above - it might be very
useful to have this value available to tests so you can write tests like
EXPECT_TRUE(abs(90.0 - largest_angle) <= TOLERANCE);or
EXPECT_TRUE(abs(90.0 - largest_angle) > TOLERANCE);You can also use the tolerance to create triangles that are just barely not right triangles:
double a = 45.0, b = 90 + 2*TOLERANCE, c = 180.0 - b - a;This triangle would be at the boundary between obtuse and right triangles.
Create a Makefile
that builds your system (including the test
executable), then create a Dockerfile
that does the build and
test run. Note that your tests will likely not run your main
,
but are to perform thorough testing for all functionality other than
input and output.
Capture an evidence document showing that
Submit a .ZIP file containing your source code and appropriate build files
in the topmost directory. Do not include non-source files (like
screen captures, .git
folders, etc.) or build projects
(like .o
files or executables). In particular, your .ZIP
will not contain your report. In most cases, you will
submit a .ZIP
containing Dockerfile
, Makefile
, .h
files, and .cpp
files. A Readme.txt
is always
appropriate (capturing project goals, build steps, and testing),
but not required for this assignment.
Avoid some common errors: tabs in source files, forgetting to put your name in files, submitting files I do not need, and failing to satisfy the coding standard. Double-check the writeup when you are done to make sure you have completed everything; issues like forgetting to test within a tolerance value result in 10-point deductions. This assignment is about learning to write good, automated tests, so logic of classifying triangles is just a small part of the points for this assignment. If you have any difficulty with that logic, talk to your instructor for lots of hints!