2 * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
7 package de
.xn__ho_hia
.utils
.storage_unit
;
9 import static de
.xn__ho_hia
.utils
.storage_unit
.NullsafeMath
.asBigInteger
;
10 import static de
.xn__ho_hia
.utils
.storage_unit
.TestObjects
.bigIntegerBasedConstructors
;
12 import java
.math
.BigInteger
;
13 import java
.util
.List
;
14 import java
.util
.function
.Function
;
16 import org
.junit
.Assert
;
17 import org
.junit
.experimental
.theories
.DataPoints
;
18 import org
.junit
.experimental
.theories
.Theories
;
19 import org
.junit
.experimental
.theories
.Theory
;
20 import org
.junit
.runner
.RunWith
;
22 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
28 @RunWith(Theories
.class)
29 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.NULL
, CompilerWarnings
.STATIC_METHOD
})
30 public class StorageUnitArithmeticBigIntegerTest
{
36 public static long[] BYTES_TO_ADD
= { 1, 2, 3, 5, 8, 13, 100, 500, -500,
39 /** The constructor methods to create storage units to test. */
41 public static List
<Function
<BigInteger
, StorageUnit
<?
>>> UNITS
= bigIntegerBasedConstructors();
45 * The number of bytes to add.
47 * The constructor function for the storage unit under test.
50 public void shouldAddNumberOfBytes(
52 final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
54 final BigInteger initialAmount
= asBigInteger(1);
55 final StorageUnit
<?
> unit
= constructor
.apply(initialAmount
);
58 final StorageUnit
<?
> calculatedResult
= unit
.add(bytes
);
59 final BigInteger expectedResult
= initialAmount
.add(BigInteger
.valueOf(bytes
));
62 Assert
.assertEquals(unit
.getClass().getSimpleName() + "cannot add '1' and '" + bytes
+ "'.",
63 expectedResult
, calculatedResult
.inByte());
68 * The number of bytes to add.
70 * The constructor function for the storage unit under test.
73 public void shouldAddStorageUnit(
75 final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
77 final BigInteger initialAmount
= asBigInteger(1);
78 final BigInteger amountToAdd
= asBigInteger(bytes
);
79 final StorageUnit
<?
> unit
= constructor
.apply(initialAmount
);
80 final StorageUnit
<?
> unitToAdd
= constructor
.apply(amountToAdd
);
83 final StorageUnit
<?
> calculatedResult
= unit
.add(unitToAdd
);
84 final BigInteger expectedResult
= initialAmount
.add(amountToAdd
);
87 Assert
.assertEquals(unit
.getClass().getSimpleName() + "cannot add '1' and '" + bytes
+ "'.",
88 expectedResult
, calculatedResult
.inByte());
93 * The constructor function for the storage unit under test.
96 public void shouldReturnNewInstanceAfterAddLong(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
98 final BigInteger initialAmount
= asBigInteger(1);
99 final StorageUnit
<?
> first
= constructor
.apply(initialAmount
);
102 final StorageUnit
<?
> second
= first
.add(1000);
105 Assert
.assertNotSame("The add(long) method must return a new instance.", first
, second
);
110 * The constructor function for the storage unit under test.
113 public void shouldReturnNewInstanceAfterDivide(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
115 final StorageUnit
<?
> first
= constructor
.apply(asBigInteger(100));
118 final StorageUnit
<?
> second
= first
.divide(5);
121 Assert
.assertNotSame("The divide(long) method must return a new instance.", first
, second
);
126 * The constructor function for the storage unit under test.
129 public void shouldReturnNewInstanceAfterMultiply(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
131 final StorageUnit
<?
> first
= constructor
.apply(asBigInteger(100));
134 final StorageUnit
<?
> second
= first
.multiply(5);
137 Assert
.assertNotSame("The multiply(long) method must return a new instance.", first
, second
);
142 * The constructor function for the storage unit under test.
145 public void shouldReturnNewInstanceAfterSubtractLong(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
147 final StorageUnit
<?
> first
= constructor
.apply(asBigInteger(100));
150 final StorageUnit
<?
> second
= first
.subtract(20);
153 Assert
.assertNotSame("The subtract(long) method must return a new instance.", first
, second
);
158 * The number of bytes to add.
160 * The constructor function for the storage unit under test.
163 public void shouldSubtractStorageUnit(
165 final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
167 final StorageUnit
<?
> unit
= constructor
.apply(asBigInteger(bytes
));
168 final StorageUnit
<?
> unitToSubtract
= constructor
.apply(asBigInteger(1));
171 final StorageUnit
<?
> calculatedResult
= unit
.subtract(unitToSubtract
);
172 final BigInteger expectedResult
= BigInteger
.valueOf(bytes
- 1);
175 Assert
.assertEquals(unit
.getClass().getSimpleName() + "cannot subtract '1' and '" + bytes
+ "'.",
176 expectedResult
, calculatedResult
.inByte());