Starting expr-eval's tests.
This commit is contained in:
parent
345458f453
commit
edf4518494
8 changed files with 203 additions and 22 deletions
89
common/test/module/base.mjs
Normal file
89
common/test/module/base.mjs
Normal file
|
@ -0,0 +1,89 @@
|
|||
/**
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
||||
* Copyright (C) 2021-2024 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Load prior tests
|
||||
import "../basics/events.mjs"
|
||||
import "../basics/interface.mjs"
|
||||
|
||||
import { describe, it } from "mocha"
|
||||
import { expect } from "chai"
|
||||
import { MockDialog } from "../mock/dialog.mjs"
|
||||
import { BOOLEAN, DialogInterface, FUNCTION, NUMBER, STRING } from "../../src/module/interface.mjs"
|
||||
import { Module } from "../../src/module/common.mjs"
|
||||
|
||||
class MockModule extends Module {
|
||||
constructor() {
|
||||
super("mock", {
|
||||
number: NUMBER,
|
||||
bool: BOOLEAN,
|
||||
str: STRING,
|
||||
func: FUNCTION,
|
||||
dialog: DialogInterface
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
describe("Module/Base", function() {
|
||||
it("defined a Modules global", function() {
|
||||
expect(globalThis.Modules).to.not.be.undefined
|
||||
})
|
||||
|
||||
it("is not be initialized upon construction", function() {
|
||||
const module = new MockModule()
|
||||
expect(module.name).to.equal("mock")
|
||||
expect(module.initialized).to.be.false
|
||||
})
|
||||
|
||||
it("is only be initialized with the right arguments", function() {
|
||||
const module = new MockModule()
|
||||
const initializeWithNoArg = () => module.initialize({})
|
||||
const initializeWithSomeArg = () => module.initialize({ number: 0, str: "" })
|
||||
const initializeWithWrongType = () => module.initialize({
|
||||
number: () => {},
|
||||
str: 0,
|
||||
bool: "",
|
||||
func: false,
|
||||
dialog: null
|
||||
})
|
||||
const initializeProperly = () => module.initialize({
|
||||
number: 0,
|
||||
str: "",
|
||||
bool: true,
|
||||
func: FUNCTION,
|
||||
dialog: new MockDialog()
|
||||
})
|
||||
expect(initializeWithNoArg).to.throw(Error)
|
||||
expect(initializeWithSomeArg).to.throw(Error)
|
||||
expect(initializeWithWrongType).to.throw(Error)
|
||||
expect(initializeProperly).to.not.throw(Error)
|
||||
expect(module.initialized).to.be.true
|
||||
})
|
||||
|
||||
it("cannot be initialized twice", function() {
|
||||
const module = new MockModule()
|
||||
const initialize = () => module.initialize({
|
||||
number: 0,
|
||||
str: "",
|
||||
bool: true,
|
||||
func: FUNCTION,
|
||||
dialog: new MockDialog()
|
||||
})
|
||||
expect(initialize).to.not.throw(Error)
|
||||
expect(initialize).to.throw(Error)
|
||||
})
|
||||
})
|
187
common/test/module/expreval.mjs
Normal file
187
common/test/module/expreval.mjs
Normal file
|
@ -0,0 +1,187 @@
|
|||
/**
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
||||
* Copyright (C) 2021-2024 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Load prior tests
|
||||
import "./base.mjs"
|
||||
|
||||
import { describe, it } from "mocha"
|
||||
import { expect } from "chai"
|
||||
|
||||
import ExprEval from "../../src/module/expreval.mjs"
|
||||
|
||||
describe("Module/ExprEval", function() {
|
||||
describe("#parse", function() {
|
||||
const evaluate = (expr, vals) => ExprEval.parse(expr).evaluate(vals)
|
||||
it("parses simple mathematical expressions", function() {
|
||||
expect(evaluate("1", {})).to.equal(1)
|
||||
expect(evaluate("-1", {})).to.equal(-1)
|
||||
expect(evaluate("-(-1)", {})).to.equal(1)
|
||||
expect(evaluate("+(-1)", {})).to.equal(-1)
|
||||
expect(evaluate("3!", {})).to.equal(6)
|
||||
expect(evaluate("1+1", {})).to.equal(2)
|
||||
expect(evaluate("4*3", {})).to.equal(12)
|
||||
expect(evaluate("64/4", {})).to.equal(16)
|
||||
expect(evaluate("2^10", {})).to.equal(1024)
|
||||
expect(evaluate("10%3", {})).to.equal(1)
|
||||
expect(evaluate("10%3", {})).to.equal(1)
|
||||
// Test priorities
|
||||
expect(evaluate("10*10+10*10", {})).to.equal(200)
|
||||
expect(evaluate("10/10+10/10", {})).to.equal(2)
|
||||
expect(evaluate("10/10+10/10", {})).to.equal(2)
|
||||
expect(evaluate("2^2-2^2", {})).to.equal(0)
|
||||
expect(evaluate("(2^2-2)^2", {})).to.equal(4)
|
||||
})
|
||||
|
||||
it("parses equality and test statements", function() {
|
||||
expect(evaluate("10%3 == 1 ? 2 : 1", {})).to.equal(2)
|
||||
expect(evaluate("10%3 != 1 ? 2 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 < 3 ? 2 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 > 3 ? (2+1) : 1", {})).to.equal(3)
|
||||
expect(evaluate("10 <= 3 ? 4 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 >= 3 ? 4 : 1", {})).to.equal(4)
|
||||
// Check equality
|
||||
expect(evaluate("10 < 10 ? 2 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 > 10 ? 2 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 <= 10 ? 4 : 1", {})).to.equal(4)
|
||||
expect(evaluate("10 >= 10 ? 4 : 1", {})).to.equal(4)
|
||||
// Check 'and' and 'or'
|
||||
expect(evaluate("10 <= 3 and 10 < 10 ? 4 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 <= 10 and 10 < 10 ? 4 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 <= 10 and 10 < 20 ? 4 : 1", {})).to.equal(4)
|
||||
expect(evaluate("10 <= 3 or 10 < 10 ? 4 : 1", {})).to.equal(1)
|
||||
expect(evaluate("10 <= 10 or 10 < 10 ? 4 : 1", {})).to.equal(4)
|
||||
expect(evaluate("10 <= 10 or 10 < 20 ? 4 : 1", {})).to.equal(4)
|
||||
})
|
||||
|
||||
it("parses singular function operators (functions with one arguments and no parenthesis)", function() {
|
||||
// Trigonometric functions
|
||||
expect(evaluate("sin π", { })).to.be.approximately(0, Number.EPSILON)
|
||||
expect(evaluate("cos π", { })).to.be.approximately(-1, Number.EPSILON)
|
||||
expect(evaluate("tan π", { })).to.be.approximately(0, Number.EPSILON)
|
||||
expect(evaluate("asin 1", { })).to.be.approximately(Math.PI/2, Number.EPSILON)
|
||||
expect(evaluate("acos 1", { })).to.be.approximately(0, Number.EPSILON)
|
||||
expect(evaluate("atan 1", { })).to.be.approximately(Math.PI/4, Number.EPSILON)
|
||||
expect(evaluate("sinh 1", { })).to.be.approximately(Math.sinh(1), Number.EPSILON)
|
||||
expect(evaluate("cosh 1", { })).to.be.approximately(Math.cosh(1), Number.EPSILON)
|
||||
expect(evaluate("tanh 1", { })).to.be.approximately(Math.tanh(1), Number.EPSILON)
|
||||
expect(evaluate("asinh 1", { })).to.be.approximately(Math.asinh(1), Number.EPSILON)
|
||||
expect(evaluate("acosh 1", { })).to.be.approximately(Math.acosh(1), Number.EPSILON)
|
||||
expect(evaluate("atanh 0.5", { })).to.be.approximately(Math.atanh(0.5), Number.EPSILON)
|
||||
// Reverse trigonometric
|
||||
expect(evaluate("asin sin 1", { })).to.be.approximately(1, Number.EPSILON)
|
||||
expect(evaluate("acos cos 1", { })).to.be.approximately(1, Number.EPSILON)
|
||||
expect(evaluate("atan tan 1", { })).to.be.approximately(1, Number.EPSILON)
|
||||
expect(evaluate("asinh sinh 1", { })).to.be.approximately(1, Number.EPSILON)
|
||||
expect(evaluate("acosh cosh 1", { })).to.be.approximately(1, Number.EPSILON)
|
||||
expect(evaluate("atanh tanh 1", { })).to.be.approximately(1, Number.EPSILON)
|
||||
// Other functions
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe("#integral", function() {
|
||||
it("returns the integral value between two integers", function() {
|
||||
expect(ExprEval.integral(0, 1, "1", "t")).to.be.approximately(1, Number.EPSILON)
|
||||
expect(ExprEval.integral(0, 1, "t", "t")).to.be.approximately(1 / 2, Number.EPSILON)
|
||||
expect(ExprEval.integral(0, 1, "t^2", "t")).to.be.approximately(1 / 3, Number.EPSILON)
|
||||
expect(ExprEval.integral(0, 1, "t^3", "t")).to.be.approximately(1 / 4, 0.01)
|
||||
expect(ExprEval.integral(0, 1, "t^4", "t")).to.be.approximately(1 / 5, 0.01)
|
||||
|
||||
expect(ExprEval.integral(10, 40, "1", "t")).to.equal(30)
|
||||
expect(ExprEval.integral(20, 40, "1", "t")).to.equal(20)
|
||||
|
||||
expect(ExprEval.integral(0, 10, { execute: (x) => 1 })).to.equal(10)
|
||||
expect(ExprEval.integral(0, 10, { execute: (x) => x })).to.equal(50)
|
||||
expect(ExprEval.integral(0, 1, { execute: (x) => Math.pow(x, 2) })).to.equal(1 / 3)
|
||||
})
|
||||
|
||||
|
||||
it("throws error when provided with invalid arguments", function() {
|
||||
const noArg1 = () => ExprEval.integral()
|
||||
const noArg2 = () => ExprEval.integral(0)
|
||||
const noFunction = () => ExprEval.integral(0, 1)
|
||||
const invalidObjectProvided = () => ExprEval.integral(0, 1, { a: 2 })
|
||||
const notAnObjectProvided = () => ExprEval.integral(0, 1, "string")
|
||||
const invalidFromProvided = () => ExprEval.integral("ze", 1, "t^2", "t")
|
||||
const invalidToProvided = () => ExprEval.integral(0, "ze", "t^2", "t")
|
||||
const notStringProvided1 = () => ExprEval.integral(0, 1, { a: 2 }, { b: 1 })
|
||||
const notStringProvided2 = () => ExprEval.integral(0, 1, { a: 2 }, "t")
|
||||
const notStringProvided3 = () => ExprEval.integral(0, 1, "t^2", { b: 1 })
|
||||
const invalidVariableProvided = () => ExprEval.integral(0, 1, "t^2", "93IO74")
|
||||
const invalidExpressionProvided = () => ExprEval.integral(0, 1, "t^2t", "t")
|
||||
const invalidVariableInExpression = () => ExprEval.integral(0, 1, "t^2+x", "t")
|
||||
expect(noArg1).to.throw()
|
||||
expect(noArg2).to.throw()
|
||||
expect(noFunction).to.throw()
|
||||
expect(invalidObjectProvided).to.throw()
|
||||
expect(invalidFromProvided).to.throw()
|
||||
expect(invalidToProvided).to.throw()
|
||||
expect(notAnObjectProvided).to.throw()
|
||||
expect(notStringProvided1).to.throw()
|
||||
expect(notStringProvided2).to.throw()
|
||||
expect(notStringProvided3).to.throw()
|
||||
expect(invalidVariableProvided).to.throw()
|
||||
expect(invalidExpressionProvided).to.throw()
|
||||
expect(invalidVariableInExpression).to.throw()
|
||||
})
|
||||
})
|
||||
|
||||
describe("#derivative", function() {
|
||||
const DELTA = 1e-5
|
||||
it("returns the derivative value between two integers", function() {
|
||||
expect(ExprEval.derivative("1", "t", 2)).to.be.approximately(0, DELTA)
|
||||
expect(ExprEval.derivative("t", "t", 2)).to.be.approximately(1, DELTA)
|
||||
expect(ExprEval.derivative("t^2", "t", 2)).to.be.approximately(4, DELTA)
|
||||
expect(ExprEval.derivative("t^3", "t", 2)).to.be.approximately(12, DELTA)
|
||||
expect(ExprEval.derivative("t^4", "t", 2)).to.be.approximately(32, DELTA)
|
||||
|
||||
expect(ExprEval.derivative({ execute: (x) => 1 }, 10)).to.equal(0)
|
||||
expect(ExprEval.derivative({ execute: (x) => x }, 10)).to.be.approximately(1, DELTA)
|
||||
expect(ExprEval.derivative({ execute: (x) => Math.pow(x, 2) }, 10)).to.be.approximately(20, DELTA)
|
||||
})
|
||||
|
||||
it("throws error when provided with invalid arguments", function() {
|
||||
const noArg1 = () => ExprEval.derivative()
|
||||
const noArg2 = () => ExprEval.derivative("1")
|
||||
const noValue1 = () => ExprEval.derivative("0", "1")
|
||||
const noValue2 = () => ExprEval.derivative({ execute: (x) => 1 })
|
||||
const invalidObjectProvided = () => ExprEval.derivative({ a: 2 }, 1)
|
||||
const notAnObjectProvided = () => ExprEval.derivative("string", 1)
|
||||
const invalidXProvided = () => ExprEval.derivative("t^2+x", "t", "ze")
|
||||
const notStringProvided1 = () => ExprEval.derivative({ a: 2 }, { b: 1 }, 1)
|
||||
const notStringProvided2 = () => ExprEval.derivative({ a: 2 }, "t", 1)
|
||||
const notStringProvided3 = () => ExprEval.derivative("t^2", { b: 1 }, 1)
|
||||
const invalidVariableProvided = () => ExprEval.derivative("t^2", "93IO74", 1)
|
||||
const invalidExpressionProvided = () => ExprEval.derivative("t^2t", "t", 1)
|
||||
const invalidVariableInExpression = () => ExprEval.derivative("t^2+x", "t", 1)
|
||||
expect(noArg1).to.throw()
|
||||
expect(noArg2).to.throw()
|
||||
expect(noValue1).to.throw()
|
||||
expect(noValue2).to.throw()
|
||||
expect(invalidObjectProvided).to.throw()
|
||||
expect(invalidXProvided).to.throw()
|
||||
expect(notAnObjectProvided).to.throw()
|
||||
expect(notStringProvided1).to.throw()
|
||||
expect(notStringProvided2).to.throw()
|
||||
expect(notStringProvided3).to.throw()
|
||||
expect(invalidVariableProvided).to.throw()
|
||||
expect(invalidExpressionProvided).to.throw()
|
||||
expect(invalidVariableInExpression).to.throw()
|
||||
})
|
||||
})
|
||||
})
|
30
common/test/module/objects.mjs
Normal file
30
common/test/module/objects.mjs
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
||||
* Copyright (C) 2021-2024 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Load prior tests
|
||||
import "./base.mjs"
|
||||
import "../basics/utils.mjs"
|
||||
|
||||
import { describe, it } from "mocha"
|
||||
import { expect } from "chai"
|
||||
|
||||
// import Objects from "../../src/module/objects.mjs"
|
||||
//
|
||||
// describe("Module/Objects", function() {
|
||||
//
|
||||
// })
|
101
common/test/module/settings.mjs
Normal file
101
common/test/module/settings.mjs
Normal file
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
||||
* Copyright (C) 2021-2024 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Load prior tests
|
||||
import "./base.mjs"
|
||||
import "../basics/utils.mjs"
|
||||
|
||||
import { describe, it } from "mocha"
|
||||
import { expect } from "chai"
|
||||
|
||||
const { spy } = chaiPlugins
|
||||
|
||||
import Settings from "../../src/module/settings.mjs"
|
||||
import { BaseEvent } from "../../src/events.mjs"
|
||||
|
||||
describe("Module/Settings", function() {
|
||||
it("is defined as a global", function() {
|
||||
expect(globalThis.Modules.Settings).to.equal(Settings)
|
||||
})
|
||||
|
||||
it("has base defined properties even before initialization", function() {
|
||||
expect(Settings.saveFilename).to.be.a("string")
|
||||
expect(Settings.xzoom).to.be.a("number")
|
||||
expect(Settings.yzoom).to.be.a("number")
|
||||
expect(Settings.xmin).to.be.a("number")
|
||||
expect(Settings.ymax).to.be.a("number")
|
||||
expect(Settings.xaxisstep).to.be.a("string")
|
||||
expect(Settings.yaxisstep).to.be.a("string")
|
||||
expect(Settings.xlabel).to.be.a("string")
|
||||
expect(Settings.ylabel).to.be.a("string")
|
||||
expect(Settings.linewidth).to.be.a("number")
|
||||
expect(Settings.textsize).to.be.a("number")
|
||||
expect(Settings.logscalex).to.be.a("boolean")
|
||||
expect(Settings.showxgrad).to.be.a("boolean")
|
||||
expect(Settings.showygrad).to.be.a("boolean")
|
||||
})
|
||||
|
||||
it("can be set values, but only of the right type", function() {
|
||||
expect(() => Settings.set("xzoom", "", false)).to.throw()
|
||||
expect(() => Settings.set("xlabel", true, false)).to.throw()
|
||||
expect(() => Settings.set("showxgrad", 2, false)).to.throw()
|
||||
|
||||
expect(() => Settings.set("xzoom", 200, false)).to.not.throw()
|
||||
expect(() => Settings.set("xlabel", "x", false)).to.not.throw()
|
||||
expect(() => Settings.set("showxgrad", false, false)).to.not.throw()
|
||||
})
|
||||
|
||||
it("cannot be set unknown settings", function() {
|
||||
expect(() => Settings.set("unknown", "", false)).to.throw()
|
||||
})
|
||||
|
||||
it("sends an event when a value is set", function() {
|
||||
const listener = spy((e) => {
|
||||
expect(e).to.be.an.instanceof(BaseEvent)
|
||||
expect(e.name).to.equal("changed")
|
||||
expect(e.property).to.equal("xzoom")
|
||||
expect(e.newValue).to.equal(300)
|
||||
expect(e.byUser).to.be.true
|
||||
})
|
||||
Settings.on("changed", listener)
|
||||
Settings.set("xzoom", 300, true)
|
||||
expect(listener).to.have.been.called.once
|
||||
Settings.off("changed", listener)
|
||||
})
|
||||
|
||||
it("requires a helper to set default values", function() {
|
||||
spy.on(Settings, "set")
|
||||
expect(() => Settings.initialize({})).to.throw()
|
||||
expect(() => Settings.initialize({ helper: globalThis.Helper })).to.not.throw()
|
||||
expect(Settings.set).to.have.been.called.exactly(13)
|
||||
expect(Settings.set).to.not.have.been.called.with("saveFilename")
|
||||
expect(Settings.set).to.have.been.called.with("xzoom")
|
||||
expect(Settings.set).to.have.been.called.with("yzoom")
|
||||
expect(Settings.set).to.have.been.called.with("xmin")
|
||||
expect(Settings.set).to.have.been.called.with("ymax")
|
||||
expect(Settings.set).to.have.been.called.with("xaxisstep")
|
||||
expect(Settings.set).to.have.been.called.with("yaxisstep")
|
||||
expect(Settings.set).to.have.been.called.with("xlabel")
|
||||
expect(Settings.set).to.have.been.called.with("ylabel")
|
||||
expect(Settings.set).to.have.been.called.with("linewidth")
|
||||
expect(Settings.set).to.have.been.called.with("textsize")
|
||||
expect(Settings.set).to.have.been.called.with("logscalex")
|
||||
expect(Settings.set).to.have.been.called.with("showxgrad")
|
||||
expect(Settings.set).to.have.been.called.with("showygrad")
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue