-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Check the return code, stdout, and stderr of (almost) every exa…
…mple Runs each of the files ending in '.js' in the examples directory or a subdirectory thereof via `node`. Verifies the return code is normal for each one. Compares stdout and stderr to reference copies.
- Loading branch information
Showing
29 changed files
with
434 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Only use native node.js API's and references to ./lib here, this file is not transpiled! | ||
const assert = require('assert') | ||
const cp = require('child_process') | ||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
describe('examples/', () => { | ||
const exampleDir = path.resolve(__dirname, '../../examples/') | ||
const referenceDir = path.resolve(__dirname, './examples/') | ||
const rawExamples = fs.readdirSync(exampleDir, { withFileTypes: true }) | ||
let examples = [] | ||
for (const example of rawExamples) { | ||
if (example.isDirectory()) { | ||
const subDir = path.join(exampleDir, example.name) | ||
const subFiles = fs.readdirSync(subDir) | ||
for (const subFile of subFiles) { | ||
if (subFile.substr(-3) === '.js') { | ||
examples.push(path.join(example.name, subFile)) | ||
} | ||
} | ||
} else { | ||
if (example.name.substr(-3) === '.js') { | ||
examples.push(example.name) | ||
} | ||
} | ||
} | ||
for (const example of examples) { | ||
it(example, (done) => { | ||
const source = path.join(exampleDir, example) | ||
const outfile = path.join(referenceDir, example + '.out') | ||
const expected = fs.readFileSync(outfile).toString() | ||
let expectError = '' | ||
const errfile = path.join(referenceDir, example + '.err') | ||
if (fs.existsSync(errfile)) { | ||
expectError = fs.readFileSync(errfile).toString() | ||
} | ||
cp.exec('node ' + source, (error, stdout, stderr) => { | ||
assert.strictEqual(error, null) | ||
assert.strictEqual(stderr, expectError) | ||
assert.strictEqual(stdout, expected) | ||
done() | ||
}) | ||
}) | ||
} | ||
}) |
3 changes: 3 additions & 0 deletions
3
test/node-tests/examples/advanced/convert_fraction_to_bignumber.js.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fraction 7/12 | ||
BigNumber 2 | ||
BigNumber 3.4 |
3 changes: 3 additions & 0 deletions
3
test/node-tests/examples/advanced/custom_argument_parsing.js.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0.6667254718034714 | ||
0.6667254718034714 | ||
4.000000000000003 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CustomValue:5 | ||
CustomValue:9 |
1 change: 1 addition & 0 deletions
1
test/node-tests/examples/advanced/custom_evaluate_using_factories.js.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
14 |
1 change: 1 addition & 0 deletions
1
test/node-tests/examples/advanced/custom_evaluate_using_import.js.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
14 |
2 changes: 2 additions & 0 deletions
2
test/node-tests/examples/advanced/custom_relational_functions.js.err
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"a" == "b" Error: Cannot convert "a" to a number | ||
"a" == "a" Error: Cannot convert "a" to a number |
9 changes: 9 additions & 0 deletions
9
test/node-tests/examples/advanced/custom_relational_functions.js.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
default (compare string by their numerical value) | ||
2 < 10 true | ||
"2" < "10" true | ||
|
||
custom (compare strings lexically) | ||
2 < 10 true | ||
"2" < "10" false | ||
"a" == "b" false | ||
"a" == "a" true |
31 changes: 31 additions & 0 deletions
31
test/node-tests/examples/advanced/custom_scope_objects.js.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Object scope: { | ||
x: 3, | ||
y: 6, | ||
scalar: 1, | ||
area: [Function: area] { | ||
signatures: { 'any,any': [Function (anonymous)] }, | ||
syntax: 'area(length, width)' | ||
}, | ||
A: 18 | ||
} | ||
Map-like scope (simple Map): undefined | ||
Map-like scope (MapScope example): Map(5) { | ||
'x' => 3, | ||
'y' => 6, | ||
'scalar' => 1, | ||
'area' => [Function: area] { | ||
signatures: { 'any,any': [Function (anonymous)] }, | ||
syntax: 'area(length, width)' | ||
}, | ||
'A' => 18 | ||
} | ||
Map-like scope (AdvancedScope example): Map(5) { | ||
'x' => 3, | ||
'y' => 6, | ||
'scalar' => 1, | ||
'area' => [Function: area] { | ||
signatures: { 'any,any': [Function (anonymous)] }, | ||
syntax: 'area(length, width)' | ||
}, | ||
'A' => 18 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Filter all symbol nodes "x" in the expression "x^2 + x/4 + 3*y" | ||
SymbolNode x | ||
SymbolNode x | ||
|
||
Traverse the expression tree of expression "3 * x + 2" | ||
OperatorNode + | ||
OperatorNode * | ||
ConstantNode 3 | ||
SymbolNode x | ||
ConstantNode 2 | ||
|
||
Replace all symbol nodes "x" in expression "x^2 + 5*x" with a constant 3 | ||
3 ^ 2 + 5 * 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Using expression parser: | ||
input: a=2, b=4 | ||
result: 6 | ||
2+4=6 | ||
|
||
Using plain JavaScript: | ||
2+4=6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 | ||
Error: Function parse is disabled |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
simplify expressions | ||
7 / 2 | ||
5 * x | ||
24 | ||
2 * x ^ 2 + x + 3 | ||
-y | ||
3 * x | ||
12 | ||
|
||
calculate derivatives | ||
4 * x + 3 | ||
2 * cos(2 x) | ||
2 * x + 1 | ||
7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
functions and constants | ||
2.718 | ||
0.75 | ||
4 | ||
2i | ||
[[7, 0], [0, 7]] | ||
2 * x + 1 | ||
|
||
expressions | ||
7.8 | ||
5 inch | ||
0.5 | ||
3 + 2i | ||
-7 | ||
|
||
chained operations | ||
14 | ||
|
||
mixed use of data types | ||
[9, 10] | ||
15 mm | ||
[-3, -2, -1] | ||
[6, 8] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
round-off errors with numbers | ||
0.30000000000000004 | ||
1.4999999999999998 | ||
|
||
no round-off errors with BigNumbers | ||
0.3 | ||
1.5 | ||
|
||
create BigNumbers from strings when exceeding the range of a number | ||
Infinity | ||
1.2e+500 | ||
|
||
BigNumbers still have a limited precision and are no silve bullet | ||
0.99999999999999999999 | ||
|
||
use BigNumbers in the expression parser | ||
0.3 | ||
1.5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
14 | ||
0.5 | ||
"0.6666666666666666" | ||
0.66666666666667 | ||
2.6666666666667 | ||
3 | ||
[[24, 6], [9, 12]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
create and manipulate complex numbers | ||
2 + 3i | ||
2 | ||
3 | ||
2 + 3i | ||
5 + 3i | ||
3 - 7i | ||
|
||
perform operations | ||
8 - 4i | ||
36 - 26i | ||
-9.6541254768548 + 2.8416922956064i | ||
2 | ||
2i | ||
|
||
create complex numbers with polar coordinates | ||
1 + i | ||
5 0.9272952180016122 | ||
|
||
|
||
comparision and sorting operations | ||
equal false | ||
values: [5 + 3i, 3 - 7i, 1 + i] | ||
sorted: [1 + i, 3 - 7i, 5 + 3i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
1. USING FUNCTION MATH.EVAL | ||
|
||
evaluate expressions | ||
5 | ||
2i | ||
5.08 cm | ||
0.70710678118655 | ||
|
||
evaluate multiple expressions at once | ||
[3, 4, 12] | ||
|
||
evaluate expressions providing a scope with variables and functions | ||
12 | ||
6.8 | ||
6.8 | ||
"hello, hero!" | ||
8 | ||
8 | ||
|
||
2. USING FUNCTION MATH.PARSE | ||
|
||
parse an expression into a node tree | ||
"sqrt(3 ^ 2 + 4 ^ 2)" | ||
5 | ||
|
||
provide a scope | ||
"x ^ a" | ||
9 | ||
27 | ||
|
||
3. USING FUNCTION MATH.COMPILE | ||
|
||
compile an expression | ||
5 | ||
|
||
provide a scope | ||
10 | ||
|
||
4. USING A PARSER | ||
|
||
evaluate expressions | ||
5 | ||
2i | ||
5.08 cm | ||
0.70710678118655 | ||
|
||
define variables and functions | ||
3.5 | ||
6.5 | ||
f2(x, y) | ||
8 | ||
|
||
manipulate matrices | ||
[[1, 2], [3, 4]] | ||
[[0, 0], [0, 0]] | ||
[5, 6] | ||
[[5, 6], [0, 0]] | ||
[7, 8] | ||
[[5, 6], [7, 8]] | ||
[[19, 22], [43, 50]] | ||
43 | ||
[[19], [43]] | ||
|
||
get and set variables and function in the scope of the parser | ||
x = 3.5 | ||
f2 = f2(x, y) | ||
h = 27 | ||
250 | ||
"hello, hero!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
basic usage | ||
1/8 | ||
8/25 | ||
1/3 | ||
1/3 | ||
2/3 | ||
2/7 | ||
|
||
round-off errors with numbers | ||
0.30000000000000004 | ||
1.4999999999999998 | ||
|
||
no round-off errors with fractions :) | ||
0.3 | ||
1.5 | ||
|
||
represent an infinite number of repeating digits | ||
0.(3) | ||
0.(285714) | ||
2.(09) | ||
|
||
use fractions in the expression parser | ||
3/10 | ||
3/2 | ||
23/11 | ||
|
||
output formatting of fractions | ||
2/3 | ||
2/3 | ||
0.(6) | ||
0.(6) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
84 | ||
"hello, user!" | ||
52 | ||
"hello, user!" | ||
Warning: To use numbers.js, the library must be installed first via `npm install numbers`. | ||
Warning: To use numeric.js, the library must be installed first via `npm install numeric`. | ||
3.14 |
Oops, something went wrong.