// Simple test program for the Color ADT. import picture.Color; public class TestColor { private static final int MIN = 0; private static final int MAX = 255; private static int totalErrors = 0; public static void main (String[] args) { totalErrors = 0; Color black = mkColor (MIN, MIN, MIN); Color red = mkColor (MAX, MIN, MIN); Color green = mkColor (MIN, MAX, MIN); Color blue = mkColor (MIN, MIN, MAX); Color cyan = mkColor (MIN, MAX, MAX); Color white = mkColor (MAX, MAX, MAX); testColor (1, black, MIN, MIN, MIN); testColor (2, red, MAX, MIN, MIN); testColor (3, green, MIN, MAX, MIN); testColor (4, blue, MIN, MIN, MAX); testColor (5, cyan, MIN, MAX, MAX); testColor (6, white, MAX, MAX, MAX); testIllegal (7, MIN-1, MIN, MIN); testIllegal (8, MIN, MIN-1, MIN); testIllegal (9, MIN, MIN, MIN-1); testIllegal (10, MAX-1, MAX, MAX); testIllegal (11, MAX, MAX-1, MAX); testIllegal (12, MAX, MAX, MAX-1); testIllegal (13, MAX, MIN-1000, MAX); testIllegal (14, MIN, MAX, MAX+1000); testEqual (15, true, black, black); testEqual (16, false, black, white); testEqual (17, true, green, green); testEqual (18, false, cyan, green); testEqual (19, true, white, white); testEqual (20, false, blue, white); if (totalErrors == 0) { System.out.println ("Passed all tests"); } } private static void success (int n) { } private static void failure (int n) { totalErrors = totalErrors + 1; System.out.println ("Failed test " + n); } // Returns Color.mkColor (r, g, b) if there is no exception; // returns null if there is an exception. private static Color mkColor (int r, int g, int b) { Color c = null; try { c = Color.mkColor (r, g, b); } catch (Throwable e) { } return c; } private static void testColor (int n, Color c, int r, int g, int b) { try { if (c != null && Color.red (c) == r && Color.green (c) == g && Color.blue (c) == b) ; // do nothing else { failure (n); } } catch (Throwable e) { failure (n); } } private static void testIllegal (int n, int r, int g, int b) { try { Color c = Color.mkColor (MIN-1, MIN, MIN); failure (n); } catch (IllegalArgumentException e) { success (n); } catch (Throwable e) { failure (n); } } private static void testEqual (int n, boolean b, Color c1, Color c2) { try { if (b == Color.isEqual (c1, c2)) success (n); else failure (n); } catch (Throwable e) { failure (n); } } }