|
| 1 | +function assert(condition, message) { |
| 2 | + if (!condition) |
| 3 | + throw new Error(message || "Assertion failed"); |
| 4 | +} |
| 5 | + |
| 6 | +// Test array allocation with closure capture scenarios |
| 7 | + |
| 8 | +// Function that creates a closure value |
| 9 | +function createClosureValue(multiplier) { |
| 10 | + return function(x) { |
| 11 | + return x * multiplier; |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +// Function that allocates an array with fixed size |
| 16 | +function allocateArrayForClosure(baseValue) { |
| 17 | + const arr = new Array(5); |
| 18 | + for (let i = 0; i < 5; i++) { |
| 19 | + arr[i] = baseValue + i; |
| 20 | + } |
| 21 | + return arr; |
| 22 | +} |
| 23 | + |
| 24 | +// Function that uses array and captures closure |
| 25 | +function processWithClosure(closureFn, arr) { |
| 26 | + let result = 0; |
| 27 | + for (let i = 0; i < arr.length; i++) { |
| 28 | + result += closureFn(arr[i]); |
| 29 | + } |
| 30 | + return result; |
| 31 | +} |
| 32 | + |
| 33 | +// Complex scenario: array allocation, closure capture, and usage all in different functions |
| 34 | +function complexClosureTest(multiplier, baseValue) { |
| 35 | + const closureFn = createClosureValue(multiplier); |
| 36 | + const arr = allocateArrayForClosure(baseValue); |
| 37 | + return processWithClosure(closureFn, arr); |
| 38 | +} |
| 39 | + |
| 40 | +// Scenario where closure modifies array behavior |
| 41 | +function closureModifiesArray(capturedValue) { |
| 42 | + return function(baseValue) { |
| 43 | + const arr = new Array(4); |
| 44 | + for (let i = 0; i < 4; i++) { |
| 45 | + arr[i] = (baseValue + i) + capturedValue; |
| 46 | + } |
| 47 | + |
| 48 | + // Array usage within the closure |
| 49 | + let sum = 0; |
| 50 | + for (let i = 0; i < arr.length; i++) { |
| 51 | + sum += arr[i] * 2; |
| 52 | + } |
| 53 | + return sum; |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +// Nested closure with array |
| 58 | +function nestedClosureWithArray(outerValue) { |
| 59 | + return function(middleValue) { |
| 60 | + return function(baseValue) { |
| 61 | + const arr = new Array(3); |
| 62 | + for (let i = 0; i < 3; i++) { |
| 63 | + arr[i] = baseValue + i + outerValue + middleValue; |
| 64 | + } |
| 65 | + |
| 66 | + // Process array using captured values |
| 67 | + let product = 1; |
| 68 | + for (let i = 0; i < arr.length; i++) { |
| 69 | + product *= (arr[i] % 10) || 1; // avoid zero |
| 70 | + } |
| 71 | + return product; |
| 72 | + }; |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +let escapedArray = null; |
| 77 | +// Array that sometimes escapes through closure |
| 78 | +function conditionalEscapeViaClosure(shouldEscape) { |
| 79 | + |
| 80 | + return function(baseValue) { |
| 81 | + const arr = new Array(4); |
| 82 | + for (let i = 0; i < 4; i++) { |
| 83 | + arr[i] = baseValue + i * 2; |
| 84 | + } |
| 85 | + |
| 86 | + if (shouldEscape) { |
| 87 | + escapedArray = arr; |
| 88 | + return arr.length; |
| 89 | + } else { |
| 90 | + // Use array locally |
| 91 | + let sum = 0; |
| 92 | + for (let i = 0; i < arr.length; i++) { |
| 93 | + sum += arr[i]; |
| 94 | + } |
| 95 | + return sum; |
| 96 | + } |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +// Closure that captures array from outer scope |
| 101 | +function captureArrayInClosure(baseValue) { |
| 102 | + const arr = new Array(6); |
| 103 | + for (let i = 0; i < 6; i++) { |
| 104 | + arr[i] = baseValue + i; |
| 105 | + } |
| 106 | + |
| 107 | + // Return closure that uses the captured array |
| 108 | + return function(multiplier) { |
| 109 | + let result = 0; |
| 110 | + for (let i = 0; i < arr.length; i++) { |
| 111 | + result += arr[i] * multiplier; |
| 112 | + } |
| 113 | + return result; |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +// Multiple arrays with closure interactions |
| 118 | +function multiArrayClosure(factor1, factor2) { |
| 119 | + return function(baseValue) { |
| 120 | + const arr1 = new Array(3); |
| 121 | + const arr2 = new Array(3); |
| 122 | + |
| 123 | + for (let i = 0; i < 3; i++) { |
| 124 | + arr1[i] = (baseValue + i) * factor1; |
| 125 | + arr2[i] = (baseValue + i) * factor2; |
| 126 | + } |
| 127 | + |
| 128 | + // Combine arrays using closure variables |
| 129 | + let combined = 0; |
| 130 | + for (let i = 0; i < 3; i++) { |
| 131 | + combined += arr1[i] + arr2[i]; |
| 132 | + } |
| 133 | + return combined; |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +noInline(complexClosureTest); |
| 138 | +noInline(closureModifiesArray); |
| 139 | +noInline(nestedClosureWithArray); |
| 140 | +noInline(conditionalEscapeViaClosure); |
| 141 | +noInline(captureArrayInClosure); |
| 142 | +noInline(multiArrayClosure); |
| 143 | + |
| 144 | +// Run tests |
| 145 | +for (let i = 0; i < testLoopCount; i++) { |
| 146 | + const baseValue = i % 8; |
| 147 | + const multiplier = 1 + (i % 3); // multipliers 1-3 |
| 148 | + |
| 149 | + // Test complex closure scenario (array size 5) |
| 150 | + let result1 = complexClosureTest(multiplier, baseValue); |
| 151 | + let expected1 = 0; |
| 152 | + for (let j = 0; j < 5; j++) { |
| 153 | + expected1 += (baseValue + j) * multiplier; |
| 154 | + } |
| 155 | + assert(result1 === expected1, `complexClosureTest(${multiplier}, ${baseValue}) failed: got ${result1}, expected ${expected1}`); |
| 156 | + |
| 157 | + // Test closure that modifies array behavior (array size 4) |
| 158 | + const closureFn = closureModifiesArray(i % 5); |
| 159 | + let result2 = closureFn(baseValue); |
| 160 | + let expected2 = 0; |
| 161 | + for (let j = 0; j < 4; j++) { |
| 162 | + expected2 += (baseValue + j + (i % 5)) * 2; |
| 163 | + } |
| 164 | + assert(result2 === expected2, `closureModifiesArray test failed: got ${result2}, expected ${expected2}`); |
| 165 | + |
| 166 | + // Test nested closure (array size 3) |
| 167 | + const nestedFn = nestedClosureWithArray(i % 3)(i % 4); |
| 168 | + let result3 = nestedFn(baseValue); |
| 169 | + let expected3 = 1; |
| 170 | + for (let j = 0; j < 3; j++) { |
| 171 | + let val = (baseValue + j + (i % 3) + (i % 4)) % 10; |
| 172 | + if (val !== 0) expected3 *= val; |
| 173 | + } |
| 174 | + assert(result3 === expected3, `nestedClosureWithArray test failed: got ${result3}, expected ${expected3}`); |
| 175 | + |
| 176 | + // Test conditional escape via closure - non-escaping case (array size 4) |
| 177 | + const condEscapeFn = conditionalEscapeViaClosure(false); |
| 178 | + let result4 = condEscapeFn(baseValue); |
| 179 | + let expected4 = 0; |
| 180 | + for (let j = 0; j < 4; j++) { |
| 181 | + expected4 += baseValue + j * 2; |
| 182 | + } |
| 183 | + assert(result4 === expected4, `conditionalEscapeViaClosure(false) test failed: got ${result4}, expected ${expected4}`); |
| 184 | + |
| 185 | + // Test conditional escape via closure - escaping case (array size 4) |
| 186 | + const condEscapeFn2 = conditionalEscapeViaClosure(true); |
| 187 | + let result5 = condEscapeFn2(baseValue); |
| 188 | + assert(result5 === 4, `conditionalEscapeViaClosure(true) test failed: got ${result5}, expected 4`); |
| 189 | + |
| 190 | + // Test capture array in closure (array size 6) |
| 191 | + const capturedFn = captureArrayInClosure(baseValue); |
| 192 | + let result6 = capturedFn(multiplier); |
| 193 | + let expected6 = 0; |
| 194 | + for (let j = 0; j < 6; j++) { |
| 195 | + expected6 += (baseValue + j) * multiplier; |
| 196 | + } |
| 197 | + assert(result6 === expected6, `captureArrayInClosure test failed: got ${result6}, expected ${expected6}`); |
| 198 | + |
| 199 | + // Test multiple arrays with closure (arrays size 3 each) |
| 200 | + const multiArrayFn = multiArrayClosure(2, 3); |
| 201 | + let result7 = multiArrayFn(baseValue); |
| 202 | + let expected7 = 0; |
| 203 | + for (let j = 0; j < 3; j++) { |
| 204 | + expected7 += (baseValue + j) * 2 + (baseValue + j) * 3; // factor1 + factor2 |
| 205 | + } |
| 206 | + assert(result7 === expected7, `multiArrayClosure test failed: got ${result7}, expected ${expected7}`); |
| 207 | +} |
0 commit comments