Skip to content
This repository
Browse code

Clean global namespace in unit2.js

v1.2.5 was a bugfix release. One of the bugs could have been caught had
unit2.js been more careful about maintaining a global namespace. This
new version of unit2.js ensures that global variables are deleted from
the global namespace after each block of unit tests.
  • Loading branch information...
commit a2233e0deb9b2830a87227fa1c12930a9568f679 1 parent 3cf17f0
Sébastien Loisel authored
30 documentation.html
@@ -200,16 +200,16 @@
200 200 The <tt>numeric</tt> library provides functions that implement most of the usual Javascript
201 201 operators for vectors and matrices:
202 202 <pre>
203   -IN> y = [10,1,2];
204   -OUT> [10,1,2]
205   -IN> numeric['+'](x,y)
  203 +IN> x = [7,8,9];
  204 + y = [10,1,2];
  205 + numeric['+'](x,y)
206 206 OUT> [17,9,11]
207 207 IN> numeric['>'](x,y)
208 208 OUT> [false,true,true]
209 209 </pre>
210 210 These operators can also be called with plain Javascript function names:
211 211 <pre>
212   -IN> numeric.add(x,y)
  212 +IN> numeric.add([7,8,9],[10,1,2])
213 213 OUT> [17,9,11]
214 214 </pre>
215 215 You can also use these operators with three or more parameters:
@@ -847,7 +847,10 @@
847 847 </pre>
848 848 We provide an LU=PA decomposition:
849 849 <pre>
850   -IN> LUP = numeric.ccsLUP(A);
  850 +IN> A = [[0,5,10,15,20,25],
  851 + [0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4],
  852 + [3,7,6,2,1,5,10,3,6,2,8,3,5,7,9,10,5,1,1,3,8,3,8,2,9]]
  853 + LUP = numeric.ccsLUP(A);
851 854 OUT> {L:[[0,5,9,12,14,15],
852 855 [0,2,4,1,3,1,3,4,2,2,4,3,3,4,4],
853 856 [1,0.1429,0.2857,0.8571,0.4286,1,-0.1282,-0.5641,-0.1026,1,0.8517,0.7965,1,-0.67,1]],
@@ -870,12 +873,10 @@
870 873 [0,0,1,0,0],
871 874 [0,0,0,1,0],
872 875 [0,0,0,0,1]]
873   -</pre>
874   -The LUP decomposition uses partial pivoting and has an optional thresholding argument:
875   -<pre>
876 876 IN> numeric.ccsLUP(A,0.4).P;
877 877 OUT> [0,2,1,3,4]
878 878 </pre>
  879 +The LUP decomposition uses partial pivoting and has an optional thresholding argument.
879 880 With a threshold of 0.4, the pivots are [0,2,1,3,4] (only rows 1 and 2 have been exchanged) instead of the
880 881 "full partial pivoting" order above which was [1,2,4,0,3]. Threshold=0 gives no pivoting
881 882 and threshold=1 gives normal partial pivoting. Note that a small or zero threshold can result in numerical
@@ -1043,14 +1044,11 @@
1043 1044
1044 1045
1045 1046 <h1>Fast Fourier Transforms</h1>
1046   -FFT on numeric.T objects:
  1047 +FFT and IFFT on numeric.T objects:
1047 1048 <pre>
1048 1049 IN> z = (new numeric.T([1,2,3,4,5],[6,7,8,9,10])).fft()
1049 1050 OUT> {x:[15,-5.941,-3.312,-1.688, 0.941],
1050 1051 y:[40, 0.941,-1.688,-3.312,-5.941]}
1051   -</pre>
1052   -Also inverse FFT:
1053   -<pre>
1054 1052 IN> z.ifft()
1055 1053 OUT> {x:[1,2,3,4,5],
1056 1054 y:[6,7,8,9,10]}
@@ -1280,15 +1278,13 @@
1280 1278 iterations:8,
1281 1279 events:,
1282 1280 message:""}
  1281 +IN> sol.at([0.3,0.7])
  1282 +OUT> [1.35,2.014]
1283 1283 </pre>
1284 1284 The return value <tt>sol</tt> contains the x and y values of the solution.
1285 1285 If you need to know the value of the solution between the given x values, use the function
1286 1286 <tt>sol.at()</tt>, which uses the extra information contained in <tt>sol.ymid</tt> and <tt>sol.f</tt> to
1287   -produce approximations between these points:
1288   -<pre>
1289   -IN> sol.at([0.3,0.7])
1290   -OUT> [1.35,2.014]
1291   -</pre>
  1287 +produce approximations between these points.
1292 1288 The integrator is also able to handle vector equations. This is a harmonic oscillator:
1293 1289 <pre>
1294 1290 IN> sol = numeric.dopri(0,10,[3,0],function (x,y) { return [y[1],-y[0]]; });
30 src/documentation.html
@@ -200,16 +200,16 @@
200 200 The <tt>numeric</tt> library provides functions that implement most of the usual Javascript
201 201 operators for vectors and matrices:
202 202 <pre>
203   -IN> y = [10,1,2];
204   -OUT> [10,1,2]
205   -IN> numeric['+'](x,y)
  203 +IN> x = [7,8,9];
  204 + y = [10,1,2];
  205 + numeric['+'](x,y)
206 206 OUT> [17,9,11]
207 207 IN> numeric['>'](x,y)
208 208 OUT> [false,true,true]
209 209 </pre>
210 210 These operators can also be called with plain Javascript function names:
211 211 <pre>
212   -IN> numeric.add(x,y)
  212 +IN> numeric.add([7,8,9],[10,1,2])
213 213 OUT> [17,9,11]
214 214 </pre>
215 215 You can also use these operators with three or more parameters:
@@ -847,7 +847,10 @@
847 847 </pre>
848 848 We provide an LU=PA decomposition:
849 849 <pre>
850   -IN> LUP = numeric.ccsLUP(A);
  850 +IN> A = [[0,5,10,15,20,25],
  851 + [0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4],
  852 + [3,7,6,2,1,5,10,3,6,2,8,3,5,7,9,10,5,1,1,3,8,3,8,2,9]]
  853 + LUP = numeric.ccsLUP(A);
851 854 OUT> {L:[[0,5,9,12,14,15],
852 855 [0,2,4,1,3,1,3,4,2,2,4,3,3,4,4],
853 856 [1,0.1429,0.2857,0.8571,0.4286,1,-0.1282,-0.5641,-0.1026,1,0.8517,0.7965,1,-0.67,1]],
@@ -870,12 +873,10 @@
870 873 [0,0,1,0,0],
871 874 [0,0,0,1,0],
872 875 [0,0,0,0,1]]
873   -</pre>
874   -The LUP decomposition uses partial pivoting and has an optional thresholding argument:
875   -<pre>
876 876 IN> numeric.ccsLUP(A,0.4).P;
877 877 OUT> [0,2,1,3,4]
878 878 </pre>
  879 +The LUP decomposition uses partial pivoting and has an optional thresholding argument.
879 880 With a threshold of 0.4, the pivots are [0,2,1,3,4] (only rows 1 and 2 have been exchanged) instead of the
880 881 "full partial pivoting" order above which was [1,2,4,0,3]. Threshold=0 gives no pivoting
881 882 and threshold=1 gives normal partial pivoting. Note that a small or zero threshold can result in numerical
@@ -1043,14 +1044,11 @@
1043 1044
1044 1045
1045 1046 <h1>Fast Fourier Transforms</h1>
1046   -FFT on numeric.T objects:
  1047 +FFT and IFFT on numeric.T objects:
1047 1048 <pre>
1048 1049 IN> z = (new numeric.T([1,2,3,4,5],[6,7,8,9,10])).fft()
1049 1050 OUT> {x:[15,-5.941,-3.312,-1.688, 0.941],
1050 1051 y:[40, 0.941,-1.688,-3.312,-5.941]}
1051   -</pre>
1052   -Also inverse FFT:
1053   -<pre>
1054 1052 IN> z.ifft()
1055 1053 OUT> {x:[1,2,3,4,5],
1056 1054 y:[6,7,8,9,10]}
@@ -1280,15 +1278,13 @@
1280 1278 iterations:8,
1281 1279 events:,
1282 1280 message:""}
  1281 +IN> sol.at([0.3,0.7])
  1282 +OUT> [1.35,2.014]
1283 1283 </pre>
1284 1284 The return value <tt>sol</tt> contains the x and y values of the solution.
1285 1285 If you need to know the value of the solution between the given x values, use the function
1286 1286 <tt>sol.at()</tt>, which uses the extra information contained in <tt>sol.ymid</tt> and <tt>sol.f</tt> to
1287   -produce approximations between these points:
1288   -<pre>
1289   -IN> sol.at([0.3,0.7])
1290   -OUT> [1.35,2.014]
1291   -</pre>
  1287 +produce approximations between these points.
1292 1288 The integrator is also able to handle vector equations. This is a harmonic oscillator:
1293 1289 <pre>
1294 1290 IN> sol = numeric.dopri(0,10,[3,0],function (x,y) { return [y[1],-y[0]]; });
2  src/numeric.js
@@ -3,7 +3,7 @@
3 3 var numeric = (typeof exports === "undefined")?(function numeric() {}):(exports);
4 4 if(typeof global !== "undefined") { global.numeric = numeric; }
5 5
6   -numeric.version = "1.2.5";
  6 +numeric.version = "1.2.6";
7 7
8 8 // 1. Utility functions
9 9 numeric.bench = function bench (f,interval) {
66 tools/unit2.js
... ... @@ -1,20 +1,21 @@
1   -if(typeof console === "undefined") console = {
  1 +_console = {
2 2 log: function() {
3 3 var k;
4 4 for(k=0;k<arguments.length;k++) {
5 5 if(k>0) { write(' '); }
6 6 write(arguments[k]); }
7 7 write('\n');
8   - if(typeof system !== "undefined") { system.stdout.flush(); }
  8 + if(typeof system !== 'undefined') { system.stdout.flush(); }
9 9 }
10 10 };
  11 +if(typeof console === "undefined") console = _console;
11 12 var myread;
12 13 if(typeof system !== "undefined") {
13 14 myread = (function(x) { return new Stream(x).readFile(); });
14 15 } else if(typeof read !== "undefined") {
15 16 myread = read;
16 17 } else {
17   - _fs = require('fs');
  18 + var _fs = require('fs');
18 19 myread = (function(x) { return _fs.readFileSync(x,'utf8') });
19 20 console.log(process.cwd());
20 21 }
@@ -27,13 +28,9 @@ if(typeof _fs !== "undefined") global.XMLHttpRequest = XMLHttpRequest;
27 28 var _foo = myread('./documentation.html');
28 29 var _baz = myread('./src/numeric.js');
29 30 var _ver = _baz.match(/numeric.version[ =]*"([0-9.]*)".*/)[1];
30   -var _bar = _foo.match(/<pre>[\s\S]*?(?=<\/pre>)/g).join('\n').replace(/<pre>/g,'').split('\nIN> ');
  31 +var _bar = _foo.match(/<pre>[\s\S]*?(?=<\/pre>)/g);
  32 +//.join('\n').replace(/<pre>/g,'').split('\nIN> ');
31 33 _baz = [];
32   -var _k,k0=0;
33   -for(_k=0;_k<_bar.length;_k++) {
34   - var _j = _bar[_k].indexOf('\nOUT>');
35   - if(_j>0) { _baz[k0] = [_bar[_k].substring(0,_j),_bar[_k].substring(_j+5)]; k0++; }
36   -}
37 34
38 35 var numfile = './lib/numeric-'+_ver+'.js';
39 36 var numeric;
@@ -41,26 +38,41 @@ if(typeof _fs === "undefined") load(numfile);
41 38 else { numeric = require('../'+numfile); }
42 39 if(typeof numeric === "undefined") { throw new Error("Could not load numeric.js"); }
43 40 var unit_pass = 0, unit_fail = 0;
44   -var _a,_b,_msg;
45 41 var k1 = 0;
46 42 var workshop = {};
47   -for(_k=0;_k<k0;_k++) {
48   - k1++;
49   - _bar = '';
50   - try {
51   - workshop.html = function(x) { _bar += x; }
52   - _foo = numeric.prettyPrint(eval(_baz[_k][0].replace(/&lt;/g,'<').replace(/&gt;/g,'>')));
53   - _foo = _bar+_foo;
54   - } catch(_e) { _foo = _e.toString(); }
55   - _a = _foo.replace(/\s/g,'');
56   - _b = _baz[_k][1].replace(/\s/g,'');
57   - if(_a===_b) {
58   - _msg = k1+" PASS "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"==="+_b;
59   - unit_pass++;
60   - } else {
61   - _msg = k1+" FAIL "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"!=="+_b;
62   - unit_fail++;
  43 +var _global = this;
  44 +function runner(_bar) {
  45 + var _k,k0=0;
  46 + var _a,_b,_msg;
  47 + var _tmp = {};
  48 + for(_k in _global) _tmp[_k] = true;
  49 + for(_k=0;_k<_bar.length;_k++) {
  50 + var _j = _bar[_k].indexOf('\nOUT>');
  51 + if(_j>0) { _baz[k0] = [_bar[_k].substring(0,_j),_bar[_k].substring(_j+5)]; k0++; }
  52 + }
  53 + for(_k=0;_k<k0;_k++) {
  54 + k1++;
  55 + _bar = '';
  56 + try {
  57 + workshop.html = function(x) { _bar += x; }
  58 + _foo = numeric.prettyPrint(eval(_baz[_k][0].replace(/&lt;/g,'<').replace(/&gt;/g,'>')));
  59 + _foo = _bar+_foo;
  60 + } catch(_e) { _foo = _e.toString(); }
  61 + _a = _foo.replace(/\s/g,'');
  62 + _b = _baz[_k][1].replace(/\s/g,'');
  63 + if(_a===_b) {
  64 + _msg = k1+" PASS "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"==="+_b;
  65 + unit_pass++;
  66 + } else {
  67 + _msg = k1+" FAIL "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"!=="+_b;
  68 + unit_fail++;
  69 + }
  70 + console.log(_msg);
63 71 }
64   - console.log(_msg);
  72 + for(_k in _global) if(!_tmp[_k]) eval('delete '+_k);
  73 +}
  74 +var _j;
  75 +for(_j=0;_j<_bar.length;_j++) {
  76 + runner(_bar[_j].replace(/<pre>/g,'').split('\nIN> '));
65 77 }
66 78 console.log('unit2: '+k1+' tests, '+unit_pass+' pass and '+unit_fail+' fail.');
8 workshop.php
@@ -474,7 +474,7 @@ function submit() {
474 474 $foo = json_decode($restore,true) or die("json error");
475 475 $incs = $foo['scripts'];
476 476 if(is_null($incs)) {
477   - $incs = array(1 => 'lib/numeric-1.2.5.js');
  477 + $incs = array(1 => 'lib/numeric-1.2.6.js');
478 478 }
479 479 echo <<<EOT
480 480 workshop._restore = $restore;
@@ -484,13 +484,13 @@ function submit() {
484 484 workshop._restore = ((typeof localStorage.savedata === "string")?
485 485 (JSON.parse(localStorage.savedata)):
486 486 {inputs: [], outputs: [],
487   - scripts: ["lib/numeric-1.2.5.js"] });
  487 + scripts: ["lib/numeric-1.2.6.js"] });
488 488 EOT;
489 489 }
490 490 ?>
491 491
492   -workshop.version = "1.2.5";
493   -workshop.updateVersion = "lib/numeric-1.2.5.js";
  492 +workshop.version = "1.2.6";
  493 +workshop.updateVersion = "lib/numeric-1.2.6.js";
494 494 workshop.preload(workshop._restore);
495 495 </script>
496 496

0 comments on commit a2233e0

Please sign in to comment.
Something went wrong with that request. Please try again.