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
15package com.google.common.primitives;
16
17import java.math.BigInteger;
18
19import junit.framework.TestCase;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.annotations.GwtIncompatible;
23import com.google.common.collect.ImmutableSet;
24import com.google.common.testing.EqualsTester;
25import com.google.common.testing.NullPointerTester;
26import com.google.common.testing.SerializableTester;
27
28/**
29 * Tests for {@code UnsignedLong}.
30 *
31 * @author Louis Wasserman
32 */
33@GwtCompatible(emulated = true)
34public class UnsignedLongTest extends TestCase {
35  private static final ImmutableSet<Long> TEST_LONGS;
36
37  static {
38    ImmutableSet.Builder<Long> testLongsBuilder = 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    }
47    TEST_LONGS = testLongsBuilder.build();
48  }
49
50  public void testAsUnsignedAndLongValueAreInverses() {
51    for (long value : TEST_LONGS) {
52      assertEquals(
53          UnsignedLongs.toString(value), value, UnsignedLong.asUnsigned(value).longValue());
54    }
55  }
56
57  public void testAsUnsignedBigIntegerValue() {
58    for (long value : TEST_LONGS) {
59      BigInteger expected = (value >= 0)
60          ? BigInteger.valueOf(value)
61          : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64));
62      assertEquals(UnsignedLongs.toString(value), expected,
63          UnsignedLong.asUnsigned(value).bigIntegerValue());
64    }
65  }
66
67  public void testToString() {
68    for (long value : TEST_LONGS) {
69      UnsignedLong unsignedValue = UnsignedLong.asUnsigned(value);
70      assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString());
71    }
72  }
73
74  @GwtIncompatible("too slow")
75  public void testToStringRadix() {
76    for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
77      for (long l : TEST_LONGS) {
78        UnsignedLong value = UnsignedLong.asUnsigned(l);
79        assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
80      }
81    }
82  }
83
84  public void testToStringRadixQuick() {
85    int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36};
86    for (int radix : radices) {
87      for (long l : TEST_LONGS) {
88        UnsignedLong value = UnsignedLong.asUnsigned(l);
89        assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
90      }
91    }
92  }
93
94  public void testFloatValue() {
95    for (long value : TEST_LONGS) {
96      UnsignedLong unsignedValue = UnsignedLong.asUnsigned(value);
97      assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue());
98    }
99  }
100
101  public void testDoubleValue() {
102    for (long value : TEST_LONGS) {
103      UnsignedLong unsignedValue = UnsignedLong.asUnsigned(value);
104      assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue());
105    }
106  }
107
108  public void testAdd() {
109    for (long a : TEST_LONGS) {
110      for (long b : TEST_LONGS) {
111        UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
112        UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b);
113        long expected = aUnsigned
114            .bigIntegerValue()
115            .add(bUnsigned.bigIntegerValue())
116            .longValue();
117        UnsignedLong unsignedSum = aUnsigned.add(bUnsigned);
118        assertEquals(expected, unsignedSum.longValue());
119      }
120    }
121  }
122
123  public void testSubtract() {
124    for (long a : TEST_LONGS) {
125      for (long b : TEST_LONGS) {
126        UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
127        UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b);
128        long expected = aUnsigned
129            .bigIntegerValue()
130            .subtract(bUnsigned.bigIntegerValue())
131            .longValue();
132        UnsignedLong unsignedSub = aUnsigned.subtract(bUnsigned);
133        assertEquals(expected, unsignedSub.longValue());
134      }
135    }
136  }
137
138  public void testMultiply() {
139    for (long a : TEST_LONGS) {
140      for (long b : TEST_LONGS) {
141        UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
142        UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b);
143        long expected = aUnsigned
144            .bigIntegerValue()
145            .multiply(bUnsigned.bigIntegerValue())
146            .longValue();
147        UnsignedLong unsignedMul = aUnsigned.multiply(bUnsigned);
148        assertEquals(expected, unsignedMul.longValue());
149      }
150    }
151  }
152
153  public void testDivide() {
154    for (long a : TEST_LONGS) {
155      for (long b : TEST_LONGS) {
156        if (b != 0) {
157          UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
158          UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b);
159          long expected = aUnsigned
160              .bigIntegerValue()
161              .divide(bUnsigned.bigIntegerValue())
162              .longValue();
163          UnsignedLong unsignedDiv = aUnsigned.divide(bUnsigned);
164          assertEquals(expected, unsignedDiv.longValue());
165        }
166      }
167    }
168  }
169
170  public void testDivideByZeroThrows() {
171    for (long a : TEST_LONGS) {
172      try {
173        UnsignedLong.asUnsigned(a).divide(UnsignedLong.ZERO);
174        fail("Expected ArithmeticException");
175      } catch (ArithmeticException expected) {}
176    }
177  }
178
179  public void testRemainder() {
180    for (long a : TEST_LONGS) {
181      for (long b : TEST_LONGS) {
182        if (b != 0) {
183          UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
184          UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b);
185          long expected = aUnsigned
186              .bigIntegerValue()
187              .remainder(bUnsigned.bigIntegerValue())
188              .longValue();
189          UnsignedLong unsignedRem = aUnsigned.remainder(bUnsigned);
190          assertEquals(expected, unsignedRem.longValue());
191        }
192      }
193    }
194  }
195
196  public void testRemainderByZero() {
197    for (long a : TEST_LONGS) {
198      try {
199        UnsignedLong.asUnsigned(a).remainder(UnsignedLong.ZERO);
200        fail("Expected ArithmeticException");
201      } catch (ArithmeticException expected) {}
202    }
203  }
204
205  public void testCompare() {
206    for (long a : TEST_LONGS) {
207      for (long b : TEST_LONGS) {
208        UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
209        UnsignedLong bUnsigned = UnsignedLong.asUnsigned(b);
210        assertEquals(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()),
211            aUnsigned.compareTo(bUnsigned));
212      }
213    }
214  }
215
216  @GwtIncompatible("too slow")
217  public void testEqualsAndValueOf() {
218    EqualsTester equalsTester = new EqualsTester();
219    for (long a : TEST_LONGS) {
220      BigInteger big =
221          (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64));
222      equalsTester.addEqualityGroup(UnsignedLong.asUnsigned(a), UnsignedLong.valueOf(big),
223          UnsignedLong.valueOf(big.toString()), UnsignedLong.valueOf(big.toString(16), 16));
224    }
225    equalsTester.testEquals();
226  }
227
228  public void testIntValue() {
229    for (long a : TEST_LONGS) {
230      UnsignedLong aUnsigned = UnsignedLong.asUnsigned(a);
231      int intValue = aUnsigned.bigIntegerValue().intValue();
232      assertEquals(intValue, aUnsigned.intValue());
233    }
234  }
235
236  @GwtIncompatible("serialization")
237  public void testSerialization() {
238    for (long a : TEST_LONGS) {
239      SerializableTester.reserializeAndAssert(UnsignedLong.asUnsigned(a));
240    }
241  }
242
243  @GwtIncompatible("NullPointerTester")
244  public void testNulls() throws Exception {
245    NullPointerTester tester = new NullPointerTester();
246    tester.setDefault(UnsignedLong.class, UnsignedLong.ONE);
247    tester.testAllPublicStaticMethods(UnsignedLong.class);
248  }
249}
250