diff --git a/inputs/failing/copyCounting/arraySwap.ts b/inputs/failing/copyCounting/arraySwap.ts deleted file mode 100644 index 8abe734a..00000000 --- a/inputs/failing/copyCounting/arraySwap.ts +++ /dev/null @@ -1,20 +0,0 @@ -//! test_output(2) -// Should be: 0 - -/// - -export default function main() { - return measure(true) - measure(false); -} - -function measure(swap: boolean) { - const x = Debug.makeCopyCounter("x"); - - let arr = [x, "y", "z"]; - - if (swap) { - [arr[1], arr[2]] = [arr[2], arr[1]]; - } - - return x.count; -} diff --git a/inputs/failing/copyCounting/externalMethod.ts b/inputs/failing/copyCounting/externalMethod.ts new file mode 100644 index 00000000..2a6fa429 --- /dev/null +++ b/inputs/failing/copyCounting/externalMethod.ts @@ -0,0 +1,29 @@ +//! test_output(1) +// Should be: 0 + +/// + +export default function main() { + return measure(true) - measure(false); +} + +function measure(doPush: boolean) { + const x = Debug.makeCopyCounter("x"); + + let arr: unknown[] = echo([x]); + + if (doPush) { + arr = push(arr, "y"); + } + + return x.count; +} + +function push(x: T[], value: T) { + x.push(value); + return x; +} + +function echo(x: T) { + return x; +} diff --git a/inputs/passing/copyCounting/arraySwap.ts b/inputs/passing/copyCounting/arraySwap.ts new file mode 100644 index 00000000..da1d6d9f --- /dev/null +++ b/inputs/passing/copyCounting/arraySwap.ts @@ -0,0 +1,28 @@ +//! test_output(0) + +/// + +export default function main() { + return measure(true) - measure(false); +} + +function measure(doSwap: boolean) { + const x = Debug.makeCopyCounter("x"); + + let arr: unknown[] = [x, "y", "z"]; + arr = swapFn(arr, 1, 2, doSwap); + + return len(arr) + x.count; +} + +function swapFn(arr: unknown[], i: number, j: number, doSwap: boolean) { + if (doSwap) { + [arr[i], arr[j]] = [arr[j], arr[i]]; + } + + return arr; +} + +function len(arr: unknown[]) { + return arr.length; +}