• 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 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 
24 import junit.framework.TestCase;
25 
26 import java.math.BigInteger;
27 
28 /**
29  * Tests for {@code UnsignedInteger}.
30  *
31  * @author Louis Wasserman
32  */
33 @GwtCompatible(emulated = true)
34 public class UnsignedIntegerTest extends TestCase {
35   private static final ImmutableSet<Integer> TEST_INTS;
36   private static final ImmutableSet<Long> TEST_LONGS;
37 
force32(int value)38   private static int force32(int value) {
39     // GWT doesn't consistently overflow values to make them 32-bit, so we need to force it.
40     return value & 0xffffffff;
41   }
42 
43   static {
44     ImmutableSet.Builder<Integer> testIntsBuilder = ImmutableSet.builder();
45     ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder();
46     for (int i = -3; i <= 3; i++) {
47       testIntsBuilder
48         .add(i)
49         .add(force32(Integer.MIN_VALUE + i))
50         .add(force32(Integer.MAX_VALUE + i));
51       testLongsBuilder
52         .add((long) i)
53         .add((long) Integer.MIN_VALUE + i)
54         .add((long) Integer.MAX_VALUE + i)
55         .add((1L << 32) + i);
56     }
57     TEST_INTS = testIntsBuilder.build();
58     TEST_LONGS = testLongsBuilder.build();
59   }
60 
testFromIntBitsAndIntValueAreInverses()61   public void testFromIntBitsAndIntValueAreInverses() {
62     for (int value : TEST_INTS) {
63       assertEquals(UnsignedInts.toString(value), value, UnsignedInteger.fromIntBits(value)
64           .intValue());
65     }
66   }
67 
testFromIntBitsLongValue()68   public void testFromIntBitsLongValue() {
69     for (int value : TEST_INTS) {
70       long expected = value & 0xffffffffL;
71       assertEquals(UnsignedInts.toString(value), expected, UnsignedInteger.fromIntBits(value)
72           .longValue());
73     }
74   }
75 
testValueOfLong()76   public void testValueOfLong() {
77     long min = 0;
78     long max = (1L << 32) - 1;
79     for (long value : TEST_LONGS) {
80       boolean expectSuccess = value >= min && value <= max;
81       try {
82         assertEquals(value, UnsignedInteger.valueOf(value).longValue());
83         assertTrue(expectSuccess);
84       } catch (IllegalArgumentException e) {
85         assertFalse(expectSuccess);
86       }
87     }
88   }
89 
testValueOfBigInteger()90   public void testValueOfBigInteger() {
91     long min = 0;
92     long max = (1L << 32) - 1;
93     for (long value : TEST_LONGS) {
94       boolean expectSuccess = value >= min && value <= max;
95       try {
96         assertEquals(value, UnsignedInteger.valueOf(BigInteger.valueOf(value))
97             .longValue());
98         assertTrue(expectSuccess);
99       } catch (IllegalArgumentException e) {
100         assertFalse(expectSuccess);
101       }
102     }
103   }
104 
testToString()105   public void testToString() {
106     for (int value : TEST_INTS) {
107       UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value);
108       assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString());
109     }
110   }
111 
112   @GwtIncompatible("too slow")
testToStringRadix()113   public void testToStringRadix() {
114     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
115       for (int l : TEST_INTS) {
116         UnsignedInteger value = UnsignedInteger.fromIntBits(l);
117         assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
118       }
119     }
120   }
121 
testToStringRadixQuick()122   public void testToStringRadixQuick() {
123     int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36};
124     for (int radix : radices) {
125       for (int l : TEST_INTS) {
126         UnsignedInteger value = UnsignedInteger.fromIntBits(l);
127         assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
128       }
129     }
130   }
131 
testFloatValue()132   public void testFloatValue() {
133     for (int value : TEST_INTS) {
134       UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value);
135       assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue());
136     }
137   }
138 
testDoubleValue()139   public void testDoubleValue() {
140     for (int value : TEST_INTS) {
141       UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value);
142       assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue());
143     }
144   }
145 
testPlus()146   public void testPlus() {
147     for (int a : TEST_INTS) {
148       for (int b : TEST_INTS) {
149         UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
150         UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b);
151         int expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).intValue();
152         UnsignedInteger unsignedSum = aUnsigned.plus(bUnsigned);
153         assertEquals(expected, unsignedSum.intValue());
154       }
155     }
156   }
157 
testMinus()158   public void testMinus() {
159     for (int a : TEST_INTS) {
160       for (int b : TEST_INTS) {
161         UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
162         UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b);
163         int expected =
164             force32(aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).intValue());
165         UnsignedInteger unsignedSub = aUnsigned.minus(bUnsigned);
166         assertEquals(expected, unsignedSub.intValue());
167       }
168     }
169   }
170 
171   @GwtIncompatible("multiply")
testTimes()172   public void testTimes() {
173     for (int a : TEST_INTS) {
174       for (int b : TEST_INTS) {
175         UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
176         UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b);
177         int expected =
178             force32(aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).intValue());
179         UnsignedInteger unsignedMul = aUnsigned.times(bUnsigned);
180         assertEquals(aUnsigned + " * " + bUnsigned, expected, unsignedMul.intValue());
181       }
182     }
183   }
184 
testDividedBy()185   public void testDividedBy() {
186     for (int a : TEST_INTS) {
187       for (int b : TEST_INTS) {
188         if (b != 0) {
189           UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
190           UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b);
191           int expected =
192               aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).intValue();
193           UnsignedInteger unsignedDiv = aUnsigned.dividedBy(bUnsigned);
194           assertEquals(expected, unsignedDiv.intValue());
195         }
196       }
197     }
198   }
199 
testDivideByZeroThrows()200   public void testDivideByZeroThrows() {
201     for (int a : TEST_INTS) {
202       try {
203         UnsignedInteger ignored =
204             UnsignedInteger.fromIntBits(a).dividedBy(UnsignedInteger.ZERO);
205         fail("Expected ArithmeticException");
206       } catch (ArithmeticException expected) {}
207     }
208   }
209 
testMod()210   public void testMod() {
211     for (int a : TEST_INTS) {
212       for (int b : TEST_INTS) {
213         if (b != 0) {
214           UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
215           UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b);
216           int expected =
217               aUnsigned.bigIntegerValue().mod(bUnsigned.bigIntegerValue()).intValue();
218           UnsignedInteger unsignedRem = aUnsigned.mod(bUnsigned);
219           assertEquals(expected, unsignedRem.intValue());
220         }
221       }
222     }
223   }
224 
225   @SuppressWarnings("ReturnValueIgnored")
testModByZero()226   public void testModByZero() {
227     for (int a : TEST_INTS) {
228       try {
229         UnsignedInteger.fromIntBits(a).mod(UnsignedInteger.ZERO);
230         fail("Expected ArithmeticException");
231       } catch (ArithmeticException expected) {}
232     }
233   }
234 
testCompare()235   public void testCompare() {
236     for (int a : TEST_INTS) {
237       for (int b : TEST_INTS) {
238         UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
239         UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b);
240         assertEquals(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()),
241             aUnsigned.compareTo(bUnsigned));
242       }
243     }
244   }
245 
246   @GwtIncompatible("too slow")
testEquals()247   public void testEquals() {
248     EqualsTester equalsTester = new EqualsTester();
249     for (int a : TEST_INTS) {
250       long value = a & 0xffffffffL;
251       equalsTester.addEqualityGroup(UnsignedInteger.fromIntBits(a), UnsignedInteger.valueOf(value),
252           UnsignedInteger.valueOf(Long.toString(value)),
253           UnsignedInteger.valueOf(Long.toString(value, 16), 16));
254     }
255     equalsTester.testEquals();
256   }
257 
testIntValue()258   public void testIntValue() {
259     for (int a : TEST_INTS) {
260       UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a);
261       int intValue = aUnsigned.bigIntegerValue().intValue();
262       assertEquals(intValue, aUnsigned.intValue());
263     }
264   }
265 
266   @GwtIncompatible("serialization")
testSerialization()267   public void testSerialization() {
268     for (int a : TEST_INTS) {
269       SerializableTester.reserializeAndAssert(UnsignedInteger.fromIntBits(a));
270     }
271   }
272 
273   @GwtIncompatible("NullPointerTester")
testNulls()274   public void testNulls() {
275     new NullPointerTester().testAllPublicStaticMethods(UnsignedInteger.class);
276   }
277 }
278