Computer Sciences Department logo

CS 368 (Summer 2009) — Day 14 Homework

Due Thursday, August 6th, at the start of class.

Description

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

Details

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 like “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'

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:

#!/usr/bin/perl

use strict;
use warnings;

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

# write your main script & 2 subroutines here

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

  # write your test cases here

  exit 0;
}

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.

For what it’s worth, it has taken me far more lines of text to describe this problem than it took me lines of Perl (about 25, for the record) to code the solution. Of course, I know lots of Perl tricks, but still, the point is that this is fairly easy to code in Perl.

Hand In

A printout of your script on a single sheet of paper. At the top of the printout, please include “CS 368 Summer 2009”, your name, and “Homework 14, August 4, 2009”. Identifying your work is important, or you may not receive appropriate credit.