Skip to content
Browse files

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...
1 parent 3cf17f0 commit a2233e0deb9b2830a87227fa1c12930a9568f679 Sébastien Loisel committed Dec 18, 2012
Showing with 70 additions and 66 deletions.
  1. +13 −17 documentation.html
  2. +13 −17 src/documentation.html
  3. +1 −1 src/numeric.js
  4. +39 −27 tools/unit2.js
  5. +4 −4 workshop.php
View
30 documentation.html
@@ -200,16 +200,16 @@
The <tt>numeric</tt> library provides functions that implement most of the usual Javascript
operators for vectors and matrices:
<pre>
-IN> y = [10,1,2];
-OUT> [10,1,2]
-IN> numeric['+'](x,y)
+IN> x = [7,8,9];
+ y = [10,1,2];
+ numeric['+'](x,y)
OUT> [17,9,11]
IN> numeric['>'](x,y)
OUT> [false,true,true]
</pre>
These operators can also be called with plain Javascript function names:
<pre>
-IN> numeric.add(x,y)
+IN> numeric.add([7,8,9],[10,1,2])
OUT> [17,9,11]
</pre>
You can also use these operators with three or more parameters:
@@ -847,7 +847,10 @@
</pre>
We provide an LU=PA decomposition:
<pre>
-IN> LUP = numeric.ccsLUP(A);
+IN> A = [[0,5,10,15,20,25],
+ [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],
+ [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]]
+ LUP = numeric.ccsLUP(A);
OUT> {L:[[0,5,9,12,14,15],
[0,2,4,1,3,1,3,4,2,2,4,3,3,4,4],
[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 @@
[0,0,1,0,0],
[0,0,0,1,0],
[0,0,0,0,1]]
-</pre>
-The LUP decomposition uses partial pivoting and has an optional thresholding argument:
-<pre>
IN> numeric.ccsLUP(A,0.4).P;
OUT> [0,2,1,3,4]
</pre>
+The LUP decomposition uses partial pivoting and has an optional thresholding argument.
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
"full partial pivoting" order above which was [1,2,4,0,3]. Threshold=0 gives no pivoting
and threshold=1 gives normal partial pivoting. Note that a small or zero threshold can result in numerical
@@ -1043,14 +1044,11 @@
<h1>Fast Fourier Transforms</h1>
-FFT on numeric.T objects:
+FFT and IFFT on numeric.T objects:
<pre>
IN> z = (new numeric.T([1,2,3,4,5],[6,7,8,9,10])).fft()
OUT> {x:[15,-5.941,-3.312,-1.688, 0.941],
y:[40, 0.941,-1.688,-3.312,-5.941]}
-</pre>
-Also inverse FFT:
-<pre>
IN> z.ifft()
OUT> {x:[1,2,3,4,5],
y:[6,7,8,9,10]}
@@ -1280,15 +1278,13 @@
iterations:8,
events:,
message:""}
+IN> sol.at([0.3,0.7])
+OUT> [1.35,2.014]
</pre>
The return value <tt>sol</tt> contains the x and y values of the solution.
If you need to know the value of the solution between the given x values, use the function
<tt>sol.at()</tt>, which uses the extra information contained in <tt>sol.ymid</tt> and <tt>sol.f</tt> to
-produce approximations between these points:
-<pre>
-IN> sol.at([0.3,0.7])
-OUT> [1.35,2.014]
-</pre>
+produce approximations between these points.
The integrator is also able to handle vector equations. This is a harmonic oscillator:
<pre>
IN> sol = numeric.dopri(0,10,[3,0],function (x,y) { return [y[1],-y[0]]; });
View
30 src/documentation.html
@@ -200,16 +200,16 @@
The <tt>numeric</tt> library provides functions that implement most of the usual Javascript
operators for vectors and matrices:
<pre>
-IN> y = [10,1,2];
-OUT> [10,1,2]
-IN> numeric['+'](x,y)
+IN> x = [7,8,9];
+ y = [10,1,2];
+ numeric['+'](x,y)
OUT> [17,9,11]
IN> numeric['>'](x,y)
OUT> [false,true,true]
</pre>
These operators can also be called with plain Javascript function names:
<pre>
-IN> numeric.add(x,y)
+IN> numeric.add([7,8,9],[10,1,2])
OUT> [17,9,11]
</pre>
You can also use these operators with three or more parameters:
@@ -847,7 +847,10 @@
</pre>
We provide an LU=PA decomposition:
<pre>
-IN> LUP = numeric.ccsLUP(A);
+IN> A = [[0,5,10,15,20,25],
+ [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],
+ [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]]
+ LUP = numeric.ccsLUP(A);
OUT> {L:[[0,5,9,12,14,15],
[0,2,4,1,3,1,3,4,2,2,4,3,3,4,4],
[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 @@
[0,0,1,0,0],
[0,0,0,1,0],
[0,0,0,0,1]]
-</pre>
-The LUP decomposition uses partial pivoting and has an optional thresholding argument:
-<pre>
IN> numeric.ccsLUP(A,0.4).P;
OUT> [0,2,1,3,4]
</pre>
+The LUP decomposition uses partial pivoting and has an optional thresholding argument.
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
"full partial pivoting" order above which was [1,2,4,0,3]. Threshold=0 gives no pivoting
and threshold=1 gives normal partial pivoting. Note that a small or zero threshold can result in numerical
@@ -1043,14 +1044,11 @@
<h1>Fast Fourier Transforms</h1>
-FFT on numeric.T objects:
+FFT and IFFT on numeric.T objects:
<pre>
IN> z = (new numeric.T([1,2,3,4,5],[6,7,8,9,10])).fft()
OUT> {x:[15,-5.941,-3.312,-1.688, 0.941],
y:[40, 0.941,-1.688,-3.312,-5.941]}
-</pre>
-Also inverse FFT:
-<pre>
IN> z.ifft()
OUT> {x:[1,2,3,4,5],
y:[6,7,8,9,10]}
@@ -1280,15 +1278,13 @@
iterations:8,
events:,
message:""}
+IN> sol.at([0.3,0.7])
+OUT> [1.35,2.014]
</pre>
The return value <tt>sol</tt> contains the x and y values of the solution.
If you need to know the value of the solution between the given x values, use the function
<tt>sol.at()</tt>, which uses the extra information contained in <tt>sol.ymid</tt> and <tt>sol.f</tt> to
-produce approximations between these points:
-<pre>
-IN> sol.at([0.3,0.7])
-OUT> [1.35,2.014]
-</pre>
+produce approximations between these points.
The integrator is also able to handle vector equations. This is a harmonic oscillator:
<pre>
IN> sol = numeric.dopri(0,10,[3,0],function (x,y) { return [y[1],-y[0]]; });
View
2 src/numeric.js
@@ -3,7 +3,7 @@
var numeric = (typeof exports === "undefined")?(function numeric() {}):(exports);
if(typeof global !== "undefined") { global.numeric = numeric; }
-numeric.version = "1.2.5";
+numeric.version = "1.2.6";
// 1. Utility functions
numeric.bench = function bench (f,interval) {
View
66 tools/unit2.js
@@ -1,20 +1,21 @@
-if(typeof console === "undefined") console = {
+_console = {
log: function() {
var k;
for(k=0;k<arguments.length;k++) {
if(k>0) { write(' '); }
write(arguments[k]); }
write('\n');
- if(typeof system !== "undefined") { system.stdout.flush(); }
+ if(typeof system !== 'undefined') { system.stdout.flush(); }
}
};
+if(typeof console === "undefined") console = _console;
var myread;
if(typeof system !== "undefined") {
myread = (function(x) { return new Stream(x).readFile(); });
} else if(typeof read !== "undefined") {
myread = read;
} else {
- _fs = require('fs');
+ var _fs = require('fs');
myread = (function(x) { return _fs.readFileSync(x,'utf8') });
console.log(process.cwd());
}
@@ -27,40 +28,51 @@ if(typeof _fs !== "undefined") global.XMLHttpRequest = XMLHttpRequest;
var _foo = myread('./documentation.html');
var _baz = myread('./src/numeric.js');
var _ver = _baz.match(/numeric.version[ =]*"([0-9.]*)".*/)[1];
-var _bar = _foo.match(/<pre>[\s\S]*?(?=<\/pre>)/g).join('\n').replace(/<pre>/g,'').split('\nIN> ');
+var _bar = _foo.match(/<pre>[\s\S]*?(?=<\/pre>)/g);
+//.join('\n').replace(/<pre>/g,'').split('\nIN> ');
_baz = [];
-var _k,k0=0;
-for(_k=0;_k<_bar.length;_k++) {
- var _j = _bar[_k].indexOf('\nOUT>');
- if(_j>0) { _baz[k0] = [_bar[_k].substring(0,_j),_bar[_k].substring(_j+5)]; k0++; }
-}
var numfile = './lib/numeric-'+_ver+'.js';
var numeric;
if(typeof _fs === "undefined") load(numfile);
else { numeric = require('../'+numfile); }
if(typeof numeric === "undefined") { throw new Error("Could not load numeric.js"); }
var unit_pass = 0, unit_fail = 0;
-var _a,_b,_msg;
var k1 = 0;
var workshop = {};
-for(_k=0;_k<k0;_k++) {
- k1++;
- _bar = '';
- try {
- workshop.html = function(x) { _bar += x; }
- _foo = numeric.prettyPrint(eval(_baz[_k][0].replace(/&lt;/g,'<').replace(/&gt;/g,'>')));
- _foo = _bar+_foo;
- } catch(_e) { _foo = _e.toString(); }
- _a = _foo.replace(/\s/g,'');
- _b = _baz[_k][1].replace(/\s/g,'');
- if(_a===_b) {
- _msg = k1+" PASS "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"==="+_b;
- unit_pass++;
- } else {
- _msg = k1+" FAIL "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"!=="+_b;
- unit_fail++;
+var _global = this;
+function runner(_bar) {
+ var _k,k0=0;
+ var _a,_b,_msg;
+ var _tmp = {};
+ for(_k in _global) _tmp[_k] = true;
+ for(_k=0;_k<_bar.length;_k++) {
+ var _j = _bar[_k].indexOf('\nOUT>');
+ if(_j>0) { _baz[k0] = [_bar[_k].substring(0,_j),_bar[_k].substring(_j+5)]; k0++; }
+ }
+ for(_k=0;_k<k0;_k++) {
+ k1++;
+ _bar = '';
+ try {
+ workshop.html = function(x) { _bar += x; }
+ _foo = numeric.prettyPrint(eval(_baz[_k][0].replace(/&lt;/g,'<').replace(/&gt;/g,'>')));
+ _foo = _bar+_foo;
+ } catch(_e) { _foo = _e.toString(); }
+ _a = _foo.replace(/\s/g,'');
+ _b = _baz[_k][1].replace(/\s/g,'');
+ if(_a===_b) {
+ _msg = k1+" PASS "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"==="+_b;
+ unit_pass++;
+ } else {
+ _msg = k1+" FAIL "+_baz[_k][0].replace(/\s/g,'')+"==>"+_a+"!=="+_b;
+ unit_fail++;
+ }
+ console.log(_msg);
}
- console.log(_msg);
+ for(_k in _global) if(!_tmp[_k]) eval('delete '+_k);
+}
+var _j;
+for(_j=0;_j<_bar.length;_j++) {
+ runner(_bar[_j].replace(/<pre>/g,'').split('\nIN> '));
}
console.log('unit2: '+k1+' tests, '+unit_pass+' pass and '+unit_fail+' fail.');
View
8 workshop.php
@@ -474,7 +474,7 @@ function submit() {
$foo = json_decode($restore,true) or die("json error");
$incs = $foo['scripts'];
if(is_null($incs)) {
- $incs = array(1 => 'lib/numeric-1.2.5.js');
+ $incs = array(1 => 'lib/numeric-1.2.6.js');
}
echo <<<EOT
workshop._restore = $restore;
@@ -484,13 +484,13 @@ function submit() {
workshop._restore = ((typeof localStorage.savedata === "string")?
(JSON.parse(localStorage.savedata)):
{inputs: [], outputs: [],
- scripts: ["lib/numeric-1.2.5.js"] });
+ scripts: ["lib/numeric-1.2.6.js"] });
EOT;
}
?>
-workshop.version = "1.2.5";
-workshop.updateVersion = "lib/numeric-1.2.5.js";
+workshop.version = "1.2.6";
+workshop.updateVersion = "lib/numeric-1.2.6.js";
workshop.preload(workshop._restore);
</script>

0 comments on commit a2233e0

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