added test case for implicit initialization
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 4 Dec 2021 20:20:25 +0000 (14:20 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 4 Dec 2021 20:20:25 +0000 (14:20 -0600)
core/container/array.onyx
core/container/map.onyx
tests/implicit_initialize_locals [new file with mode: 0644]
tests/implicit_initialize_locals.onyx [new file with mode: 0644]

index 5f8d52397d09935bdc80c7200fc67d4adc7ec1bb..d3be4b1706f33d7741cd2a2881f23f7d35cc57f6 100644 (file)
@@ -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);
index a6604fa2a5607fc3bfec00cfc25fa978fdbd5685..765384385331493782adaa1c5976ccfe13b604d2 100644 (file)
@@ -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 (file)
index 0000000..d7800aa
--- /dev/null
@@ -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 (file)
index 0000000..75b6987
--- /dev/null
@@ -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