Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Check return code, stdout, stderr of (almost) every example #2447

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/advanced/custom_argument_parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ integrate.transform = function (args, math, scope) {

// create a new scope, linked to the provided scope. We use this new scope
// to apply the variable.
const fnScope = Object.create(scope)
const fnScope = new Map(scope)

// construct a function which evaluates the first parameter f after applying
// a value for parameter x.
const fnCode = args[0].compile()
const f = function (x) {
fnScope[variable] = x
fnScope.set(variable, x)
return fnCode.evaluate(fnScope)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ console.log('d =', format(d)) // outputs "d = 7/9"
// Now, when bundling your application for use in the browser, only the used
// parts of math.js will be bundled. For example to create a bundle using Webpack:
//
// npx webpack custom_loading.js -o custom_loading.bundle.js --mode=production
// npx webpack custom_loading.mjs -o custom_loading.bundle.js --mode=production
//
7 changes: 6 additions & 1 deletion examples/advanced/more_secure_eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ math.import({
}, { override: true })

console.log(limitedEvaluate('sqrt(16)')) // Ok, 4
console.log(limitedEvaluate('parse("2+3")')) // Error: Function parse is disabled
try {
console.log(limitedEvaluate('parse("2+3")')) // Error: Function parse is disabled
console.log('Oops, it worked!')
} catch (err) {
console.log(err.toString())
}
45 changes: 45 additions & 0 deletions test/node-tests/examples.test.js
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 })
const 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()
})
})
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fraction 7/12
BigNumber 2
BigNumber 3.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.6667254718034714
0.6667254718034714
4.000000000000003
2 changes: 2 additions & 0 deletions test/node-tests/examples/advanced/custom_datatype.js.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CustomValue:5
CustomValue:9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
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
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 test/node-tests/examples/advanced/custom_scope_objects.js.out
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
}
13 changes: 13 additions & 0 deletions test/node-tests/examples/advanced/expression_trees.js.out
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
7 changes: 7 additions & 0 deletions test/node-tests/examples/advanced/function_transform.js.out
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
2 changes: 2 additions & 0 deletions test/node-tests/examples/advanced/more_secure_eval.js.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4
Error: Function parse is disabled
3 changes: 3 additions & 0 deletions test/node-tests/examples/advanced/use_bigint.js.out

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions test/node-tests/examples/algebra.js.out
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
24 changes: 24 additions & 0 deletions test/node-tests/examples/basic_usage.js.out
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]

19 changes: 19 additions & 0 deletions test/node-tests/examples/bignumbers.js.out
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

7 changes: 7 additions & 0 deletions test/node-tests/examples/chaining.js.out
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]]
24 changes: 24 additions & 0 deletions test/node-tests/examples/complex_numbers.js.out
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]
69 changes: 69 additions & 0 deletions test/node-tests/examples/expressions.js.out
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!"
32 changes: 32 additions & 0 deletions test/node-tests/examples/fractions.js.out
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)

7 changes: 7 additions & 0 deletions test/node-tests/examples/import.js.out
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
Loading