From 8ba3b370f8d1fc419950d437e4669988dcc8dd1d Mon Sep 17 00:00:00 2001 From: vsonnier Date: Mon, 20 Jul 2015 20:48:02 +0200 Subject: [PATCH] Fixes HPPCRT-45 --- CHANGES.txt | 4 + hppcrt-benchmarks/pom.xml | 6 +- hppcrt-template-processor/pom.xml | 4 +- hppcrt/pom.xml | 4 +- .../hppcrt/lists/KTypeArrayDeque.java | 127 ++++++++++++------ .../hppcrt/lists/KTypeArrayList.java | 38 +++--- .../hppcrt/lists/KTypeLinkedList.java | 60 +++++---- .../hppcrt/sorting/KTypeSort.java | 20 +-- .../AbstractKTypeIndexedContainerTest.java | 91 ++++++------- .../hppcrt/lists/KTypeArrayDequeTest.java | 61 +++++---- .../hppcrt/lists/KTypeArrayListTest.java | 61 +++++---- .../hppcrt/lists/KTypeLinkedListTest.java | 65 ++++----- .../hppcrt/sorting/KTypeSortTest.java | 64 +++++---- pom.xml | 2 +- 14 files changed, 326 insertions(+), 281 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 0a8281255..f14e3d1b9 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,7 @@ +[0.7.1] +** Bug fixes +HPPCRT-45: API with intervals arguments is inconsistent with JDK conventions. + [0.7.0] This release fix important bugs, and align on some refactorings or changes of HPPC v0.7.1, hence the version number. diff --git a/hppcrt-benchmarks/pom.xml b/hppcrt-benchmarks/pom.xml index 17ac60c2f..9de60e8dc 100644 --- a/hppcrt-benchmarks/pom.xml +++ b/hppcrt-benchmarks/pom.xml @@ -5,13 +5,13 @@ com.github.vsonnier hppcrt-parent - 0.7.0 + 0.7.1 ../pom.xml hppcrt-benchmarks - 0.7.0 + 0.7.1 jar HPPC-RT Benchmarks @@ -24,7 +24,7 @@ 2.6 UTF-8 - 1.10.2 + 1.10.3 1.7 benchmarks diff --git a/hppcrt-template-processor/pom.xml b/hppcrt-template-processor/pom.xml index 6ee512ee1..54b9804b4 100644 --- a/hppcrt-template-processor/pom.xml +++ b/hppcrt-template-processor/pom.xml @@ -5,14 +5,14 @@ com.github.vsonnier hppcrt-parent - 0.7.0 + 0.7.1 ../pom.xml com.github.vsonnier hppcrt-template-processor - 0.7.0 + 0.7.1 jar HPPC-RT Template Processor diff --git a/hppcrt/pom.xml b/hppcrt/pom.xml index 6e00fe9b9..a07f4d99c 100644 --- a/hppcrt/pom.xml +++ b/hppcrt/pom.xml @@ -6,14 +6,14 @@ com.github.vsonnier hppcrt-parent - 0.7.0 + 0.7.1 ../pom.xml com.github.vsonnier hppcrt - 0.7.0 + 0.7.1 jar HPPC-RT Collections diff --git a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDeque.java b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDeque.java index 6a8dad096..9c21272eb 100644 --- a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDeque.java +++ b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDeque.java @@ -52,7 +52,7 @@ */ /*! ${TemplateOptions.generatedAnnotation} !*/ public class KTypeArrayDeque - extends AbstractKTypeCollection implements KTypeDeque, KTypeIndexedContainer, Cloneable +extends AbstractKTypeCollection implements KTypeDeque, KTypeIndexedContainer, Cloneable { /** * Internal array for storing elements. @@ -65,8 +65,8 @@ public class KTypeArrayDeque KType [] #else !*/ Object[] - /*! #end !*/ - buffer; + /*! #end !*/ + buffer; /** * The index of the element at the head of the deque or an @@ -751,14 +751,18 @@ public > T forEach(final T procedure) { @Override public > T forEach(final T procedure, final int fromIndex, final int toIndex) { - if (fromIndex >= toIndex) { + checkRangeBounds(fromIndex, toIndex); - throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex); + if (fromIndex == toIndex) { + + return procedure; //nothing to do } + final int bufferPositionStart = indexToBufferPosition(fromIndex); + final int endBufferPosInclusive = indexToBufferPosition(toIndex - 1); //must be a valid index - internalForEach(procedure, indexToBufferPosition(fromIndex), oneRight(endBufferPosInclusive, this.buffer.length)); + internalForEach(procedure, bufferPositionStart, oneRight(endBufferPosInclusive, this.buffer.length)); return procedure; } @@ -791,14 +795,18 @@ public > T forEach(final T predicate) { @Override public > T forEach(final T predicate, final int fromIndex, final int toIndex) { - if (fromIndex >= toIndex) { + checkRangeBounds(fromIndex, toIndex); + + if (fromIndex == toIndex) { - throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex); + return predicate; //nothing to do } + final int bufferPositionStart = indexToBufferPosition(fromIndex); + final int endBufferPosInclusive = indexToBufferPosition(toIndex - 1); //must be a valid index - internalForEach(predicate, indexToBufferPosition(fromIndex), oneRight(endBufferPosInclusive, this.buffer.length)); + internalForEach(predicate, bufferPositionStart, oneRight(endBufferPosInclusive, this.buffer.length)); return predicate; } @@ -1035,7 +1043,7 @@ private boolean allIndexesEqual(final KTypeIndexedContainer b1, final KTy * instead of using a constructor). */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayDeque newInstance() { + KTypeArrayDeque newInstance() { return new KTypeArrayDeque(); } @@ -1044,7 +1052,7 @@ KTypeArrayDeque newInstance() { * instead of using a constructor). */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayDeque newInstance(final int initialCapacity) { + KTypeArrayDeque newInstance(final int initialCapacity) { return new KTypeArrayDeque(initialCapacity); } @@ -1052,7 +1060,7 @@ KTypeArrayDeque newInstance(final int initialCapacity) { * Create a new deque by pushing a variable number of arguments to the end of it. */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayDeque from(final KType... elements) { + KTypeArrayDeque from(final KType... elements) { final KTypeArrayDeque coll = new KTypeArrayDeque(elements.length); coll.addLast(elements); return coll; @@ -1062,7 +1070,7 @@ KTypeArrayDeque from(final KType... elements) { * Create a new deque by pushing a variable number of arguments to the end of it. */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayDeque from(final KTypeContainer container) { + KTypeArrayDeque from(final KTypeContainer container) { return new KTypeArrayDeque(container); } @@ -1081,21 +1089,27 @@ KTypeArrayDeque from(final KTypeContainer container) { */ public void sort(final int beginIndex, final int endIndex) { - if (endIndex - beginIndex > 1) { - //Fast path : if the actual indices matching [beginIndex; endIndex[ - //in the underlying buffer are in increasing order (means there is no folding of buffer in the interval), - // use quicksort array version directly. - final int bufferPosStart = indexToBufferPosition(beginIndex); - final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be a valid index + checkRangeBounds(beginIndex, endIndex); - if (bufferPosEndInclusive > bufferPosStart) { + if (beginIndex == endIndex) { - KTypeSort.quicksort(this.buffer, bufferPosStart, bufferPosEndInclusive + 1); - } else { - //Use the slower KTypeIndexedContainer sort - KTypeSort.quicksort(this, beginIndex, endIndex); - } + return; //nothing to do + } + + //Fast path : if the actual indices matching [beginIndex; endIndex[ + //in the underlying buffer are in increasing order (means there is no folding of buffer in the interval), + // use quicksort array version directly. + final int bufferPosStart = indexToBufferPosition(beginIndex); + final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be a valid index + + if (bufferPosEndInclusive > bufferPosStart) { + + KTypeSort.quicksort(this.buffer, bufferPosStart, bufferPosEndInclusive + 1); + } else { + //Use the slower KTypeIndexedContainer sort + KTypeSort.quicksort(this, beginIndex, endIndex); } + } /** @@ -1112,26 +1126,32 @@ public void sort(final int beginIndex, final int endIndex) { public void sort(final int beginIndex, final int endIndex, /*! #if ($TemplateOptions.KTypeGeneric) !*/ final Comparator - /*! #else - KTypeComparator - #end !*/ - comp) { + /*! #else + KTypeComparator + #end !*/ + comp) { - if (endIndex - beginIndex > 1) { - //Fast path : if the actual indices matching [beginIndex; endIndex[ - //in the underlying buffer are in increasing order (means there is no folding of buffer in the interval), - // use quicksort array version directly. - final int bufferPosStart = indexToBufferPosition(beginIndex); - final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be valid indices + checkRangeBounds(beginIndex, endIndex); - if (bufferPosEndInclusive > bufferPosStart) { + if (beginIndex == endIndex) { - KTypeSort.quicksort(Intrinsics. cast(this.buffer), bufferPosStart, bufferPosEndInclusive + 1, comp); - } else { - //Use the slower KTypeIndexedContainer sort - KTypeSort.quicksort(this, beginIndex, endIndex, comp); - } + return; //nothing to do } + + //Fast path : if the actual indices matching [beginIndex; endIndex[ + //in the underlying buffer are in increasing order (means there is no folding of buffer in the interval), + // use quicksort array version directly. + final int bufferPosStart = indexToBufferPosition(beginIndex); + final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be valid indices + + if (bufferPosEndInclusive > bufferPosStart) { + + KTypeSort.quicksort(Intrinsics. cast(this.buffer), bufferPosStart, bufferPosEndInclusive + 1, comp); + } else { + //Use the slower KTypeIndexedContainer sort + KTypeSort.quicksort(this, beginIndex, endIndex, comp); + } + } /** @@ -1298,12 +1318,15 @@ private void removeBufferIndicesRange(final int fromBufferIndex, final int toBuf @Override public void removeRange(final int fromIndex, final int toIndex) { - if (fromIndex >= toIndex) { + checkRangeBounds(fromIndex, toIndex); + + if (fromIndex == toIndex) { - throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex); + return; //nothing to do } final int bufferPositionStart = indexToBufferPosition(fromIndex); + final int bufferPositionEndInclusive = indexToBufferPosition(toIndex - 1); //must be a valid index removeBufferIndicesRange(bufferPositionStart, oneRight(bufferPositionEndInclusive, this.buffer.length)); @@ -1360,6 +1383,24 @@ private int indexToBufferPosition(final int index) { return (int) bufferPos; } + private void checkRangeBounds(final int beginIndex, final int endIndex) { + + if (beginIndex > endIndex) { + + throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is > endIndex " + endIndex); + } + + if (beginIndex < 0) { + + throw new IndexOutOfBoundsException("Index beginIndex < 0"); + } + + if (endIndex > size()) { + + throw new IndexOutOfBoundsException("Index endIndex " + endIndex + " out of bounds [" + 0 + ", " + size() + "]."); + } + } + /*! #if ($TemplateOptions.declareInline("oneLeft(index, modulus)", "<*>==>(index >= 1) ? index - 1 : modulus - 1")) !*/ /** * Move one index to the left, wrapping around buffer of size modulus. diff --git a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayList.java b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayList.java index f66bfff9d..cd246e35e 100644 --- a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayList.java +++ b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayList.java @@ -55,7 +55,7 @@ */ /*! ${TemplateOptions.generatedAnnotation} !*/ public class KTypeArrayList -extends AbstractKTypeCollection implements KTypeIndexedContainer, Cloneable + extends AbstractKTypeCollection implements KTypeIndexedContainer, Cloneable { /** * Internal array for storing the list. The array may be larger than the current size @@ -69,8 +69,8 @@ public class KTypeArrayList KType [] #else !*/ Object[] - /*! #end !*/ - buffer; + /*! #end !*/ + buffer; /** * Current number of elements stored in {@link #buffer}. @@ -787,7 +787,7 @@ public > T forEach(final T predicate, fi * instead of using a constructor). */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayList newInstance() { + KTypeArrayList newInstance() { return new KTypeArrayList(); } @@ -796,7 +796,7 @@ KTypeArrayList newInstance() { * instead of using a constructor). */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayList newInstance(final int initialCapacity) { + KTypeArrayList newInstance(final int initialCapacity) { return new KTypeArrayList(initialCapacity); } @@ -805,7 +805,7 @@ KTypeArrayList newInstance(final int initialCapacity) { * KType. */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayList from(final KType... elements) { + KTypeArrayList from(final KType... elements) { final KTypeArrayList list = new KTypeArrayList(elements.length); list.add(elements); return list; @@ -815,7 +815,7 @@ KTypeArrayList from(final KType... elements) { * Create a list from elements of another container. */ public static/* #if ($TemplateOptions.KTypeGeneric) */ /* #end */ - KTypeArrayList from(final KTypeContainer container) { + KTypeArrayList from(final KTypeContainer container) { return new KTypeArrayList(container); } @@ -833,9 +833,7 @@ KTypeArrayList from(final KTypeContainer container) { */ public void sort(final int beginIndex, final int endIndex) { - if (endIndex - beginIndex > 1) { - KTypeSort.quicksort(this.buffer, beginIndex, endIndex); - } + KTypeSort.quicksort(this.buffer, beginIndex, endIndex); } /** @@ -866,14 +864,12 @@ public void sort() { public void sort(final int beginIndex, final int endIndex, /*! #if ($TemplateOptions.KTypeGeneric) !*/ final Comparator - /*! #else - KTypeComparator - #end !*/ - comp) { + /*! #else + KTypeComparator + #end !*/ + comp) { - if (endIndex - beginIndex > 1) { - KTypeSort.quicksort(Intrinsics. cast(this.buffer), beginIndex, endIndex, comp); - } + KTypeSort.quicksort(Intrinsics. cast(this.buffer), beginIndex, endIndex, comp); } /** @@ -1030,14 +1026,14 @@ public KType peekLast() { private void checkRangeBounds(final int beginIndex, final int endIndex) { - if (beginIndex >= endIndex) { + if (beginIndex > endIndex) { - throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is >= endIndex " + endIndex); + throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is > endIndex " + endIndex); } - if (beginIndex < 0 || beginIndex >= this.elementsCount) { + if (beginIndex < 0) { - throw new IndexOutOfBoundsException("Index beginIndex " + beginIndex + " out of bounds [" + 0 + ", " + this.elementsCount + "[."); + throw new IndexOutOfBoundsException("Index beginIndex < 0"); } if (endIndex > this.elementsCount) { diff --git a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedList.java b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedList.java index 61f4a4cea..227f2dc1a 100644 --- a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedList.java +++ b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedList.java @@ -348,19 +348,15 @@ public KType remove(final int index) { @Override public void removeRange(final int fromIndex, final int toIndex) { - if (fromIndex >= toIndex) { + checkRangeBounds(fromIndex, toIndex); - throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex); + if (fromIndex == toIndex) { + return; //nothing to do } //goto pos int currentPos = gotoIndex(fromIndex); - if (toIndex > size()) { - - throw new IndexOutOfBoundsException("Index toIndex " + toIndex + " out of bounds [" + fromIndex + ", " + size() + "]."); - } - //start removing size elements... final int size = toIndex - fromIndex; int count = 0; @@ -1418,19 +1414,15 @@ public > T forEach(final T procedure, fi private void internalForEach(final KTypeProcedure procedure, final int fromIndex, final int toIndex) { - if (fromIndex >= toIndex) { + checkRangeBounds(fromIndex, toIndex); - throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex); + if (fromIndex == toIndex) { + return; //nothing to do } //goto pos int currentPos = gotoIndex(fromIndex); - if (toIndex > size()) { - - throw new IndexOutOfBoundsException("Index toIndex " + toIndex + " out of bounds [" + fromIndex + ", " + size() + "]."); - } - final long[] pointers = this.beforeAfterPointers; final KType[] buffer = Intrinsics. cast(this.buffer); @@ -1472,19 +1464,15 @@ public > T forEach(final T predicate, fi private void internalForEach(final KTypePredicate predicate, final int fromIndex, final int toIndex) { - if (fromIndex >= toIndex) { + checkRangeBounds(fromIndex, toIndex); - throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex); + if (fromIndex == toIndex) { + return; //nothing to do } //goto pos int currentPos = gotoIndex(fromIndex); - if (toIndex > size()) { - - throw new IndexOutOfBoundsException("Index toIndex " + toIndex + " out of bounds [" + fromIndex + ", " + size() + "]."); - } - final long[] pointers = this.beforeAfterPointers; final KType[] buffer = Intrinsics. cast(this.buffer); @@ -1633,9 +1621,7 @@ KTypeLinkedList from(final KTypeContainer container) { */ public void sort(final int beginIndex, final int endIndex) { - if (endIndex - beginIndex > 1) { - KTypeSort.quicksort(this, beginIndex, endIndex); - } + KTypeSort.quicksort(this, beginIndex, endIndex); } /** @@ -1658,13 +1644,11 @@ public void sort(final int beginIndex, final int endIndex, /*! #if ($TemplateOptions.KTypeGeneric) !*/ final Comparator /*! #else - KTypeComparator - #end !*/ + KTypeComparator + #end !*/ comp) { - if (endIndex - beginIndex > 1) { - KTypeSort.quicksort(this, beginIndex, endIndex, comp); - } + KTypeSort.quicksort(this, beginIndex, endIndex, comp); } /** @@ -1861,6 +1845,24 @@ public KType getLast() { return Intrinsics. cast(this.buffer[getLinkBefore(this.beforeAfterPointers[KTypeLinkedList.TAIL_POSITION])]); } + private void checkRangeBounds(final int beginIndex, final int endIndex) { + + if (beginIndex > endIndex) { + + throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is > endIndex " + endIndex); + } + + if (beginIndex < 0) { + + throw new IndexOutOfBoundsException("Index beginIndex < 0"); + } + + if (endIndex > size()) { + + throw new IndexOutOfBoundsException("Index endIndex " + endIndex + " out of bounds [" + 0 + ", " + size() + "]."); + } + } + /*! #if ($TemplateOptions.declareInline("getLinkNodeValue(beforeIndex, afterIndex)", "<*>==>((long) beforeIndex << 32) | afterIndex")) !*/ /** diff --git a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/sorting/KTypeSort.java b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/sorting/KTypeSort.java index fa0cff06d..e26e90e07 100644 --- a/hppcrt/src/main/templates/com/carrotsearch/hppcrt/sorting/KTypeSort.java +++ b/hppcrt/src/main/templates/com/carrotsearch/hppcrt/sorting/KTypeSort.java @@ -137,10 +137,10 @@ private KTypeSort() public static/*! #if ($TemplateOptions.KTypeGeneric) !*/ /*! #end !*/void quicksort(final KType[] table, /*! #if ($TemplateOptions.KTypeGeneric) !*/ final Comparator - /*! #else + /*! #else KTypeComparator #end !*/ - comp) + comp) { KTypeSort.quicksort(table, 0, table.length, comp); } @@ -179,10 +179,10 @@ private KTypeSort() public static/*! #if ($TemplateOptions.KTypeGeneric) !*/ /*! #end !*/void quicksort(final KTypeIndexedContainer table, /*! #if ($TemplateOptions.KTypeGeneric) !*/ final Comparator - /*! #else - KTypeComparator - #end !*/ - comp) + /*! #else + KTypeComparator + #end !*/ + comp) { KTypeSort.quicksort(table, 0, table.size(), comp); } @@ -1151,14 +1151,14 @@ else if (comp.compare(x, pivot2) == 0 /*x == pivot2*/) private static void checkRanges(final int beginIndex, final int endIndex, final int size) { - if (beginIndex >= endIndex) { + if (beginIndex > endIndex) { - throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is >= endIndex " + endIndex); + throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is > endIndex " + endIndex); } - if (beginIndex < 0 || beginIndex >= size) { + if (beginIndex < 0) { - throw new IndexOutOfBoundsException("Index beginIndex " + beginIndex + " out of bounds [" + 0 + ", " + size + "[."); + throw new IndexOutOfBoundsException("Index beginIndex < 0"); } if (endIndex > size) { diff --git a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/AbstractKTypeIndexedContainerTest.java b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/AbstractKTypeIndexedContainerTest.java index a704a72d9..340903bdd 100644 --- a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/AbstractKTypeIndexedContainerTest.java +++ b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/AbstractKTypeIndexedContainerTest.java @@ -200,18 +200,19 @@ public void testRemove() } /** - * Try lots of combinations of size, range + * Try lots of combinations of size, range, + * validity is 0 <= from <= to <= size() */ @Test public void testRemoveRange() { final ArrayList referenceDeque = new ArrayList(); - final int TEST_SIZE = (int) 126; + final int TEST_SIZE = (int) 55; final Random prng = new Random(0xBADCAFE); - final int NB_ITERATION = 50000; + final int NB_ITERATION = 200000; for (int ii = 0; ii < NB_ITERATION; ii++) { @@ -220,26 +221,20 @@ public void testRemoveRange() //create deque of this size final int dequeSize = prng.nextInt(TEST_SIZE); - if (dequeSize <= 0) { - - continue; - } - final KTypeIndexedContainer testQ = createIndexedContainerWithRandomData(dequeSize, 0xBADCAFE); //will attempt to remove this range - final int upperRange = prng.nextInt(dequeSize); + final int upperRange = prng.nextInt(dequeSize + 1); - if (upperRange <= 0) { - continue; - } - - final int lowerRange = prng.nextInt(upperRange); + final int lowerRange = prng.nextInt(upperRange + 1); //Fill the reference JCF deque for (int jj = 0; jj < testQ.size(); jj++) { - if (jj < lowerRange || jj >= upperRange) { + if (upperRange == lowerRange) { + referenceDeque.add(castType(testQ.get(jj))); //no removal at all + } + else if (jj < lowerRange || jj >= upperRange) { referenceDeque.add(castType(testQ.get(jj))); } } @@ -265,6 +260,7 @@ public void testRemoveRange() /** * forEach(procedure, slice) Try lots of combinations of size, range + * validity is 0 <= from <= to <= size() */ @Test public void testForEachProcedureRange() @@ -273,11 +269,11 @@ public void testForEachProcedureRange() final ArrayList procedureResult = new ArrayList(); - final int TEST_SIZE = (int) 126; + final int TEST_SIZE = (int) 55; final Random prng = new Random(0xBADCAFE); - final int NB_ITERATION = 50000; + final int NB_ITERATION = 200000; for (int ii = 0; ii < NB_ITERATION; ii++) { @@ -289,26 +285,21 @@ public void testForEachProcedureRange() //create deque of this size final int dequeSize = prng.nextInt(TEST_SIZE); - if (dequeSize <= 0) { - - continue; - } - final KTypeIndexedContainer testQ = createIndexedContainerWithRandomData(dequeSize, 0xBADCAFE); //will attempt to remove this range - final int upperRange = prng.nextInt(dequeSize); + final int upperRange = prng.nextInt(dequeSize + 1); - if (upperRange <= 0) { - continue; - } - - final int lowerRange = prng.nextInt(upperRange); + final int lowerRange = prng.nextInt(upperRange + 1); //Fill the reference JCF deque for (int jj = 0; jj < testQ.size(); jj++) { - if (jj >= lowerRange && jj < upperRange) { + if (lowerRange == upperRange) { + expectedSum = 0; + break; + } + else if (jj >= lowerRange && jj < upperRange) { referenceList.add(castType(testQ.get(jj))); expectedSum += castType(testQ.get(jj)); } @@ -350,7 +341,9 @@ public void apply(final KType value) { /** * forEach(predicate, slice) Try lots of combinations of size, range + * validity is 0 <= from <= to <= size() */ + @Seed("6EB81D6F6E68EBF") @Test public void testForEachPredicateRange() { @@ -358,11 +351,11 @@ public void testForEachPredicateRange() final ArrayList predicateResult = new ArrayList(); - final int TEST_SIZE = (int) 126; + final int TEST_SIZE = (int) 55; final Random prng = RandomizedTest.getRandom(); - final int NB_ITERATION = 50000; + final int NB_ITERATION = 200000; for (int ii = 0; ii < NB_ITERATION; ii++) { @@ -374,30 +367,31 @@ public void testForEachPredicateRange() //create container final int containerSize = prng.nextInt(TEST_SIZE); - if (containerSize <= 0) { - - continue; - } - final KTypeIndexedContainer testQ = createIndexedContainerWithRandomData(containerSize, 0xBADCAFE); //will attempt to remove this range - final int upperRange = prng.nextInt(containerSize); - - if (upperRange <= 0) { - continue; - } + final int upperRange = prng.nextInt(containerSize + 1); - final int lowerRange = prng.nextInt(upperRange); + final int lowerRange = prng.nextInt(upperRange + 1); //stop at a random position between [lowerRange, upperRange[ + final int stopRange; //stopRange is included, i.e computations goes from [lowerRange; stopRange] ! - final int stopRange = RandomizedTest.randomIntBetween(lowerRange, upperRange - 1); + if (upperRange - lowerRange <= 1) { + stopRange = lowerRange; + } else { + + stopRange = RandomizedTest.randomIntBetween(lowerRange, upperRange - 1); + } //Fill the reference JCF deque for (int jj = 0; jj < testQ.size(); jj++) { - if (jj >= lowerRange && jj <= stopRange) { + if (lowerRange == upperRange) { + expectedSum = 0; + break; + } + else if (jj >= lowerRange && jj <= stopRange) { //stopRange is included into the computation referenceList.add(castType(testQ.get(jj))); expectedSum += castType(testQ.get(jj)); } @@ -409,21 +403,22 @@ public void testForEachPredicateRange() long count = 0; - long nbIterations = 0; + int currentRange = lowerRange; @Override public boolean apply(final KType value) { - this.nbIterations++; - + //execute predicateResult.add(castType(value)); this.count += castType(value); - if (this.nbIterations > (stopRange - lowerRange)) { + if (this.currentRange >= stopRange) { //stopRange computation is included, then iteration stops return false; } + this.currentRange++; + return true; } diff --git a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDequeTest.java b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDequeTest.java index 2e95d5818..315be9de8 100644 --- a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDequeTest.java +++ b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDequeTest.java @@ -168,7 +168,6 @@ public void testRelease() } @Seed("1F4A04B1D776DCB6") - @Repeat(iterations = 100) @Test public void testSort() { @@ -193,38 +192,42 @@ else if (castType(e1) > castType(e2)) } }; - final int TEST_SIZE = (int) 1e4; + final int TEST_SIZE = (int) 50; + final int NB_ITERATIONS = (int) 1e5; //get a new seed for the current iteration final long currentSeed = RandomizedTest.randomLong(); - final int upperRange = RandomizedTest.randomInt(TEST_SIZE); - final int lowerRange = RandomizedTest.randomInt(upperRange); - - //A) Sort a deque of random values of primitive types - - //A-1) full sort - KTypeArrayDeque primitiveDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); - KTypeArrayDeque primitiveDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); - primitiveDeque.sort(); - assertOrder(primitiveDequeOriginal, primitiveDeque, 0, primitiveDequeOriginal.size()); - //A-2) Partial sort - primitiveDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); - primitiveDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); - primitiveDeque.sort(lowerRange, upperRange); - assertOrder(primitiveDequeOriginal, primitiveDeque, lowerRange, upperRange); - - //B) Sort with Comparator - //B-1) Full sort - KTypeArrayDeque comparatorDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); - KTypeArrayDeque comparatorDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); - comparatorDeque.sort(comp); - assertOrder(comparatorDequeOriginal, comparatorDeque, 0, comparatorDequeOriginal.size()); - //B-2) Partial sort - comparatorDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); - comparatorDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); - comparatorDeque.sort(lowerRange, upperRange, comp); - assertOrder(comparatorDequeOriginal, comparatorDeque, lowerRange, upperRange); + for (int ii = 0; ii < NB_ITERATIONS; ii++) { + + final int upperRange = RandomizedTest.randomInt(TEST_SIZE); + final int lowerRange = RandomizedTest.randomInt(upperRange); + + //A) Sort a deque of random values of primitive types + + //A-1) full sort + KTypeArrayDeque primitiveDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); + KTypeArrayDeque primitiveDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); + primitiveDeque.sort(); + assertOrder(primitiveDequeOriginal, primitiveDeque, 0, primitiveDequeOriginal.size()); + //A-2) Partial sort + primitiveDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); + primitiveDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); + primitiveDeque.sort(lowerRange, upperRange); + assertOrder(primitiveDequeOriginal, primitiveDeque, lowerRange, upperRange); + + //B) Sort with Comparator + //B-1) Full sort + KTypeArrayDeque comparatorDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); + KTypeArrayDeque comparatorDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); + comparatorDeque.sort(comp); + assertOrder(comparatorDequeOriginal, comparatorDeque, 0, comparatorDequeOriginal.size()); + //B-2) Partial sort + comparatorDeque = createDequeWithRandomData(TEST_SIZE, currentSeed); + comparatorDequeOriginal = createDequeWithRandomData(TEST_SIZE, currentSeed); + comparatorDeque.sort(lowerRange, upperRange, comp); + assertOrder(comparatorDequeOriginal, comparatorDeque, lowerRange, upperRange); + } } /* */ diff --git a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayListTest.java b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayListTest.java index 78816ce72..b87468592 100644 --- a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayListTest.java +++ b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayListTest.java @@ -277,7 +277,6 @@ public void testGrowth() this.arrayList.buffer.length <= count + maxGrowth); } - @Repeat(iterations = 100) @Test public void testSort() { @@ -302,38 +301,42 @@ else if (castType(e1) > castType(e2)) } }; - final int TEST_SIZE = (int) 1e4; + final int TEST_SIZE = (int) 50; + final int NB_ITERATIONS = (int) 1e5; //get a new seed for the current iteration final long currentSeed = RandomizedTest.randomLong(); - final int upperRange = RandomizedTest.randomInt(TEST_SIZE); - final int lowerRange = RandomizedTest.randomInt(upperRange); - - //A) Sort an array of random values of primitive types - - //A-1) full sort - KTypeArrayList primitiveList = createArrayListWithRandomData(TEST_SIZE, currentSeed); - KTypeArrayList primitiveListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); - primitiveList.sort(); - assertOrder(primitiveListOriginal, primitiveList, 0, primitiveList.size()); - //A-2) Partial sort - primitiveList = createArrayListWithRandomData(TEST_SIZE, currentSeed); - primitiveListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); - primitiveList.sort(lowerRange, upperRange); - assertOrder(primitiveListOriginal, primitiveList, lowerRange, upperRange); - - //B) Sort with Comparator - //B-1) Full sort - KTypeArrayList comparatorList = createArrayListWithRandomData(TEST_SIZE, currentSeed); - KTypeArrayList comparatorListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); - comparatorList.sort(comp); - assertOrder(comparatorListOriginal, comparatorList, 0, comparatorList.size()); - //B-2) Partial sort - comparatorList = createArrayListWithRandomData(TEST_SIZE, currentSeed); - comparatorListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); - comparatorList.sort(lowerRange, upperRange, comp); - assertOrder(comparatorListOriginal, comparatorList, lowerRange, upperRange); + for (int ii = 0; ii < NB_ITERATIONS; ii++) { + + final int upperRange = RandomizedTest.randomInt(TEST_SIZE); + final int lowerRange = RandomizedTest.randomInt(upperRange); + + //A) Sort an array of random values of primitive types + + //A-1) full sort + KTypeArrayList primitiveList = createArrayListWithRandomData(TEST_SIZE, currentSeed); + KTypeArrayList primitiveListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); + primitiveList.sort(); + assertOrder(primitiveListOriginal, primitiveList, 0, primitiveList.size()); + //A-2) Partial sort + primitiveList = createArrayListWithRandomData(TEST_SIZE, currentSeed); + primitiveListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); + primitiveList.sort(lowerRange, upperRange); + assertOrder(primitiveListOriginal, primitiveList, lowerRange, upperRange); + + //B) Sort with Comparator + //B-1) Full sort + KTypeArrayList comparatorList = createArrayListWithRandomData(TEST_SIZE, currentSeed); + KTypeArrayList comparatorListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); + comparatorList.sort(comp); + assertOrder(comparatorListOriginal, comparatorList, 0, comparatorList.size()); + //B-2) Partial sort + comparatorList = createArrayListWithRandomData(TEST_SIZE, currentSeed); + comparatorListOriginal = createArrayListWithRandomData(TEST_SIZE, currentSeed); + comparatorList.sort(lowerRange, upperRange, comp); + assertOrder(comparatorListOriginal, comparatorList, lowerRange, upperRange); + } } /* */ diff --git a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedListTest.java b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedListTest.java index 58216d2d4..84dfc77c0 100644 --- a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedListTest.java +++ b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeLinkedListTest.java @@ -56,10 +56,10 @@ public void checkConsistency() int count = 0; //check access by get() for (/*! #if ($TemplateOptions.KTypeGeneric) !*/final Object - /*! #else + /*! #else final KType #end !*/ - val : this.list.toArray()) { + val : this.list.toArray()) { /*! #if ($TemplateOptions.KTypeGeneric) !*/ TestUtils.assertEquals2(val, (Object) this.list.get(count)); @@ -176,7 +176,6 @@ public void testEnsureCapacity() /** * The beast is slow, don't do too much */ - @Repeat(iterations = 20) @Test public void testSort() { @@ -201,38 +200,42 @@ else if (castType(e1) > castType(e2)) } }; - final int TEST_SIZE = (int) 1e3; + final int TEST_SIZE = (int) 20; + final int NB_ITERATIONS = (int) 1e5; //get a new seed for the current iteration final long currentSeed = RandomizedTest.randomLong(); - final int upperRange = RandomizedTest.randomInt(TEST_SIZE); - final int lowerRange = RandomizedTest.randomInt(upperRange); - - //A) Sort an array of random values of primitive types - - //A-1) full sort - KTypeLinkedList primitiveList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - KTypeLinkedList primitiveListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - primitiveList.sort(); - assertOrder(primitiveListOriginal, primitiveList, 0, primitiveListOriginal.size()); - //A-2) Partial sort - primitiveList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - primitiveListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - primitiveList.sort(lowerRange, upperRange); - assertOrder(primitiveListOriginal, primitiveList, lowerRange, upperRange); - - //B) Sort with Comparator - //B-1) Full sort - KTypeLinkedList comparatorList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - KTypeLinkedList comparatorListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - comparatorList.sort(comp); - assertOrder(comparatorListOriginal, comparatorList, 0, comparatorListOriginal.size()); - //B-2) Partial sort - comparatorList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - comparatorListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); - comparatorList.sort(lowerRange, upperRange, comp); - assertOrder(comparatorListOriginal, comparatorList, lowerRange, upperRange); + for (int ii = 0; ii < NB_ITERATIONS; ii++) { + + final int upperRange = RandomizedTest.randomInt(TEST_SIZE); + final int lowerRange = RandomizedTest.randomInt(upperRange); + + //A) Sort an array of random values of primitive types + + //A-1) full sort + KTypeLinkedList primitiveList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + KTypeLinkedList primitiveListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + primitiveList.sort(); + assertOrder(primitiveListOriginal, primitiveList, 0, primitiveListOriginal.size()); + //A-2) Partial sort + primitiveList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + primitiveListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + primitiveList.sort(lowerRange, upperRange); + assertOrder(primitiveListOriginal, primitiveList, lowerRange, upperRange); + + //B) Sort with Comparator + //B-1) Full sort + KTypeLinkedList comparatorList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + KTypeLinkedList comparatorListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + comparatorList.sort(comp); + assertOrder(comparatorListOriginal, comparatorList, 0, comparatorListOriginal.size()); + //B-2) Partial sort + comparatorList = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + comparatorListOriginal = createLinkedListWithRandomData(TEST_SIZE, currentSeed); + comparatorList.sort(lowerRange, upperRange, comp); + assertOrder(comparatorListOriginal, comparatorList, lowerRange, upperRange); + } } /* */ diff --git a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/sorting/KTypeSortTest.java b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/sorting/KTypeSortTest.java index cc85237fd..935f80672 100644 --- a/hppcrt/src/test/templates/com/carrotsearch/hppcrt/sorting/KTypeSortTest.java +++ b/hppcrt/src/test/templates/com/carrotsearch/hppcrt/sorting/KTypeSortTest.java @@ -43,7 +43,6 @@ public void testQuicksortComparator() sortCertification(Algorithm.QUICKSORT_COMPARATOR); } - @Repeat(iterations = 200) @Test public void testRandomizedSort() { @@ -68,43 +67,42 @@ else if (castType(e1) > castType(e2)) } }; - final int TEST_SIZE = (int) 1e4; + final int TEST_SIZE = (int) 50; + final int NB_ITERATIONS = (int) 1e6; //get a new seed for the current iteration final long currentSeed = RandomizedTest.randomLong(); - final int upperRange = RandomizedTest.randomInt(TEST_SIZE); - - if (upperRange <= 0) { - return; + for (int ii = 0; ii < NB_ITERATIONS; ii++) { + + final int upperRange = RandomizedTest.randomInt(TEST_SIZE); + final int lowerRange = RandomizedTest.randomInt(upperRange); + + //A) Sort an array of random values of primitive types + + //A-1) full sort + KType[] primitiveList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + KType[] primitiveListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + KTypeSort.quicksort(primitiveList); + assertOrder(primitiveListOriginal, primitiveList, 0, primitiveList.length); + //A-2) Partial sort + primitiveList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + primitiveListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + KTypeSort.quicksort(primitiveList, lowerRange, upperRange); + assertOrder(primitiveListOriginal, primitiveList, lowerRange, upperRange); + + //B) Sort with Comparator + //B-1) Full sort + KType[] comparatorList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + KType[] comparatorListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + KTypeSort.quicksort(comparatorList, comp); + assertOrder(comparatorListOriginal, comparatorList, 0, comparatorList.length); + //B-2) Partial sort + comparatorList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + comparatorListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); + KTypeSort.quicksort(comparatorList, lowerRange, upperRange, comp); + assertOrder(comparatorListOriginal, comparatorList, lowerRange, upperRange); } - - final int lowerRange = RandomizedTest.randomInt(upperRange - 1); - - //A) Sort an array of random values of primitive types - - //A-1) full sort - KType[] primitiveList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - KType[] primitiveListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - KTypeSort.quicksort(primitiveList); - assertOrder(primitiveListOriginal, primitiveList, 0, primitiveList.length); - //A-2) Partial sort - primitiveList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - primitiveListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - KTypeSort.quicksort(primitiveList, lowerRange, upperRange); - assertOrder(primitiveListOriginal, primitiveList, lowerRange, upperRange); - - //B) Sort with Comparator - //B-1) Full sort - KType[] comparatorList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - KType[] comparatorListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - KTypeSort.quicksort(comparatorList, comp); - assertOrder(comparatorListOriginal, comparatorList, 0, comparatorList.length); - //B-2) Partial sort - comparatorList = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - comparatorListOriginal = createArrayWithComparableRandomData(TEST_SIZE, currentSeed); - KTypeSort.quicksort(comparatorList, lowerRange, upperRange, comp); - assertOrder(comparatorListOriginal, comparatorList, lowerRange, upperRange); } /////////////////////////////////////////////////////////// diff --git a/pom.xml b/pom.xml index dea5ed770..d1788c853 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.github.vsonnier hppcrt-parent - 0.7.0 + 0.7.1 pom HPPC-RT (parent POM)