Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c826c5

Browse files
authored
Merge pull request #59 from vobango/master
Add unit tests to postfix expression evaluation function. Resolves #34
2 parents 9e07a94 + d153c6c commit 5c826c5

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

‎src/_DataStructures_/Stack/postfix-expression-evaluation/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ function evaluatePostfixExpression(expression) {
1515
s.push(Number(char));
1616
} else {
1717
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
18-
//push the result to stack
18+
//push the result to stack
1919
let val1 = s.pop();
20-
let val2 = s.pop()
20+
let val2 = s.pop();
2121
switch (char) {
2222
case '+':
2323
s.push(val2 + val1);
@@ -38,3 +38,7 @@ function evaluatePostfixExpression(expression) {
3838
//pop the value of postfix expression
3939
return s.pop();
4040
}
41+
42+
module.exports = {
43+
evaluatePostfixExpression,
44+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { evaluatePostfixExpression } = require('.');
2+
3+
describe('Postfix expression evaluation', function () {
4+
it('should be a function', function () {
5+
expect(typeof evaluatePostfixExpression).toEqual('function');
6+
});
7+
8+
it('should return a number', function () {
9+
const expression = '11+';
10+
11+
expect(typeof evaluatePostfixExpression(expression)).toEqual('number')
12+
});
13+
14+
it('should handle addition', function () {
15+
const expression = '23+';
16+
const expected = 5;
17+
18+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
19+
});
20+
21+
it('should handle subtraction', function () {
22+
const expression = '54-';
23+
const expected = 1;
24+
25+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
26+
});
27+
28+
it('should handle multiplication', function () {
29+
const expression = '34*';
30+
const expected = 12;
31+
32+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
33+
});
34+
35+
it('should handle division', function () {
36+
const expression = '62/';
37+
const expected = 3;
38+
39+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
40+
});
41+
42+
it('should handle negative numbers', function () {
43+
const expression = '25-';
44+
const expected = -3;
45+
46+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
47+
});
48+
49+
it('should handle multiple operators', function () {
50+
const expression = '123*+';
51+
const expected = 7;
52+
53+
expect(evaluatePostfixExpression(expression)).toEqual(expected);
54+
});
55+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /