Best JavaScript code snippet using wpt
test.ts
Source:test.ts
1// This is a set of tests for the type-inference algorithm applied to lambda Calculus and the Concatenative calculus.2// Running these tests require installation of the Myna parsing module. 3// TODO: reject recursion and jump out. 4// TODO: check identities5// TODO: from Cat to Lambda-cat (dup/pop/papply/dip)6// TODO: rewriting rules7// TODO: simplify the type presentation (real parseable version)8// TODO: make type presentation look like TypeScript types 9// TODO: make type presentation look like Haskell types10import { parseType } from "./type-parser";11import { parseCat, CatExpr, CatQuotation } from "./cat-parser";12import { catTypes, inferCatType } from "./cat-types";13import { uniquelyNameVars, getVarUsages, removeVars, makeVarsSingleUse } from "./cat-lambda";14import { parseRedex, freeVariables, etaConversion, Redex, lambdaLift } from "./lambda-calculus";15import { abstractionElimination, combinators, combinatorToLambdaCalculus } from "./combinatory-logic";16import { lambdaToPureCat, lambdaToCat } from "./lambda-calculus-to-cat";17import { catStdOps } from "./cat-library";18import { Type, normalizeVarNames, alphabetizeVarNames, isFunctionType, functionInput, TypeArray, TypeVariable, TypeConstant, functionOutput, typeToString } from "./type-system";19//==========================================================================================20// Testing helper functions 21export function runTest<TInput, TOutput>(testName:string, f:(TInput) => TOutput, input: TInput, output: TOutput=undefined, expectFail:boolean = false) {22 try {23 console.log("test " + testName);24 console.log(" input: " + input.toString());25 const result = f(input);26 console.log(" result: " + result.toString());27 const success = !result || (result && !expectFail || !result && expectFail);28 console.log(success ? " passed" : " FAILED"); 29 } 30 catch (e) {31 if (expectFail) {32 console.log(" passed: expected fail, error caught: " + e.message);33 }34 else {35 console.log(" FAILED: error caught = " + e.message);36 }37 }38}39export function catTypeToString(t:Type) : string {40 if (t instanceof TypeVariable) 41 return "'" + t.name;42 else if (t instanceof TypeArray) 43 return "(" + t.types.map(catTypeToString).join(" ") + ")";44 else 45 return t.toString();46}47export function compareTypes(t1:Type, t2:Type) {48 {49 var r1 = normalizeVarNames(t1).toString();50 var r2 = normalizeVarNames(t2).toString();51 if (r1 != r2) {52 throw new Error("Types are not the same when normalized: " + r1 + " and " + r2);53 }54 }55 {56 var r1 = alphabetizeVarNames(t1).toString();57 var r2 = alphabetizeVarNames(t2).toString();58 if (r1 != r2) {59 throw new Error("Types are not the same when alphabetized: " + r1 + " and " + r2);60 }61 } 62}63// Some identities 64// NOTE: the intuitionistic logic part is interesting. 65// https://en.wikipedia.org/wiki/B,_C,_K,_W_system#Connection_to_intuitionistic_logic66export const combinatorDefs = { 67 "B" : "S (K S) K",68 "C" : "S (B B S)(K K)",69 "D" : "B B",70 "E" : "B (B B B)",71 "F" : "E T T E T",72 "G" : "B B C",73 "H" : "B W (B C)",74 "I" : "S K K",75 "J" : "B (B C) (W (B C (B (B B B))))",76 "L" : "C B M",77 "M" : "S I I",78 "O" : "S I",79 "Q" : "C B",80 "R" : "B B T",81 "T" : "C I",82 "U" : "L O",83 "V" : "B C T",84 "W" : "C (B M R)",85 "Y" : "S L L", 86 "S" : "B (B W) (B B C)",87 "I2" : "W K",88 // TODO: test the arithmetic identities 89}90const catTests = [91 // Primitive forms 92 ["", "!t0.(t0 -> t0)"],93 // ["id", "!t0!t1.((t0 t1) -> (t0 t1))"],94 ["apply", "!t1.(!t0.((t0 -> t1) t0) -> t1)"],95 ["compose", "!t1!t2!t3.(!t0.((t0 -> t1) ((t2 -> t0) t3)) -> ((t2 -> t1) t3))"],96 ["quote", "!t0!t1.((t0 t1) -> (!t2.(t2 -> (t0 t2)) t1))"],97 ["dup", "!t0!t1.((t0 t1) -> (t0 (t0 t1)))"],98 ["swap", "!t0!t1!t2.((t0 (t1 t2)) -> (t1 (t0 t2)))"],99 ["pop", "!t1.(!t0.(t0 t1) -> t1)"],100 // Quotations of Primitives 101 ["[]", "!t0.(t0 -> (!t1.(t1 -> t1) t0))"],102 //["[id]", "!t0.(t0 -> (!t1!t2.((t1 t2) -> (t1 t2)) t0))"],103 ["[apply]", "!t0.(t0 -> (!t2.(!t1.((t1 -> t2) t1) -> t2) t0))"],104 ["[pop]", "!t0.(t0 -> (!t2.(!t1.(t1 t2) -> t2) t0))"],105 ["[dup]", "!t0.(t0 -> (!t1!t2.((t1 t2) -> (t1 (t1 t2))) t0))"],106 ["[compose]", "!t0.(t0 -> (!t2!t3!t4.(!t1.((t1 -> t2) ((t3 -> t1) t4)) -> ((t3 -> t2) t4)) t0))"],107 ["[quote]", "!t0.(t0 -> (!t1!t2.((t1 t2) -> (!t3.(t3 -> (t1 t3)) t2)) t0))"],108 ["[swap]", "!t0.(t0 -> (!t1!t2!t3.((t1 (t2 t3)) -> (t2 (t1 t3))) t0))"],109 // Basic composition tests110 ["[[pop]]", "!t0.(t0 -> (!t1.(t1 -> (!t3.(!t2.(t2 t3) -> t3) t1)) t0))"],111 ["[pop pop]", "!t0.(t0 -> (!t3.(!t1.(t1 !t2.(t2 t3)) -> t3) t0))"],112 ["pop []", "!t1.(!t0.(t0 t1) -> (!t2.(t2 -> t2) t1))"], 113 ["[] pop", "!t0.(t0 -> t0)"], 114 ["[pop []]", "!t0.(t0 -> (!t2.(!t1.(t1 t2) -> (!t3.(t3 -> t3) t2)) t0))"],115 ["[] []", "!t0.(t0 -> (!t1.(t1 -> t1) (!t2.(t2 -> t2) t0)))"],116 ["[] dup", "!t0.(t0 -> (!t1.(t1 -> t1) (!t2.(t2 -> t2) t0)))"], 117 ["[] dup dup", "!t0.(t0 -> (!t1.(t1 -> t1) (!t2.(t2 -> t2) (!t3.(t3 -> t3) t0))))"], 118 ["dup pop", "!t0!t1.((t0 t1) -> (t0 t1))"], 119 120 // Compositions of Primitives 121 ["apply apply", "!t2.(!t0.((t0 -> !t1.((t1 -> t2) t1)) t0) -> t2)"],122 ["apply compose", "!t2!t3!t4.(!t0.((t0 -> !t1.((t1 -> t2) ((t3 -> t1) t4))) t0) -> ((t3 -> t2) t4))"],123 ["apply quote", "!t1!t2.(!t0.((t0 -> (t1 t2)) t0) -> (!t3.(t3 -> (t1 t3)) t2))"],124 ["apply dup", "!t1!t2.(!t0.((t0 -> (t1 t2)) t0) -> (t1 (t1 t2)))"],125 ["apply swap", "!t1!t2!t3.(!t0.((t0 -> (t1 (t2 t3))) t0) -> (t2 (t1 t3)))"],126 ["apply pop", "!t2.(!t0.((t0 -> !t1.(t1 t2)) t0) -> t2)"],127 ["compose apply", "!t1.(!t0.((t0 -> t1) !t2.((t2 -> t0) t2)) -> t1)"],128 ["compose compose", "!t1!t3!t4.(!t0.((t0 -> t1) !t2.((t2 -> t0) ((t3 -> t2) t4))) -> ((t3 -> t1) t4))"],129 ["compose quote", "!t1!t2!t3.(!t0.((t0 -> t1) ((t2 -> t0) t3)) -> (!t4.(t4 -> ((t2 -> t1) t4)) t3))"],130 ["compose dup", "!t1!t2!t3.(!t0.((t0 -> t1) ((t2 -> t0) t3)) -> ((t2 -> t1) ((t2 -> t1) t3)))"],131 ["compose swap", "!t1!t2!t3!t4.(!t0.((t0 -> t1) ((t2 -> t0) (t3 t4))) -> (t3 ((t2 -> t1) t4)))"],132 ["compose pop", "!t3.(!t0.(!t1.(t0 -> t1) (!t2.(t2 -> t0) t3)) -> t3)"],133 ["quote apply", "!t0!t1.((t0 t1) -> (t0 t1))"],134 ["quote compose", "!t0!t1!t2!t3.((t0 ((t1 -> t2) t3)) -> ((t1 -> (t0 t2)) t3))"],135 ["quote quote", "!t0!t1.((t0 t1) -> (!t2.(t2 -> (!t3.(t3 -> (t0 t3)) t2)) t1))"],136 ["quote dup", "!t0!t1.((t0 t1) -> (!t2.(t2 -> (t0 t2)) (!t3.(t3 -> (t0 t3)) t1)))"], 137 ["quote swap", "!t0!t1!t2.((t0 (t1 t2)) -> (t1 (!t3.(t3 -> (t0 t3)) t2)))"],138 ["quote pop", "!t1.(!t0.(t0 t1) -> t1)"],139 ["dup apply", "!t1.(!t0.((((rec 1) t0) -> t1) t0) -> t1)"],140 ["dup compose", "!t0!t1.(((t0 -> t0) t1) -> ((t0 -> t0) t1))"],141 ["dup quote", "!t0!t1.((t0 t1) -> (!t2.(t2 -> (t0 t2)) (t0 t1)))"],142 ["dup dup", "!t0!t1.((t0 t1) -> (t0 (t0 (t0 t1))))"],143 ["dup swap", "!t0!t1.((t0 t1) -> (t0 (t0 t1)))"],144 ["dup pop", "!t0!t1.((t0 t1) -> (t0 t1))"],145 ["swap apply", "!t2.(!t0.(t0 !t1.(((t0 t1) -> t2) t1)) -> t2)"],146 ["swap compose", "!t0!t2!t3.(!t1.((t0 -> t1) ((t1 -> t2) t3)) -> ((t0 -> t2) t3))"],147 ["swap quote", "!t0!t1!t2.((t0 (t1 t2)) -> (!t3.(t3 -> (t1 t3)) (t0 t2)))"],148 ["swap dup", "!t0!t1!t2.((t0 (t1 t2)) -> (t1 (t1 (t0 t2))))"],149 ["swap swap", "!t0!t1!t2.((t0 (t1 t2)) -> (t0 (t1 t2)))"],150 ["swap pop", "!t0!t2.((t0 !t1.(t1 t2)) -> (t0 t2))"],151 ["pop apply", "!t2.(!t0.(t0 !t1.((t1 -> t2) t1)) -> t2)"],152 ["pop compose", "!t2!t3!t4.(!t0.(t0 !t1.((t1 -> t2) ((t3 -> t1) t4))) -> ((t3 -> t2) t4))"],153 ["pop quote", "!t1!t2.(!t0.(t0 (t1 t2)) -> (!t3.(t3 -> (t1 t3)) t2))"],154 ["pop dup", "!t1!t2.(!t0.(t0 (t1 t2)) -> (t1 (t1 t2)))"],155 ["pop swap", "!t1!t2!t3.(!t0.(t0 (t1 (t2 t3))) -> (t2 (t1 t3)))"],156 ["pop pop", "!t2.(!t0.(t0 !t1.(t1 t2)) -> t2)"],157 // Some standard library operators 158 ["swap quote compose apply", "!t1!t2.(!t0.((t0 -> t1) (t2 t0)) -> (t2 t1))"], // Dip159 // Various tests160 ["swap quote compose apply pop", "!t1.(!t0.((t0 -> t1) !t2.(t2 t0)) -> t1)"], // Dip pop == pop apply 161 ["[id] dup 0 swap apply swap [id] swap apply apply", "!t0.(t0 -> (Num t0))"], // Issue reported by StoryYeller162];163function printCatTypes() {164 for (var k in catTypes) {165 var ts = catTypes[k];166 var t = parseType(ts);167 console.log(k);168 console.log(ts);169 console.log(t.toString());170 }171}172function testCatTermsCommon() {173 console.log("Testing Cat expression inference")174 let i = 0;175 for (var xs of catTests) 176 runTest("Cat Inference Test " + i++, 177 (x) => normalizeVarNames(inferCatType(x)).toString(), 178 xs[0], 179 xs[1]);180}181function issue1InCat() {182 var t = inferCatType("");183 t = alphabetizeVarNames(t) as TypeArray;184 console.log(t.toString());185 t = inferCatType("[id] dup [0 swap apply] swap quote compose apply [id] swap apply apply swap apply")186 t = alphabetizeVarNames(t) as TypeArray;187 console.log(t.toString());188}189/*190function testCloning() {191 var types = catTesmap(t => inferCatType(t[0]));192 for (var t of types) {193 try {194 var r1 = ti.freshParameterNames(t, 0);195 var r2 = ti.freshVariableNames(t, 0);196 var r3 = t.clone({});197 compareTypes(t, r1);198 compareTypes(t, r2);199 compareTypes(t, r3);200 }201 catch (e) {202 console.log("FAILED cloning test of " + t + " with message " + e);203 }204 }205}206*/207const lambdaCatTests = [208 "\\a",209 "\\a \\b",210 "\\a \\b @a",211 "\\a \\b @b @a apply",212 "\\a \\b [@a] apply",213 "\\a @a @a",214 "\\a \\b @b @a",215 "\\a \\b @a @a",216 "\\a \\b \\c @c @a @b",217 "\\a \\b \\c @c @b @a",218 "\\a \\b \\c @a @b @c",219 "\\a \\b \\c @a @c @b",220 "\\a \\b \\c @b @a @c",221 "\\a \\b \\c @b @c @a",222];223// SK Combinators expressed in Lambda Cat. 224const lambdaCatCombinators = {225 i : "\\x @x", 226 k : "\\x [\\y @x]", 227 s : "\\x [\\y [\\z @y @z apply @z @x apply apply]]",228 b : "\\x [\\y [\\z @z @y apply @x apply]]",229 c : "\\x [\\y [\\z @z @y @x apply apply]]",230 w : "\\x [\\y [@y @y @x apply apply]]",231 m : "\\x [@x @x apply]",232 issue : "[\\x @x] [\\i 0 @i apply [\\x @x] @i apply apply] apply"233}234export const lambdaTests = [ 235 ["(\\i.(i \\x.x) (i 0)) \\y.y", "Num"], // This cu\rrently fails: just like it does with HM236 ["(\\i.(i \\x.x) (i 0))", "?"], // This cu\rrently fails: just like it does with HM237 ["(\\i.(i 0)) \\y.y", "?"], // This cu\rrently fails: just like it does with HM238 ["(\\i.(i \\x.x)) \\y.y", "?"], // This cu\rrently fails: just like it does with HM239 ["0", "Num"],240 ["\\x.x", "!a.(a -> a)"],241 ["\\i.0", "!a.(a -> Num)"],242 ["\\i.i 0", "!a.((Num -> a) -> a)"],243 ["(\\i.0)", "!a.(a -> Num)"],244 ["(\\i.i 0)", "!a.((Num -> a) -> a)"],245 ["\\i.i (0)", "!a.((Num -> a) -> a)"],246 ["(\\i.0) \\y.y", "Num"],247 ["(\\i.0) (\\y.y)", "Num"],248 ["(\\i.i) \\y.y", "!a.(a -> a)"],249 ["(\\i.i) (\\y.y)", "!a.(a -> a)"],250 ["(\\i.i) (\\x.x) (\\y.y)","!a.(a -> a)"],251];252function testRedex(x: Redex) 253{254 //var vars = Object.keys(freeVariables(x));255 //console.log("Free variables: " + vars);256 // TODO: proper alpha naming 257 258 // TODO: validate this in isolation 259 //var eta = etaConversion(x);260 //console.log("Eta reduction: " + eta);261 262 // TODO: valdiate this properly 263 //var lift = lambdaLift(x);264 //console.log("Lambda lifting: " + lift);265 // Convert to SKI combinators266 //var elim = abstractionElimination(x);267 //console.log("Abstraction elimination: " + elim);268 // Produces very long terms 269 //var combinator = combinatorToLambdaCalculus(elim);270 //console.log("As combinators: " + combinator); 271}272function testLambdaToCat(s: string) {273 const redex = parseRedex(s);274 const xs = lambdaToPureCat(redex);275 console.log("Lambda expression: " + s);276 console.log("Cat expression: " + xs.join(' '));277}278function printCatType(s: string) {279 try {280 var t = inferCatType(s);281 console.log("Inferred type: " + t);282 }283 catch (e) {284 console.log("FAILED: " + e.message);285 }286}287function testCombinator(name: string, term: string, exp?: string) {288 try {289 console.log("Testing Lambda term " + name);290 const redex = parseRedex(term);291 console.log("Parsed redex: " + redex);292 testRedex(redex);293 const ys = lambdaToCat(redex);294 console.log("Lambda Cat: " + ys.join(' '));295 const xs = removeVars(ys);296 const cat = xs.join(' '); 297 console.log("Pure Cat: " + cat);298 // TODO: test rewriting 299 // TODO: test converting back 300 const t = inferCatType(cat);301 console.log("Inferred type: " + t);302 console.log("Simplified type: " + typeToString(t));303 if (exp != null)304 console.log("Expected type: " + exp);305 }306 catch (e) {307 console.log("ERROR: " + e.message);308 }309}310function testCombinators() {311 for (var k in combinators) 312 testCombinator(k, combinators[k]);313}314function testLambdaTerms() {315 for (var lt of lambdaTests) 316 testCombinator(lt[0], lt[0], lt[1]);317}318const catTestStrings = [319 "[] dup",320 "[[] dup]",321 "[] [dup]",322 "[] [dup] apply",323 "dup [0] dip apply", // Is recursive. Not surprising. 324 "dup [[0] dip apply []] dip", // what is type?325 "[] dup [[0] dip apply []] dip apply", // Should not fail326 "dup [[0] dip apply []] dip apply", // fails327 "dup [[0] dip apply []] dip apply apply",328 "[dup [[0] dip apply []] dip apply apply]",329 "[dup [[0] dip apply []] dip apply apply] apply",330 "[] [dup [[0] dip apply []] dip apply apply] apply",331 "[[0] dip apply []]",332 "[0] dip apply",333 "[0] dip",334 "[] []",335 "[] [0] dip",336 "[] dup [0] dip",337 "[] dup [0] dip apply",338 "[] dup [[0] dip apply []] dip",339 "[] dup [[0] dip apply []] dip apply",340 "[] dup [[0] dip apply []] dip apply apply",341 "[] [dup [[0] dip apply []] dip apply apply]",342 "[] [0] apply",343 "[] [[0] dip] apply", 344];345function testCatTermsWithVariance() {346 let i=0;347 for (var ct of catTestStrings) {348 runTest("Test Cat Inference with Variance " + i++,349 inferCatType, 350 ct); 351 }352}353testCatTermsCommon();354testCatTermsWithVariance();355testLambdaTerms();356//testCombinators();357//testLambdaTerms();358// A B => A [B] apply 359// A B => [A] apply B 360// A B => A B apply 361//ti.setTrace(true);362// Type of [dup [[0] dip apply []] dip apply apply] apply : !t1.((((Num !t0.((t0 -> t1) t0)) -> !t2.((t2 -> t1) t2)) !t3.((t3 -> t1) t3)) -> t1)363// Type of [] [dup [[0] dip apply []] dip apply apply] : !t0.(t0 -> (!t2.((((Num !t1.((t1 -> t2) t1)) -> !t3.((t3 -> t2) t3)) !t4.((t4 -> t2) t4)) -> t2) (!t5.(t5 -> t5) t0)))364declare var process;...
Interpolant.js
Source:Interpolant.js
1/**2 * Abstract base class of interpolants over parametric samples.3 *4 * The parameter domain is one dimensional, typically the time or a path5 * along a curve defined by the data.6 *7 * The sample values can have any dimensionality and derived classes may8 * apply special interpretations to the data.9 *10 * This class provides the interval seek in a Template Method, deferring11 * the actual interpolation to derived classes.12 *13 * Time complexity is O(1) for linear access crossing at most two points14 * and O(log N) for random access, where N is the number of positions.15 *16 * References:17 *18 * http://www.oodesign.com/template-method-pattern.html19 *20 * @author tschw21 */22function Interpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {23 this.parameterPositions = parameterPositions;24 this._cachedIndex = 0;25 this.resultBuffer = resultBuffer !== undefined ?26 resultBuffer : new sampleValues.constructor( sampleSize );27 this.sampleValues = sampleValues;28 this.valueSize = sampleSize;29}30Object.assign( Interpolant.prototype, {31 evaluate: function ( t ) {32 var pp = this.parameterPositions,33 i1 = this._cachedIndex,34 t1 = pp[ i1 ],35 t0 = pp[ i1 - 1 ];36 validate_interval: {37 seek: {38 var right;39 linear_scan: {40 //- See http://jsperf.com/comparison-to-undefined/341 //- slower code:42 //-43 //- if ( t >= t1 || t1 === undefined ) {44 forward_scan: if ( ! ( t < t1 ) ) {45 for ( var giveUpAt = i1 + 2; ; ) {46 if ( t1 === undefined ) {47 if ( t < t0 ) break forward_scan;48 // after end49 i1 = pp.length;50 this._cachedIndex = i1;51 return this.afterEnd_( i1 - 1, t, t0 );52 }53 if ( i1 === giveUpAt ) break; // this loop54 t0 = t1;55 t1 = pp[ ++ i1 ];56 if ( t < t1 ) {57 // we have arrived at the sought interval58 break seek;59 }60 }61 // prepare binary search on the right side of the index62 right = pp.length;63 break linear_scan;64 }65 //- slower code:66 //- if ( t < t0 || t0 === undefined ) {67 if ( ! ( t >= t0 ) ) {68 // looping?69 var t1global = pp[ 1 ];70 if ( t < t1global ) {71 i1 = 2; // + 1, using the scan for the details72 t0 = t1global;73 }74 // linear reverse scan75 for ( var giveUpAt = i1 - 2; ; ) {76 if ( t0 === undefined ) {77 // before start78 this._cachedIndex = 0;79 return this.beforeStart_( 0, t, t1 );80 }81 if ( i1 === giveUpAt ) break; // this loop82 t1 = t0;83 t0 = pp[ -- i1 - 1 ];84 if ( t >= t0 ) {85 // we have arrived at the sought interval86 break seek;87 }88 }89 // prepare binary search on the left side of the index90 right = i1;91 i1 = 0;92 break linear_scan;93 }94 // the interval is valid95 break validate_interval;96 } // linear scan97 // binary search98 while ( i1 < right ) {99 var mid = ( i1 + right ) >>> 1;100 if ( t < pp[ mid ] ) {101 right = mid;102 } else {103 i1 = mid + 1;104 }105 }106 t1 = pp[ i1 ];107 t0 = pp[ i1 - 1 ];108 // check boundary cases, again109 if ( t0 === undefined ) {110 this._cachedIndex = 0;111 return this.beforeStart_( 0, t, t1 );112 }113 if ( t1 === undefined ) {114 i1 = pp.length;115 this._cachedIndex = i1;116 return this.afterEnd_( i1 - 1, t0, t );117 }118 } // seek119 this._cachedIndex = i1;120 this.intervalChanged_( i1, t0, t1 );121 } // validate_interval122 return this.interpolate_( i1, t0, t, t1 );123 },124 settings: null, // optional, subclass-specific settings structure125 // Note: The indirection allows central control of many interpolants.126 // --- Protected interface127 DefaultSettings_: {},128 getSettings_: function () {129 return this.settings || this.DefaultSettings_;130 },131 copySampleValue_: function ( index ) {132 // copies a sample value to the result buffer133 var result = this.resultBuffer,134 values = this.sampleValues,135 stride = this.valueSize,136 offset = index * stride;137 for ( var i = 0; i !== stride; ++ i ) {138 result[ i ] = values[ offset + i ];139 }140 return result;141 },142 // Template methods for derived classes:143 interpolate_: function ( /* i1, t0, t, t1 */ ) {144 throw new Error( 'call to abstract method' );145 // implementations shall return this.resultBuffer146 },147 intervalChanged_: function ( /* i1, t0, t1 */ ) {148 // empty149 }150} );151//!\ DECLARE ALIAS AFTER assign prototype !152Object.assign( Interpolant.prototype, {153 //( 0, t, t0 ), returns this.resultBuffer154 beforeStart_: Interpolant.prototype.copySampleValue_,155 //( N-1, tN-1, t ), returns this.resultBuffer156 afterEnd_: Interpolant.prototype.copySampleValue_,157} );...
CubicInterpolant.js
Source:CubicInterpolant.js
1import { ZeroCurvatureEnding } from '../../constants.js';2import { Interpolant } from '../Interpolant.js';3import { WrapAroundEnding, ZeroSlopeEnding } from '../../constants.js';4/**5 * Fast and simple cubic spline interpolant.6 *7 * It was derived from a Hermitian construction setting the first derivative8 * at each sample position to the linear slope between neighboring positions9 * over their parameter interval.10 *11 * @author tschw12 */13function CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {14 Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );15 this._weightPrev = - 0;16 this._offsetPrev = - 0;17 this._weightNext = - 0;18 this._offsetNext = - 0;19}20CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {21 constructor: CubicInterpolant,22 DefaultSettings_: {23 endingStart: ZeroCurvatureEnding,24 endingEnd: ZeroCurvatureEnding25 },26 intervalChanged_: function ( i1, t0, t1 ) {27 var pp = this.parameterPositions,28 iPrev = i1 - 2,29 iNext = i1 + 1,30 tPrev = pp[ iPrev ],31 tNext = pp[ iNext ];32 if ( tPrev === undefined ) {33 switch ( this.getSettings_().endingStart ) {34 case ZeroSlopeEnding:35 // f'(t0) = 036 iPrev = i1;37 tPrev = 2 * t0 - t1;38 break;39 case WrapAroundEnding:40 // use the other end of the curve41 iPrev = pp.length - 2;42 tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ];43 break;44 default: // ZeroCurvatureEnding45 // f''(t0) = 0 a.k.a. Natural Spline46 iPrev = i1;47 tPrev = t1;48 }49 }50 if ( tNext === undefined ) {51 switch ( this.getSettings_().endingEnd ) {52 case ZeroSlopeEnding:53 // f'(tN) = 054 iNext = i1;55 tNext = 2 * t1 - t0;56 break;57 case WrapAroundEnding:58 // use the other end of the curve59 iNext = 1;60 tNext = t1 + pp[ 1 ] - pp[ 0 ];61 break;62 default: // ZeroCurvatureEnding63 // f''(tN) = 0, a.k.a. Natural Spline64 iNext = i1 - 1;65 tNext = t0;66 }67 }68 var halfDt = ( t1 - t0 ) * 0.5,69 stride = this.valueSize;70 this._weightPrev = halfDt / ( t0 - tPrev );71 this._weightNext = halfDt / ( tNext - t1 );72 this._offsetPrev = iPrev * stride;73 this._offsetNext = iNext * stride;74 },75 interpolate_: function ( i1, t0, t, t1 ) {76 var result = this.resultBuffer,77 values = this.sampleValues,78 stride = this.valueSize,79 o1 = i1 * stride, o0 = o1 - stride,80 oP = this._offsetPrev, oN = this._offsetNext,81 wP = this._weightPrev, wN = this._weightNext,82 p = ( t - t0 ) / ( t1 - t0 ),83 pp = p * p,84 ppp = pp * p;85 // evaluate polynomials86 var sP = - wP * ppp + 2 * wP * pp - wP * p;87 var s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1;88 var s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p;89 var sN = wN * ppp - wN * pp;90 // combine data linearly91 for ( var i = 0; i !== stride; ++ i ) {92 result[ i ] =93 sP * values[ oP + i ] +94 s0 * values[ o0 + i ] +95 s1 * values[ o1 + i ] +96 sN * values[ oN + i ];97 }98 return result;99 }100} );...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Test Results for ' + data.data.testUrl);7 console.log('First View: ' + data.data.average.firstView.t);8 console.log('Repeat View: ' + data.data.average.repeatView.t);9 }10});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test status:', data.statusText);5 console.log('Test ID:', data.data.testId);6});7var wpt = require('webpagetest');8var wpt = new WebPageTest('www.webpagetest.org');9 if (err) return console.error(err);10 console.log('Test status:', data.statusText);11 console.log('Test ID:', data.data.testId);12});13var wpt = require('webpagetest');14var wpt = new WebPageTest('www.webpagetest.org');15 if (err) return console.error(err);16 console.log('Test status:', data.statusText);17 console.log('Test ID:', data.data.testId);18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21 if (err) return console.error(err);22 console.log('Test status:', data.statusText);23 console.log('Test ID:', data.data.testId);24});25var wpt = require('webpagetest');26var wpt = new WebPageTest('www.webpagetest.org');27 if (err) return console.error(err
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3 if (err) return console.error(err);4 var testId = data.data.testId;5 test.getTestResults(testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});10{11 "scripts": {12 },13 "repository": {
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Test status: ' + data.statusCode);7 console.log('Test ID: ' + data.data.testId);8 console.log('Test URL: ' + data.data.summary);9 console.log('Test results: ' + data.data.userUrl);10 }11});12var wpt = require('webpagetest');13var api = new wpt('www.webpagetest.org');14 if (err) {15 console.log('Error: ' + err);16 } else {17 console.log('Test status: ' + data.statusCode);18 console.log('Test ID: ' + data.data.testId);19 console.log('Test URL: ' + data.data.summary);20 console.log('Test results: ' + data.data.userUrl);21 }22});23var wpt = require('webpagetest');24var api = new wpt('www.webpagetest.org');25 if (err) {26 console.log('Error: ' + err);27 } else {28 console.log('Test status: ' + data.statusCode);29 console.log('Test ID: ' + data.data.testId);30 console.log('Test URL: ' + data.data.summary);31 console.log('Test results: ' + data.data.userUrl);32 }33});34var wpt = require('webpagetest');35var api = new wpt('www.webpagetest.org');36 if (err) {37 console.log('Error: ' + err);38 } else {39 console.log('Test status: ' + data.statusCode);40 console.log('Test ID: ' + data.data.testId);41 console.log('Test URL: ' + data.data.summary);
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test submitted to WebPageTest! Check back here for results: ' + data.data.userUrl);5});6var wpt = require('webpagetest');7var wpt = new WebPageTest('www.webpagetest.org');8 if (err) return console.error(err);9 console.log('Test status: ' + data.statusText);10});11 if (err) return console.error(err);12 console.log('Test results: ' + data.data.average.firstView.loadTime);13});
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('www.webpagetest.org', 'A.6b8b6b8b8b8b8b8b8b8b8b8b8b8b8b8b');3 if (err) throw err;4 console.log(data);5 test.getTestResults(data.data.testId, function(err, data) {6 if (err) throw err;7 console.log(data);8 });9});10{ statusCode: 403,11 data: { statusCode: 403, statusText: 'Forbidden' } }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!