Skip to content

Commit

Permalink
assertBigArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
vigna committed Mar 7, 2023
1 parent 2853bab commit a4f529c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
methods of hash maps. Thanks to Nikita Sokolov for reporting that some
methods (e.g., fastForEach()) were not supporting setValue().

- New assertBigArray() methods help to check that arguments are
well-formed big arrays.

8.5.11

- Hash-based containers now expose ensureCapacity().
Expand Down
16 changes: 16 additions & 0 deletions drv/BigArraysFragment.drv
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@
#endif


/** Asserts that the provided big array is correct.
*
* @param array a big array.
* @throw IllegalStateException if the last segment is empty or longer than {@link BigArrays#SEGMENT_SIZE},
* or any other segment has length different from {@link BigArrays#SEGMENT_SIZE}.
*/
public static KEY_GENERIC void assertBigArray(final KEY_GENERIC_TYPE[][] array) {
final int l = array.length;
if (l == 0) return;
for(int i = 0; i < l - 1; i++)
if (array[i].length != SEGMENT_SIZE)
throw new IllegalStateException("All segments except for the last one must be of length 2^" + Integer.toString(SEGMENT_SHIFT));
if (array[l - 1].length > SEGMENT_SIZE) throw new IllegalStateException("The last segment must be of length at most 2^" + Integer.toString(SEGMENT_SHIFT));
if (array[l - 1].length == 0 && l == 1) throw new IllegalStateException("The last segment must be of nonzero length");
}

/** Returns the length of the given big array.
*
* @param array a big array.
Expand Down
30 changes: 29 additions & 1 deletion test/it/unimi/dsi/fastutil/ints/IntBigArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package it.unimi.dsi.fastutil.ints;

import static it.unimi.dsi.fastutil.BigArrays.SEGMENT_SIZE;
import static it.unimi.dsi.fastutil.BigArrays.copy;
import static it.unimi.dsi.fastutil.BigArrays.get;
import static it.unimi.dsi.fastutil.BigArrays.length;
Expand Down Expand Up @@ -525,4 +526,31 @@ public void testBinarySearchLargeKey() {
public void testLegacyMainMethodTests() throws Exception {
MainRunner.callMainIfExists(IntBigArrays.class, "test", /*num=*/"10000", /*seed=*/"293843");
}
}

@Test(expected = IllegalStateException.class)
public void testAssertBigArrayLastEmpty() {
BigArrays.assertBigArray(new int[][] { new int[] {} });
}

@Test(expected = IllegalStateException.class)
public void testAssertBigArrayLastLong() {
BigArrays.assertBigArray(new int[][] { new int[SEGMENT_SIZE + 1] });
}

@Test(expected = IllegalStateException.class)
public void testAssertBigArrayMiddleWrong() {
BigArrays.assertBigArray(new int[][] { new int[SEGMENT_SIZE - 1], new int[1] });
}

@Test
public void testAssertBigArrayOK() {
BigArrays.assertBigArray(IntBigArrays.newBigArray(0));
BigArrays.assertBigArray(IntBigArrays.newBigArray(SEGMENT_SIZE));
BigArrays.assertBigArray(IntBigArrays.newBigArray(SEGMENT_SIZE - 1));
BigArrays.assertBigArray(IntBigArrays.newBigArray(SEGMENT_SIZE + 1));
BigArrays.assertBigArray(IntBigArrays.newBigArray(2 * SEGMENT_SIZE));
BigArrays.assertBigArray(IntBigArrays.newBigArray(2 * SEGMENT_SIZE - 1));
BigArrays.assertBigArray(IntBigArrays.newBigArray(2 * SEGMENT_SIZE + 1));
}

}

0 comments on commit a4f529c

Please sign in to comment.