diff --git a/std/src/std/array.inko b/std/src/std/array.inko index 3991a70fa..a6af4aac7 100644 --- a/std/src/std/array.inko +++ b/std/src/std/array.inko @@ -117,11 +117,17 @@ class builtin Array[T] { # Returns a new `Array` with enough space for at least `amount` values. # # The actual capacity may be greater than the given value. - fn pub static with_capacity(amount: Int) -> Array[T] { + # + # # Panics + # + # This method panics of `size` if less than zero. + fn pub static with_capacity(size: Int) -> Array[T] { + if size < 0 { panic('The capacity must be greater than or equal to zero') } + let buffer = alloc_imp - .resize(0x0 as Pointer[UInt8], size: amount * VALUE_SIZE) + .resize(0x0 as Pointer[UInt8], size: size * VALUE_SIZE) - Array { @size = 0, @capacity = amount, @buffer = buffer } + Array { @size = 0, @capacity = size, @buffer = buffer } } # Returns an array filled with a certain amount of values. diff --git a/std/test/std/test_array.inko b/std/test/std/test_array.inko index c6c3880a3..d2a889cf4 100644 --- a/std/test/std/test_array.inko +++ b/std/test/std/test_array.inko @@ -46,6 +46,10 @@ fn pub tests(t: mut Tests) { t.equal(ary2.capacity, 2) } + t.panic('Array.with_capacity with an invalid size') fn { + Array.with_capacity(-4) as Array[Int] + } + t.test('Array.filled') fn (t) { t.equal(Array.filled(with: 1, times: 0), []) t.equal(Array.filled(with: 1, times: 1), [1])