Computer Sciences Department logo

CS 368-1 (2011 Summer) — Day 11 Homework

Due Friday, July 29, at the start of class.

Description

Write unit tests for some code.

Details

There are two parts to this problem. First, you must have some code to test — either new code for this assignment or your own code from homework assignment #9. Once you have code, you will write unit tests to demonstrate that (a) you understand how to write tests, and (b) that your code is good.

Option 1: Write Two New Subroutines

If you would prefer to write some new code to test, here are two subroutines to write. If you choose this option, you must write and test both of the following subroutines.

Subroutine #1: Write a subroutine that takes a single numeric argument and computes the greatest Fibonacci number that is less than or equal to the given number.

Subroutine #2: Write a subroutine that takes a single string argument and forces the string to be in “title case”, as though you were formatting titles of books or article. That is, capitalize the first letter of each word, except for short words including (but not limited to) “a”, “the”, and “in”. I will accept any reasonable variation on the definition of title case.

The main script should take two command-line arguments, one number and one string (quoted in the shell), and print the results of the Fibonacci subroutine for the number and the results of the Title Case subroutine for the string. No one ever said that this was going to be a useful script…

% ./solution-14.pl 42 'the art of computer programming'
Greatest Fibonacci number up through 42 = 34
Title = 'The Art of Computer Programming'

Option 2: Reuse Homework #9

Remember homework #9, using regular expression on Perl code, which you just got back today? Well, most solutions did not really work. How about writing some unit tests against it and making it work better?

If you take this option, you will probably have to reorganize your code into subroutines that can be tested. For example, you may wish to have a subroutine called is_real_line_of_code that accepts a single argument, which is a line of Perl script, and returns true if it is a “real” line of code, and false otherwise. How would homework #9 be written to use this subroutine? How could you test this subroutine?

You may wish to simplify your homework #9, in order to focus your testing. The requirements are the same as the original homework #9: You must have 2 reports and 1 modification. But if you wrote more than that, feel free to cut out the extra parts.

Both Options: Writing Unit Tests

OK, now let’s test your script.

Using the “standalone” or self-testing script template provided in class (and copied below), write a reasonable set of tests for your subroutines. Maybe aim for about 10 tests total (more is fine, but not too many, OK?). Be sure to check for normal functioning, but especially focus on the error, tricky, and boundary cases; that is typically where bugs are found.

Here is the template for your whole script, to start with:

#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
GetOptions('test' => \&run_tests);

# Put the main part of your code here
# Subroutines, main code, everything

# Test harness starts here
sub run_tests {
    require Test::More;
    Test::More->import;
    plan(tests => nnn);   # BE SURE TO CHANGE THE PLAN!

    # Write all of your test cases here

    exit 0;
}

To run the script as normal (will not run unit tests):

perl homework-11.pl arguments

To run the unit tests instead:

perl homework-11.pl --test

Reminders

Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.

Hand In

A printout of your code on a single sheet of paper (if possible, which it probably will not be). Be sure to put your own name in the initial comment block of the code. Identifying your work is important, or you may not receive appropriate credit.