Skip to content

Commit

Permalink
Replace assert with IllegalArgumentException for size checks
Browse files Browse the repository at this point in the history
Signed-off-by: Ketan Verma <ketan9495@gmail.com>
  • Loading branch information
ketanv3 committed Nov 14, 2023
1 parent e050b04 commit 370cdad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class BidirectionalLinearSearcher implements Roundable {
private final long[] descending;

BidirectionalLinearSearcher(long[] values, int size) {
assert size > 0 : "at least one value must be present";
if (size <= 0) {
throw new IllegalArgumentException("at least one value must be present");
}

int len = (size + 1) >>> 1; // rounded-up to handle odd number of values
ascending = new long[len];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class BinarySearcher implements Roundable {
private final int size;

BinarySearcher(long[] values, int size) {
assert size > 0 : "at least one value must be present";
if (size <= 0) {
throw new IllegalArgumentException("at least one value must be present");
}

this.values = values;
this.size = size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public void testFloor() {
}
}

public void testAssertions() {
AssertionError exception;

exception = assertThrows(AssertionError.class, () -> new BinarySearcher(new long[0], 0));
assertEquals("at least one value must be present", exception.getMessage());
exception = assertThrows(AssertionError.class, () -> new BidirectionalLinearSearcher(new long[0], 0));
assertEquals("at least one value must be present", exception.getMessage());

exception = assertThrows(AssertionError.class, () -> new BinarySearcher(new long[] { 100 }, 1).floor(50));
assertEquals("key must be greater than or equal to 100", exception.getMessage());
exception = assertThrows(AssertionError.class, () -> new BidirectionalLinearSearcher(new long[] { 100 }, 1).floor(50));
assertEquals("key must be greater than or equal to 100", exception.getMessage());
public void testFailureCases() {
Throwable throwable;

throwable = assertThrows(IllegalArgumentException.class, () -> new BinarySearcher(new long[0], 0));
assertEquals("at least one value must be present", throwable.getMessage());
throwable = assertThrows(IllegalArgumentException.class, () -> new BidirectionalLinearSearcher(new long[0], 0));
assertEquals("at least one value must be present", throwable.getMessage());

throwable = assertThrows(AssertionError.class, () -> new BinarySearcher(new long[] { 100 }, 1).floor(50));
assertEquals("key must be greater than or equal to 100", throwable.getMessage());
throwable = assertThrows(AssertionError.class, () -> new BidirectionalLinearSearcher(new long[] { 100 }, 1).floor(50));
assertEquals("key must be greater than or equal to 100", throwable.getMessage());
}
}

0 comments on commit 370cdad

Please sign in to comment.