Skip to content

Commit

Permalink
Improve swap copy testing and add new failing copy test
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jun 30, 2023
1 parent 264b8de commit 996ffc2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
20 changes: 0 additions & 20 deletions inputs/failing/copyCounting/arraySwap.ts

This file was deleted.

29 changes: 29 additions & 0 deletions inputs/failing/copyCounting/externalMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! test_output(1)
// Should be: 0

/// <reference path="../../../concept-code/vs.d.ts" />

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<T>(x: T[], value: T) {
x.push(value);
return x;
}

function echo<T>(x: T) {
return x;
}
28 changes: 28 additions & 0 deletions inputs/passing/copyCounting/arraySwap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! test_output(0)

/// <reference path="../../../concept-code/vs.d.ts" />

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;
}

0 comments on commit 996ffc2

Please sign in to comment.