Skip to content

Commit fd21f07

Browse files
committed
fix: test suite
1 parent e794b9f commit fd21f07

File tree

8 files changed

+40
-39
lines changed

8 files changed

+40
-39
lines changed

src/compute-engine/dictionary/arithmetic.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,18 @@ function simplifyAdd(
520520
} else {
521521
const c = getComplexValue(arg);
522522
if (c !== null) {
523-
cTotal = cTotal.add(c);
523+
if (Number.isInteger(c.re) && Number.isInteger(c.im)) {
524+
cTotal = cTotal.add(c);
525+
} else {
526+
others.push(arg);
527+
}
524528
} else {
525529
const d = getDecimalValue(arg);
526-
if (d !== null) {
530+
if (d !== null && d.isInteger()) {
527531
dTotal = dTotal.add(d);
528532
} else {
529533
const val = getNumberValue(arg);
530-
if (val !== null) {
534+
if (val !== null && Number.isInteger(val)) {
531535
numerTotal += val;
532536
} else if (isNotZero(ce, arg) !== false) {
533537
others.push(arg);

test/forms.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { expression, engine } from './utils';
1313

1414
describe('FORMS', () => {
1515
const exprs: [string, Expression, Expression][] = [
16-
['-0', { num: '-0' }, -0],
16+
['-0', { num: '-0' }, { num: '-0' }],
1717
['a-0', [SUBTRACT, 'a', 0], 'a'],
1818
['0-a', [SUBTRACT, 0, 'a'], ['Negate', 'a']],
1919
['7+2+5', [ADD, 7, 2, 5], [ADD, 2, 5, 7]],

test/groups.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('SEQUENCES AND PARENTHESES', () => {
66
`['Parentheses', ['Add', 'a', 'b']]`
77
);
88
expect(expression('-(a+b)')).toMatchInlineSnapshot(
9-
`['Negate', ['Add', 'a', 'b']]`
9+
`['Add', ['Negate', 'b'], ['Negate', 'a']]`
1010
);
1111
expect(expression('(a+(c+d))')).toMatchInlineSnapshot(
1212
`['Parentheses', ['Add', 'a', 'c', 'd']]`

test/index.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ <h1>Cortex Compute Engine REPL</h1>
114114
<div class='row'>
115115
<div id='input-label'>a = </div>
116116
<math-field id="mf" class="mathfield" tabindex="0" virtual-keyboard-mode="manual">
117-
\frac{5+3i}{2}
117+
1 + 2 + 3.001
118+
<!-- \frac{5+3i}{2} -->
118119
<!-- \frac{6}{2}+\frac{3i}{2} -->
119120
<!-- \frac{3+2i}{2} -->
120121
<!-- 2(-i)+i -->
@@ -519,12 +520,12 @@ <h2><code>evaluate(a)</code></h2>
519520
ce.assume(['Greater', 'x', 4]);
520521
ce.assume(['Equal','a', 1]);
521522

522-
console.log(ce.assumptions);
523-
console.log(ce.is(['Greater', "x", 0]));
524-
console.log(ce.is(['Equal', "x", 0]));
523+
// console.log(ce.assumptions);
524+
// console.log(ce.is(['Greater', "x", 0]));
525+
// console.log(ce.is(['Equal', "x", 0]));
525526

526-
console.log(ce.is(['Equal', "a", 1]));
527-
console.log(ce.is(['Greater', "a", 0]));
527+
// console.log(ce.is(['Equal', "a", 1]));
528+
// console.log(ce.is(['Greater', "a", 0]));
528529

529530

530531
document.getElementById('assumptions').innerHTML =

test/numbers.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ describe('NUMBERS', () => {
55
test('Parsing', () => {
66
expect(expression('1')).toMatchInlineSnapshot(`1`);
77
expect(expression('-1')).toMatchInlineSnapshot(`-1`);
8-
expect(expression('1.0')).toMatchInlineSnapshot(`1`);
9-
expect(expression('-1.0')).toMatchInlineSnapshot(`-1`);
8+
expect(expression('1.0')).toMatchInlineSnapshot(`{num: '1.0'}`);
9+
expect(expression('-1.0')).toMatchInlineSnapshot(`{num: '-1.0'}`);
1010
expect(expression('-1.1234')).toMatchInlineSnapshot(`-1.1234`);
11-
expect(expression('-1.1234e5')).toMatchInlineSnapshot(`-112340`);
12-
expect(expression('-1.1234E5')).toMatchInlineSnapshot(`-112340`);
11+
expect(expression('-1.1234e5')).toMatchInlineSnapshot(`{num: '-1.1234e5'}`);
12+
expect(expression('-1.1234E5')).toMatchInlineSnapshot(`{num: '-1.1234e5'}`);
1313
expect(expression('-1.1234e-5')).toMatchInlineSnapshot(
1414
`{num: '-1.1234e-5'}`
1515
);
1616
// Invalid expression (the argument of "num" should be a string)
1717
expect(latex({ num: 4 } as any as Expression)).toMatchInlineSnapshot(`'4'`);
18-
expect(expression('3\\times10^4')).toMatchInlineSnapshot(`30000`);
18+
expect(expression('3\\times10^4')).toMatchInlineSnapshot(`{num: '3e4'}`);
1919
});
2020
test('Parsing plus/minus', () => {
2121
expect(expression('+1')).toMatchInlineSnapshot(`1`);
@@ -31,7 +31,7 @@ describe('NUMBERS', () => {
3131
});
3232
test('Parsing digits', () => {
3333
// Number with exactly three digits after the decimal point
34-
expect(expression('3.423e4')).toMatchInlineSnapshot(`34230`);
34+
expect(expression('3.423e4')).toMatchInlineSnapshot(`{num: '3.423e4'}`);
3535
// Number with more than three, less than six digits after the decimal point
3636
expect(expression('3.42334e4')).toMatchInlineSnapshot(`{num: '3.42334e4'}`);
3737
// Number with more then 6 digits after the decimal point

test/operators.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ describe('OPERATORS', () => {
142142
`['Add', ['Negate', 'x'], -1]`
143143
);
144144
expect(expression('-(x+1)')).toMatchInlineSnapshot(
145-
`['Negate', ['Add', 'x', 1]]`
145+
`['Add', ['Negate', 'x'], -1]`
146146
);
147147
expect(expression('-x+(-(x+1))')).toMatchInlineSnapshot(
148-
`['Add', ['Negate', ['Add', 'x', 1]], ['Negate', 'x']]`
148+
`['Add', ['Negate', 'x'], ['Negate', 'x'], -1]`
149149
);
150150
expect(
151151
expression('-\\frac{-x+2\\times x}{-2\\times x + 1}')
@@ -249,15 +249,13 @@ describe('ARITHMETIC FUNCTIONS', () => {
249249
expect(expression('-x')).toMatchInlineSnapshot(`['Negate', 'x']`);
250250
expect(expression('--x')).toMatchInlineSnapshot(`['PreDecrement', 'x']`);
251251
expect(expression('-(-x)')).toMatchInlineSnapshot(`'x'`); // @todo ["Negate", ["Negate", "x"]] ?
252-
expect(expression('-i')).toMatchInlineSnapshot(
253-
`['Negate', 'ImaginaryUnit']`
254-
);
252+
expect(expression('-i')).toMatchInlineSnapshot(`['Complex', 0, -1]`);
255253
expect(expression('-\\infty')).toMatchInlineSnapshot(`{num: '-Infinity'}`);
256254
});
257255
test('Infix plus', () => {
258256
expect(expression('+1')).toMatchInlineSnapshot(`1`);
259257
expect(expression('+x')).toMatchInlineSnapshot(`'x'`);
260-
expect(expression('+i')).toMatchInlineSnapshot(`'ImaginaryUnit'`);
258+
expect(expression('+i')).toMatchInlineSnapshot(`['Complex', 0, 1]`);
261259
expect(expression('+\\infty')).toMatchInlineSnapshot(`{num: 'Infinity'}`);
262260
});
263261
test('Add/subtract', () => {

test/patterns.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ import { match } from '../src/compute-engine/patterns';
44
describe('PATTERNS MATCH', () => {
55
test('Universal wildcard', () => {
66
const pattern = ['Add', 1, '_'];
7-
expect(match(['Add', 1, 2], pattern)).toMatchInlineSnapshot(`Object {}`);
7+
expect(match(pattern, ['Add', 1, 2])).toMatchInlineSnapshot(`Object {}`);
88
// Commutative
9-
expect(match(['Add', 2, 1], pattern)).toMatchInlineSnapshot(`null`);
10-
expect(match(['Add', 2, 1, 3], pattern)).toMatchInlineSnapshot(`null`);
9+
expect(match(pattern, ['Add', 2, 1])).toMatchInlineSnapshot(`null`);
10+
expect(match(pattern, ['Add', 2, 1, 3])).toMatchInlineSnapshot(`null`);
1111
// Associative
12-
expect(match(['Add', 1, ['Add', 2, 3]], pattern)).toMatchInlineSnapshot(
12+
expect(match(pattern, ['Add', 1, ['Add', 2, 3]])).toMatchInlineSnapshot(
1313
`Object {}`
1414
);
1515
});
1616

1717
test('Named wildcard', () => {
1818
const pattern = ['Add', 1, '_a'];
19-
expect(match(['Add', 1, 2], pattern)).toMatchInlineSnapshot(`
19+
expect(match(pattern, ['Add', 1, 2])).toMatchInlineSnapshot(`
2020
Object {
2121
"a": 2,
2222
}
2323
`);
2424
// Commutative
25-
expect(match(['Add', 2, 1], pattern)).toMatchInlineSnapshot(`null`);
26-
expect(match(['Add', 2, 1, 3], pattern)).toMatchInlineSnapshot(`null`);
25+
expect(match(pattern, ['Add', 2, 1])).toMatchInlineSnapshot(`null`);
26+
expect(match(pattern, ['Add', 2, 1, 3])).toMatchInlineSnapshot(`null`);
2727
// Associative
28-
expect(match(['Add', 1, ['Add', 2, 3]], pattern)).toMatchInlineSnapshot(`
28+
expect(match(pattern, ['Add', 1, ['Add', 2, 3]])).toMatchInlineSnapshot(`
2929
Object {
3030
"a": Array [
3131
"Add",
@@ -37,16 +37,16 @@ describe('PATTERNS MATCH', () => {
3737
});
3838

3939
test('Sequence wildcard', () => {
40-
expect(match(['Add', 1, 2, 3, 4], ['Add', 1, '__a'])).toMatchInlineSnapshot(
40+
expect(match(['Add', 1, '__a'], ['Add', 1, 2, 3, 4])).toMatchInlineSnapshot(
4141
`null`
4242
);
4343
expect(
44-
match(['Add', 1, 2, 3, 4], ['Add', 1, '__a', 4])
44+
match(['Add', 1, '__a', 4], ['Add', 1, 2, 3, 4])
4545
).toMatchInlineSnapshot(`null`);
4646
expect(
47-
match(['Add', 1, 2, 3, 4], ['Add', 2, '__a', 3])
47+
match(['Add', 2, '__a', 3], ['Add', 1, 2, 3, 4])
4848
).toMatchInlineSnapshot(`null`);
49-
expect(match(['Add', 1, 2, 3, 4, 5], ['Add', 1, 2, '__a', 4, 5]))
49+
expect(match(['Add', 1, 2, '__a', 4, 5], ['Add', 1, 2, 3, 4, 5]))
5050
.toMatchInlineSnapshot(`
5151
Object {
5252
"a": Array [
@@ -56,7 +56,7 @@ describe('PATTERNS MATCH', () => {
5656
}
5757
`);
5858
expect(
59-
match(['Add', 1, 2, 4, 5], ['Add', 1, 2, '__a', 4, 5])
59+
match(['Add', 1, 2, '__a', 4, 5], ['Add', 1, 2, 4, 5])
6060
).toMatchInlineSnapshot(`null`);
6161
});
6262
});

test/supsub.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ describe('SUPSUB', () => {
114114
expect(expression('\\vec{AB}')).toMatchInlineSnapshot(
115115
`['OverVector', ['Multiply', 'A', 'B']]`
116116
); // @todo: nope...
117-
expect(expression('\\vec{AB}^{-1}')).toMatchInlineSnapshot(
118-
`['Root', ['OverVector', ['Multiply', 'A', 'B']], 1]`
119-
); // @todo: nope...
117+
expect(expression('\\vec{AB}^{-1}')).toMatchInlineSnapshot(`0`);
120118
});
121119
});
122120

0 commit comments

Comments
 (0)