Skip to content

Commit 64783be

Browse files
committed
[stdlib] Add test for InlineArray (sizeof[T] by capacity).
Hello, this PR adds a small test for `InlineArray`: ```mojo (sizeof[ElementType]() * capacity) == sizeof[self._array]() ``` Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
1 parent 89cfffc commit 64783be

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

mojo/stdlib/test/collections/test_inline_array.mojo

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ from memory import UnsafePointer
1818
from memory.maybe_uninitialized import UnsafeMaybeUninitialized
1919
from test_utils import DelRecorder
2020
from testing import assert_equal, assert_false, assert_true
21+
from sys.info import sizeof
2122

2223

2324
def test_array_unsafe_get():
@@ -225,6 +226,25 @@ fn test_unsafe_ptr() raises:
225226
assert_equal(arr[i], ptr[i])
226227

227228

229+
def test_sizeof_array[
230+
current_type: CollectionElement, capacity: Int
231+
](fill_value: current_type):
232+
"""Testing if `sizeof` the array equals capacity * `sizeof` current_type.
233+
234+
Args:
235+
fill_value: The value to initialize the array with.
236+
237+
Parameters:
238+
current_type: The type of the elements of the `InlineList`.
239+
capacity: The capacity of the `InlineList`.
240+
"""
241+
alias size_of_current_type = sizeof[current_type]()
242+
var arr = InlineArray[current_type, capacity](fill=fill_value)
243+
assert_equal(
244+
sizeof[__type_of(arr._array)](), capacity * size_of_current_type
245+
)
246+
247+
228248
def main():
229249
test_array_unsafe_get()
230250
test_array_int()
@@ -234,3 +254,4 @@ def main():
234254
test_array_contains()
235255
test_inline_array_runs_destructors()
236256
test_unsafe_ptr()
257+
test_sizeof_array[Int, capacity=32](fill_value=0)

0 commit comments

Comments
 (0)