Align behavoir of collection constructors across platforms #KT-59192

The initialCapacity should be non-negative and loadFactor should be positive.

As a part of efforts to stabilize Native stdlib.
This commit is contained in:
Abduqodiri Qurbonzoda
2023-06-08 15:25:07 +03:00
committed by Space Team
parent 0bef1533e2
commit 830c78773c
9 changed files with 91 additions and 7 deletions
@@ -36,7 +36,9 @@ public actual open class ArrayList<E> internal constructor(private var array: Ar
*
* @throws IllegalArgumentException if [initialCapacity] is negative.
*/
public actual constructor(initialCapacity: Int) : this(emptyArray()) {}
public actual constructor(initialCapacity: Int) : this(emptyArray()) {
require(initialCapacity >= 0) { "Negative initial capacity: $initialCapacity" }
}
/**
* Creates a new [ArrayList] filled with the elements of the specified collection.
@@ -93,7 +93,7 @@ public actual open class HashMap<K, V> : AbstractMutableMap<K, V>, MutableMap<K,
*
* @throws IllegalArgumentException if [initialCapacity] is negative.
*/
actual constructor(initialCapacity: Int) : this(initialCapacity, 0.0f)
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
/**
@@ -63,7 +63,7 @@ public actual open class HashSet<E> : AbstractMutableSet<E>, MutableSet<E> {
*
* @throws IllegalArgumentException if [initialCapacity] is negative.
*/
actual constructor(initialCapacity: Int) : this(initialCapacity, 0.0f)
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
/**
* Protected constructor to specify the underlying map. This is used by
@@ -211,7 +211,7 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
*
* @throws IllegalArgumentException if [initialCapacity] is negative.
*/
actual constructor(initialCapacity: Int) : this(initialCapacity, 0.0f)
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
/**
* Creates a new [LinkedHashMap] filled with the contents of the specified [original] map.
@@ -65,7 +65,7 @@ public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
*
* @throws IllegalArgumentException if [initialCapacity] is negative.
*/
actual constructor(initialCapacity: Int) : this(initialCapacity, 0.0f)
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
@PublishedApi
internal fun build(): Set<E> {