mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 04:50:29 +00:00
Merge pull request #4186 from Feoramund/fix-4177
Return false if `Small_Array` can't append multiple elements
This commit is contained in:
@@ -139,9 +139,13 @@ clear :: proc "contextless" (a: ^$A/Small_Array($N, $T)) {
|
||||
resize(a, 0)
|
||||
}
|
||||
|
||||
push_back_elems :: proc "contextless" (a: ^$A/Small_Array($N, $T), items: ..T) {
|
||||
n := copy(a.data[a.len:], items[:])
|
||||
a.len += n
|
||||
push_back_elems :: proc "contextless" (a: ^$A/Small_Array($N, $T), items: ..T) -> bool {
|
||||
if a.len + builtin.len(items) <= cap(a^) {
|
||||
n := copy(a.data[a.len:], items[:])
|
||||
a.len += n
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
inject_at :: proc "contextless" (a: ^$A/Small_Array($N, $T), item: T, index: int) -> bool #no_bounds_check {
|
||||
|
||||
@@ -35,6 +35,25 @@ test_small_array_inject_at :: proc(t: ^testing.T) {
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 0, 0, 1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 0 }))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_small_array_push_back_elems :: proc(t: ^testing.T) {
|
||||
array: small_array.Small_Array(2, int)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { }))
|
||||
testing.expect(t, small_array.append(&array, 0), "Expected to be able to append to empty small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 0 }))
|
||||
testing.expect(t, small_array.append(&array, 1, 2) == false, "Expected to fail appending multiple elements beyond capacity of small array")
|
||||
testing.expect(t, small_array.append(&array, 1), "Expected to be able to append to small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 0, 1 }))
|
||||
testing.expect(t, small_array.append(&array, 1) == false, "Expected to fail appending to full small array")
|
||||
testing.expect(t, small_array.append(&array, 1, 2) == false, "Expected to fail appending multiple elements to full small array")
|
||||
small_array.clear(&array)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { }))
|
||||
testing.expect(t, small_array.append(&array, 1, 2, 3) == false, "Expected to fail appending multiple elements to empty small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { }))
|
||||
testing.expect(t, small_array.append(&array, 1, 2), "Expected to be able to append multiple elements to empty small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 1, 2 }))
|
||||
}
|
||||
|
||||
slice_equal :: proc(a, b: []int) -> bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user