-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1188 sign calculator floating point errors
- Loading branch information
1 parent
54b1127
commit bc33f40
Showing
2 changed files
with
48 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
53 changes: 32 additions & 21 deletions
53
src/test/java/de/hysky/skyblocker/utils/CalculatorTest.java
This file contains 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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
package de.hysky.skyblocker.utils; | ||
|
||
import de.hysky.skyblocker.skyblock.calculators.SignCalculator; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CalculatorTest { | ||
@Test | ||
void testShorthands() { | ||
Assertions.assertEquals(Calculator.calculate("1k"), 1000); | ||
Assertions.assertEquals(Calculator.calculate("0.12k"), 120); | ||
Assertions.assertEquals(Calculator.calculate("1k + 0.12k"), 1120); | ||
Assertions.assertEquals(Calculator.calculate("1 + 1s + 1k + 1m + 1b"), 1001001065); | ||
} | ||
@Test | ||
void testShorthands() { | ||
assertCalculation(1000, "1k"); | ||
assertCalculation(120, "0.12k"); | ||
assertCalculation(1120, "1k + 0.12k"); | ||
assertCalculation(1001001065, "1 + 1s + 1k + 1m + 1b"); | ||
} | ||
|
||
@Test | ||
void testPrecedence() { | ||
Assertions.assertEquals(Calculator.calculate("5 + 2 * 2"), 9); | ||
Assertions.assertEquals(Calculator.calculate("5 - 2 / 2"), 4); | ||
Assertions.assertEquals(Calculator.calculate("5 * (1 + 2)"), 15); | ||
} | ||
@Test | ||
void testPrecedence() { | ||
assertCalculation(9, "5 + 2 * 2"); | ||
assertCalculation(4, "5 - 2 / 2"); | ||
assertCalculation(15, "5 * (1 + 2)"); | ||
} | ||
|
||
@Test | ||
void testImplicitMultiplication() { | ||
Assertions.assertEquals(Calculator.calculate("5(2 + 2)"), 20); | ||
} | ||
@Test | ||
void testImplicitMultiplication() { | ||
assertCalculation(20, "5(2 + 2)"); | ||
} | ||
|
||
@Test | ||
void testImplicitClosingParenthesis() { | ||
Assertions.assertEquals(Calculator.calculate("5(2 + 2"), 20); | ||
} | ||
@Test | ||
void testImplicitClosingParenthesis() { | ||
assertCalculation(20, "5(2 + 2"); | ||
} | ||
|
||
@Test | ||
void testFloatingPointError() { | ||
SignCalculator.calculate("262.6m"); | ||
Assertions.assertEquals("2.626E8", SignCalculator.getNewValue(true)); | ||
} | ||
|
||
private void assertCalculation(double expected, String input) { | ||
Assertions.assertEquals(expected, Calculator.calculate(input)); | ||
} | ||
} |