diff --git a/OMCompiler/Compiler/Util/ExpandableArray.mo b/OMCompiler/Compiler/Util/ExpandableArray.mo index fe6b2e77ac1..450098be8dd 100644 --- a/OMCompiler/Compiler/Util/ExpandableArray.mo +++ b/OMCompiler/Compiler/Util/ExpandableArray.mo @@ -149,7 +149,13 @@ protected algorithm if index > 0 and (index > capacity or isNone(Dangerous.arrayGetNoBoundsChecking(data, index))) then if index > capacity then - expandToSize(index, exarray); + capacity := max(capacity, 1); + + while index > capacity loop + capacity := capacity * 2; + end while; + + expandToSize(capacity, exarray); data := Dangerous.arrayGetNoBoundsChecking(exarray.data, 1); end if; diff --git a/testsuite/openmodelica/bootstrapping/ExpandableArrayTest.mos b/testsuite/openmodelica/bootstrapping/ExpandableArrayTest.mos index 0041e60fc77..4e59f1b8114 100644 --- a/testsuite/openmodelica/bootstrapping/ExpandableArrayTest.mos +++ b/testsuite/openmodelica/bootstrapping/ExpandableArrayTest.mos @@ -85,7 +85,7 @@ ExpandableArrayTest.Test(); getErrorString(); // set(2, 2) (1/2) // ======================================== // 2: 2 -// add(1) (2/3) +// add(1) (2/4) // ======================================== // 2: 2 // 3: 1 @@ -94,13 +94,13 @@ ExpandableArrayTest.Test(); getErrorString(); // 2: 2 // 3: 1 // 4: 3 -// set(5, 6) (4/5) +// set(5, 6) (4/8) // ======================================== // 2: 2 // 3: 1 // 4: 3 // 5: 6 -// update(5, 5) (4/5) +// update(5, 5) (4/8) // ======================================== // 2: 2 // 3: 1 @@ -112,14 +112,14 @@ ExpandableArrayTest.Test(); getErrorString(); // 2: 2 // 3: 1 // 4: 3 -// set(6, 6) (5/6) +// set(6, 6) (5/8) // ======================================== // 1: 5 // 2: 2 // 3: 1 // 4: 3 // 6: 6 -// clear() (0/6) +// clear() (0/8) // ======================================== // // shrink() (0/0) @@ -128,10 +128,10 @@ ExpandableArrayTest.Test(); getErrorString(); // shrink() (0/0) // ======================================== // -// set(7, 1) (1/7) +// set(7, 1) (1/8) // ======================================== // 7: 1 -// delete(7) (0/7) +// delete(7) (0/8) // ======================================== // // shrink() (0/0)