COSC-052: Computer Science II

Homework 3 (optional)
Spring 2017

Due: F 2/17 @ 5 PM

This optional homework is designed to produce an autograder for p2 based on tests that you design. You will be able to use the autograder based on your tests prior to submitting to the autograder for the project.

First off, grab the file hw3.zip by copying it into your home directory on cs-class:

cs-class% cd
cs-class% cp ~maloofm/cosc052/hw3.zip ./
cs-class% unzip hw3.zip
cs-class% cd hw3
cs-class% ls
bike.h   delivery.h  main.cc  Makefile    urban.h
bikes.h  factory.h   main.h   maloofm.cc
In this directory, you will find the .h files for p2, a Makefile, and a main function in main.cc that executes the test in the case of a switch statement based on the test number passed in through the command-line arguments. You'll also notice that in the switch statement, instead of cases, there is an include directive that includes the cases in the file maloofm.cc.

To compile the cases against the class definitions, type make. You'll see that this compiles and produces main.o. We can not produce an executable until you have written all of the method implementations, but we can still use the compiler to check the syntax of your cases or tests against the project's .h files.

We put the test cases for this homework in a separate .cc file because it will be easier to combine the cases from all of you into a single file. Once we have a file containing all of your test cases, then we can incorporate them into a main function, which will end up on Autolab.

A test consists of a description, situations in which the routine being tested passes the test, and situations in which the routine fails the test. For the passing condition, the test should write PASS to cout. For failing conditions, the test should write FAIL to cout and set returnCode to 1.

Consider the following test case that evaluates whether Bike's explicit constructor throws invalid_argument when passed a negative value for gears:

// author(s): cosc052
case 2: {
  cout << "test " << test << ": Bike::Bike(string,...) and throw on negative gears: " << flush;
  try {
    Bike bike( "U01", "Trek", -2 );
    cout << "FAIL" << endl;
    returnCode = 1;
  } // try
  catch ( invalid_argument& e ) {
    cout << "PASS" << endl;
  } // catch
  catch ( ... ) {
    cout << "FAIL" << endl;
    returnCode = 1;
  } // catch
  break; }
Notice that the test is successful if the construction of a Bike with a negative value for gears throws invalid_argument, and so the catch handler for that exception prints PASS. On the other hand, if the construction succeeds with negative gears it is a failure. It is also a failure if the constructor throws any exception other than invalid_argument. In both of these cases, the code for the test prints FAIL and assigns 1 to returnCode.

As you write your own tests, it is important that you do not change certain aspects of the structure of the examples we have given you. It is important that the code of each case has its own scope, hence the need for the block encompassing the code for the case. It is important to flush cout after inserting the description of the test because this ensures that the description will appear on the console even if the test case causes the grading routine to crash. It is important for PASS and FAIL to be uppercase because a driver routine expects these exact strings and uses them to determine the number of passed tests.

To write your own tests, make copies of the tests we have provided and modify them to evaluate aspects of the project's classes and methods. Put these tests in a file with the same name as your NetID with the extension .cc or .cpp. If you use the latter file extension, you'll need to make some modifications to the files in the zip file. Use the Makefile to compile your test cases against the .h files. Make sure your test cases compile before submitting them. We will discard any tests that don't compile.

You can write your own test cases and submit them. You can also work with other students to develop test cases. Please submit only one copy of your or your group's test cases. You can list any other authors of the tests in the commands above the case statement.

When you and any collaborators are ready to submit your test cases, copy the single file containing them to a submission directory. If I were submitting my file of test cases, I would type:

cs-class% cp maloofm.cc ~maloofm/hw3
Note that you will be able to copy files to this directory, but you will not be able to read or view the files in it. Make sure you submit your test cases before the deadline.

Copyright © 2019 Mark Maloof. All Rights Reserved. This material may not be published, broadcast, rewritten, or redistributed.