Lab 01: Introduction To C++ and Testing: Due: by The End of Your Lab Period
Lab 01: Introduction To C++ and Testing: Due: by The End of Your Lab Period
Lab 01: Introduction To C++ and Testing: Due: by The End of Your Lab Period
Goal
In your first laboratory assignment, you will use a simple pre-written Calculator class and driver
code in C++ to learn how to write and test a simple C++ program.
You will then implement test cases for the Calculator class by using the CxxTest framework.
Learning Objectives
Familiarity with Eclipse C++ projects
Exposure to basic C++ syntax
Exposure to simple CxxTest unit tests
class Calculator
{
public:
Calculator( int Initial = 0 );
void Clear();
private:
int mValue;
};
#endif // CALCULATOR_H
c) Delete the contents of Calculator.cpp, then copy and paste the following code
into the file:
#include "Calculator.h"
// --------------------------------------------------------------
Calculator::Calculator( int Initial )
{
setValue( Initial );
}
// --------------------------------------------------------------
int Calculator::getValue() const
{
return mValue;
}
// --------------------------------------------------------------
void Calculator::setValue( int Value )
{
mValue = Value;
}
// --------------------------------------------------------------
void Calculator::Add( int Value )
{
mValue += Value;
}
// --------------------------------------------------------------
void Calculator::Subtract( int Value )
{
mValue -= Value;
}
// --------------------------------------------------------------
void Calculator::Multiply( int Value )
{
mValue *= Value;
}
// --------------------------------------------------------------
void Calculator::DivideBy( int Value )
{
mValue /= Value;
}
// --------------------------------------------------------------
void Calculator::Clear()
{
mValue = 0;
}
Part II: Completing the C++ Program
3. Complete the C++ program
a) As you found in lecture, every C++ program must include a function main() in
which execution begins. Of course, most C++ programs consist of much more
than just main() and a single class. In this exercise, we will also require another
function that will drive the testing of our Calculator class.
Right-click on the Calculator project again, but this time select New / Source File.
Enter the file name "driver.cpp" and click "Finish". This will add a new empty
file to the project. Copy and paste the following code into that file and save it:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#include "Calculator.h"
int main() {
Calculator myCalculator(0);
ifstream Script("CalculatorScript.txt");
ofstream Tape("CalculatorTape.txt");
Script.close();
Tape.close();
return 0;
}
string Command;
int Operand;
unsigned int cmdNum = 0;
if ( Command != "clear" ) {
Log << setw(16) << Operand;
}
Log << endl;
if ( Command == "add" ) {
Calc.Add( Operand );
}
else if ( Command == "subtract" ) {
Calc.Subtract( Operand );
}
else if ( Command == "multiply" ) {
Calc.Multiply( Operand );
}
else if ( Command == "divide by" ) {
Calc.DivideBy( Operand );
}
else if ( Command == "clear" ) {
Calc.Clear();
}
else {
Log << "The command " << Command
<< " is not recognized." << endl;
}
Log << setfill('-') << setw(50) << '-' << setfill(' ')
<< endl;
}
b) At this point, if you have properly copied the code into the appropriate files and
saved them, the project should build with no errors. Click the "Build All" icon on
the toolbar (if Eclipse didn't perform an automatic build). Then go to the lower-
right corner of the Eclipse window and find the icons for "Problems" and
"Console". Click on both and observe the results. The Problems window should
show 0 errors and 0 warnings. The Console window will show you the output
generated by the g++ compiler and linker when the build was performed. For
example:
**** Build of configuration Debug for project Calculator ****
make all
Building file: ../Calculator.cpp
Invoking: Cygwin C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Calculator.d" -
MT"Calculator.d" -o"Calculator.o" "../Calculator.cpp"
Finished building: ../Calculator.cpp
b) Run your program (Run / Run). This should add a new file to the project, named
"CalculatorTape.txt". Open the file; the contents should be:
--------------------------------------------------
0: add 10
calculator display: 10
--------------------------------------------------
0: add 7
calculator display: 17
--------------------------------------------------
0: multiply 31
calculator display: 527
--------------------------------------------------
0: subtract 42
calculator display: 485
--------------------------------------------------
0: divide by 7
calculator display: 69
--------------------------------------------------
0: add 3
calculator display: 72
--------------------------------------------------
0: subtract 1
calculator display: 71
--------------------------------------------------
0: clear
calculator display: 0
--------------------------------------------------
The file "CalculatorScript.txt" was parsed by the given code in main() and
processScript(), and this file was produced.
5. Expand your testing and submit to the Curator
a) Modify the script file to execute different sequences of commands for the
Calculator and see whether the results appear to be correct. You should perform a
number of these tests, and validate the results by hand and/or using a calculator.
b) Once you're satisfied you don't see any errors in your own tests, submit your
Calculator class to the Curator for automated testing.
Create a zip file containing the files Calculator.h and Calculator.cpp
(and absolutely no other files). It doesn't matter what you call your file.
Go to the Curator Project website (www.cs.vt.edu/curator/) and click on
the link for the index of Curator servers, and then click on the submit link for CS
2605. The driver for this part should then log on to the Curator and submit the zip
file to the Curator under the heading Lab01PartIII.
View the grade report and check the results from the Curator's testing. If you
haven't achieved the maximum score of 500 (which means you didn't copy/paste
the given code correctly), examine the test output to determine where the errors in
your source code were, fix them, create a new zip file, and submit again.
It shouldn't take you long to achieve a perfect score. Each student will be allowed
to make up to 25 submissions to the Curator.
Both members of your pair must eventually submit the Calculator class files to the
Curator. You may alternate who submits until you get a perfect score, or not.
Part IV: Perform some Unit Testing with CxxTest
6. Create a CxxTest suite to hold your tests
a) Repeat the procedure in Part I to create a new Eclipse project, called Calculator2,
but this time select the option "Empty Project with CxxTest".
b) Using Windows Explorer, copy the files Calculator.h and
Calculator.cpp from the earlier project into this one. Do not copy any other
files!
c) You will now create a CxxTest suite that will hold your unit tests to verify that the
Calculator class works as intended. As was the case with JUnit in Java, we
provide a wizard in Eclipse to assist in the creation of CxxTest suites. Since we
want to test the class declared in Calculator.h, right-click on that file in the
project, then choose New / CxxTest Suite from the menu. You will see that the
"Header under test" field has been set to point to the selected file. In the "Name"
field, enter "CalculatorTest" (without the quotes), then click "Next."
d) You may now choose the methods in the Calculator class, if any, for which you
wish to have empty stub functions generated. For now, let us test the four
arithmetic operations — so, place checkmarks next to the Add, Subtract,
Multiply, and divideBy methods in the list, and click "Finish."
Notice that the generated file has placeholders for the method code in the header
file itself, rather than in an implementation file; indeed, no implementation file
was generated for the test suite. As was mentioned previously, it is possible for a
C++ class to have the method code implemented in the header file rather than the
implementation file (as in Java's single file approach), but this is usually reserved
for very simple functions or classes. In this course, we implement the test
methods directly in the test class in the header file for the sake of simplicity (for
test classes only).
As with JUnit, any method whose name begins with test will be executed as a
test case when you build your project. This allows you to provide private helper
functions that you can call from multiple test cases by giving it a name that is not
prefixed with test.
7. Implement and use your first test
a) The first test that we will implement will test the behavior of the add function. To
do this, we will create a Calculator object with a default value, call the add()
method to add a quantity to that value, and then use the getValue() method to
retrieve the final value.
In order to check that the value is equal to our expectation, we use the CxxTest
macro TS_ASSERT_EQUALS(x, y), where x and y are the values that we wish to
compare. If x and y are unequal, then the test fails and a message indicating this
will be shown in the CxxTest view in Eclipse. Similarly to JUnit, CxxTest has a
set of macros of the form TS_ASSERT_<Condition>() that check whether some
Condition is true.
To implement the test for the add() method, find the testAdd() method in
CalculatorTest.h. The body of the method will contain a comment:
// TODO: Implement testAdd() function.
Replace this comment with the following code fragment, which creates a new
Calculator object named calc with value 6, adds 2 to it, then verifies that the
result is 8:
Calculator Calc( 6 );
Calc.Add( 2 );
TS_ASSERT_EQUALS( Calc.getValue(), 8 );
b) When you save your changes to CalculatorTest.h, Eclipse will again
automatically build the project, and once finished it will run all the tests in the
project. If you made no errors, you will see that the progress bar in the CxxTest
view is green, indicating success.
c) Expand the CalculatorTest entry in the test hierarchy of the CxxTest view.
You will see green checkmarks next to each of the tests, indicating that they all
succeeded. Also note that the testSubtract, testMultiply, and
testDivideBy tests succeeded by virtue of the fact that they did not contain
any assertions that could fail.
8. Implement More Tests
a) Implement the remaining three test methods in a similar fashion to the one above.
b) Modify one or more of your existing tests so that they will intentionally fail. What
kind of visual feedback do you see in the source editor and the CxxTest view?
Enter your description of this as a comment preceding the corresponding test
code.
c) When you are done, create another zip archive containing the files
Calculator.h, Calculator.cpp and CalculatorTest.h and upload
that to the Curator under the heading Lab01PartIV. Again, both members of your
pair must make this submission. Note that this will not be automatically graded
by the Curator, but it will be reviewed by the TA.