forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix-expression-evaluation.test.js
64 lines (47 loc) · 1.77 KB
/
postfix-expression-evaluation.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { evaluatePostfixExpression, ERROR_STRING } = require('.');
describe('Postfix expression evaluation', () => {
it('should be a function', () => {
expect(typeof evaluatePostfixExpression).toEqual('function');
});
it('should return a number', () => {
const expression = '11+';
expect(typeof evaluatePostfixExpression(expression)).toEqual('number');
});
it('should handle addition', () => {
const expression = '23+';
const expected = 5;
expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle subtraction', () => {
const expression = '54-';
const expected = 1;
expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle multiplication', () => {
const expression = '34*';
const expected = 12;
expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle division', () => {
const expression = '62/';
const expected = 3;
expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle negative numbers', () => {
const expression = '25-';
const expected = -3;
expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
it('should handle multiple operators', () => {
const expression = '123*+';
const expected = 7;
expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
describe('should throw error on invalid expressions', () => {
const invalidExpressions = ['12', '1', '+', '1+2', '+12'];
test.each(invalidExpressions)('running for %p', (expression) => {
expect(() => evaluatePostfixExpression(expression)).toThrow(ERROR_STRING);
});
expect(() => evaluatePostfixExpression('1&2')).toThrow('Operation is not valid');
});
});