Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests to postfix expression evaluation function. Resolves #34 #59

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function evaluatePostfixExpression(expression) {
s.push(Number(char));
} else {
// if char is an operator then pop two elements from stack, evaluate them accordingly based on operator.
//push the result to stack
//push the result to stack
let val1 = s.pop();
let val2 = s.pop()
let val2 = s.pop();
switch (char) {
case '+':
s.push(val2 + val1);
Expand All @@ -38,3 +38,7 @@ function evaluatePostfixExpression(expression) {
//pop the value of postfix expression
return s.pop();
}

module.exports = {
evaluatePostfixExpression,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { evaluatePostfixExpression } = require('.');

describe('Postfix expression evaluation', function () {
it('should be a function', function () {
expect(typeof evaluatePostfixExpression).toEqual('function');
});

it('should return a number', function () {
const expression = '11+';

expect(typeof evaluatePostfixExpression(expression)).toEqual('number')
});

it('should handle addition', function () {
const expression = '23+';
const expected = 5;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});

it('should handle subtraction', function () {
const expression = '54-';
const expected = 1;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});

it('should handle multiplication', function () {
const expression = '34*';
const expected = 12;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});

it('should handle division', function () {
const expression = '62/';
const expected = 3;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});

it('should handle negative numbers', function () {
const expression = '25-';
const expected = -3;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});

it('should handle multiple operators', function () {
const expression = '123*+';
const expected = 7;

expect(evaluatePostfixExpression(expression)).toEqual(expected);
});
});