diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99ab8eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.classpath +/.project +/.gradle +/.settings +/build \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..709f2db --- /dev/null +++ b/build.gradle @@ -0,0 +1,56 @@ +apply plugin: 'java' +apply plugin: 'eclipse' + +/* + * The shared configuration for all sub-projects: + */ +subprojects { + apply plugin: 'java' + apply plugin: 'eclipse' + apply plugin: 'maven' + apply plugin: 'maven-publish' + + // java compatibility version + sourceCompatibility = 1.8 + + configurations { + tests + } + + // the repositories for external depenencies + repositories { + mavenCentral() + jcenter() + } + + uploadArchives { + repositories { + mavenDeployer { + if (!project.hasProperty('mavenDeployRepo')){ + throw new IllegalStateException("Set the property 'mavenDeployRepo' to the repository URL. A good place is ~/.gradle/gradle.properties") + } + repository(url: "${mavenDeployRepo}") + println "publishing to: ${mavenDeployRepo}" + } + } + } + + + task sourcesJar(type: Jar, dependsOn: classes) { + classifier 'sources' + from sourceSets.main.allSource + } + + artifacts { + archives sourcesJar + } + + // dependencies that all sub-projects have + dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' + } +} + +task wrapper(type: Wrapper) { + gradleVersion = '3.2.1' +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..b761216 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..66dbdb0 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Sep 13 09:45:49 CEST 2014 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..aec9973 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/gradlew.sh b/gradlew.sh new file mode 100755 index 0000000..79238e3 --- /dev/null +++ b/gradlew.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +mkdir -p tmp +cd tmp + +if ! test -d "gradle-2.0"; then + wget https://services.gradle.org/distributions/gradle-2.0-bin.zip + unzip gradle-2.0-bin.zip +fi + + +#echo setting environment + +export GRADLE_HOME=`pwd`/gradle-2.0 +#echo GRADLE_HOME=$GRADLE_HOME + +export PATH=$PATH:$GRADLE_HOME/bin +#echo $PATH + + +cd .. +gradle --daemon $1 $2 $3 $4 $5 $6 $7 diff --git a/primitiveCollections/.gitignore b/primitiveCollections/.gitignore new file mode 100644 index 0000000..080ea93 --- /dev/null +++ b/primitiveCollections/.gitignore @@ -0,0 +1,5 @@ +/bin/ +/build/ +/.settings/ +/.classpath +/.project diff --git a/primitiveCollections/build.gradle b/primitiveCollections/build.gradle new file mode 100644 index 0000000..d446615 --- /dev/null +++ b/primitiveCollections/build.gradle @@ -0,0 +1,8 @@ +import java.text.SimpleDateFormat; + +group='org.lucares' +version = '0.1.'+new SimpleDateFormat("YYYYMMddHHmmss").format(new Date()); + +dependencies { +} + diff --git a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java new file mode 100644 index 0000000..3c5ad39 --- /dev/null +++ b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java @@ -0,0 +1,313 @@ +package org.lucares.collections; + +import java.util.List; + +/** + * A list for primitive ints. + *
+ * This class does not (yet) implements all methods a java.util {@link List} + * would have. + */ +public class IntList { + + private static final int DEFAULT_CAPACITY = 10; + + private int[] data; + + private int index = 0; + + /** + * Create a new {@link IntList} with initial capacity 10. + */ + public IntList() { + this(DEFAULT_CAPACITY); + } + + /** + * Create a new {@link IntList}. + * + * @param initialCapacity + * initial capacity + * @throws IllegalArgumentException + * if initial capacity is negative + */ + public IntList(final int initialCapacity) { + if (initialCapacity < 0) { + throw new IllegalArgumentException("initial capacity must not be negative"); + } + + data = new int[initialCapacity]; + } + + /** + * Create a new {@link IntList} with a copy of the elements of + * {@code intList}. + * + * @param intList + * the list to copy + */ + public IntList(final IntList intList) { + data = new int[intList.getCapacity()]; + System.arraycopy(intList.data, 0, data, 0, intList.size()); + index = intList.size(); + } + + /** + * Returns {@code true} if this list contains no elements. + * + * @return {@code true} if this list contains no elements + */ + public boolean isEmpty() { + return index == 0; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return index; + } + + /** + * Returns the number of elements this list can hold before a resize is + * necessary. + * + * @return the number of elements this list can hold before a resize is + * necessary + */ + public int getCapacity() { + return data.length; + } + + /** + * Adds {@code value} to the list. + * + * @param value + * the value to add + */ + public void add(final int value) { + addAll(value); + } + + /** + * Inserts {@code values} at position {@code pos} into the list. + * + * @param pos + * the position to insert the elements + * @param values + * the elements to insert + * @throws IndexOutOfBoundsException + * if pos is out of bounds {@code pos < 0 || pos > size()} + * @throws NullPointerException + * if {@code values} is {@code null} + */ + public void insert(final int pos, final int... values) { + + if (pos < 0) { + throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos); + } + if (pos > index) { + throw new IndexOutOfBoundsException("pos must not smaller than size(), but was: " + pos); + } + if (values == null) { + throw new NullPointerException("values parameter must not be null"); + } + + ensureCapacity(values.length); + + final int[] newData = new int[data.length]; + System.arraycopy(data, 0, newData, 0, pos); + System.arraycopy(values, 0, newData, pos, values.length); + System.arraycopy(data, pos, newData, pos + values.length, index - pos); + data = newData; + index += values.length; + } + + /** + * Set the value {@code value} at position {@code pos}. + * + * @param pos + * the position to overwrite + * @param value + * the new value + * @throws IndexOutOfBoundsException + * if pos is out of bounds {@code pos < 0 || pos >= size()} + */ + public void set(final int pos, final int value) { + + if (pos < 0) { + throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos); + } + + if (pos >= index) { + throw new IndexOutOfBoundsException("pos must not smaller than size(), but was: " + pos); + } + + data[pos] = value; + } + + /** + * Add {@code values} to the list. + * + * @param values + * the values to add + * @throws NullPointerException + * if {@code values} is {@code null} + */ + public void addAll(final int... values) { + ensureCapacity(values.length); + + System.arraycopy(values, 0, data, index, values.length); + index += values.length; + } + + /** + * Removes elements from the list. + * + * @param from + * index of the first element to remove + * @param length + * number of elements to remove + * @throws IndexOutOfBoundsException + * if {@code from} or {@code length} is negative, or if the + * range defined by {@code from} and {@code length} is out of + * bounds + */ + public void remove(final int from, final int length) { + + if (from < 0) { + throw new IndexOutOfBoundsException("from must not be negative, but was: " + from); + } + if (length < 0) { + throw new IndexOutOfBoundsException("length must not be negative, but was: " + length); + } + if (from + length > index) { + throw new IndexOutOfBoundsException("from: " + from + " length: " + length); + } + + final int[] newData = new int[data.length]; + System.arraycopy(data, 0, newData, 0, from); + System.arraycopy(data, from + length, newData, from, data.length - from - length); + data = newData; + index -= length; + } + + /** + * Returns the element at position {@code pos}. + * + * @param pos + * position of the element to return + * @return the element at position {@code pos} + * @throws IndexOutOfBoundsException + * if {@code pos} is out of bounds + * {@code index < 0 || index >= size()} + */ + public int get(final int pos) { + if (pos < 0 || pos >= index) { + throw new IndexOutOfBoundsException(); + } + return data[pos]; + } + + /** + * Returns the {@code length} elements starting at {@code from}. + * + * @param from + * position of the first element + * @param length + * number of elements + * @return the {@code length} elements starting at {@code from} + * @throws IndexOutOfBoundsException + * if {@code from} or {@code length} is negative, or if the + * range defined by {@code from} and {@code length} is out of + * bounds + */ + public int[] get(final int from, final int length) { + if (from < 0) { + throw new IndexOutOfBoundsException("from must not be negative, but was: " + from); + } + if (length < 0) { + throw new IndexOutOfBoundsException("length must not be negative, but was: " + length); + } + + if (from + length > index) { + throw new IndexOutOfBoundsException("from: " + from + " length: " + length); + } + final int[] result = new int[length]; + System.arraycopy(data, from, result, 0, length); + return result; + } + + /** + * Returns an array containing all elements of this list. + * + * @return an array containing all elements of this list + */ + public int[] toArray() { + return get(0, index); + } + + private void ensureCapacity(final int newElements) { + + final int requiredCapacity = index + newElements; + if (requiredCapacity - 1 >= data.length) { + final int newCapacity = data.length * 2 > requiredCapacity ? data.length * 2 : requiredCapacity; + + final int[] newData = new int[newCapacity]; + System.arraycopy(data, 0, newData, 0, index); + data = newData; + } + } + + /** + * Reduces the capacity to the size of the list. + *
+ * Call this method to reduce the memory consumption of this list.
+ */
+ public void trim() {
+ final int[] newData = new int[index];
+ System.arraycopy(data, 0, newData, 0, index);
+ data = newData;
+ }
+
+ @Override
+ public int hashCode() {
+ if (index == 0) {
+ return 0;
+ }
+
+ int result = 1;
+ for (int i = 0; i < index; i++) {
+ result = 31 * result + data[i];
+ }
+
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final IntList other = (IntList) obj;
+ if (index != other.index) {
+ return false;
+ }
+
+ for (int i = 0; i < index; i++) {
+ if (data[i] != other.data[i]) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
diff --git a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java
new file mode 100644
index 0000000..2e9d570
--- /dev/null
+++ b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java
@@ -0,0 +1,403 @@
+package org.lucares.collections;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class IntListTest {
+
+ @Test
+ public void testEmptyList() {
+ final IntList list = new IntList();
+
+ Assert.assertEquals(true, list.isEmpty());
+ }
+
+ @Test
+ public void testCopyConstructor() {
+ final IntList list = new IntList();
+ list.addAll(1, 2, 3, 4, 5);
+
+ final IntList copy = new IntList(list);
+
+ Assert.assertArrayEquals(copy.toArray(), list.toArray());
+ }
+
+ @Test
+ public void testInvalidInitialCapacity() {
+ try {
+ new IntList(-1);
+ Assert.fail();
+ } catch (final IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testAdd() {
+ final IntList list = new IntList();
+
+ list.addAll();
+ Assert.assertTrue(list.isEmpty());
+ Assert.assertEquals(0, list.size());
+
+ list.add(1);
+
+ Assert.assertFalse(list.isEmpty());
+ Assert.assertEquals(1, list.size());
+
+ list.add(2);
+ Assert.assertEquals(2, list.size());
+
+ Assert.assertEquals(1, list.get(0));
+ Assert.assertEquals(2, list.get(1));
+ }
+
+ @Test
+ public void testInsert() {
+ final IntList list = new IntList();
+
+ list.insert(0, 1);
+ Assert.assertArrayEquals(new int[] { 1 }, list.toArray());
+
+ list.insert(1, 2);
+ Assert.assertArrayEquals(new int[] { 1, 2 }, list.toArray());
+
+ list.insert(1, 3);
+ Assert.assertArrayEquals(new int[] { 1, 3, 2 }, list.toArray());
+
+ list.insert(2, 4, 4, 4);
+ Assert.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2 }, list.toArray());
+
+ list.insert(6, 5, 5);
+ Assert.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2, 5, 5 }, list.toArray());
+ }
+
+ @Test
+ public void testInsertOutOfBounds() {
+ final IntList list = new IntList();
+ try {
+ list.insert(-1, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ list.insert(1, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ list.add(1);
+ list.insert(2, 2);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testSet() {
+ final IntList list = new IntList();
+ list.addAll(0, 1, 2, 3, 4, 5);
+
+ list.set(0, 10);
+ Assert.assertArrayEquals(new int[] { 10, 1, 2, 3, 4, 5 }, list.toArray());
+
+ list.set(1, 11);
+ Assert.assertArrayEquals(new int[] { 10, 11, 2, 3, 4, 5 }, list.toArray());
+
+ list.set(5, 55);
+ Assert.assertArrayEquals(new int[] { 10, 11, 2, 3, 4, 55 }, list.toArray());
+ }
+
+ @Test
+ public void testSetOutOfBounds() {
+ final IntList list = new IntList();
+ try {
+ list.set(-1, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ try {
+ list.set(1, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ list.addAll(0, 1, 2, 3, 4, 5);
+
+ try {
+ list.set(6, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testGrow() {
+ final IntList list = new IntList(1);
+
+ final int size = 100;
+ for (int i = 0; i < size; i++) {
+ list.add(i);
+ }
+ Assert.assertEquals(size, list.size());
+
+ for (int i = 0; i < size; i++) {
+ Assert.assertEquals(i, list.get(i));
+ }
+ }
+
+ @Test
+ public void testGrowIntListOfCapacityNull() {
+ final IntList list = new IntList(0);
+
+ final int size = 100;
+ for (int i = 0; i < size; i++) {
+ list.add(i);
+ }
+ Assert.assertEquals(size, list.size());
+
+ for (int i = 0; i < size; i++) {
+ Assert.assertEquals(i, list.get(i));
+ }
+ }
+
+ @Test
+ public void testAddArray() {
+ final IntList list = new IntList();
+
+ final int size = 100;
+ final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
+
+ list.addAll(ints);
+ Assert.assertEquals(size, list.size());
+
+ for (int i = 0; i < size; i++) {
+ Assert.assertEquals(ints[i], list.get(i));
+ }
+
+ final int[] anotherInts = ThreadLocalRandom.current().ints(size).toArray();
+ list.addAll(anotherInts);
+ Assert.assertEquals(size * 2, list.size());
+ for (int i = 0; i < size; i++) {
+ Assert.assertEquals(anotherInts[i], list.get(size + i));
+ }
+ }
+
+ @Test
+ public void testGetArray() {
+ final IntList list = new IntList();
+
+ final int size = 100;
+ final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
+
+ list.addAll(ints);
+
+ final int length = 20;
+ final int from = 10;
+ final int[] actualInts = list.get(from, length);
+
+ for (int i = 0; i < length; i++) {
+ Assert.assertEquals(actualInts[i], list.get(from + i));
+ Assert.assertEquals(actualInts[i], ints[from + i]);
+ }
+ }
+
+ @Test
+ public void testGetOutOfBounds() {
+ final IntList list = new IntList();
+
+ list.addAll(0, 1, 2, 3);
+
+ try {
+ list.get(-1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.get(4);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testGetArrayOutOfBounds() {
+ final IntList list = new IntList();
+
+ list.addAll(0, 1, 2, 3);
+
+ try {
+ list.get(-1, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.get(1, -1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.get(3, 2);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.get(4, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testToArrayEmptyLIst() {
+ final IntList list = new IntList();
+
+ final int[] actual = list.toArray();
+ Assert.assertArrayEquals(actual, new int[0]);
+ }
+
+ @Test
+ public void testRemove() {
+ final IntList list = new IntList();
+
+ final int size = 10;
+ final int[] ints = new int[size];
+ for (int i = 0; i < size; i++) {
+ ints[i] = i;
+ }
+
+ list.addAll(ints);
+
+ list.remove(2, 0);
+ Assert.assertArrayEquals(ints, list.toArray());
+
+ list.remove(9, 1);
+ final int[] expectedA = removeElements(ints, 9);
+ Assert.assertArrayEquals(expectedA, list.toArray());
+
+ list.remove(0, 1);
+ final int[] expectedB = removeElements(expectedA, 0);
+ Assert.assertArrayEquals(expectedB, list.toArray());
+
+ list.remove(7, 1);
+ final int[] expectedC = removeElements(expectedB, 7);
+ Assert.assertArrayEquals(expectedC, list.toArray());
+
+ list.remove(3, 3);
+ final int[] expectedD = removeElements(expectedC, 3, 4, 5);
+ Assert.assertArrayEquals(expectedD, list.toArray());
+
+ list.remove(0, 4);
+ final int[] expectedE = removeElements(expectedD, 0, 1, 2, 3);
+ Assert.assertArrayEquals(expectedE, list.toArray());
+ }
+
+ @Test
+ public void testRemoveOutOfRange() {
+ final IntList list = new IntList();
+
+ list.addAll(0, 1, 2, 3);
+
+ try {
+ list.remove(-1, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.remove(1, -1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.remove(3, 2);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ list.remove(4, 1);
+ Assert.fail();
+ } catch (final IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testTrim() {
+ final IntList list = new IntList();
+
+ list.addAll(0, 1, 2, 3);
+
+ list.trim();
+ Assert.assertEquals(4, list.getCapacity());
+ }
+
+ @Test
+ public void testTrimOnEmptyList() {
+ final IntList list = new IntList();
+
+ list.trim();
+ Assert.assertEquals(0, list.getCapacity());
+ }
+
+ @Test
+ public void testHashCodeEquals() {
+ final IntList a = new IntList();
+ final IntList b = new IntList();
+
+ a.addAll(1, 2, 3, 4);
+ b.addAll(1, 2, 3, 4);
+
+ Assert.assertTrue(a.equals(b));
+ Assert.assertEquals(a.hashCode(), b.hashCode());
+
+ // trim only one of them
+ // we want to compare the actual content
+ a.trim();
+ Assert.assertTrue(a.equals(b));
+ Assert.assertEquals(a.hashCode(), b.hashCode());
+
+ // change one value
+ a.remove(2, 1);
+ a.insert(2, 99);
+ Assert.assertFalse(a.equals(b));
+ Assert.assertNotEquals(a.hashCode(), b.hashCode());
+ }
+
+ private int[] removeElements(final int[] data, final int... removedElements) {
+ final int[] result = new int[data.length - removedElements.length];
+ final List