CSC300 / CSC402: Automatic Testing [7/20] Previous pageContentsNext page

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package algs11;
import stdlib.*;
public class PlaygroundMax {
  public static int max (int x, int y, int z) {
    int m = x;
    if (m < y) { m = z; }
    if (m < z) { m = z; }
    return m;
  }
  public static void testMax (int expected, int x, int y, int z) {
    int actual = max (x, y, z);
    if (expected != actual) {
      StdOut.format ("max failed: Expecting [%d] Actual [%d] with arguments (%d,%d,%d)\n", expected, actual, x, y, z);
    }
  }
  public static void main (String[] args) { 
    testMax(31, 11, 21, 31);
    testMax(31, 11, 31, 21);
    testMax(31, 31, 11, 21);
    StdOut.println ("Finished tests");
  }
}

Passing tests are silent.

Failing tests print a meaningful message.

There are many frameworks for writing tests.

Previous pageContentsNext page