• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
18 import static com.google.common.truth.Truth.assertWithMessage;
19 
20 import com.google.common.annotations.GwtCompatible;
21 import com.google.common.annotations.GwtIncompatible;
22 import com.google.common.annotations.J2ktIncompatible;
23 import com.google.common.collect.ImmutableSet;
24 import com.google.common.testing.EqualsTester;
25 import com.google.common.testing.NullPointerTester;
26 import com.google.common.testing.SerializableTester;
27 import java.math.BigInteger;
28 import junit.framework.TestCase;
29 
30 /**
31  * Tests for {@code UnsignedLong}.
32  *
33  * @author Louis Wasserman
34  */
35 @GwtCompatible(emulated = true)
36 public class UnsignedLongTest extends TestCase {
37   private static final ImmutableSet<Long> TEST_LONGS;
38   private static final ImmutableSet<BigInteger> TEST_BIG_INTEGERS;
39 
40   static {
41     ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder();
42     ImmutableSet.Builder<BigInteger> testBigIntegersBuilder = ImmutableSet.builder();
43 
44     // The values here look like 111...11101...010 in binary, where the initial 111...1110 takes
45     // up exactly as many bits as can be represented in the significand (24 for float, 53 for
46     // double). That final 0 should be rounded up to 1 because the remaining bits make that number
47     // slightly nearer.
48     long floatConversionTest = 0xfffffe8000000002L;
49     long doubleConversionTest = 0xfffffffffffff402L;
50 
51     for (long i = -3; i <= 3; i++) {
52       testLongsBuilder
53           .add(i)
54           .add(Long.MAX_VALUE + i)
55           .add(Long.MIN_VALUE + i)
56           .add(Integer.MIN_VALUE + i)
57           .add(Integer.MAX_VALUE + i)
58           .add(floatConversionTest + i)
59           .add(doubleConversionTest + i);
60       BigInteger bigI = BigInteger.valueOf(i);
61       testBigIntegersBuilder
62           .add(bigI)
63           .add(BigInteger.valueOf(Long.MAX_VALUE).add(bigI))
64           .add(BigInteger.valueOf(Long.MIN_VALUE).add(bigI))
65           .add(BigInteger.valueOf(Integer.MAX_VALUE).add(bigI))
66           .add(BigInteger.valueOf(Integer.MIN_VALUE).add(bigI))
67           .add(BigInteger.ONE.shiftLeft(63).add(bigI))
add(bigI)68           .add(BigInteger.ONE.shiftLeft(64).add(bigI));
69     }
70     TEST_LONGS = testLongsBuilder.build();
71     TEST_BIG_INTEGERS = testBigIntegersBuilder.build();
72   }
73 
testAsUnsignedAndLongValueAreInverses()74   public void testAsUnsignedAndLongValueAreInverses() {
75     for (long value : TEST_LONGS) {
76       assertWithMessage(UnsignedLongs.toString(value))
77           .that(UnsignedLong.fromLongBits(value).longValue())
78           .isEqualTo(value);
79     }
80   }
81 
testAsUnsignedBigIntegerValue()82   public void testAsUnsignedBigIntegerValue() {
83     for (long value : TEST_LONGS) {
84       BigInteger expected =
85           (value >= 0)
86               ? BigInteger.valueOf(value)
87               : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64));
88       assertWithMessage(UnsignedLongs.toString(value))
89           .that(UnsignedLong.fromLongBits(value).bigIntegerValue())
90           .isEqualTo(expected);
91     }
92   }
93 
testValueOfLong()94   public void testValueOfLong() {
95     for (long value : TEST_LONGS) {
96       boolean expectSuccess = value >= 0;
97       try {
98         assertThat(UnsignedLong.valueOf(value).longValue()).isEqualTo(value);
99         assertThat(expectSuccess).isTrue();
100       } catch (IllegalArgumentException e) {
101         assertThat(expectSuccess).isFalse();
102       }
103     }
104   }
105 
106 
testValueOfBigInteger()107   public void testValueOfBigInteger() {
108     BigInteger min = BigInteger.ZERO;
109     BigInteger max = UnsignedLong.MAX_VALUE.bigIntegerValue();
110     for (BigInteger big : TEST_BIG_INTEGERS) {
111       boolean expectSuccess = big.compareTo(min) >= 0 && big.compareTo(max) <= 0;
112       try {
113         assertThat(UnsignedLong.valueOf(big).bigIntegerValue()).isEqualTo(big);
114         assertThat(expectSuccess).isTrue();
115       } catch (IllegalArgumentException e) {
116         assertThat(expectSuccess).isFalse();
117       }
118     }
119   }
120 
testToString()121   public void testToString() {
122     for (long value : TEST_LONGS) {
123       UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
124       assertThat(unsignedValue.toString()).isEqualTo(unsignedValue.bigIntegerValue().toString());
125     }
126   }
127 
128   @GwtIncompatible // too slow
testToStringRadix()129   public void testToStringRadix() {
130     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
131       for (long l : TEST_LONGS) {
132         UnsignedLong value = UnsignedLong.fromLongBits(l);
133         assertThat(value.toString(radix)).isEqualTo(value.bigIntegerValue().toString(radix));
134       }
135     }
136   }
137 
testToStringRadixQuick()138   public void testToStringRadixQuick() {
139     int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36};
140     for (int radix : radices) {
141       for (long l : TEST_LONGS) {
142         UnsignedLong value = UnsignedLong.fromLongBits(l);
143         assertThat(value.toString(radix)).isEqualTo(value.bigIntegerValue().toString(radix));
144       }
145     }
146   }
147 
148   @AndroidIncompatible // b/28251030, re-enable when the fix is everywhere we run this test
testFloatValue()149   public void testFloatValue() {
150     for (long value : TEST_LONGS) {
151       UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
152       assertWithMessage("Float value of " + unsignedValue)
153           .that(unsignedValue.floatValue())
154           .isEqualTo(unsignedValue.bigIntegerValue().floatValue());
155     }
156   }
157 
testDoubleValue()158   public void testDoubleValue() {
159     for (long value : TEST_LONGS) {
160       UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
161       assertWithMessage("Double value of " + unsignedValue)
162           .that(unsignedValue.doubleValue())
163           .isEqualTo(unsignedValue.bigIntegerValue().doubleValue());
164     }
165   }
166 
testPlus()167   public void testPlus() {
168     for (long a : TEST_LONGS) {
169       for (long b : TEST_LONGS) {
170         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
171         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
172         long expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).longValue();
173         UnsignedLong unsignedSum = aUnsigned.plus(bUnsigned);
174         assertThat(unsignedSum.longValue()).isEqualTo(expected);
175       }
176     }
177   }
178 
testMinus()179   public void testMinus() {
180     for (long a : TEST_LONGS) {
181       for (long b : TEST_LONGS) {
182         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
183         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
184         long expected =
185             aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).longValue();
186         UnsignedLong unsignedSub = aUnsigned.minus(bUnsigned);
187         assertThat(unsignedSub.longValue()).isEqualTo(expected);
188       }
189     }
190   }
191 
testTimes()192   public void testTimes() {
193     for (long a : TEST_LONGS) {
194       for (long b : TEST_LONGS) {
195         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
196         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
197         long expected =
198             aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).longValue();
199         UnsignedLong unsignedMul = aUnsigned.times(bUnsigned);
200         assertThat(unsignedMul.longValue()).isEqualTo(expected);
201       }
202     }
203   }
204 
testDividedBy()205   public void testDividedBy() {
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 =
212               aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).longValue();
213           UnsignedLong unsignedDiv = aUnsigned.dividedBy(bUnsigned);
214           assertThat(unsignedDiv.longValue()).isEqualTo(expected);
215         }
216       }
217     }
218   }
219 
testDivideByZeroThrows()220   public void testDivideByZeroThrows() {
221     for (long a : TEST_LONGS) {
222       try {
223         UnsignedLong.fromLongBits(a).dividedBy(UnsignedLong.ZERO);
224         fail("Expected ArithmeticException");
225       } catch (ArithmeticException expected) {
226       }
227     }
228   }
229 
testMod()230   public void testMod() {
231     for (long a : TEST_LONGS) {
232       for (long b : TEST_LONGS) {
233         if (b != 0) {
234           UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
235           UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
236           long expected =
237               aUnsigned.bigIntegerValue().remainder(bUnsigned.bigIntegerValue()).longValue();
238           UnsignedLong unsignedRem = aUnsigned.mod(bUnsigned);
239           assertThat(unsignedRem.longValue()).isEqualTo(expected);
240         }
241       }
242     }
243   }
244 
testModByZero()245   public void testModByZero() {
246     for (long a : TEST_LONGS) {
247       try {
248         UnsignedLong.fromLongBits(a).mod(UnsignedLong.ZERO);
249         fail("Expected ArithmeticException");
250       } catch (ArithmeticException expected) {
251       }
252     }
253   }
254 
testCompare()255   public void testCompare() {
256     for (long a : TEST_LONGS) {
257       for (long b : TEST_LONGS) {
258         UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
259         UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b);
260         assertThat(aUnsigned.compareTo(bUnsigned))
261             .isEqualTo(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()));
262       }
263     }
264   }
265 
266   @GwtIncompatible // too slow
testEquals()267   public void testEquals() {
268     EqualsTester equalsTester = new EqualsTester();
269     for (long a : TEST_LONGS) {
270       BigInteger big =
271           (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64));
272       equalsTester.addEqualityGroup(
273           UnsignedLong.fromLongBits(a),
274           UnsignedLong.valueOf(big),
275           UnsignedLong.valueOf(big.toString()),
276           UnsignedLong.valueOf(big.toString(16), 16));
277     }
278     equalsTester.testEquals();
279   }
280 
testIntValue()281   public void testIntValue() {
282     for (long a : TEST_LONGS) {
283       UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a);
284       int intValue = aUnsigned.bigIntegerValue().intValue();
285       assertThat(aUnsigned.intValue()).isEqualTo(intValue);
286     }
287   }
288 
289   @J2ktIncompatible
290   @GwtIncompatible // serialization
testSerialization()291   public void testSerialization() {
292     for (long a : TEST_LONGS) {
293       SerializableTester.reserializeAndAssert(UnsignedLong.fromLongBits(a));
294     }
295   }
296 
297   @J2ktIncompatible
298   @GwtIncompatible // NullPointerTester
testNulls()299   public void testNulls() {
300     new NullPointerTester().testAllPublicStaticMethods(UnsignedLong.class);
301   }
302 }
303