From 39e6ade1421ef8e3c19e24fddaec270789f515cf Mon Sep 17 00:00:00 2001 From: hppeng Date: Fri, 24 Jun 2022 06:06:24 -0700 Subject: [PATCH 1/4] HOTFIX: Patch dps_vis --- js/dps_vis.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/dps_vis.js b/js/dps_vis.js index 85386b0..2a7828d 100644 --- a/js/dps_vis.js +++ b/js/dps_vis.js @@ -213,14 +213,14 @@ function redraw(data) { let tier_mod = tiers_mod.get(tier); let y_max = baseline_y.map(x => 2.1*x*tier_mod*type_mod); let y_min = baseline_y.map(x => 2.0*x*tier_mod*type_mod); - line_top.datum(zip(baseline_x, y_max)) + line_top.datum(zip2(baseline_x, y_max)) .attr("fill", "none") .attr("stroke", d => colorMap.get(tier)) .attr("d", d3.line() .x(function(d) { return x(d[0]) }) .y(function(d) { return y(d[1]) }) ) - line_bot.datum(zip(baseline_x, y_min)) + line_bot.datum(zip2(baseline_x, y_min)) .attr("fill", "none") .attr("stroke", d => colorMap.get(tier)) .attr("d", d3.line() From 17311ff3b19f07f1a751283ebd25340ff2e4c1ac Mon Sep 17 00:00:00 2001 From: ferricles Date: Fri, 24 Jun 2022 19:19:15 -0700 Subject: [PATCH 2/4] first batch of generic assert tests --- js/utils.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/js/utils.js b/js/utils.js index fed7df8..a14afff 100644 --- a/js/utils.js +++ b/js/utils.js @@ -412,4 +412,101 @@ async function hardReload() { function capitalizeFirst(str) { return str[0].toUpperCase() + str.substring(1); -} \ No newline at end of file +} + +/** https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript + * If we ever want to write something that needs to import other js files + */ + const getScript = url => new Promise((resolve, reject) => { + const script = document.createElement('script') + script.src = url + script.async = true + + script.onerror = reject + + script.onload = script.onreadystatechange = function() { + const loadState = this.readyState + + if (loadState && loadState !== 'loaded' && loadState !== 'complete') return + + script.onload = script.onreadystatechange = null + + resolve() + } + + document.head.appendChild(script) + }) + +/* +GENERIC TEST FUNCTIONS +*/ +/** The generic assert function. Fails on all "false-y" values. Useful for non-object equality checks, boolean value checks, and existence checks. + * + * @param {*} arg - argument to assert. + * @param {String} msg - the error message to throw. + */ + function assert(arg, msg) { + if (!arg) { + throw new Error(msg ? msg : "Assert failed."); + } +} + +/** Asserts object equality of the 2 parameters. For loose and strict asserts, use assert(). + * + * @param {*} arg1 - first argument to compare. + * @param {*} arg2 - second argument to compare. + * @param {String} msg - the error message to throw. + */ +function assert_equals(arg1, arg2, msg) { + if (!Object.is(arg1, arg2)) { + throw new Error(msg ? msg : "Assert Equals failed. " + arg1 + " is not " + arg2 + "."); + } +} + +/** Asserts object inequality of the 2 parameters. For loose and strict asserts, use assert(). + * + * @param {*} arg1 - first argument to compare. + * @param {*} arg2 - second argument to compare. + * @param {String} msg - the error message to throw. + */ + function assert_not_equals(arg1, arg2, msg) { + if (Object.is(arg1, arg2)) { + throw new Error(msg ? msg : "Assert Not Equals failed. " + arg1 + " is " + arg2 + "."); + } +} + +/** Asserts proximity between 2 arguments. Should be used for any floating point datatype. + * + * @param {*} arg1 - first argument to compare. + * @param {*} arg2 - second argument to compare. + * @param {Number} epsilon - the margin of error (<= del difference is ok). + * @param {String} msg - the error message to throw. + */ +function assert_near(arg1, arg2, epsilon, msg) { + if (Math.abs(arg1 - arg2) > epsilon) { + throw new Error(msg ? msg : "Assert Near failed. " + arg1 + " is not within " + epsilon + " of " + arg2 + "."); + } +} + +/** Asserts that the input argument is null. + * + * @param {*} arg - the argument to test for null. + * @param {String} msg - the error message to throw. + */ +function assert_null(arg, msg) { + if (arg !== null) { + throw new Error(msg ? msg : "Assert Near failed. " + arg + " is not null."); + } +} + +/** Asserts that the input argument is undefined. + * + * @param {*} arg - the argument to test for undefined. + * @param {String} msg - the error message to throw. + */ + function assert_undefined(arg, msg) { + if (arg !== undefined) { + throw new Error(msg ? msg : "Assert Near failed. " + arg + " is not undefined."); + } +} + From 61dfe2de65f4afa067eb37b2a24b8d73448972a1 Mon Sep 17 00:00:00 2001 From: ferricles Date: Fri, 24 Jun 2022 21:01:54 -0700 Subject: [PATCH 3/4] assert error --- js/utils.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/js/utils.js b/js/utils.js index a14afff..f3352fd 100644 --- a/js/utils.js +++ b/js/utils.js @@ -510,3 +510,16 @@ function assert_null(arg, msg) { } } +/** Asserts that there is an error when a callback function is run. + * + * @param {Function} func_binding - a function binding to run. Can be passed in with func.bind(null, arg1, ..., argn) + * @param {String} msg - the error message to throw. + */ +function assert_error(func_binding, msg) { + try { + func_binding(); + console.trace(msg ? msg : "Function didn't throw an error."); + } catch (err) { + return; + } +} From 8805521ce257d523a6feb1742c40e50cf8cdd961 Mon Sep 17 00:00:00 2001 From: ferricles Date: Fri, 24 Jun 2022 21:24:22 -0700 Subject: [PATCH 4/4] assert error change for error thrown, formatted getScript, added default epsilon for assert near --- js/utils.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/js/utils.js b/js/utils.js index f3352fd..7efdd21 100644 --- a/js/utils.js +++ b/js/utils.js @@ -417,25 +417,25 @@ function capitalizeFirst(str) { /** https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript * If we ever want to write something that needs to import other js files */ - const getScript = url => new Promise((resolve, reject) => { - const script = document.createElement('script') - script.src = url - script.async = true - - script.onerror = reject - - script.onload = script.onreadystatechange = function() { - const loadState = this.readyState - - if (loadState && loadState !== 'loaded' && loadState !== 'complete') return - - script.onload = script.onreadystatechange = null - - resolve() +const getScript = url => new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.src = url; + script.async = true; + + script.onerror = reject; + + script.onload = script.onreadystatechange = function () { + const loadState = this.readyState; + + if (loadState && loadState !== 'loaded' && loadState !== 'complete') return + + script.onload = script.onreadystatechange = null; + + resolve(); } - - document.head.appendChild(script) - }) + + document.head.appendChild(script); +}) /* GENERIC TEST FUNCTIONS @@ -479,10 +479,10 @@ function assert_equals(arg1, arg2, msg) { * * @param {*} arg1 - first argument to compare. * @param {*} arg2 - second argument to compare. - * @param {Number} epsilon - the margin of error (<= del difference is ok). + * @param {Number} epsilon - the margin of error (<= del difference is ok). Defaults to -1E5. * @param {String} msg - the error message to throw. */ -function assert_near(arg1, arg2, epsilon, msg) { +function assert_near(arg1, arg2, epsilon = 1E-5, msg) { if (Math.abs(arg1 - arg2) > epsilon) { throw new Error(msg ? msg : "Assert Near failed. " + arg1 + " is not within " + epsilon + " of " + arg2 + "."); } @@ -518,8 +518,8 @@ function assert_null(arg, msg) { function assert_error(func_binding, msg) { try { func_binding(); - console.trace(msg ? msg : "Function didn't throw an error."); } catch (err) { return; } + throw new Error(msg ? msg : "Function didn't throw an error."); }