fix #23
[storage-units.git] / src / test / java / de / xn__ho_hia / utils / storage_unit / StorageUnitNumberOfBytesPerUnitTest.java
blob858d26ada2be0a86d0a3d0ead5d158c243085c6f
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 java.math.BigInteger;
10 import java.util.AbstractMap.SimpleEntry;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.function.Function;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
19 import org.junit.Assert;
20 import org.junit.experimental.theories.DataPoints;
21 import org.junit.experimental.theories.Theories;
22 import org.junit.experimental.theories.Theory;
23 import org.junit.runner.RunWith;
25 import de.xn__ho_hia.quality.null_analysis.Nullsafe;
26 import de.xn__ho_hia.quality.suppression.CompilerWarnings;
28 /**
29 * Test cases for the {@link StorageUnit} implementations of <code>getNumberOfBytesPerUnit()</code>.
31 @RunWith(Theories.class)
32 public class StorageUnitNumberOfBytesPerUnitTest {
34 /** The factory methods to create storage units to test. */
35 @DataPoints
36 public static List<Function<BigInteger, StorageUnit<?>>> UNITS = TestObjects.bigIntegerBasedConstructors();
38 @SuppressWarnings({ CompilerWarnings.NLS })
39 private static final Map<Class<?>, BigInteger> EXPECTED_NUMBERS = Collections.unmodifiableMap(Stream.of(
40 // binary units
41 new SimpleEntry<>(Byte.class, new BigInteger("1")),
42 new SimpleEntry<>(Kibibyte.class, new BigInteger("1024")),
43 new SimpleEntry<>(Mebibyte.class, new BigInteger("1048576")),
44 new SimpleEntry<>(Gibibyte.class, new BigInteger("1073741824")),
45 new SimpleEntry<>(Tebibyte.class, new BigInteger("1099511627776")),
46 new SimpleEntry<>(Pebibyte.class, new BigInteger("1125899906842624")),
47 new SimpleEntry<>(Exbibyte.class, new BigInteger("1152921504606846976")),
48 new SimpleEntry<>(Zebibyte.class, new BigInteger("1180591620717411303424")),
49 new SimpleEntry<>(Yobibyte.class, new BigInteger("1208925819614629174706176")),
50 // metric units
51 new SimpleEntry<>(Kilobyte.class, new BigInteger("1000")),
52 new SimpleEntry<>(Megabyte.class, new BigInteger("1000000")),
53 new SimpleEntry<>(Gigabyte.class, new BigInteger("1000000000")),
54 new SimpleEntry<>(Terabyte.class, new BigInteger("1000000000000")),
55 new SimpleEntry<>(Petabyte.class, new BigInteger("1000000000000000")),
56 new SimpleEntry<>(Exabyte.class, new BigInteger("1000000000000000000")),
57 new SimpleEntry<>(Zettabyte.class, new BigInteger("1000000000000000000000")),
58 new SimpleEntry<>(Yottabyte.class, new BigInteger("1000000000000000000000000")),
59 // common units
60 new SimpleEntry<>(CommonKilobyte.class, new BigInteger("1024")),
61 new SimpleEntry<>(CommonMegabyte.class, new BigInteger("1048576")),
62 new SimpleEntry<>(CommonGigabyte.class, new BigInteger("1073741824")),
63 new SimpleEntry<>(CommonTerabyte.class, new BigInteger("1099511627776")),
64 new SimpleEntry<>(CommonPetabyte.class, new BigInteger("1125899906842624")),
65 new SimpleEntry<>(CommonExabyte.class, new BigInteger("1152921504606846976")),
66 new SimpleEntry<>(CommonZettabyte.class, new BigInteger("1180591620717411303424")),
67 new SimpleEntry<>(CommonYottabyte.class, new BigInteger("1208925819614629174706176")))
68 .collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
70 /**
71 * Ensures that the correct number of bytes per unit is returned.
73 * @param constructor
74 * The constructor function for the storage unit under test.
76 @Theory
77 @SuppressWarnings({ "nls", "static-method" })
78 public void shouldReturnNumberOfBytesPerUnit(final Function<BigInteger, StorageUnit<?>> constructor) {
79 // Given
80 final BigInteger bytes = Nullsafe.asBigInteger(1);
81 final StorageUnit<?> unit = constructor.apply(bytes);
82 final Class<?> unitClass = unit.getClass();
84 // When
85 final BigInteger numberOfBytesPerUnit = unit.getNumberOfBytesPerUnit();
86 final BigInteger expectedNumberOfBytesPerUnit = EXPECTED_NUMBERS.get(unitClass);
88 // Then
89 Assert.assertEquals("Incorrect number of bytes per unit returned.",
90 expectedNumberOfBytesPerUnit, numberOfBytesPerUnit);