|
| 1 | +/*eslint-env node, mocha */ |
| 2 | + |
| 3 | +// to run: from firefly/src/firefly |
| 4 | +// node ../../node_modules/mocha/bin/mocha js/util/expr/__test__/*test.js --compilers js:babel-core/register --requires ignore-styles |
| 5 | + |
| 6 | +import {Expression} from '../expr/Expression.js'; |
| 7 | + |
| 8 | +describe('A test suite for expr/Expression.js', function () { |
| 9 | + |
| 10 | + /* run before every test-case*/ |
| 11 | + beforeEach(() => { |
| 12 | + } |
| 13 | + ); |
| 14 | + /* run after every test-case*/ |
| 15 | + afterEach(function () { |
| 16 | + |
| 17 | + } |
| 18 | + ); |
| 19 | + |
| 20 | + /* Valid operators: * / + - ^ < > = , ( ) |
| 21 | + * Supported functions: |
| 22 | + * abs, acos, asin, atan, |
| 23 | + * ceil, cos, exp, floor, |
| 24 | + * lg, ln, round, sin, sqrt, |
| 25 | + * tan, atan2, max, min |
| 26 | + * Supported conditional if(condition, then, alternative) |
| 27 | + * Supported constants: pi |
| 28 | + */ |
| 29 | + test('valid expression (functions)', function () { |
| 30 | + var x,y; |
| 31 | + |
| 32 | + for (x=-1; x<=1; x+=0.2) { |
| 33 | + const utests = [ |
| 34 | + {e: 'abs(x)', r: Math.abs(x)}, |
| 35 | + {e: 'acos(x)', r: Math.acos(x)}, |
| 36 | + {e: 'asin(x)', r: Math.asin(x)}, |
| 37 | + {e: 'atan(x)', r: Math.atan(x)}, |
| 38 | + {e: 'ceil(x)', r: Math.ceil(x)}, |
| 39 | + {e: 'cos(x)', r: Math.cos(x)}, |
| 40 | + {e: 'exp(x)', r: Math.exp(x)}, |
| 41 | + {e: 'floor(x)', r: Math.floor(x)}, |
| 42 | + {e: 'lg(x)', r: Math.log10(x)}, |
| 43 | + {e: 'log10(x)', r: Math.log10(x)}, |
| 44 | + {e: 'log(x)', r: Math.log(x)}, |
| 45 | + {e: 'ln(x)', r: Math.log(x)}, |
| 46 | + {e: 'round(x)', r: Math.round(x)}, |
| 47 | + {e: 'sin(x)', r: Math.sin(x)}, |
| 48 | + {e: 'sqrt(x)', r: Math.sqrt(x)}, |
| 49 | + {e: 'tan(x)', r: Math.tan(x)} |
| 50 | + ]; |
| 51 | + utests.forEach((t)=> { |
| 52 | + //console.log(`${t.e} with ${x}`); |
| 53 | + const e = new Expression(t.e, ['x']); |
| 54 | + expect(e.isValid()).toBeTruthy(); |
| 55 | + expect(!e.getError()).toBeTruthy(); |
| 56 | + e.setVariableValue('x', x); |
| 57 | + const r = e.getValue(); |
| 58 | + !(Number.isNaN(r)&&Number.isNaN(t.r)) && expect(r).toBe(t.r); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + for (x=-2; x<=2; x+=0.5) { |
| 63 | + for (y=-2; y<=2; y+=0.5) { |
| 64 | + const btests = [ |
| 65 | + {e: 'min(x,y)', r: Math.min(x,y)}, |
| 66 | + {e: 'max(x,y)', r: Math.max(x,y)}, |
| 67 | + {e: 'atan2(x,y)', r: Math.atan2(x,y)}, |
| 68 | + {e: 'if(x>y,x,y)', r: (x>y?x:y)} |
| 69 | + ]; |
| 70 | + btests.forEach((t)=> { |
| 71 | + //console.log(`${t.e} with ${x} and ${y}`); |
| 72 | + const e = new Expression(t.e, ['x', 'y']); |
| 73 | + expect(e.isValid()).toBeTruthy(); |
| 74 | + expect(!e.getError()).toBeTruthy(); |
| 75 | + e.setVariableValue('x', x); |
| 76 | + e.setVariableValue('y', y); |
| 77 | + const r = e.getValue(); |
| 78 | + !(Number.isNaN(r)&&Number.isNaN(t.r)) && expect(r).toBe(t.r); |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + |
| 85 | + it('valid expression (operators)', function () { |
| 86 | + const x= 0.5, y=-2.2; |
| 87 | + const expressionGood = new Expression('2*(sin(pi()-acos(x))+abs(y)^3)/x', ['x','y']); |
| 88 | + expect(expressionGood.isValid()).toBeTruthy(); |
| 89 | + expect(!expressionGood.getError()).toBeTruthy(); |
| 90 | + expressionGood.setVariableValue('x', x); |
| 91 | + expressionGood.setVariableValue('y', y); |
| 92 | + expect(expressionGood.getValue()).toBe(2*(Math.sin(Math.PI-Math.acos(x))+Math.pow(Math.abs(y),3))/x); |
| 93 | + }); |
| 94 | + it('Check order)', function () { |
| 95 | + const x= 10.0; |
| 96 | + const expressionGood = new Expression('2*x', ['x','y']); |
| 97 | + const expressionSame = new Expression('x*2', ['x','y']); |
| 98 | + expect(expressionGood.isValid()).toBeTruthy(); |
| 99 | + expect(!expressionGood.getError()).toBeTruthy(); |
| 100 | + expressionGood.setVariableValue('x', x); |
| 101 | + expect(expressionGood.getValue()).toBe(2*x); |
| 102 | + expect(expressionGood.getValue()).toBe(expressionSame.getValue()); |
| 103 | + // expect(expressionGood.getValue()).toBe(2^x); |
| 104 | + }); |
| 105 | + |
| 106 | + it('invalid expression', function () { |
| 107 | + const expressionBad = new Expression('2*sin(x)+y/z', ['x','y']); |
| 108 | + expect(expressionBad.isValid()).toBeFalsy(); |
| 109 | + const se = expressionBad.getError(); |
| 110 | + expect(se).toHaveProperty('error'); |
| 111 | + expect(se).toHaveProperty('details'); |
| 112 | + expect(se).toHaveProperty('where'); |
| 113 | + expect(se).toHaveProperty('why'); |
| 114 | + console.log(se); |
| 115 | + }); |
| 116 | + |
| 117 | + it('invalid expression', function () { |
| 118 | + const expressionBad = new Expression('2*ra + ra*2', ['x','y']); |
| 119 | + expect(expressionBad.isValid()).toBeFalsy(); |
| 120 | + const se = expressionBad.getError(); |
| 121 | + expect(se).toHaveProperty('error'); |
| 122 | + expect(se).toHaveProperty('details'); |
| 123 | + expect(se).toHaveProperty('where'); |
| 124 | + expect(se).toHaveProperty('why'); |
| 125 | + console.log(se); |
| 126 | + }); |
| 127 | + |
| 128 | + |
| 129 | +}); |
0 commit comments