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
.quality
.null_analysis
.Nullsafe
.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, 123456789 };
38 /** The constructor methods to create storage units to test. */
40 public static List
<Function
<BigInteger
, StorageUnit
<?
>>> UNITS
= bigIntegerBasedConstructors();
44 * The number of bytes to add.
46 * The constructor function for the storage unit under test.
49 public void shouldAddNumberOfBytes(
51 final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
53 final BigInteger initialAmount
= asBigInteger(1);
54 final StorageUnit
<?
> unit
= constructor
.apply(initialAmount
);
57 final StorageUnit
<?
> calculatedResult
= unit
.add(bytes
);
58 final BigInteger expectedResult
= initialAmount
.add(BigInteger
.valueOf(bytes
));
61 Assert
.assertEquals(unit
.getClass().getSimpleName() + "cannot add '1' and '" + bytes
+ "'.",
62 expectedResult
, calculatedResult
.inByte());
67 * The number of bytes to add.
69 * The constructor function for the storage unit under test.
72 public void shouldAddStorageUnit(
74 final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
76 final BigInteger initialAmount
= asBigInteger(1);
77 final BigInteger amountToAdd
= asBigInteger(bytes
);
78 final StorageUnit
<?
> unit
= constructor
.apply(initialAmount
);
79 final StorageUnit
<?
> unitToAdd
= constructor
.apply(amountToAdd
);
82 final StorageUnit
<?
> calculatedResult
= unit
.add(unitToAdd
);
83 final BigInteger expectedResult
= initialAmount
.add(amountToAdd
);
86 Assert
.assertEquals(unit
.getClass().getSimpleName() + "cannot add '1' and '" + bytes
+ "'.",
87 expectedResult
, calculatedResult
.inByte());
92 * The constructor function for the storage unit under test.
95 public void shouldReturnNewInstanceAfterAddLong(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
97 final BigInteger initialAmount
= asBigInteger(1);
98 final StorageUnit
<?
> first
= constructor
.apply(initialAmount
);
101 final StorageUnit
<?
> second
= first
.add(1000);
104 Assert
.assertNotSame("The add(long) method must return a new instance.", first
, second
);
109 * The constructor function for the storage unit under test.
112 public void shouldReturnNewInstanceAfterDivide(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
114 final StorageUnit
<?
> first
= constructor
.apply(asBigInteger(100));
117 final StorageUnit
<?
> second
= first
.divide(5);
120 Assert
.assertNotSame("The divide(long) method must return a new instance.", first
, second
);
125 * The constructor function for the storage unit under test.
128 public void shouldReturnNewInstanceAfterMultiply(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
130 final StorageUnit
<?
> first
= constructor
.apply(asBigInteger(100));
133 final StorageUnit
<?
> second
= first
.multiply(5);
136 Assert
.assertNotSame("The multiply(long) method must return a new instance.", first
, second
);
141 * The constructor function for the storage unit under test.
144 public void shouldReturnNewInstanceAfterSubtractLong(final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
146 final StorageUnit
<?
> first
= constructor
.apply(asBigInteger(100));
149 final StorageUnit
<?
> second
= first
.subtract(20);
152 Assert
.assertNotSame("The subtract(long) method must return a new instance.", first
, second
);
157 * The number of bytes to add.
159 * The constructor function for the storage unit under test.
162 public void shouldSubtractStorageUnit(
164 final Function
<BigInteger
, StorageUnit
<?
>> constructor
) {
166 final StorageUnit
<?
> unit
= constructor
.apply(asBigInteger(bytes
));
167 final StorageUnit
<?
> unitToSubtract
= constructor
.apply(asBigInteger(1));
170 final StorageUnit
<?
> calculatedResult
= unit
.subtract(unitToSubtract
);
171 final BigInteger expectedResult
= BigInteger
.valueOf(bytes
- 1);
174 Assert
.assertEquals(unit
.getClass().getSimpleName() + "cannot subtract '1' and '" + bytes
+ "'.",
175 expectedResult
, calculatedResult
.inByte());