-
Notifications
You must be signed in to change notification settings - Fork 0
Mock #10
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
Open
ilkivoo
wants to merge
1
commit into
master
Choose a base branch
from
hw8.Mock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Mock #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>08</groupId> | ||
| <artifactId>Mock</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.jetbrains</groupId> | ||
| <artifactId>annotations-java5</artifactId> | ||
| <version>15.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-all</artifactId> | ||
| <version>1.8.4</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
165 changes: 165 additions & 0 deletions
165
08.Mock/src/main/java/ru/spbau/mit/alyokhina/Calculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** By arithmetic expression receives a reverse Polish notation and computes the value */ | ||
| public class Calculator { | ||
| public static void main(String[] argv) { | ||
| if (argv.length == 0) { | ||
| System.out.println("Expected expression"); | ||
| } else { | ||
| Stack<String> expr = new Stack<String>(); | ||
| for (int i = argv[0].length() - 1; i >= 0; i--) { | ||
| if ((argv[0].charAt(i) >= '0' && argv[0].charAt(i) <= '9') || (argv[0].charAt(i) == '.')) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| while (i < argv[0].length() && ((argv[0].charAt(i) >= '0' && argv[0].charAt(i) <= '9') || (argv[0].charAt(i) == '.'))) { | ||
| sb.insert(0, argv[0].charAt(i)); | ||
| i--; | ||
| } | ||
| i++; | ||
| expr.push(sb.toString()); | ||
| } else if (argv[0].charAt(i) == '+' || argv[0].charAt(i) == '-' || argv[0].charAt(i) == '*' || | ||
| argv[0].charAt(i) == '/' || argv[0].charAt(i) == '(' || argv[0].charAt(i) == ')') { | ||
| expr.push(argv[0].substring(i, i + 1)); | ||
| } | ||
| } | ||
| Calculator calculator = new Calculator(expr); | ||
| System.out.println(calculator.getRPN()); | ||
| System.out.println(calculator.calculate()); | ||
| } | ||
| } | ||
| /** Stack for storing the expression */ | ||
| private Stack<String> expression = new Stack<String>(); | ||
|
|
||
| /** Stack for storing current operands */ | ||
| private Stack<String> operand = new Stack<String>(); | ||
|
|
||
| /** Stack for storing reverse Polish notation */ | ||
| private Stack<String> rpn = new Stack<String>(); | ||
|
|
||
| /** | ||
| * Constructor | ||
| * @param newExpression an expression that needs to be translated into a reverse Polish notation | ||
| */ | ||
| public Calculator(@NotNull Stack<String> newExpression) { | ||
| expression = newExpression; | ||
| rpn = new Stack<String>(); | ||
| operand = new Stack<String>(); | ||
| } | ||
|
|
||
| /** | ||
| * Constructor | ||
| * @param newExpression an expression that needs to be translated into a reverse Polish notation | ||
| * @param forOperand stack for storing the operands | ||
| * @param forRPN stack for storing reverse Polish notation | ||
| */ | ||
| public Calculator(@NotNull Stack<String> newExpression, @NotNull Stack<String> forOperand, @NotNull Stack<String> forRPN) { | ||
| expression = newExpression; | ||
| rpn = forRPN; | ||
| operand = forOperand; | ||
| } | ||
|
|
||
| /** | ||
| * Get reverse Polish notation | ||
| * @return reverse Polish notation as a string. All elements are separated by a space. The same value is stored in rpn | ||
| */ | ||
| public String getRPN() { | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
|
|
||
| while (!expression.isEmpty()) { | ||
| String cur = expression.pop(); | ||
| if (cur.equals("(")) { | ||
| operand.push("("); | ||
| } else if (cur.equals(")")) { | ||
| while (!operand.isEmpty() && !operand.peek().equals("(")) { | ||
| String str = operand.pop(); | ||
| rpn.push(str); | ||
| stringBuilder.append(" "); | ||
| stringBuilder.append(str); | ||
| } | ||
| if (!operand.isEmpty()) { | ||
| operand.pop(); | ||
| } | ||
| } else if (isOperand(cur)) { | ||
| while (!operand.isEmpty() && !operand.peek().equals("(") | ||
| && (priority(operand.peek()) >= priority(cur))) { | ||
| String str = operand.pop(); | ||
| rpn.push(str); | ||
| stringBuilder.append(" "); | ||
| stringBuilder.append(str); | ||
| } | ||
| operand.push(cur); | ||
| } else { | ||
| rpn.push(cur); | ||
| stringBuilder.append(" "); | ||
| stringBuilder.append(cur); | ||
| } | ||
| } | ||
| while (!operand.isEmpty()) { | ||
| String str = operand.pop(); | ||
| rpn.push(str); | ||
| stringBuilder.append(" "); | ||
| stringBuilder.append(str); | ||
| } | ||
| return stringBuilder.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the value of the expression | ||
| * @return The result of calculation of type Double | ||
| */ | ||
| public Double calculate() { | ||
| if (!rpn.isEmpty()) { | ||
| if (isOperand(rpn.peek())) { | ||
| String op = rpn.pop(); | ||
| return apply(calculate(), calculate(), op); | ||
| } else { | ||
| return (Double.parseDouble(rpn.pop())); | ||
| } | ||
| } else { | ||
| return 0.0; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Whether the string is an operand | ||
| * @param str string, which we want to check | ||
| * @return true, if this string is operand, else - false | ||
| */ | ||
| private boolean isOperand(String str) { | ||
| return (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")); | ||
| } | ||
|
|
||
| /** | ||
| * Application of operations | ||
| * @param a,b these values will be applied to the operation | ||
| * @param op the operation to be applied | ||
| * @return result of type Double | ||
| */ | ||
| @NotNull | ||
| private Double apply(Double a, Double b, String op) { | ||
| if (op.equals("+")) { | ||
| return a + b; | ||
| } else if (op.equals("-")) { | ||
| return a - b; | ||
| } else if (op.equals("*")) { | ||
| return a * b; | ||
| } else { | ||
| return a / b; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * the priority of an operation | ||
| * @param operand the operand, the priority of which will be issued | ||
| * @return priority | ||
| */ | ||
| private static int priority(@NotNull String operand) { | ||
| if (operand.equals("+") || operand.equals("-")) { | ||
| return 1; | ||
| } else { | ||
| return 2; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * Stack for Calculator | ||
| * @param <T> type of stored items | ||
| */ | ||
| public class Stack<T> { | ||
| /** First element in Stack */ | ||
| Node head = null; | ||
|
|
||
| /** | ||
| * Add element in Stack | ||
| * @param newValue element, which will be add | ||
| */ | ||
| public void push(@NotNull T newValue) { | ||
| Node newHead = new Node(); | ||
| newHead.value = newValue; | ||
| newHead.next = head; | ||
| head = newHead; | ||
| } | ||
|
|
||
| /** | ||
| * Delete last added item | ||
| * @return item that was deleted or null, if there was no such element | ||
| */ | ||
| public T pop() { | ||
| if (head != null) { | ||
| T returnValue = head.value; | ||
| head = head.next; | ||
| return returnValue; | ||
| } else | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * last added item | ||
| * @return last added item or null if stack is empty | ||
| */ | ||
| public T peek() { | ||
| if (head != null) | ||
| return head.value; | ||
| else | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Checking the existence of elements in stack | ||
| * @return tru, if stack is empty, else - false | ||
| */ | ||
| public boolean isEmpty() { | ||
| return (head == null); | ||
| } | ||
|
|
||
| /** | ||
| * Moves the stack to a string, separated by a space | ||
| * @return resulting string | ||
| */ | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| Node v = head; | ||
| while (v != null) { | ||
| sb.append((v.value).toString()); | ||
| sb.append(" "); | ||
| v = v.next; | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| /** Class for Stack. Element of Stack */ | ||
| private class Node { | ||
| private Node next; | ||
| private T value; | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
08.Mock/src/test/java/ru/spbau/mit/alyokhina/CalculatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.mockito.Mockito.*; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| public class CalculatorTest { | ||
| @Test | ||
| public void testGetRPN() { | ||
| Stack<String> expression = mock(Stack.class); | ||
| Stack<String> operand = mock(Stack.class); | ||
| Stack<String> rpn = mock(Stack.class); | ||
| when(expression.isEmpty()) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(true); | ||
|
|
||
| when(expression.pop()) | ||
| .thenReturn("(") | ||
| .thenReturn("1") | ||
| .thenReturn("+") | ||
| .thenReturn("3") | ||
| .thenReturn(")") | ||
| .thenReturn("*") | ||
| .thenReturn("2"); | ||
|
|
||
| when(operand.isEmpty()) | ||
| .thenReturn(true) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(true) | ||
| .thenReturn(false) | ||
| .thenReturn(true); | ||
|
|
||
| when(operand.peek()) | ||
| .thenReturn("+") | ||
| .thenReturn("("); | ||
|
|
||
| when(operand.pop()) | ||
| .thenReturn("+") | ||
| .thenReturn("(") | ||
| .thenReturn("*"); | ||
|
|
||
| Calculator calculator = new Calculator(expression, operand, rpn); | ||
| assertEquals(" 1 3 + 2 *", calculator.getRPN()); | ||
|
|
||
|
|
||
| verify(operand).push("("); | ||
| verify(rpn).push("1"); | ||
| verify(operand).push("+"); | ||
| verify(rpn).push("3"); | ||
| verify(rpn).push("+"); | ||
| verify(operand).push("*"); | ||
| verify(rpn).push("2"); | ||
| verify(rpn).push("*"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCalculate() { | ||
| Stack<String> expression = mock(Stack.class); | ||
| Stack<String> operand = mock(Stack.class); | ||
| Stack<String> rpn = mock(Stack.class); | ||
| when(rpn.isEmpty()) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(false) | ||
| .thenReturn(true); | ||
| when(rpn.peek()) | ||
| .thenReturn("*") | ||
| .thenReturn("2") | ||
| .thenReturn("+") | ||
| .thenReturn("3") | ||
| .thenReturn("1"); | ||
|
|
||
| when(rpn.pop()) | ||
| .thenReturn("*") | ||
| .thenReturn("2") | ||
| .thenReturn("+") | ||
| .thenReturn("3") | ||
| .thenReturn("1"); | ||
| Calculator calculator = new Calculator(expression, operand, rpn); | ||
| assertEquals((Double) 8.0, calculator.calculate()); | ||
|
|
||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
в thenReturn() можно через запятую сколько угодно элементов передавать. хотя так может и более наглядно