From d153c6c668e6f3f57ee99f089dec21e0c4c67eb5 Mon Sep 17 00:00:00 2001 From: Kaspar Arme Date: Wed, 9 Oct 2019 09:10:22 +0300 Subject: [PATCH] Add unit tests to postfix expression evaluation function. Resolves #34 --- .../postfix-expression-evaluation/index.js | 8 ++- .../postfix-expression-evaluation.test.js | 55 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js index 3441abb5..9db393e9 100644 --- a/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/index.js @@ -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); @@ -38,3 +38,7 @@ function evaluatePostfixExpression(expression) { //pop the value of postfix expression return s.pop(); } + +module.exports = { + evaluatePostfixExpression, +}; diff --git a/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js new file mode 100644 index 00000000..47e0de42 --- /dev/null +++ b/src/_DataStructures_/Stack/postfix-expression-evaluation/postfix-expression-evaluation.test.js @@ -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); + }); +});

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