As part of NTNU's increased focus on security, all users are now forced to enable and use Two-factor authentication.

Commit b643a36a authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Laget to varianter til og la til flere tester

parent ccc6a983
{
"java.configuration.updateBuildConfiguration": "automatic"
}
\ No newline at end of file
......@@ -25,6 +25,7 @@ public class Calc {
/**
* Pushes a new operand onto top of the stack.
*
* @param d the new operand
*/
public void pushOperand(double d) {
......@@ -52,6 +53,7 @@ public class Calc {
/**
* Removes and returns the top operand.
*
* @return the top operand
* @throws IllegalStateException if the stack is empty
*/
......@@ -65,6 +67,7 @@ public class Calc {
/**
* Performs the provided operation in the top operand, and
* replaces it with the result.
*
* @param op the operation to perform
* @return the result of performing the operation
* @throws IllegalStateException if the operand stack is empty
......@@ -77,6 +80,7 @@ public class Calc {
/**
* Performs the provided operation in the two topmost operands, and
* replaces them with the result.
*
* @param op the operation to perform
* @return the result of performing the operation
* @throws IllegalStateException if the operand count is less than two
......@@ -94,6 +98,8 @@ public class Calc {
/**
* Swaps the two topmost operands.
*
* @throws IllegalStateException if the operand count is less than two
*/
public void swap() {
// TODO
......@@ -101,6 +107,8 @@ public class Calc {
/**
* Duplicates the top operand.
*
* @throws IllegalStateException if the operand stack is empty
*/
public void dup() {
// TODO
......
......@@ -28,6 +28,21 @@ public class CalcTest {
checkCalc(calc, 3.14, 1.0);
}
@Test
public void testPeekOperand() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.peekOperand());
Assertions.assertThrows(IllegalArgumentException.class, () -> new Calc().peekOperand());
}
@Test
public void testPeekOperandN() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.peekOperand(0));
Assertions.assertEquals(1.0, calc.peekOperand(1));
Assertions.assertThrows(IllegalArgumentException.class, () -> calc.peekOperand(2));
}
@Test
public void testPopOperand() {
Calc calc = new Calc(1.0, 3.14);
......@@ -36,4 +51,64 @@ public class CalcTest {
Assertions.assertEquals(1.0, calc.popOperand());
checkCalc(calc);
}
@Test
public void testPopOperand_emptyStack() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().popOperand());
}
@Test
public void testPerformOperation1() {
Calc calc = new Calc(1.0);
Assertions.assertEquals(-1.0, calc.performOperation(n -> -n));
checkCalc(calc, -1.0);
}
@Test
public void testPerformOperation1_emptyOperandStack() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().performOperation(n -> -n));
}
@Test
public void testPerformOperation2() {
Calc calc = new Calc(1.0, 3.0);
Assertions.assertEquals(-2.0, calc.performOperation((n1, n2) -> n1 - n2));
checkCalc(calc, -2.0);
}
@Test
public void testPerformOperation2_lessThanTwoOperands() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc(1.0).performOperation((n1, n2) -> n1 - n2));
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().performOperation((n1, n2) -> n1 - n2));
}
@Test
public void testSwap() {
Calc calc = new Calc(1.0, 3.14);
calc.swap();
checkCalc(calc, 3.14, 1.0);
calc.swap();
checkCalc(calc, 1.0, 3.14);
}
@Test
public void testSwap_lessThanTwoOperands() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc(1.0).swap());
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().swap());
}
@Test
public void testDup() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.popOperand());
checkCalc(calc, 1.0);
Assertions.assertEquals(1.0, calc.popOperand());
checkCalc(calc);
}
@Test
public void testDup_emptyOperandStack() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().dup());
}
}
# ignore maven build folder
target/
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it1901</groupId>
<artifactId>modules-core</artifactId>
<parent>
<groupId>it1901</groupId>
<artifactId>modules-template</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<!-- junit testing with jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package core;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;
public class Calc {
private final List<Double> operandStack;
public Calc(double... operands) {
operandStack = new ArrayList<>(operands.length + 2);
for (var d : operands) {
operandStack.add(d);
}
}
/**
* @return the number of operands on the stack
*/
public int getOperandCount() {
return operandStack.size();
}
/**
* Pushes a new operand onto top of the stack.
* @param d the new operand
*/
public void pushOperand(double d) {
operandStack.add(d);
}
/**
* @param n the place (from the top) to peek
* @return the n'th operand from the top
* @throws IllegalArgumentException if n is larger than the operand count
*/
public double peekOperand(int n) {
if (n > getOperandCount()) {
throw new IllegalArgumentException("Cannot peek at position " + n + " when the operand count is " + getOperandCount());
}
return operandStack.get(getOperandCount() - n - 1);
}
/**
* @return the top operand
*/
public double peekOperand() {
return peekOperand(0);
}
/**
* Removes and returns the top operand.
* @return the top operand
* @throws IllegalStateException if the stack is empty
*/
public double popOperand() {
if (getOperandCount() == 0) {
throw new IllegalStateException("Cannot pop from an empty stack");
}
return operandStack.remove(operandStack.size() - 1);
}
/**
* Performs the provided operation in the top operand, and
* replaces it with the result.
* @param op the operation to perform
* @return the result of performing the operation
* @throws IllegalStateException if the operand stack is empty
*/
public double performOperation(UnaryOperator<Double> op) throws IllegalStateException {
// TODO
return 0.0;
}
/**
* Performs the provided operation in the two topmost operands, and
* replaces them with the result.
* @param op the operation to perform
* @return the result of performing the operation
* @throws IllegalStateException if the operand count is less than two
*/
public double performOperation(BinaryOperator<Double> op) throws IllegalStateException {
if (getOperandCount() < 2) {
throw new IllegalStateException("Too few operands (" + getOperandCount() + ") on the stack");
}
var op1 = popOperand();
var op2 = popOperand();
var result = op.apply(op1, op2);
pushOperand(result);
return result;
}
/**
* Swaps the two topmost operands.
*/
public void swap() {
// TODO
}
/**
* Duplicates the top operand.
*/
public void dup() {
// TODO
}
}
\ No newline at end of file
package core;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalcTest {
private static void checkCalc(Calc calc, double... operands) {
Assertions.assertEquals(operands.length, calc.getOperandCount(), "Wrong operand count");
for (int i = 0; i < operands.length; i++) {
Assertions.assertEquals(operands[i], calc.peekOperand(i), "Wrong value at #" + i + " of operand stack");
}
}
@Test
public void testCalc() {
checkCalc(new Calc());
checkCalc(new Calc(1.0), 1.0);
checkCalc(new Calc(3.14, 1.0), 1.0, 3.14);
}
@Test
public void testPushOperand() {
Calc calc = new Calc();
calc.pushOperand(1.0);
checkCalc(calc, 1.0);
calc.pushOperand(3.14);
checkCalc(calc, 3.14, 1.0);
}
@Test
public void testPeekOperand() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.peekOperand());
Assertions.assertThrows(IllegalArgumentException.class, () -> new Calc().peekOperand());
}
@Test
public void testPeekOperandN() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.peekOperand(0));
Assertions.assertEquals(1.0, calc.peekOperand(1));
Assertions.assertThrows(IllegalArgumentException.class, () -> calc.peekOperand(2));
}
@Test
public void testPopOperand() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.popOperand());
checkCalc(calc, 1.0);
Assertions.assertEquals(1.0, calc.popOperand());
checkCalc(calc);
}
@Test
public void testPopOperand_emptyStack() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().popOperand());
}
@Test
public void testPerformOperation1() {
Calc calc = new Calc(1.0);
Assertions.assertEquals(-1.0, calc.performOperation(n -> -n));
checkCalc(calc, -1.0);
}
@Test
public void testPerformOperation1_emptyOperandStack() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().performOperation(n -> -n));
}
@Test
public void testPerformOperation2() {
Calc calc = new Calc(1.0, 3.0);
Assertions.assertEquals(-2.0, calc.performOperation((n1, n2) -> n1 - n2));
checkCalc(calc, -2.0);
}
@Test
public void testPerformOperation2_lessThanTwoOperands() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc(1.0).performOperation((n1, n2) -> n1 - n2));
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().performOperation((n1, n2) -> n1 - n2));
}
@Test
public void testSwap() {
Calc calc = new Calc(1.0, 3.14);
calc.swap();
checkCalc(calc, 3.14, 1.0);
calc.swap();
checkCalc(calc, 1.0, 3.14);
}
@Test
public void testSwap_lessThanTwoOperands() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc(1.0).swap());
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().swap());
}
@Test
public void testDup() {
Calc calc = new Calc(1.0, 3.14);
Assertions.assertEquals(3.14, calc.popOperand());
checkCalc(calc, 1.0);
Assertions.assertEquals(1.0, calc.popOperand());
checkCalc(calc);
}
@Test
public void testDup_emptyOperandStack() {
Assertions.assertThrows(IllegalStateException.class, () -> new Calc().dup());
}
}
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it1901</groupId>
<artifactId>modules-template</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>16</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>core</module>
<module>ui</module>
</modules>
</project>
package core;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;
public class Calc {
private final List<Double> operandStack;
public Calc(double... operands) {
operandStack = new ArrayList<>(operands.length + 2);
for (var d : operands) {
operandStack.add(d);
}
}
/**
* @return the number of operands on the stack
*/
public int getOperandCount() {
return operandStack.size();
}
/**
* Pushes a new operand onto top of the stack.
* @param d the new operand
*/
public void pushOperand(double d) {
operandStack.add(d);
}
/**
* @param n the place (from the top) to peek
* @return the n'th operand from the top
* @throws IllegalArgumentException if n is larger than the operand count
*/
public double peekOperand(int n) {
if (n > getOperandCount()) {
throw new IllegalArgumentException("Cannot peek at position " + n + " when the operand count is " + getOperandCount());
}
return operandStack.get(getOperandCount() - n - 1);
}
/**
* @return the top operand
*/
public double peekOperand() {
return peekOperand(0);
}
/**
* Removes and returns the top operand.
* @return the top operand
* @throws IllegalStateException if the stack is empty
*/
public double popOperand() {
if (getOperandCount() == 0) {
throw new IllegalStateException("Cannot pop from an empty stack");
}
return operandStack.remove(operandStack.size() - 1);
}
/**
* Performs the provided operation in the top operand, and
* replaces it with the result.
* @param op the operation to perform
* @return the result of performing the operation
* @throws IllegalStateException if the operand stack is empty
*/
public double performOperation(UnaryOperator<Double> op) throws IllegalStateException {
// TODO
return 0.0;
}
/**
* Performs the provided operation in the two topmost operands, and
* replaces them with the result.
* @param op the operation to perform
* @return the result of performing the operation
* @throws IllegalStateException if the operand count is less than two
*/
public double performOperation(BinaryOperator<Double> op) throws IllegalStateException {
if (getOperandCount() < 2) {
throw new IllegalStateException("Too few operands (" + getOperandCount() + ") on the stack");
}
var op1 = popOperand();
var op2 = popOperand();
var result = op.apply(op1, op2);
pushOperand(result);
return result;
}
/**
* Swaps the two topmost operands.
*/
public void swap() {
// TODO
}
/**
* Duplicates the top operand.
*/
public void dup() {
// TODO
}
}
\ No newline at end of file
package core;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalcTest {
private static void checkCalc(Calc calc, double... operands) {
Assertions.assertEquals(operands.length, calc.getOperandCount(), "Wrong operand count");
for (int i = 0; i < operands.length; i++) {
Assertions.assertEquals(operands[i], calc.peekOperand(i), "Wrong value at #" + i + " of operand stack");
}
}
@Test
public void testCalc() {
checkCalc(new Calc());