Assignment 4, due Feb 8

Solutions

Part of the homework for CS:2820, Spring 2019
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Every Java for loop can be rewritten in several forms. Consider the following loop:
    String s = "something";
    for ( int i = 0; i < s.length(); i++ ) {
        system.out.print( s.charAt( i ) );
    }
    

    a) Rewrite this as a while loop, still using an integer control variable. (0.5 points)

    String s = "something";
    {   // these braces aren't strictly required
        int i = 0;
        while (i < s.length()) {
            system.out.print( s.charAt( i ) );
            i ++;
        }
    }   // they keep i from being visible elsewhere in the code
    

    b) Rewrite this using a while loop, but using class StringCharacterIterator to deliver consecutive characters of the string. (0.5 points)

    String s = "something";
    {   // these braces aren't strictly required
        CharacterIterator it = StringCharacterIterator( s );
        char ch = it.first();
        while (ch != CharacterIterator.DONE) {
            system.out.print( ch );
            ch = it.next();
        }
    }   // they keep i and ch from being visible elsewhere in the code
    

    Note: Class StringCharacterIterator is a wretched class that ought to correspond in some way to Iterator <Character> but isn't even vaguely compatible.

  2. Background: Think about testing a program that builds the data structure representing a system of digital logic gates. At the very least, you want every statement in the program to be executed at least once by some test case. A test suite that does this is said to use path testing. Path testing is dumb, all you are trying to do is make the program follow every path through the code, so the only data values that matter are values that push determine the control structure.

    Assuming that your solution to MP2 will process an input file looking something like this:

    gate a xor 6.7
    gate b threshold 1 3.6
    wire a 3.2 b
    wire b 1.5 a 1.8 a
    

    Wires must contain at least one destination but may contain more, each preceeded by the delay from the source. Gates must have a delay, and threshold gates have a threshold given before the delay. Of course, the input file must be given on the command line, and it must be possible to open it.

    A problem: Given what you know about MP1 and what you can guess about your solution to MP2, what is the minimum number of distinct test runs required to do path testing on the program. Give a very brief justification for each run, showing the parameters you would provide when you launch the program and the test file, if any. (1.0 point)

    Note: A fully developed path test suite can only be developed after you write the code, but this problem asks you to start designing some appropriate tests before you have any code, in keeping with the extreme programming philosophy of developing tests before writing code. With no code, you can try to work on testing all the ways you could make an error in the input text.

    The purpose of this problem is to get you thinking about developing tests for your solution to MP2. It is an open-ended problem, which means that there is no strictly correct answer, but here are some good ideas:

    Here, I've used the echo shell command to create test files on the fly, with embedded \n indicating newlines in the test files. You can specify test files any way you want as long as it is clear.

    # tests for missing arguments, extra arguments and unreadable input files
    java MP2
    java MP2 "not a file name"
    java MP2 "not a file name" "extra stuff here"
    
    # tests for response to various nonsense input
    echo "treacle desolate echinoderm" > testfile
    java MP2 testfile
    echo "gate desolate echinoderm" > testfile
    java MP2 testfile
    
    # test for a simple correct circuit
    echo "gate a xor\ngate b threshold\nwire a 1.5 b" > testfile
    java MP2 testfile
    
    # test for wires with multiple destinations
    echo "gate a xor\ngate b threshold\nwire a 1.5 b 1.7 a" > testfile
    java MP2 testfile
    
    # test for missing and duplicate declarations
    echo "gate a xor\n gate a xor\n wire a 1.5 b" > testfile
    java MP2 testfile
    
    # tests for missing fields
    echo "gate\ngate a xor\nwire a 1.5 b" > testfile
    java MP2 testfile
    echo "gate a\ngate b xor\nwire a 1.5 b" > testfile
    java MP2 testfile
    echo "gate a xor\ngate b xor\nwire 1.5 b" > testfile
    java MP2 testfile
    echo "gate a xor\ngate b xor\nwire a b" > testfile
    java MP2 testfile
    echo "gate a xor\ngate b xor\nwire a 1.5" > testfile
    java MP2 testfile
    echo "gate a xor\ngate b xor\nwire a 1.5 b 1.7" > testfile
    java MP2 testfile
    echo "gate a xor\ngate b xor\nwire a 1.5 b a" > testfile
    

    The above list of tests is almost certainly incomplete, but it is a good start. This set of tests could be wrapped into a shell script and used to automate testing after each debugging run, but that's a more advanced topic than what the homework asked for.