update to latest parent & fix null issues
[storage-units.git] / src / test / java / de / xn__ho_hia / utils / storage_unit / StorageUnitArithmeticBigIntegerTest.java
blobe093b400ce104a06b1d2a6780ecdeb14c760338f
1 /*
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
5 * in the LICENSE file.
6 */
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;
24 /**
28 @RunWith(Theories.class)
29 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.NULL, CompilerWarnings.STATIC_METHOD })
30 public class StorageUnitArithmeticBigIntegerTest {
32 /**
35 @DataPoints
36 public static long[] BYTES_TO_ADD = { 1, 2, 3, 5, 8, 13, 100, 500, -500,
37 123456789 };
39 /** The constructor methods to create storage units to test. */
40 @DataPoints
41 public static List<Function<BigInteger, StorageUnit<?>>> UNITS = bigIntegerBasedConstructors();
43 /**
44 * @param bytes
45 * The number of bytes to add.
46 * @param constructor
47 * The constructor function for the storage unit under test.
49 @Theory
50 public void shouldAddNumberOfBytes(
51 final long bytes,
52 final Function<BigInteger, StorageUnit<?>> constructor) {
53 // Given
54 final BigInteger initialAmount = asBigInteger(1);
55 final StorageUnit<?> unit = constructor.apply(initialAmount);
57 // When
58 final StorageUnit<?> calculatedResult = unit.add(bytes);
59 final BigInteger expectedResult = initialAmount.add(BigInteger.valueOf(bytes));
61 // Then
62 Assert.assertEquals(unit.getClass().getSimpleName() + "cannot add '1' and '" + bytes + "'.",
63 expectedResult, calculatedResult.inByte());
66 /**
67 * @param bytes
68 * The number of bytes to add.
69 * @param constructor
70 * The constructor function for the storage unit under test.
72 @Theory
73 public void shouldAddStorageUnit(
74 final long bytes,
75 final Function<BigInteger, StorageUnit<?>> constructor) {
76 // Given
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);
82 // When
83 final StorageUnit<?> calculatedResult = unit.add(unitToAdd);
84 final BigInteger expectedResult = initialAmount.add(amountToAdd);
86 // Then
87 Assert.assertEquals(unit.getClass().getSimpleName() + "cannot add '1' and '" + bytes + "'.",
88 expectedResult, calculatedResult.inByte());
91 /**
92 * @param constructor
93 * The constructor function for the storage unit under test.
95 @Theory
96 public void shouldReturnNewInstanceAfterAddLong(final Function<BigInteger, StorageUnit<?>> constructor) {
97 // Given
98 final BigInteger initialAmount = asBigInteger(1);
99 final StorageUnit<?> first = constructor.apply(initialAmount);
101 // When
102 final StorageUnit<?> second = first.add(1000);
104 // Then
105 Assert.assertNotSame("The add(long) method must return a new instance.", first, second);
109 * @param constructor
110 * The constructor function for the storage unit under test.
112 @Theory
113 public void shouldReturnNewInstanceAfterDivide(final Function<BigInteger, StorageUnit<?>> constructor) {
114 // Given
115 final StorageUnit<?> first = constructor.apply(asBigInteger(100));
117 // When
118 final StorageUnit<?> second = first.divide(5);
120 // Then
121 Assert.assertNotSame("The divide(long) method must return a new instance.", first, second);
125 * @param constructor
126 * The constructor function for the storage unit under test.
128 @Theory
129 public void shouldReturnNewInstanceAfterMultiply(final Function<BigInteger, StorageUnit<?>> constructor) {
130 // Given
131 final StorageUnit<?> first = constructor.apply(asBigInteger(100));
133 // When
134 final StorageUnit<?> second = first.multiply(5);
136 // Then
137 Assert.assertNotSame("The multiply(long) method must return a new instance.", first, second);
141 * @param constructor
142 * The constructor function for the storage unit under test.
144 @Theory
145 public void shouldReturnNewInstanceAfterSubtractLong(final Function<BigInteger, StorageUnit<?>> constructor) {
146 // Given
147 final StorageUnit<?> first = constructor.apply(asBigInteger(100));
149 // When
150 final StorageUnit<?> second = first.subtract(20);
152 // Then
153 Assert.assertNotSame("The subtract(long) method must return a new instance.", first, second);
157 * @param bytes
158 * The number of bytes to add.
159 * @param constructor
160 * The constructor function for the storage unit under test.
162 @Theory
163 public void shouldSubtractStorageUnit(
164 final long bytes,
165 final Function<BigInteger, StorageUnit<?>> constructor) {
166 // Given
167 final StorageUnit<?> unit = constructor.apply(asBigInteger(bytes));
168 final StorageUnit<?> unitToSubtract = constructor.apply(asBigInteger(1));
170 // When
171 final StorageUnit<?> calculatedResult = unit.subtract(unitToSubtract);
172 final BigInteger expectedResult = BigInteger.valueOf(bytes - 1);
174 // Then
175 Assert.assertEquals(unit.getClass().getSimpleName() + "cannot subtract '1' and '" + bytes + "'.",
176 expectedResult, calculatedResult.inByte());