1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 package com.google.common.primitives;
16 
17 import com.google.common.annotations.GwtCompatible;
18 import com.google.common.annotations.GwtIncompatible;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.testing.EqualsTester;
21 import com.google.common.testing.NullPointerTester;
22 import com.google.common.testing.SerializableTester;
23 import java.math.BigInteger;
24 import junit.framework.TestCase;
25 
26 /**
27  * Tests for {@code UnsignedLong}.
28  *
29  * @author Louis Wasserman
30  */
31 @GwtCompatible(emulated = true)
32 public class UnsignedLongTest extends TestCase {
33   private static final ImmutableSet<Long> TEST_LONGS;
34   private static final ImmutableSet<BigInteger> TEST_BIG_INTEGERS;
35 
36   static {
37     ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder();
38     ImmutableSet.Builder<BigInteger> testBigIntegersBuilder = ImmutableSet.builder();
39     for (long i = -3; i <= 3; i++) {
40       testLongsBuilder
41           .add(i)
42           .add(Long.MAX_VALUE + i)
43           .add(Long.MIN_VALUE + i)
44           .add(Integer.MIN_VALUE + i)
45           .add(Integer.MAX_VALUE + i);
46       BigInteger bigI = BigInteger.valueOf(i);
47       testBigIntegersBuilder
48           .add(bigI)
49           .add(BigInteger.valueOf(Long.MAX_VALUE).add(bigI))
50           .add(BigInteger.valueOf(Long.MIN_VALUE).add(bigI))
51           .add(BigInteger.valueOf(Integer.MAX_VALUE).add(bigI))
52           .add(BigInteger.valueOf(Integer.MIN_VALUE).add(bigI))
53           .add(BigInteger.ONE.shiftLeft(63).add(bigI))
add(bigI)54           .add(BigInteger.ONE.shiftLeft(64).add(bigI));
55     }
56     TEST_LONGS = testLongsBuilder.build();
57     TEST_BIG_INTEGERS = testBigIntegersBuilder.build();
58   }
59 
testAsUnsignedAndLongValueAreInverses()60   public void testAsUnsignedAndLongValueAreInverses() {
61     for (long value : TEST_LONGS) {
62       assertEquals(
63           UnsignedLongs.toString(value), value, UnsignedLong.fromLongBits(value).longValue());
64     }
65   }
66 
testAsUnsignedBigIntegerValue()67   public void testAsUnsignedBigIntegerValue() {
68     for (long value : TEST_LONGS) {
69       BigInteger expected =
70           (value >= 0)
71               ? BigInteger.valueOf(value)
72               : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64));
73       assertEquals(
74           UnsignedLongs.toString(value),
75           expected,
76           UnsignedLong.fromLongBits(value).bigIntegerValue());
77     }
78   }
79 
testValueOfLong()80   public void testValueOfLong() {
81     for (long value : TEST_LONGS) {
82       boolean expectSuccess = value >= 0;
83       try {
84         assertEquals(value, UnsignedLong.valueOf(value).longValue());
85         assertTrue(expectSuccess);
86       } catch (IllegalArgumentException e) {
87         assertFalse(expectSuccess);
88       }
89     }
90   }
91 
testValueOfBigInteger()92   public void testValueOfBigInteger() {
93     BigInteger min = BigInteger.ZERO;
94     BigInteger max = UnsignedLong.MAX_VALUE.bigIntegerValue();
95     for (BigInteger big : TEST_BIG_INTEGERS) {
96       boolean expectSuccess = big.compareTo(min) >= 0 && big.compareTo(max) <= 0;
97       try {
98         assertEquals(big, UnsignedLong.valueOf(big).bigIntegerValue());
99         assertTrue(expectSuccess);
100       } catch (IllegalArgumentException e) {
101         assertFalse(expectSuccess);
102       }
103     }
104   }
105 
testToString()106   public void testToString() {
107     for (long value : TEST_LONGS) {
108       UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
109       assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString());
110     }
111   }
112 
113   @GwtIncompatible // too slow
testToStringRadix()114   public void testToStringRadix() {
115     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
116       for (long l : TEST_LONGS) {
117         UnsignedLong value = UnsignedLong.fromLongBits(l);
118         assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
119       }
120     }
121   }
122 
testToStringRadixQuick()123   public void testToStringRadixQuick() {
124     int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36};
125     for (int radix : radices) {
126       for (long l : TEST_LONGS) {
127         UnsignedLong value = UnsignedLong.fromLongBits(l);
128         assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
129       }
130     }
131   }
132 
testFloatValue()133   public void testFloatValue() {
134     for (long value : TEST_LONGS) {
135       UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
136       assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue());
137     }
138   }
139 
testDoubleValue()140   public void testDoubleValue() {
141     for (long value : TEST_LONGS) {
142       UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
143       assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue());
144     }
145   }
146 
testPlus()147   public void testPlus() {
148     for (long a : TEST_LONGS) {
149       for (long b : TEST_LONGS) {
150         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
151         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
152         long expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).longValue();
153         UnsignedLong unsignedSum = aUnsigned.plus(bUnsigned);
154         assertEquals(expected, unsignedSum.longValue());
155       }
156     }
157   }
158 
testMinus()159   public void testMinus() {
160     for (long a : TEST_LONGS) {
161       for (long b : TEST_LONGS) {
162         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
163         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
164         long expected =
165             aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).longValue();
166         UnsignedLong unsignedSub = aUnsigned.minus(bUnsigned);
167         assertEquals(expected, unsignedSub.longValue());
168       }
169     }
170   }
171 
testTimes()172   public void testTimes() {
173     for (long a : TEST_LONGS) {
174       for (long b : TEST_LONGS) {
175         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
176         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
177         long expected =
178             aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).longValue();
179         UnsignedLong unsignedMul = aUnsigned.times(bUnsigned);
180         assertEquals(expected, unsignedMul.longValue());
181       }
182     }
183   }
184 
testDividedBy()185   public void testDividedBy() {
186     for (long a : TEST_LONGS) {
187       for (long b : TEST_LONGS) {
188         if (b != 0) {
189           UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
190           UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
191           long expected =
192               aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).longValue();
193           UnsignedLong unsignedDiv = aUnsigned.dividedBy(bUnsigned);
194           assertEquals(expected, unsignedDiv.longValue());
195         }
196       }
197     }
198   }
199 
testDivideByZeroThrows()200   public void testDivideByZeroThrows() {
201     for (long a : TEST_LONGS) {
202       try {
203         UnsignedLong.fromLongBits(a).dividedBy(UnsignedLong.ZERO);
204         fail("Expected ArithmeticException");
205       } catch (ArithmeticException expected) {
206       }
207     }
208   }
209 
testMod()210   public void testMod() {
211     for (long a : TEST_LONGS) {
212       for (long b : TEST_LONGS) {
213         if (b != 0) {
214           UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
215           UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
216           long expected =
217               aUnsigned.bigIntegerValue().remainder(bUnsigned.bigIntegerValue()).longValue();
218           UnsignedLong unsignedRem = aUnsigned.mod(bUnsigned);
219           assertEquals(expected, unsignedRem.longValue());
220         }
221       }
222     }
223   }
224 
testModByZero()225   public void testModByZero() {
226     for (long a : TEST_LONGS) {
227       try {
228         UnsignedLong.fromLongBits(a).mod(UnsignedLong.ZERO);
229         fail("Expected ArithmeticException");
230       } catch (ArithmeticException expected) {
231       }
232     }
233   }
234 
testCompare()235   public void testCompare() {
236     for (long a : TEST_LONGS) {
237       for (long b : TEST_LONGS) {
238         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
239         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
240         assertEquals(
241             aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()),
242             aUnsigned.compareTo(bUnsigned));
243       }
244     }
245   }
246 
247   @GwtIncompatible // too slow
testEquals()248   public void testEquals() {
249     EqualsTester equalsTester = new EqualsTester();
250     for (long a : TEST_LONGS) {
251       BigInteger big =
252           (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64));
253       equalsTester.addEqualityGroup(
254           UnsignedLong.fromLongBits(a),
255           UnsignedLong.valueOf(big),
256           UnsignedLong.valueOf(big.toString()),
257           UnsignedLong.valueOf(big.toString(16), 16));
258     }
259     equalsTester.testEquals();
260   }
261 
testIntValue()262   public void testIntValue() {
263     for (long a : TEST_LONGS) {
264       UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
265       int intValue = aUnsigned.bigIntegerValue().intValue();
266       assertEquals(intValue, aUnsigned.intValue());
267     }
268   }
269 
270   @GwtIncompatible // serialization
testSerialization()271   public void testSerialization() {
272     for (long a : TEST_LONGS) {
273       SerializableTester.reserializeAndAssert(UnsignedLong.fromLongBits(a));
274     }
275   }
276 
277   @GwtIncompatible // NullPointerTester
testNulls()278   public void testNulls() {
279     new NullPointerTester().testAllPublicStaticMethods(UnsignedLong.class);
280   }
281 }
282