fix #23
[storage-units.git] / src / test / java / de / xn__ho_hia / utils / storage_unit / StorageUnitArithmeticBigIntegerTest.java
blob306156b5fc1aaecabef05adf9a239125cb969a19
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.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;
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, 123456789 };
38 /** The constructor methods to create storage units to test. */
39 @DataPoints
40 public static List<Function<BigInteger, StorageUnit<?>>> UNITS = bigIntegerBasedConstructors();
42 /**
43 * @param bytes
44 * The number of bytes to add.
45 * @param constructor
46 * The constructor function for the storage unit under test.
48 @Theory
49 public void shouldAddNumberOfBytes(
50 final long bytes,
51 final Function<BigInteger, StorageUnit<?>> constructor) {
52 // Given
53 final BigInteger initialAmount = asBigInteger(1);
54 final StorageUnit<?> unit = constructor.apply(initialAmount);
56 // When
57 final StorageUnit<?> calculatedResult = unit.add(bytes);
58 final BigInteger expectedResult = initialAmount.add(BigInteger.valueOf(bytes));
60 // Then
61 Assert.assertEquals(unit.getClass().getSimpleName() + "cannot add '1' and '" + bytes + "'.",
62 expectedResult, calculatedResult.inByte());
65 /**
66 * @param bytes
67 * The number of bytes to add.
68 * @param constructor
69 * The constructor function for the storage unit under test.
71 @Theory
72 public void shouldAddStorageUnit(
73 final long bytes,
74 final Function<BigInteger, StorageUnit<?>> constructor) {
75 // Given
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);
81 // When
82 final StorageUnit<?> calculatedResult = unit.add(unitToAdd);
83 final BigInteger expectedResult = initialAmount.add(amountToAdd);
85 // Then
86 Assert.assertEquals(unit.getClass().getSimpleName() + "cannot add '1' and '" + bytes + "'.",
87 expectedResult, calculatedResult.inByte());
90 /**
91 * @param constructor
92 * The constructor function for the storage unit under test.
94 @Theory
95 public void shouldReturnNewInstanceAfterAddLong(final Function<BigInteger, StorageUnit<?>> constructor) {
96 // Given
97 final BigInteger initialAmount = asBigInteger(1);
98 final StorageUnit<?> first = constructor.apply(initialAmount);
100 // When
101 final StorageUnit<?> second = first.add(1000);
103 // Then
104 Assert.assertNotSame("The add(long) method must return a new instance.", first, second);
108 * @param constructor
109 * The constructor function for the storage unit under test.
111 @Theory
112 public void shouldReturnNewInstanceAfterDivide(final Function<BigInteger, StorageUnit<?>> constructor) {
113 // Given
114 final StorageUnit<?> first = constructor.apply(asBigInteger(100));
116 // When
117 final StorageUnit<?> second = first.divide(5);
119 // Then
120 Assert.assertNotSame("The divide(long) method must return a new instance.", first, second);
124 * @param constructor
125 * The constructor function for the storage unit under test.
127 @Theory
128 public void shouldReturnNewInstanceAfterMultiply(final Function<BigInteger, StorageUnit<?>> constructor) {
129 // Given
130 final StorageUnit<?> first = constructor.apply(asBigInteger(100));
132 // When
133 final StorageUnit<?> second = first.multiply(5);
135 // Then
136 Assert.assertNotSame("The multiply(long) method must return a new instance.", first, second);
140 * @param constructor
141 * The constructor function for the storage unit under test.
143 @Theory
144 public void shouldReturnNewInstanceAfterSubtractLong(final Function<BigInteger, StorageUnit<?>> constructor) {
145 // Given
146 final StorageUnit<?> first = constructor.apply(asBigInteger(100));
148 // When
149 final StorageUnit<?> second = first.subtract(20);
151 // Then
152 Assert.assertNotSame("The subtract(long) method must return a new instance.", first, second);
156 * @param bytes
157 * The number of bytes to add.
158 * @param constructor
159 * The constructor function for the storage unit under test.
161 @Theory
162 public void shouldSubtractStorageUnit(
163 final long bytes,
164 final Function<BigInteger, StorageUnit<?>> constructor) {
165 // Given
166 final StorageUnit<?> unit = constructor.apply(asBigInteger(bytes));
167 final StorageUnit<?> unitToSubtract = constructor.apply(asBigInteger(1));
169 // When
170 final StorageUnit<?> calculatedResult = unit.subtract(unitToSubtract);
171 final BigInteger expectedResult = BigInteger.valueOf(bytes - 1);
173 // Then
174 Assert.assertEquals(unit.getClass().getSimpleName() + "cannot subtract '1' and '" + bytes + "'.",
175 expectedResult, calculatedResult.inByte());