/** * MIT License * * Copyright (c) 2021 Exercism * Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework; import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test; import java.util.Collections; import java.util.List; public class BinarySearchTest { private static final int MAX_POINTS = 100; private static final int TEST_CASES = 11; private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES; private static final int FAILURE_POINTS = 0; private final java.util.List testPoints = new java.util.ArrayList<>(); @Test(name = "finds a value in an array with one element") public void findsAValueInAnArrayWithOneElement() { runTest(() -> { List listOfUnitLength = Collections.singletonList(6); BinarySearch search = new BinarySearch(listOfUnitLength); assertEquals(0, search.indexOf(6)); }); } @Test(name = "finds a value in the middle of an array") public void findsAValueInTheMiddleOfAnArray() { runTest(() -> { List sortedList = List.of(1, 3, 4, 6, 8, 9, 11); BinarySearch search = new BinarySearch(sortedList); assertEquals(3, search.indexOf(6)); }); } @Test(name = "finds a value at the beginning of an array") public void findsAValueAtTheBeginningOfAnArray() { runTest(() -> { List sortedList = List.of(1, 3, 4, 6, 8, 9, 11); BinarySearch search = new BinarySearch(sortedList); assertEquals(0, search.indexOf(1)); }); } @Test(name = "finds a value at the end of an array") public void findsAValueAtTheEndOfAnArray() { runTest(() -> { List sortedList = List.of(1, 3, 4, 6, 8, 9, 11); BinarySearch search = new BinarySearch(sortedList); assertEquals(6, search.indexOf(11)); }); } @Test(name = "finds a value in an array of odd length") public void findsAValueInAnArrayOfOddLength() { runTest(() -> { List sortedListOfOddLength = List.of(1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634); BinarySearch search = new BinarySearch(sortedListOfOddLength); assertEquals(9, search.indexOf(144)); }); } @Test(name = "finds a value in an array of even length") public void findsAValueInAnArrayOfEvenLength() { runTest(() -> { List sortedListOfEvenLength = List.of(1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377); BinarySearch search = new BinarySearch(sortedListOfEvenLength); assertEquals(5, search.indexOf(21)); }); } @Test(name = "identifies that a value is not included in the array") public void identifiesThatAValueIsNotFoundInTheArray() { runTest(() -> { List sortedList = List.of(1, 3, 4, 6, 8, 9, 11); BinarySearch search = new BinarySearch(sortedList); assertThrows(ValueNotFoundException.class, () -> search.indexOf(7), "Value not in array"); }); } @Test(name = "a value smaller than the array's smallest value is not found") public void aValueSmallerThanTheArraysSmallestValueIsNotFound() { runTest(() -> { List sortedList = List.of(1, 3, 4, 6, 8, 9, 11); BinarySearch search = new BinarySearch(sortedList); assertThrows(ValueNotFoundException.class, () -> search.indexOf(0), "Value not in array"); }); } @Test(name = "a value larger than the array's largest value is not found") public void aValueLargerThanTheArraysLargestValueIsNotFound() { runTest(() -> { List sortedList = List.of(1, 3, 4, 6, 8, 9, 11); BinarySearch search = new BinarySearch(sortedList); assertThrows(ValueNotFoundException.class, () -> search.indexOf(13), "Value not in array"); }); } @Test(name = "nothing is found in an empty array") public void nothingIsFoundInAnEmptyArray() { runTest(() -> { List emptyList = Collections.emptyList(); BinarySearch search = new BinarySearch(emptyList); assertThrows(ValueNotFoundException.class, () -> search.indexOf(1), "Value not in array"); }); } @Test(name = "nothing is found when the left and right bounds cross") public void nothingIsFoundWhenTheLeftAndRightBoundCross() { runTest(() -> { List sortedList = List.of(1, 2); BinarySearch search = new BinarySearch(sortedList); assertThrows(ValueNotFoundException.class, () -> search.indexOf(0), "Value not in array"); }); } private void assertEquals(Object expected, Object actual) { if (expected == null ? actual == null : expected.equals(actual)) { return; } throw new AssertionError("Expected: " + expected + ", but got: " + actual); } private void assertThrows(Class expectedType, ThrowingRunnable runnable, String expectedMessage) { try { runnable.run(); throw new AssertionError("Expected exception of type: " + expectedType.getName()); } catch (Exception t) { if (expectedType.isInstance(t) && (expectedMessage == null || expectedMessage.equals(t.getMessage()))) { return; } throw new AssertionError("Expected exception type: " + expectedType.getName() + ", but got: " + t.getClass().getName() + (expectedMessage == null ? "" : ", message: " + expectedMessage + ", but was: " + t.getMessage())); } } private void runTest(Runnable test) { try { test.run(); testPoints.add(SUCCESS_POINTS); } catch (AssertionError | Exception e) { TracingFramework.printError(e.getMessage()); testPoints.add(FAILURE_POINTS); } } @FunctionalInterface private interface ThrowingRunnable { void run() throws ValueNotFoundException; } public int getResult() { int sum = 0; for (Integer p : testPoints) sum += p; if (sum > MAX_POINTS) return MAX_POINTS; if (sum < 0) return 0; return sum; } }