From e30d011273ba41d08790224b365a93051909c1a8 Mon Sep 17 00:00:00 2001 From: Speiger Date: Thu, 8 Dec 2022 10:40:21 +0100 Subject: [PATCH] Fixed Unit Test and Added putIfAbsentTest --- .../templates/lists/ImmutableList.template | 3 ++- .../templates/utils/Collections.template | 22 +++++++++++++++++++ .../tests/maps/MapPutIfAbsentTester.template | 12 +++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template index 9c3de975..22c8e9bd 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template @@ -131,7 +131,8 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T @Override public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) { - SanityChecks.checkArrayCapacity(data.length, offset, length); + SanityChecks.checkArrayCapacity(a.length, offset, length); + SanityChecks.checkArrayCapacity(data.length, from, length); System.arraycopy(data, from, a, offset, length); return a; } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template index 7b13c5d4..66ca8027 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template @@ -5,6 +5,7 @@ import java.util.Collection; #if !TYPE_BOOLEAN import java.util.ConcurrentModificationException; #endif +import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; #if TYPE_OBJECT @@ -635,6 +636,27 @@ public class COLLECTIONS } }; } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Collection)) + return false; + Collection l = (Collection)o; + if(l.size() != size()) return false; + Iterator iter = l.iterator(); + if (iter.hasNext() && !Objects.equals(element, iter.next())) { + return false; + } + return !iter.hasNext(); + } + + @Override + public int hashCode() { + return KEY_TO_HASH(element); + } + @Override public int size() { return 1; } diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapPutIfAbsentTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapPutIfAbsentTester.template index aa8b4d6d..2977aa6d 100644 --- a/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapPutIfAbsentTester.template +++ b/src/builder/resources/speiger/assets/testers/templates/tests/maps/MapPutIfAbsentTester.template @@ -32,7 +32,17 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapPutIfAbsentTester KEY_VALUE_GENERIC assertEquals("putIfAbsent(present, value) should return existing value", v0(), getMap().putIfAbsent(k0(), v3())); expectUnchanged(); } - + +#ignore + @MapFeature.Require(SUPPORTS_PUT) +#endignore + public void testPutIfAbsent_replaceableDefaultValue() { + getMap().putIfAbsent(k3(), getMap().getDefaultReturnValue()); + assertEquals("get(present) should return defaultValue value", getMap().getDefaultReturnValue(), get(k3())); + getMap().putIfAbsent(k3(), v3()); + assertEquals("get(present) value should have been replaced", v3(), get(k3())); + } + #ignore @MapFeature.Require(absent = SUPPORTS_PUT) #endignore