Computer Sciences Department logo

CS 368-1 (2010 Summer) — Day 10 Homework

Due Thursday, July 29th, at the start of class.


Write a couple simple subroutines and some unit tests for them.


There are two parts to this problem. First, you will write a couple of simple but non-trivial subroutines. Then, you will write unit tests to demonstrate that (a) you understand how to write tests, and (b) that your subroutines are good.

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…

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

OK, now let’s test this 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 3–5 tests per subroutine. Check for normal functioning as well as edge and error conditions.

Here is the template to start with:


use strict;
use warnings;

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

# write your main script & 2 subroutines here

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

    # write your test cases here

    exit 0;


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 script on a single sheet of paper. 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.