C Activity
Longest Line With Unit Tests

Overview

This C programming exercise will introduce you to character arrays and the use of functions. The program will accept a series of lines from stdin and keep track of the longest line that was entered (the most characters in one line).

The provided source code has four unit tests that must pass for you to receive full credit. However, passing these tests does not guarantee full credit.


Activity Part 1

Download the file longest_with_tests.c to a directory named LongestLine. Complete the readline() function and verify that you can correctly echo each line as it is entered. An EOF (CTRL-D) terminates the program. You should make sure that the code truncates the input so that no input line is over 80 characters long.

> gcc -g -o longest longest_with_tests.c
> ./longest
this is a long line <-- this is what you type (and hit 'enter')
this is a long line <-- this is what your code echoes
this is a longer line
this is a longer line
(CTRL-D)

  1. Create an ActivityJournal.txt and estimate the time you will need to complete the program.
  2. Keep track of the number of times you attempt to compile your program and generate a compiler error of any type. Note that number in the ActivityJournal along with the actual time to complete the program. 

Submission Part 1

Submit your source file longest_with_tests.c with Part 1 of your ActivityJournal.txt in a top level directory named LongestLine to your Git repo.

Activity Part 2

Copy longest_with_tests.c to longest_with_tests2.c (use the cp command).

In this new file keep track of the longest line entered. Only print the longest line entered after input is complete. You will need to implement the copy() function to save the longest line.

As soon as you think you have the copy function implemented compile your program as shown and run the unit tests. Check the results carefully. All four tests must pass. When all four pass then complete the rest of this part of the activity.

> gcc -g -o test longest_with_tests2.c
> ./test

If the tests all pass, you should see something like this:
Executing unit tests
Summary of unit tests:
4 tests passed
0 tests failed

Also test your code manually (i.e. without the unit tests)

> gcc -g -o longest longest_with_tests2.c
> ./longest
This is a long line.
This is a longer line.
This line has supercalifragilisticexpialidocious.
This is short.
(CTRL-D)
This line has supercalifragilisticexpialidocious.

  1. Update ActivityJournal.txt (keep your Part 1 estimates and results) and estimate the time you will need to complete the program.
  2. Keep track of the number of times you attempt to compile your program and generate a compiler error of any type. Note that number in the activity journal, along with the actual time to complete the program. 
  3. Make sure that your new longest_with_tests2.c and updated ActivityJournal.txt are in the LongestLine directory.

Submission Part 2

Submit your source file longest_with_tests2.c with a fully completed ActivityJournal.txt in a top level directory named LongestLine to your Git repo.

Grading Criteria

To receive full credit for this activity you do the following:

  1. Submit your work in a correctly named directory. This must be one of the top level directories in your repository.
  2. Use the exact filenames for all three files: longest_with_tests.c, longest_with_tests2.c and ActivityJournal.txt.
  3. The program must compile without any warnings.
  4. Complete Part 1 before starting Part 2.
  5. The program must compile exactly as shown in the activity.
    • You cannot use any other options.
    • Use the default version of C. Do not use the -std=c99 option.
  6. All unit tests must pass.
  7. Use good software style. Refer to the C Coding Standards document.
  8. Complete the Activity Journal for each Part including the time estimate, plan, actual time, and observations.