1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.primitives;
16
17 import com.google.common.annotations.GwtCompatible;
18 import com.google.common.collect.ImmutableSet;
19
20 import junit.framework.TestCase;
21
22 import java.math.BigInteger;
23
24
25
26
27
28
29 @GwtCompatible(emulated = true)
30 public class UnsignedLongTest extends TestCase {
31 private static final ImmutableSet<Long> TEST_LONGS;
32 private static final ImmutableSet<BigInteger> TEST_BIG_INTEGERS;
33
34 static {
35 ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder();
36 ImmutableSet.Builder<BigInteger> testBigIntegersBuilder = ImmutableSet.builder();
37 for (long i = -3; i <= 3; i++) {
38 testLongsBuilder
39 .add(i)
40 .add(Long.MAX_VALUE + i)
41 .add(Long.MIN_VALUE + i)
42 .add(Integer.MIN_VALUE + i)
43 .add(Integer.MAX_VALUE + i);
44 BigInteger bigI = BigInteger.valueOf(i);
45 testBigIntegersBuilder
46 .add(bigI)
47 .add(BigInteger.valueOf(Long.MAX_VALUE).add(bigI))
48 .add(BigInteger.valueOf(Long.MIN_VALUE).add(bigI))
49 .add(BigInteger.valueOf(Integer.MAX_VALUE).add(bigI))
50 .add(BigInteger.valueOf(Integer.MIN_VALUE).add(bigI))
51 .add(BigInteger.ONE.shiftLeft(63).add(bigI))
52 .add(BigInteger.ONE.shiftLeft(64).add(bigI));
53 }
54 TEST_LONGS = testLongsBuilder.build();
55 TEST_BIG_INTEGERS = testBigIntegersBuilder.build();
56 }
57
58 public void testAsUnsignedAndLongValueAreInverses() {
59 for (long value : TEST_LONGS) {
60 assertEquals(
61 UnsignedLongs.toString(value), value, UnsignedLong.fromLongBits(value).longValue());
62 }
63 }
64
65 public void testAsUnsignedBigIntegerValue() {
66 for (long value : TEST_LONGS) {
67 BigInteger expected = (value >= 0)
68 ? BigInteger.valueOf(value)
69 : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64));
70 assertEquals(UnsignedLongs.toString(value), expected,
71 UnsignedLong.fromLongBits(value).bigIntegerValue());
72 }
73 }
74
75 public void testValueOfLong() {
76 for (long value : TEST_LONGS) {
77 boolean expectSuccess = value >= 0;
78 try {
79 assertEquals(value, UnsignedLong.valueOf(value).longValue());
80 assertTrue(expectSuccess);
81 } catch (IllegalArgumentException e) {
82 assertFalse(expectSuccess);
83 }
84 }
85 }
86
87 public void testValueOfBigInteger() {
88 BigInteger min = BigInteger.ZERO;
89 BigInteger max = UnsignedLong.MAX_VALUE.bigIntegerValue();
90 for (BigInteger big : TEST_BIG_INTEGERS) {
91 boolean expectSuccess =
92 big.compareTo(min) >= 0 && big.compareTo(max) <= 0;
93 try {
94 assertEquals(big, UnsignedLong.valueOf(big).bigIntegerValue());
95 assertTrue(expectSuccess);
96 } catch (IllegalArgumentException e) {
97 assertFalse(expectSuccess);
98 }
99 }
100 }
101
102 public void testToString() {
103 for (long value : TEST_LONGS) {
104 UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
105 assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString());
106 }
107 }
108
109 public void testToStringRadixQuick() {
110 int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36};
111 for (int radix : radices) {
112 for (long l : TEST_LONGS) {
113 UnsignedLong value = UnsignedLong.fromLongBits(l);
114 assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
115 }
116 }
117 }
118
119 public void testFloatValue() {
120 for (long value : TEST_LONGS) {
121 UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
122 assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue());
123 }
124 }
125
126 public void testDoubleValue() {
127 for (long value : TEST_LONGS) {
128 UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
129 assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue());
130 }
131 }
132
133 public void testPlus() {
134 for (long a : TEST_LONGS) {
135 for (long b : TEST_LONGS) {
136 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
137 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
138 long expected = aUnsigned
139 .bigIntegerValue()
140 .add(bUnsigned.bigIntegerValue())
141 .longValue();
142 UnsignedLong unsignedSum = aUnsigned.plus(bUnsigned);
143 assertEquals(expected, unsignedSum.longValue());
144 }
145 }
146 }
147
148 public void testMinus() {
149 for (long a : TEST_LONGS) {
150 for (long b : TEST_LONGS) {
151 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
152 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
153 long expected = aUnsigned
154 .bigIntegerValue()
155 .subtract(bUnsigned.bigIntegerValue())
156 .longValue();
157 UnsignedLong unsignedSub = aUnsigned.minus(bUnsigned);
158 assertEquals(expected, unsignedSub.longValue());
159 }
160 }
161 }
162
163 public void testTimes() {
164 for (long a : TEST_LONGS) {
165 for (long b : TEST_LONGS) {
166 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
167 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
168 long expected = aUnsigned
169 .bigIntegerValue()
170 .multiply(bUnsigned.bigIntegerValue())
171 .longValue();
172 UnsignedLong unsignedMul = aUnsigned.times(bUnsigned);
173 assertEquals(expected, unsignedMul.longValue());
174 }
175 }
176 }
177
178 public void testDividedBy() {
179 for (long a : TEST_LONGS) {
180 for (long b : TEST_LONGS) {
181 if (b != 0) {
182 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
183 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
184 long expected = aUnsigned
185 .bigIntegerValue()
186 .divide(bUnsigned.bigIntegerValue())
187 .longValue();
188 UnsignedLong unsignedDiv = aUnsigned.dividedBy(bUnsigned);
189 assertEquals(expected, unsignedDiv.longValue());
190 }
191 }
192 }
193 }
194
195 @SuppressWarnings("ReturnValueIgnored")
196 public void testDivideByZeroThrows() {
197 for (long a : TEST_LONGS) {
198 try {
199 UnsignedLong.fromLongBits(a).dividedBy(UnsignedLong.ZERO);
200 fail("Expected ArithmeticException");
201 } catch (ArithmeticException expected) {}
202 }
203 }
204
205 public void testMod() {
206 for (long a : TEST_LONGS) {
207 for (long b : TEST_LONGS) {
208 if (b != 0) {
209 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
210 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
211 long expected = aUnsigned
212 .bigIntegerValue()
213 .remainder(bUnsigned.bigIntegerValue())
214 .longValue();
215 UnsignedLong unsignedRem = aUnsigned.mod(bUnsigned);
216 assertEquals(expected, unsignedRem.longValue());
217 }
218 }
219 }
220 }
221
222 @SuppressWarnings("ReturnValueIgnored")
223 public void testModByZero() {
224 for (long a : TEST_LONGS) {
225 try {
226 UnsignedLong.fromLongBits(a).mod(UnsignedLong.ZERO);
227 fail("Expected ArithmeticException");
228 } catch (ArithmeticException expected) {}
229 }
230 }
231
232 public void testCompare() {
233 for (long a : TEST_LONGS) {
234 for (long b : TEST_LONGS) {
235 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
236 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
237 assertEquals(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()),
238 aUnsigned.compareTo(bUnsigned));
239 }
240 }
241 }
242
243 public void testIntValue() {
244 for (long a : TEST_LONGS) {
245 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
246 int intValue = aUnsigned.bigIntegerValue().intValue();
247 assertEquals(intValue, aUnsigned.intValue());
248 }
249 }
250 }
251