From be13981e94f8a18a194d8eb9a2f8b8483e1fbcda Mon Sep 17 00:00:00 2001 From: Isak Einberg <45336755+einbergisak@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:49:24 +0100 Subject: [PATCH] Add tests for 2D array binary search (#3892) --- .../searches/BinarySearch2dArrayTest.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java b/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java index ace42b8e0..0db17bdb2 100644 --- a/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java +++ b/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java @@ -1,9 +1,12 @@ package com.thealgorithms.searches; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.*; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterAll; + public class BinarySearch2dArrayTest { @@ -97,4 +100,67 @@ public class BinarySearch2dArrayTest { assertEquals(-1, ans[0]); assertEquals(-1, ans[1]); } + + /** + * Test if the method works with input arrays consisting only of one row. + */ + @Test + public void BinarySearch2dArrayTestOneRow() { + int[][] arr = { { 1, 2, 3, 4 }}; + int target = 2; + + // Assert that the requirement, that the array only has one row, is fulfilled. + assertEquals(arr.length, 1); + int[] ans = BinarySearch2dArray.BinarySearch(arr, target); + System.out.println(Arrays.toString(ans)); + assertEquals(0, ans[0]); + assertEquals(1, ans[1]); + } + + /** + * Test if the method works with the target in the middle of the input. + */ + @Test + public void BinarySearch2dArrayTestTargetInMiddle() { + int[][] arr = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15} }; + int target = 8; + // Assert that the requirement, that the target is in the middle row and middle column, is fulfilled. + assertEquals(arr[arr.length/2][arr[0].length/2], target); + int[] ans = BinarySearch2dArray.BinarySearch(arr, target); + System.out.println(Arrays.toString(ans)); + assertEquals(1, ans[0]); + assertEquals(2, ans[1]); + } + + /** + * Test if the method works with the target in the middle column, + * in the row above the middle row. + */ + @Test + public void BinarySearch2dArrayTestTargetAboveMiddleRowInMiddleColumn() { + int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; + int target = 3; + + // Assert that the requirement, that he target is in the middle column, + // in an array with an even number of columns, and on the row "above" the middle row. + assertEquals(arr[0].length % 2, 0); + assertEquals(arr[arr.length/2-1][arr[0].length/2], target); + int[] ans = BinarySearch2dArray.BinarySearch(arr, target); + System.out.println(Arrays.toString(ans)); + assertEquals(0, ans[0]); + assertEquals(2, ans[1]); + } + + /** + * Test if the method works with an empty array. + */ + @Test + public void BinarySearch2dArrayTestEmptyArray() { + int[][] arr = {}; + int target = 5; + + // Assert that an empty array is not valid input for the method. + assertThrows(ArrayIndexOutOfBoundsException.class, () -> BinarySearch2dArray.BinarySearch(arr, target)); + } + }