Skip to content
Open

Mock #10

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
31 changes: 31 additions & 0 deletions 08.Mock/pom.xml
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 08.Mock/src/main/java/ru/spbau/mit/alyokhina/Calculator.java
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;
}
}
}
76 changes: 76 additions & 0 deletions 08.Mock/src/main/java/ru/spbau/mit/alyokhina/Stack.java
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 08.Mock/src/test/java/ru/spbau/mit/alyokhina/CalculatorTest.java
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в thenReturn() можно через запятую сколько угодно элементов передавать. хотя так может и более наглядно

.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());

}

}
Loading