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