Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Simeon Christoffersen
test2021
Commits
6de15986
Commit
6de15986
authored
Aug 27, 2021
by
Hallvard Trætteberg
Browse files
Fikset feil i kode og test
parent
84bd1ee3
Changes
11
Hide whitespace changes
Inline
Side-by-side
javafx-template/.gitignore
View file @
6de15986
# ignore maven build folder
target/
# pom-derived eclipse jdt files
.project
.classpath
org.eclipse.*.prefs
javafx-template/src/main/java/app/Calc.java
View file @
6de15986
...
...
@@ -38,7 +38,7 @@ public class Calc {
* @throws IllegalArgumentException if n is larger than the operand count
*/
public
double
peekOperand
(
int
n
)
{
if
(
n
>
getOperandCount
())
{
if
(
n
>
=
getOperandCount
())
{
throw
new
IllegalArgumentException
(
"Cannot peek at position "
+
n
+
" when the operand count is "
+
getOperandCount
());
}
return
operandStack
.
get
(
getOperandCount
()
-
n
-
1
);
...
...
@@ -102,7 +102,7 @@ public class Calc {
* @throws IllegalStateException if the operand count is less than two
*/
public
void
swap
()
{
// TODO
}
/**
...
...
javafx-template/src/test/java/app/CalcTest.java
View file @
6de15986
...
...
@@ -86,10 +86,11 @@ public class CalcTest {
@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
);
calc
.
swap
();
checkCalc
(
calc
,
3.14
,
1.0
);
}
@Test
...
...
modules-template/.gitignore
View file @
6de15986
# ignore maven build folder
target/
# pom-derived eclipse jdt files
.project
.classpath
org.eclipse.*.prefs
modules-template/core/src/main/java/core/Calc.java
View file @
6de15986
...
...
@@ -37,7 +37,7 @@ public class Calc {
* @throws IllegalArgumentException if n is larger than the operand count
*/
public
double
peekOperand
(
int
n
)
{
if
(
n
>
getOperandCount
())
{
if
(
n
>
=
getOperandCount
())
{
throw
new
IllegalArgumentException
(
"Cannot peek at position "
+
n
+
" when the operand count is "
+
getOperandCount
());
}
return
operandStack
.
get
(
getOperandCount
()
-
n
-
1
);
...
...
modules-template/core/src/test/java/core/CalcTest.java
View file @
6de15986
...
...
@@ -86,10 +86,11 @@ public class CalcTest {
@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
);
calc
.
swap
();
checkCalc
(
calc
,
3.14
,
1.0
);
}
@Test
...
...
modules-template/src/main/java/core/Calc.java
deleted
100644 → 0
View file @
84bd1ee3
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
modules-template/src/test/java/core/CalcTest.java
deleted
100644 → 0
View file @
84bd1ee3
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
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
);
}
}
packages-template/.gitignore
View file @
6de15986
# ignore maven build folder
target/
# pom-derived eclipse jdt files
.project
.classpath
org.eclipse.*.prefs
packages-template/src/main/java/core/Calc.java
View file @
6de15986
...
...
@@ -37,7 +37,7 @@ public class Calc {
* @throws IllegalArgumentException if n is larger than the operand count
*/
public
double
peekOperand
(
int
n
)
{
if
(
n
>
getOperandCount
())
{
if
(
n
>
=
getOperandCount
())
{
throw
new
IllegalArgumentException
(
"Cannot peek at position "
+
n
+
" when the operand count is "
+
getOperandCount
());
}
return
operandStack
.
get
(
getOperandCount
()
-
n
-
1
);
...
...
packages-template/src/test/java/core/CalcTest.java
View file @
6de15986
...
...
@@ -86,10 +86,11 @@ public class CalcTest {
@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
);
calc
.
swap
();
checkCalc
(
calc
,
3.14
,
1.0
);
}
@Test
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment