teaching machines

CS 1: Lecture 36 – Unit Testing

December 8, 2017 by . Filed under cs1, cs145, cs148, fall 2017, lectures.

Dear students,

Today, we examine one of the less glamorous aspects of writing code: testing it. There are three big reasons to write systematic tests of your software:

  1. To make sure that your code does what you think it does.
  2. To expand your thinking into situations outside your normal parameter ranges and workflow.
  3. To ensure that changes made later in development do not break code written earlier. When you add feature Z, you probably aren’t thinking much about feature A, so it’s nice to have a bunch of tests around that determine if feature A still works properly.

There’s are psychological reasons too. It’s way better to discover your own errors than to have someone else tell you your code’s broken. See SpecCheckers.

To demonstrate what it means to test our code, we will use the popular JUnit testing framework for Java. We write a method for each feature under test. These methods have the following form:

@Test
public void testFeatureA() {
  Foo f = new Foo();
  int expectedValue = ...;
  int actualValue = f.featureA();
  Assert.assertEquals("Foo.featureA didn't do what I expected it to do when...",
                      expectedValue,
                      actualValue);
}

When we run this code as a JUnit test suite in a modern development environment, we get a nice report of which features are passing and which are failing.

So, let’s write some unit tests against your ArrayList implementations from last class!

If you are curious how the folks behind the OpenJDK project chose to implement ArrayList, see their open source solution.

Here’s your TODO to complete before we meet again:

See you next class!

Sincerely,

P.S. Here’s the code we wrote together…

StringList.java

package lecture1208;

public class StringList {
  int N;
  String[] a;

  public StringList() {
    N = 0;
    a = new String[2];
  }

  public void add(String element) {
    if (N >= a.length) {
      String[] b = new String[a.length * 2];
      for (int i = 0; i < a.length; i++) {
        b[i] = a[i];
      }
      a = b;
    }
    a[N] = element;
    N++;

  }

  public void remove(int index) {
    String[] dst = new String[a.length - 1];
    for (int i = 0; i < a.length; i++) {
      if (i < index) {
        dst[i] = a[i];
      }
      if (i > index) {
        dst[i - 1] = a[i];
      }
    }
    a = dst;
    N--;
  }

  public int size() {
    return N;
  }

  public boolean contains(String str) {
    for (int i = 0; i < N; ++i) {
      if (a[i].equals(str)) {
        return true;
      }
    }
    return false;
  }

  public String get(int index) {
    return a[index];
  }

  public boolean isEmpty() {
    return N == 0;
  }

  public int indexOf(String needle) {
//    int counter = 0;
//    while (!a[counter].equals(needle)) {
//      counter++;
//    }
//    return counter;
    
    for (int i = 0; i < N; ++i) {
      if (a[i].equals(needle)) {
        return i;
      }
    }
    return -1;
  }
}

Tests.java

package lecture1208;

import org.junit.Assert;
import org.junit.Test;

public class Tests {
  @Test
  public void testEmpty() {
    StringList list = new StringList();
    Assert.assertEquals("I tried calling size on an empty list, but :(", 0, list.size());
    Assert.assertEquals("I tried asking an empty list if it contained hope, but :(", false, list.contains("hope"));
    Assert.assertEquals("I tried asking an empty list if it was empty, but :(", true, list.isEmpty());
    Assert.assertEquals("I tried getting indexOf(hope) on an empty list, but :(", -1, list.indexOf("hope"));

  }
  
  @Test
  public void testOnesie() {
    StringList list = new StringList();
    list.add("hope");
    Assert.assertEquals("I tried calling size on a 1-list, but :(", 1, list.size());
    Assert.assertEquals("I tried asking a 1-list if it contained hope, but :(", true, list.contains("hope"));
    Assert.assertEquals("I tried asking a 1-list if it was empty, but :(", false, list.isEmpty());
    Assert.assertEquals("I tried getting element 0 from a 1-list, but :(", "hope", list.get(0));
    Assert.assertEquals("I tried getting indexOf(hope) on a 1-list, but :(", 0, list.indexOf("hope"));
  }
  
  @Test
  public void testRemoveFromOnesie() {
    StringList list = new StringList();
    list.add("hope");
    list.remove(0);
    Assert.assertEquals("I tried calling size on an empty list, but :(", 0, list.size());
    Assert.assertEquals("I tried asking an empty list if it contained hope, but :(", false, list.contains("hope"));
    Assert.assertEquals("I tried asking an empty list if it was empty, but :(", true, list.isEmpty());
  }
  
  @Test
  public void testLotsies() {
    StringList list = new StringList();
    list.add("hope");
    list.add("element");
    list.add("failure");
    list.add("success");
    Assert.assertEquals("I tried calling size on a 4-list, but :(", 4, list.size());
    Assert.assertEquals("I tried asking a 4-list if it contained hope, but :(", true, list.contains("hope"));
    Assert.assertEquals("I tried asking a 4-list if it contained hope, but :(", true, list.contains("element"));
    Assert.assertEquals("I tried asking a 4-list if it contained hope, but :(", true, list.contains("failure"));
    Assert.assertEquals("I tried asking a 4-list if it contained hope, but :(", true, list.contains("success"));
    Assert.assertEquals("I tried asking a 4-list if it was empty, but :(", false, list.isEmpty());
    Assert.assertEquals("I tried getting element 0 from a 1-list, but :(", "hope", list.get(0));
    Assert.assertEquals("I tried getting element 1 from a 1-list, but :(", "element", list.get(1));
    Assert.assertEquals("I tried getting element 2 from a 1-list, but :(", "failure", list.get(2));
    Assert.assertEquals("I tried getting element 3 from a 1-list, but :(", "success", list.get(3));
    Assert.assertEquals("I tried getting indexOf(hope) on a 1-list, but :(", 0, list.indexOf("hope"));
    Assert.assertEquals("I tried getting indexOf(element) on a 1-list, but :(", 1, list.indexOf("element"));
    Assert.assertEquals("I tried getting indexOf(failure) on a 1-list, but :(", 2, list.indexOf("failure"));
    Assert.assertEquals("I tried getting indexOf(success) on a 1-list, but :(", 3, list.indexOf("success"));

  }
}

GrowableArray.java

package lecture1208;

public class GrowableArray {
  private String[] currentStringArray;
  private int nelements;

  public GrowableArray() {
    currentStringArray = new String[10];
    nelements = 0;
  }

  public String get(int i) {
    return currentStringArray[i];
  }

  public boolean isEmpty() {
    return nelements == 0;
  }

  public void remove(int index) {
    String[] newArray = new String[currentStringArray.length - 1];
    for (int i = 0; i < index; i++) {
      newArray[i] = currentStringArray[i];
    }
    for (int i = index; i < newArray.length; i++) {
      newArray[i] = currentStringArray[i + 1];
    }
    currentStringArray = newArray;
    --nelements;
  }

  public boolean contains(String string) {
    boolean isTrue = false;
    for (int i = 0; i < nelements; i++) {
      if (currentStringArray[i].equals(string)) {
        isTrue = true;
      }
    }
    return isTrue;
  }

  public int indexOf(String s) {
    for (int i = 0; i < nelements; i++) {
      if (currentStringArray[i].equals(s)) {
        return i;
      }
    }
    return -1;
  }

  public int size() {
    return nelements;
  }

  public void add(String s) {
    if (nelements == currentStringArray.length) {
      String[] moar = new String[2 * nelements];
      System.arraycopy(currentStringArray, 0, moar, 0, nelements);
      currentStringArray = moar;
    }
    currentStringArray[nelements] = s;
    ++nelements;
  }
}

Tests.java

package lecture1208;

import org.junit.Assert;
import org.junit.Test;

public class Tests {
  @Test
  public void testEmpty() {
    GrowableArray list = new GrowableArray();
    Assert.assertEquals("I tried asking an empty list if it was empty, but...", true, list.isEmpty());
    Assert.assertEquals("I tried asking an empty list its size, but...", 0, list.size());
    Assert.assertEquals("I tried asking an empty list if it had hope, but...", false, list.contains("hope"));
  }
  
  @Test
  public void testOnesie() {
    GrowableArray list = new GrowableArray();
    list.add("hope");
    Assert.assertEquals("I tried asking a 1 list if it was empty, but...", false, list.isEmpty());
    Assert.assertEquals("I tried asking a 1 list its size, but...", 1, list.size());
    Assert.assertEquals("I tried asking a 1 list if it had hope, but...", true, list.contains("hope"));
    Assert.assertEquals("I tried asking a 1 list about indexOf hope, but...", 0, list.indexOf("hope"));
    list.remove(0);
    Assert.assertEquals("I tried asking an empty list if it was empty, but...", true, list.isEmpty());
    Assert.assertEquals("I tried asking an empty list its size, but...", 0, list.size());
    Assert.assertEquals("I tried asking an empty list if it had hope, but...", false, list.contains("hope"));
  }
  
  @Test
  public void testLotsies() {
    GrowableArray list = new GrowableArray();
    list.add("hope");
    list.add("peace");
    list.add("joy");
    list.add("love");
    Assert.assertEquals("I tried asking a 4 list if it was empty, but...", false, list.isEmpty());
    Assert.assertEquals("I tried asking a 4 list its size, but...", 4, list.size());
    Assert.assertEquals("I tried asking a 4 list if it had hope, but...", true, list.contains("hope"));
    Assert.assertEquals("I tried asking a 4 list if it had peace, but...", true, list.contains("peace"));
    Assert.assertEquals("I tried asking a 4 list if it had joy, but...", true, list.contains("joy"));
    Assert.assertEquals("I tried asking a 4 list if it had love, but...", true, list.contains("love"));
    Assert.assertEquals("I tried asking a 4 list if it had love , but...", false, list.contains("love "));

    Assert.assertEquals("I tried asking a 4 list about indexOf hope, but...", 0, list.indexOf("hope"));
    Assert.assertEquals("I tried asking a 4 list about indexOf peace, but...", 1, list.indexOf("peace"));
    Assert.assertEquals("I tried asking a 4 list about indexOf joy, but...", 2, list.indexOf("joy"));
    Assert.assertEquals("I tried asking a 4 list about indexOf love, but...", 3, list.indexOf("love"));
    Assert.assertEquals("I tried asking a 4 list about indexOf love , but...", -1, list.indexOf("love "));

//    list.remove(0);
//    Assert.assertEquals("I tried asking an empty list if it was empty, but...", true, list.isEmpty());
//    Assert.assertEquals("I tried asking an empty list its size, but...", 0, list.size());
//    Assert.assertEquals("I tried asking an empty list if it had hope, but...", false, list.contains("hope"));
  }
}