-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from muun/51.2-release-branch
Apollo: Release source code for 51.2
- Loading branch information
Showing
5 changed files
with
98 additions
and
10 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
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
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
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
75 changes: 75 additions & 0 deletions
75
common/src/test/java/io/muun/common/model/BtcAmountTest.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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.muun.common.model; | ||
|
||
import org.assertj.core.util.Lists; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import java.util.List; | ||
|
||
import static io.muun.common.model.BtcAmount.MSATS_PER_BTC; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.withPrecision; | ||
|
||
/** | ||
* Checks that no unnecessary precision is lost when converting BTC amounts to {@link BtcAmount}. | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class BtcAmountTest { | ||
|
||
/** Number of decimal digits that can be represented in a double precision IEEE floating point | ||
* number without losing any precision. | ||
* @see jdk.internal.math.FloatingDecimal#MAX_DECIMAL_DIGITS | ||
*/ | ||
private static final int DECIMAL_DIGITS_REPRESENTABLE_IN_DOUBLE = 15; | ||
private static final int DECIMAL_DIGITS = 10; | ||
private static final double ONE_MILLI_SATOSHI_IN_BTC = 1.0d / MSATS_PER_BTC; | ||
private final double btc; | ||
|
||
public BtcAmountTest(double btc) { | ||
this.btc = btc; | ||
} | ||
|
||
@Test | ||
public void fromAmountToBtcMinimumResolution() { | ||
assertThat(BtcAmount.fromBtc(btc).toBtc()) | ||
.isEqualTo( | ||
btc, | ||
withPrecision(ONE_MILLI_SATOSHI_IN_BTC / 2) | ||
); | ||
} | ||
|
||
/** | ||
* Generates BTC amounts for all the possible quantities of <b>milliSatoshi</b>s of the form | ||
* d * 10^x + 1, i.e. a single decimal digit d followed of x zeros. Those amounts are then | ||
* perturbed by adding a single milliSatoshi. The exponent goes up to 15, which is the maximum | ||
* amount of decimal digits representable by a double precision IEEE number | ||
*/ | ||
@Parameters(name = "{index}: Amount {0,number,0.00000000000} btc") | ||
public static Iterable<Object> allOrdersOfMagnitude() { | ||
final List<Object> values = Lists.newArrayList(); | ||
|
||
for (int i = 1; i < DECIMAL_DIGITS; i++) { | ||
values.add(multipleOfOneMilliSatoshi(0, i)); | ||
} | ||
for (int position = 1; position < DECIMAL_DIGITS_REPRESENTABLE_IN_DOUBLE; position++) { | ||
for (int digit = 1; digit < DECIMAL_DIGITS; digit++) { | ||
values.add(multipleOfOneMilliSatoshi(position, digit) + ONE_MILLI_SATOSHI_IN_BTC); | ||
} | ||
} | ||
return values; | ||
} | ||
|
||
/** | ||
* Generates a BTC amount based on a quantity of <b>milliSatoshi</b>s of the form | ||
* <code>digitValue</code> * 10^<code>digitPosition</code> where <code>digitValue</code> | ||
* is a number in [0, 9]. | ||
* @param digitPosition the position of the digit | ||
* @param digitValue the digit of the amount | ||
* @return The BTC amount built as specified | ||
*/ | ||
private static double multipleOfOneMilliSatoshi(int digitPosition, int digitValue) { | ||
return ONE_MILLI_SATOSHI_IN_BTC * Math.pow(DECIMAL_DIGITS, digitPosition) * digitValue; | ||
} | ||
} |