CSCI125Code
TestArrayUtilities.java
public class TestArrayUtilities {
public static void runAllTests() {
testSumArray();
testLargestElement();
testContainsAZero();
testFindIndexOf();
testPrintArray();
}
private static void testSumArray() {
int[] A = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
int expected = 275;
int result = ArrayUtilities.sumArray(A);
if (result != expected) {
System.out.println("sumArray failed: expected " + expected + ", got " + result);
}
}
private static void testLargestElement() {
int[] A = {42, 7, 102, 18, 55, 99, 3, 87, 64, 28};
int expected = 102;
int result = ArrayUtilities.largestElement(A);
if (result != expected) {
System.out.println("largestElement failed: expected " + expected + ", got " + result);
}
}
private static void testContainsAZero() {
int[] A1 = {11, 22, 33, 0, 44, 55, 66, 77, 88, 99};
int[] A2 = {9, 8, 7, 6, 5, 4, 3, 2, 1};
if (!ArrayUtilities.containsAZero(A1)) {
System.out.println("containsAZero failed: expected true for array with zero");
}
if (ArrayUtilities.containsAZero(A2)) {
System.out.println("containsAZero failed: expected false for array without zero");
}
}
private static void testFindIndexOf() {
ArrayUtilities util = new ArrayUtilities();
int[] A = {10, 80, 30, 100, 50, 60, 70, 20, 90, 40};
int val = 70;
int expected = 6;
int result = util.findIndexOf(A, val);
if (result != expected) {
System.out.println("findIndexOf failed for " + val + ": expected " + expected + ", got " + result);
}
val = 999;
expected = -1;
result = util.findIndexOf(A, val);
if (result != expected) {
System.out.println("findIndexOf failed for " + val + ": expected " + expected + ", got " + result);
}
}
private static void testPrintArray() {
int[] A = {3, 6, 9, 12, 15, 18, 21, 24, 27, 30};
System.out.println("Expected output:");
System.out.println("3, 6, 9, 12, 15, 18, 21, 24, 27, 30");
System.out.println("Actual output: ");
ArrayUtilities.printArray(A);
System.out.println("(Visually check that commas and spacing match.)");
}
}
|