1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.core.util;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21
22
23
24
25 public class NumberFactoryTest {
26
27 final int INT_SIZE = 4;
28
29 final int LONG_SIZE = 8;
30
31
32 private static final long[] LONG_TEST_VALUES = new long[] {
33 0L,
34 1L,
35 -1L,
36 19337L,
37 1234567891011213L,
38 -11109876543211L,
39 Long.valueOf(Integer.MAX_VALUE),
40 Long.valueOf(Integer.MIN_VALUE),
41 Long.MAX_VALUE,
42 Long.MIN_VALUE,
43 };
44
45 private static final int[] INT_TEST_VALUES = new int[] {
46 0,
47 1,
48 -1,
49 19337,
50 1234567891,
51 -1110987656,
52 Integer.MAX_VALUE,
53 Integer.MIN_VALUE,
54 };
55
56 @Test
57 public void testMakeIntFromLong() {
58 for (long v : LONG_TEST_VALUES) {
59 final int vL = NumberFactory.extractLo(v);
60 final int vH = NumberFactory.extractHi(v);
61
62 final long actual = (((long) vH) << 32) | (vL & 0xffffffffL);
63 Assert.assertEquals(v, actual);
64 }
65 }
66
67 @Test
68 public void testLong2Long() {
69 for (long v : LONG_TEST_VALUES) {
70 final int vL = NumberFactory.extractLo(v);
71 final int vH = NumberFactory.extractHi(v);
72
73 Assert.assertEquals(v, NumberFactory.makeLong(vH, vL));
74 }
75 }
76
77 @Test
78 public void testLongFromByteArray2Long() {
79 for (long expected : LONG_TEST_VALUES) {
80 final byte[] b = NumberFactory.makeByteArray(expected);
81 Assert.assertEquals(expected, NumberFactory.makeLong(b));
82 }
83 }
84
85 @Test
86 public void testLongArrayFromByteArray2LongArray() {
87 final byte[] b = NumberFactory.makeByteArray(LONG_TEST_VALUES);
88 Assert.assertArrayEquals(LONG_TEST_VALUES,
89 NumberFactory.makeLongArray(b));
90 }
91
92 @Test
93 public void testIntFromByteArray2Int() {
94 for (int expected : INT_TEST_VALUES) {
95 final byte[] b = NumberFactory.makeByteArray(expected);
96 Assert.assertEquals(expected, NumberFactory.makeInt(b));
97 }
98 }
99
100 @Test
101 public void testIntArrayFromByteArray2IntArray() {
102 final byte[] b = NumberFactory.makeByteArray(INT_TEST_VALUES);
103 Assert.assertArrayEquals(INT_TEST_VALUES,
104 NumberFactory.makeIntArray(b));
105 }
106
107 @Test
108 public void testMakeIntPrecondition1() {
109 for (int i = 0; i <= 10; i++) {
110 try {
111 NumberFactory.makeInt(new byte[i]);
112 if (i != INT_SIZE) {
113 Assert.fail("Exception expected");
114 }
115 } catch (IllegalArgumentException e) {
116
117 }
118 }
119 }
120
121 @Test
122 public void testMakeIntArrayPrecondition1() {
123 for (int i = 0; i <= 20; i++) {
124 try {
125 NumberFactory.makeIntArray(new byte[i]);
126 if (i != 0 && (i % INT_SIZE != 0)) {
127 Assert.fail("Exception expected");
128 }
129 } catch (IllegalArgumentException e) {
130
131 }
132 }
133 }
134
135 @Test
136 public void testMakeLongPrecondition1() {
137 for (int i = 0; i <= 10; i++) {
138 try {
139 NumberFactory.makeLong(new byte[i]);
140 if (i != LONG_SIZE) {
141 Assert.fail("Exception expected");
142 }
143 } catch (IllegalArgumentException e) {
144
145 }
146 }
147 }
148
149 @Test
150 public void testMakeLongArrayPrecondition1() {
151 for (int i = 0; i <= 20; i++) {
152 try {
153 NumberFactory.makeLongArray(new byte[i]);
154 if (i != 0 && (i % LONG_SIZE != 0)) {
155 Assert.fail("Exception expected");
156 }
157 } catch (IllegalArgumentException e) {
158
159 }
160 }
161 }
162 }