/** * ============================================================ * CS 231 — Data Structures & Algorithms * Lab 1-B Test Harness * * This file tests Lab1B.java. * * HOW TO USE: * 1. Place this file in the same directory as Lab1B.java * 2. Compile both files * 3. Run this file * * IMPORTANT: * - Do NOT modify this file * - This file prints results for verification * * ============================================================ */ import java.util.Arrays; public class LabA_v2_Tester { /* ======================================================== * Utility Methods * ======================================================== */ private static void printResult(String testName, boolean passed) { if (passed) { System.out.println("[PASS] " + testName); } else { System.out.println("[FAIL] " + testName); } } private static boolean arraysEqual(int[] a, int[] b) { return Arrays.equals(a, b); } /* ======================================================== * Task 1: Multiple of Three * ======================================================== */ private static void testIsMultipleOfThree() { printResult("isMultipleOfThree(9)", LabA_v2.isMultipleOfThree(9)); printResult("isMultipleOfThree(10)", !LabA_v2.isMultipleOfThree(10)); } /* ======================================================== * Task 2: Compare Two Numbers * ======================================================== */ private static void testCompare() { printResult("compare(5,3)", LabA_v2.compare(5, 3) == 1); printResult("compare(4,4)", LabA_v2.compare(4, 4) == 0); printResult("compare(2,7)", LabA_v2.compare(2, 7) == -1); } /* ======================================================== * Task 3: Find Smallest Element * ======================================================== */ private static void testMinElement() { int[] values = {6, 3, 9, 1, 4}; printResult("minElement", LabA_v2.minElement(values) == 1); } /* ======================================================== * Task 4: Array Sum * ======================================================== */ private static void testArraySum() { int[] values = {1, 2, 3, 4}; printResult("arraySum", LabA_v2.arraySum(values) == 10); } /* ======================================================== * Task 5: Sorted Pair * ======================================================== */ private static void testSortedPair() { int[] expected = {3, 7}; int[] actual = LabA_v2.sortedPair(7, 3); printResult("sortedPair(7,3)", arraysEqual(actual, expected)); } /* ======================================================== * Task 6: Copy Array with Offset * ======================================================== */ private static void testCopyWithOffset() { int[] values = {1, 2, 3}; int[] expected = {6, 7, 8}; int[] actual = LabA_v2.copyWithOffset(values, 5); printResult("copyWithOffset", arraysEqual(actual, expected)); } /* ======================================================== * Task 7: Is Digit Character * ======================================================== */ private static void testIsDigitChar() { printResult("isDigitChar('7')", LabA_v2.isDigitChar('7')); printResult("isDigitChar('A')", !LabA_v2.isDigitChar('A')); } /* ======================================================== * Task 8: Adjacent Equals * ======================================================== */ private static void testHasAdjacentEquals() { printResult("hasAdjacentEquals({1,2,2,3})", LabA_v2.hasAdjacentEquals(new int[]{1, 2, 2, 3})); printResult("hasAdjacentEquals({1,2,3})", !LabA_v2.hasAdjacentEquals(new int[]{1, 2, 3})); } /* ======================================================== * Task 9: Appears Exactly Once * ======================================================== */ private static void testAppearsOnce() { int[] values = {1, 2, 3, 2}; printResult("appearsOnce(...,3)", LabA_v2.appearsOnce(values, 3)); printResult("appearsOnce(...,2)", !LabA_v2.appearsOnce(values, 2)); } /* ======================================================== * Task 10: Strictly Increasing Array * ======================================================== */ private static void testIsStrictlyIncreasing() { printResult("isStrictlyIncreasing({1,2,3})", LabA_v2.isStrictlyIncreasing(new int[]{1, 2, 3})); printResult("isStrictlyIncreasing({1,2,2})", !LabA_v2.isStrictlyIncreasing(new int[]{1, 2, 2})); } /* ======================================================== * Main * ======================================================== */ public static void main(String[] args) { System.out.println("=== CS231 Lab 1-B Test Harness ==="); testIsMultipleOfThree(); testCompare(); testMinElement(); testArraySum(); testSortedPair(); testCopyWithOffset(); testIsDigitChar(); testHasAdjacentEquals(); testAppearsOnce(); testIsStrictlyIncreasing(); System.out.println("=== End of Tests ==="); } }