From 53f124e26657dbe2a54965262c5966aa036cb06c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 4 Dec 2021 14:20:25 -0600 Subject: [PATCH] added test case for implicit initialization --- core/container/array.onyx | 1 + core/container/map.onyx | 2 ++ tests/implicit_initialize_locals | 40 +++++++++++++++++++++++++++ tests/implicit_initialize_locals.onyx | 23 +++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 tests/implicit_initialize_locals create mode 100644 tests/implicit_initialize_locals.onyx diff --git a/core/container/array.onyx b/core/container/array.onyx index 5f8d5239..d3be4b17 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -59,6 +59,7 @@ clear :: (arr: ^[..] $T) { ensure_capacity :: (arr: ^[..] $T, capacity: u32) -> bool { if arr.capacity >= capacity do return true; + if arr.data == null do init(arr); while capacity > arr.capacity do arr.capacity <<= 1; new_data := raw_resize(arr.allocator, arr.data, sizeof T * arr.capacity); diff --git a/core/container/map.onyx b/core/container/map.onyx index a6604fa2..76538438 100644 --- a/core/container/map.onyx +++ b/core/container/map.onyx @@ -69,6 +69,7 @@ free :: (use map: ^Map($K, $V)) { } put :: (use map: ^Map($K, $V), key: K, value: V) { + if map.hashes.data == null do init(map); lr := lookup(map, key); if lr.entry_index >= 0 { @@ -155,6 +156,7 @@ empty :: (use map: ^Map($K, $V)) -> bool { } lookup :: (use map: ^Map($K, $V), key: K) -> MapLookupResult { + if hashes.data == null do init(map); lr := MapLookupResult.{}; hash_value: u32 = hash.to_u32(key); // You cannot use this type for the key, unless you add an overload. diff --git a/tests/implicit_initialize_locals b/tests/implicit_initialize_locals new file mode 100644 index 00000000..d7800aa3 --- /dev/null +++ b/tests/implicit_initialize_locals @@ -0,0 +1,40 @@ +data: 0x0 count: 0 +data: 0x100 count: 100 +Untyped_Array { + data = (null), + count = 0, + capacity = 0, + allocator = Allocator { + data = (null), + func = func[0] + } +} +Map([] u8, i32) { + allocator = Allocator { + data = 0xB750, + func = func[38] + }, + hashes = [ + -1, + -1, + -1, + 1, + -1, + -1, + -1, + -1 + ], + entries = [ + Map.Entry([] u8, i32) { + next = -1, + key = "Joe", + value = 12 + }, + Map.Entry([] u8, i32) { + next = 0, + key = "Jane", + value = 34 + } + ], + default_value = 0 +} diff --git a/tests/implicit_initialize_locals.onyx b/tests/implicit_initialize_locals.onyx new file mode 100644 index 00000000..75b6987c --- /dev/null +++ b/tests/implicit_initialize_locals.onyx @@ -0,0 +1,23 @@ +#load "core/std" + +use package core + +main :: (args) => { + { + x: [] i32; + printf("data: {} count: {}\n", x.data, x.count); + + y := ([] i32).{ ~~ 0x100, 100 }; + printf("data: {} count: {}\n", y.data, y.count); + } + + { + arr: [..] i32; + printf("{*p}\n", cast(^array.Untyped_Array) ^arr); + + people: Map(str, i32); + people["Joe"] = 12; + people["Jane"] = 34; + printf("{*p}\n", ^people); + } +} \ No newline at end of file -- 2.25.1