Computer Sciences Department logo

CS 368-3 (2012 Summer) — Day 7 Homework

Due Thursday, July 12, at the start of class.

Goal

Write unit tests for some code.

Tasks

There are two parts to this problem. First, you must have some code to test — so you will write a little bit of new code for this purpose. 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.

Part I: Write Two New Subroutines

Here are two subroutines to write. You must write and test both of the following subroutines.

Subroutine #1: fibonacci()

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. For example, if the argument is 42, the answer is 34; if the argument is 34, the answer is still 34.

Subroutine #2: title_case()

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 definition of “short words”, but there must be a distinction between capitalized and lowercase words.

Do not worry about hard cases (unless you really want to):

It is probably easiest to process one word at a time. To help with this approach, look at the split() and join() functions in the standard library.

Main Script

Your main script must have values to pass to the subroutines, one number and one string, and then it should 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…

The two values can come from:

If the input values are, for example, 42 and “the art of computer programming”, then the script might produce output something like the following:

Greatest Fibonacci number up through 42 = 34
Title = 'The Art of Computer Programming'

Part II: 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-7.pl

To run the unit tests instead:

perl homework-7.pl --test

Reminders

Do the work yourself, consulting reasonable reference materials as needed. Any resource that provides a complete solution or offers significant material assistance toward a solution not OK to use. Asking the instructor for help is OK, asking other students for help is not. All standard UW policies concerning student conduct (esp. UWS 14) and information technology apply to this course and assignment.

Hand In

A printout of your code, ideally on a single sheet of paper. Be sure to put your own name in the initial comment block. Identifying your work is important, or you may not receive appropriate credit.