Skip to content

Instantly share code, notes, and snippets.

@wf9a5m75
Created June 18, 2022 16:19
Show Gist options
  • Save wf9a5m75/f941a09362e58661da2dcdde994f5fc2 to your computer and use it in GitHub Desktop.
Save wf9a5m75/f941a09362e58661da2dcdde994f5fc2 to your computer and use it in GitHub Desktop.
// --------------------------------------------------------------------------
// Following code is for utilities
// --------------------------------------------------------------------------
/**
* @params zeroShift {function} The function you want to test
* @params debug {boolean} Sets true for including the function result
*/
const checkTests = (targetFunction, debug) => {
// --------------------------
// Generates test cases
// --------------------------
const tests = [];
tests.push({
name: 'test1',
src: [1, 0, 2, 3, 0, 4, 5, 0],
expect: [1, 0, 0, 2, 3, 0, 0, 4]
});
tests.push({
name: 'test2',
src: [1, 2, 3],
expect: [1, 2, 3]
});
tests.push(createTest('test3', 10));
tests.push(createTest('test4', 100));
tests.push(createTest('test5', 1000));
tests.push(createTest('test6', 10000));
// tests.push(createTest('test7', 100000));
// --------------------------
// Do tests
// --------------------------
const results = tests.map((test) => {
let src = test.src;
// Do the test
const startTime = performance.now();
const result = targetFunction(src, debug);
const endTime = performance.now();
// Verification
let pass = Array.isArray(result) && (result.length === test.expect.length);
if (pass) {
const size = result.length;
let i = 0;
while(pass && (i < size)) {
pass = (result[i] === test.expect[i]);
i++;
}
}
let report = {
name: test.name,
pass,
time: endTime - startTime
};
if (debug) {
report.result = result;
}
return report;
});
// --------------------------
// Output
// --------------------------
let totalTime = 0;
results.forEach((result) => {
console.log(result);
totalTime += result.time;
});
console.log(`total = ${(totalTime).toFixed(3)} msec`)
};
/**
* Create a test object, which contains the src and the expect properties.
*
* @params testName {string} Test name
* @params arrLen {number} Number of array length
*
* @returns {object} Test case
*/
const createTest = (testName, arrLen) => {
let src = [];
let expect = [];
for (let index = 0; index < arrLen; index++) {
let val = Math.floor(Math.random() * 10);
src.push(val);
expect.push(val);
if (val === 0) {
expect.push(0)
}
}
expect.length = arrLen;
return {
name: testName,
src: src,
expect
};
};
// --------------------------------------------------------------------------
// Following code is the user code
// --------------------------------------------------------------------------
const zeroShift = (srcArray, debug) => {
for (let index = 0; index < srcArray.length; index++) {
const element = srcArray[index];
if (element === 0) {
srcArray = srcArray
.slice(0, index + 1)
.concat(srcArray.slice(index, srcArray.length - 1));
index++;
}
}
return srcArray;
};
checkTests(zeroShift, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment