New Tests & BugFixes

-Added: Tests for the Copying of Collections.
-Fixed: PriorityQueues didn't have hashCode/equals/toString implemented
This commit is contained in:
Speiger 2021-10-06 20:23:40 +02:00
parent 0c4ef7f6c4
commit 54c9660145
14 changed files with 86 additions and 5 deletions

View File

@ -184,6 +184,7 @@ public class GlobalVariables
//Abstract Classes
addAbstractMapper("ABSTRACT_COLLECTION", "Abstract%sCollection");
addAbstractMapper("ABSTRACT_PRIORITY_QUEUE", "Abstract%sPriorityQueue");
addAbstractMapper("ABSTRACT_SET", "Abstract%sSet");
addAbstractMapper("ABSTRACT_LIST", "Abstract%sList");
addAbstractBiMapper("ABSTRACT_MAP", "Abstract%sMap", "2");

View File

@ -78,6 +78,7 @@ public class PrimitiveCollectionsBuilder extends TemplateProcessor
nameRemapper.put("IArray", "I%sArray");
nameRemapper.put("AbstractMap", "Abstract%sMap");
nameRemapper.put("AbstractCollection", "Abstract%sCollection");
nameRemapper.put("AbstractPriorityQueue", "Abstract%sPriorityQueue");
nameRemapper.put("AbstractSet", "Abstract%sSet");
nameRemapper.put("AbstractList", "Abstract%sList");
nameRemapper.put("EnumMap", "Enum2%sMap");

View File

@ -0,0 +1,46 @@
package speiger.src.collections.PACKAGE.queues;
#if TYPE_OBJECT
import java.util.Objects;
#endif
import java.util.StringJoiner;
/**
* Helper class that implements all the essential methods for the PriorityQueues
* @Type(T)
*/
public abstract class ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
{
@Override
public boolean equals(Object obj) {
if(obj instanceof PRIORITY_QUEUE) {
PRIORITY_QUEUE KEY_GENERIC_TYPE queue = (PRIORITY_QUEUE KEY_GENERIC_TYPE)obj;
if(queue.size() != size()) return false;
for(int i = 0,m=size();i<m;i++) {
if(KEY_EQUALS_NOT(queue.peek(i), peek(i))) return false;
}
return true;
}
return false;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0,m=size();i<m;i++) {
result = 31 * result + KEY_TO_HASH(peek(i));
}
return result;
}
@Override
public String toString()
{
if(isEmpty()) return "[]";
StringJoiner joiner = new StringJoiner(", ", "[", "]");
for (int i = 0,m=size();i<m;i++) {
joiner.add(KEY_TO_STRING(peek(i)));
}
return joiner.toString();
}
}

View File

@ -22,7 +22,7 @@ import speiger.src.collections.utils.ITrimmable;
* Its specific implementation uses a backing array that grows and shrinks as it is needed.
* @Type(T)
*/
public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, ITrimmable
public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_GENERIC_TYPE, ITrimmable
{
/** Max Possible ArraySize without the JVM Crashing */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

View File

@ -24,7 +24,7 @@ import speiger.src.collections.PACKAGE.utils.ARRAYS;
* It is highly suggested to use HeapPriorityQueue otherwise, unless you know why you need this specific implementation
* @Type(T)
*/
public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE
{
/** The Backing Array */
protected transient KEY_TYPE[] array = EMPTY_KEY_ARRAY;

View File

@ -23,7 +23,7 @@ import speiger.src.collections.PACKAGE.utils.ARRAYS;
* It is a ArrayBased Alternative to TreeSets that has less object allocations
* @Type(T)
*/
public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE implements PRIORITY_QUEUE KEY_GENERIC_TYPE
public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC_TYPE
{
/** The Backing Array */
protected transient KEY_TYPE[] array = EMPTY_KEY_ARRAY;

View File

@ -620,6 +620,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
set.links = Arrays.copyOf(links, links.length);
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
return set;

View File

@ -591,6 +591,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
set.links = Arrays.copyOf(links, links.length);
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
return set;

View File

@ -135,6 +135,16 @@ public abstract class BaseInt2IntMapTest
Assert.assertTrue(map.remove(PUT_VALUE_ARRAY[51], PUT_ARRAY[51]));
}
@Test
public void testSort()
{
if(!getValidMapTests().contains(MapTests.COPY)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntMap copy = map.copy();
Assert.assertFalse(map == copy);
Assert.assertEquals(map, copy);
}
public static class Strategy implements IntStrategy
{
@Override

View File

@ -178,4 +178,13 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
Assert.assertEquals(base, IntCollections.synchronize(collection).toString());
Assert.assertEquals(base, IntCollections.unmodifiable(collection).toString());
}
@Test
public void testCopy() {
if(!getValidCollectionTests().contains(CollectionTest.COPY)) return;
IntCollection collection = create(BULK_ADD_ARRAY);
IntCollection copy = collection.copy();
Assert.assertFalse(collection == copy);
Assert.assertEquals(collection, copy);
}
}

View File

@ -120,4 +120,13 @@ public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
}
}
}
@Test
public void testCopy() {
if(!getValidPriorityQueueTests().contains(PriorityQueueTest.COPY)) return;
IntPriorityQueue queue = create(TEST_ARRAY);
IntPriorityQueue copy = queue.copy();
Assert.assertFalse(queue == copy);
Assert.assertEquals(queue, copy);
}
}

View File

@ -15,5 +15,6 @@ public enum CollectionTest
TO_ARRAY,
CLEAR,
WRAPPER,
TO_STRING;
TO_STRING,
COPY;
}

View File

@ -11,5 +11,6 @@ public enum MapTests
MERGE,
GET,
ITERATORS,
REMOVE;
REMOVE,
COPY;
}

View File

@ -7,4 +7,5 @@ public enum PriorityQueueTest
PEEK,
REMOVE,
TO_ARRAY,
COPY;
}