1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.primitives;
import static java.math.BigInteger.ONE;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
import com.google.common.testing.NullPointerTester;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;
/**
* Tests for UnsignedLongs
*
* @author Brian Milch
* @author Louis Wasserman
*/
@GwtCompatible(emulated = true)
public class UnsignedLongsTest extends TestCase {
private static final long LEAST = 0L;
private static final long GREATEST = 0xffffffffffffffffL;
public void testCompare() {
// max value
assertTrue(UnsignedLongs.compare(0, 0xffffffffffffffffL) < 0);
assertTrue(UnsignedLongs.compare(0xffffffffffffffffL, 0) > 0);
// both with high bit set
assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xffffffffffffffffL) < 0);
assertTrue(UnsignedLongs.compare(0xffffffffffffffffL, 0xff1a618b7f65ea12L) > 0);
// one with high bit set
assertTrue(UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0xff1a618b7f65ea12L) < 0);
assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0x5a4316b8c153ac4dL) > 0);
// neither with high bit set
assertTrue(UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0x6cf78a4b139a4e2aL) < 0);
assertTrue(UnsignedLongs.compare(0x6cf78a4b139a4e2aL, 0x5a4316b8c153ac4dL) > 0);
// same value
assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xff1a618b7f65ea12L) == 0);
}
public void testMax_noArgs() {
try {
UnsignedLongs.max();
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testMax() {
assertEquals(LEAST, UnsignedLongs.max(LEAST));
assertEquals(GREATEST, UnsignedLongs.max(GREATEST));
assertEquals(
0xff1a618b7f65ea12L,
UnsignedLongs.max(
0x5a4316b8c153ac4dL, 8L, 100L, 0L, 0x6cf78a4b139a4e2aL, 0xff1a618b7f65ea12L));
}
public void testMin_noArgs() {
try {
UnsignedLongs.min();
fail();
} catch (IllegalArgumentException expected) {
}
}
public void testMin() {
assertEquals(LEAST, UnsignedLongs.min(LEAST));
assertEquals(GREATEST, UnsignedLongs.min(GREATEST));
assertEquals(
0L,
UnsignedLongs.min(
0x5a4316b8c153ac4dL, 8L, 100L, 0L, 0x6cf78a4b139a4e2aL, 0xff1a618b7f65ea12L));
}
public void testLexicographicalComparator() {
List<long[]> ordered =
Arrays.asList(
new long[] {},
new long[] {LEAST},
new long[] {LEAST, LEAST},
new long[] {LEAST, (long) 1},
new long[] {(long) 1},
new long[] {(long) 1, LEAST},
new long[] {GREATEST, GREATEST - (long) 1},
new long[] {GREATEST, GREATEST},
new long[] {GREATEST, GREATEST, GREATEST});
Comparator<long[]> comparator = UnsignedLongs.lexicographicalComparator();
Helpers.testComparator(comparator, ordered);
}
public void testSort() {
testSort(new long[] {}, new long[] {});
testSort(new long[] {2}, new long[] {2});
testSort(new long[] {2, 1, 0}, new long[] {0, 1, 2});
testSort(new long[] {2, GREATEST, 1, LEAST}, new long[] {LEAST, 1, 2, GREATEST});
}
static void testSort(long[] input, long[] expected) {
input = Arrays.copyOf(input, input.length);
UnsignedLongs.sort(input);
assertTrue(Arrays.equals(expected, input));
}
static void testSort(long[] input, int from, int to, long[] expected) {
input = Arrays.copyOf(input, input.length);
UnsignedLongs.sort(input, from, to);
assertTrue(Arrays.equals(expected, input));
}
public void testSortIndexed() {
testSort(new long[] {}, 0, 0, new long[] {});
testSort(new long[] {2}, 0, 1, new long[] {2});
testSort(new long[] {2, 1, 0}, 0, 2, new long[] {1, 2, 0});
testSort(new long[] {2, GREATEST, 1, LEAST}, 1, 4, new long[] {2, LEAST, 1, GREATEST});
}
public void testSortDescending() {
testSortDescending(new long[] {}, new long[] {});
testSortDescending(new long[] {1}, new long[] {1});
testSortDescending(new long[] {1, 2}, new long[] {2, 1});
testSortDescending(new long[] {1, 3, 1}, new long[] {3, 1, 1});
testSortDescending(
new long[] {GREATEST - 1, 1, GREATEST - 2, 2},
new long[] {GREATEST - 1, GREATEST - 2, 2, 1});
}
private static void testSortDescending(long[] input, long[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
UnsignedLongs.sortDescending(input);
assertTrue(Arrays.equals(expectedOutput, input));
}
private static void testSortDescending(
long[] input, int fromIndex, int toIndex, long[] expectedOutput) {
input = Arrays.copyOf(input, input.length);
UnsignedLongs.sortDescending(input, fromIndex, toIndex);
assertTrue(Arrays.equals(expectedOutput, input));
}
public void testSortDescendingIndexed() {
testSortDescending(new long[] {}, 0, 0, new long[] {});
testSortDescending(new long[] {1}, 0, 1, new long[] {1});
testSortDescending(new long[] {1, 2}, 0, 2, new long[] {2, 1});
testSortDescending(new long[] {1, 3, 1}, 0, 2, new long[] {3, 1, 1});
testSortDescending(new long[] {1, 3, 1}, 0, 1, new long[] {1, 3, 1});
testSortDescending(
new long[] {GREATEST - 1, 1, GREATEST - 2, 2},
1,
3,
new long[] {GREATEST - 1, GREATEST - 2, 1, 2});
}
public void testDivide() {
assertEquals(2, UnsignedLongs.divide(14, 5));
assertEquals(0, UnsignedLongs.divide(0, 50));
assertEquals(1, UnsignedLongs.divide(0xfffffffffffffffeL, 0xfffffffffffffffdL));
assertEquals(0, UnsignedLongs.divide(0xfffffffffffffffdL, 0xfffffffffffffffeL));
assertEquals(281479271743488L, UnsignedLongs.divide(0xfffffffffffffffeL, 65535));
assertEquals(0x7fffffffffffffffL, UnsignedLongs.divide(0xfffffffffffffffeL, 2));
assertEquals(3689348814741910322L, UnsignedLongs.divide(0xfffffffffffffffeL, 5));
}
public void testRemainder() {
assertEquals(4, UnsignedLongs.remainder(14, 5));
assertEquals(0, UnsignedLongs.remainder(0, 50));
assertEquals(1, UnsignedLongs.remainder(0xfffffffffffffffeL, 0xfffffffffffffffdL));
assertEquals(
0xfffffffffffffffdL, UnsignedLongs.remainder(0xfffffffffffffffdL, 0xfffffffffffffffeL));
assertEquals(65534L, UnsignedLongs.remainder(0xfffffffffffffffeL, 65535));
assertEquals(0, UnsignedLongs.remainder(0xfffffffffffffffeL, 2));
assertEquals(4, UnsignedLongs.remainder(0xfffffffffffffffeL, 5));
}
@GwtIncompatible // Too slow in GWT (~3min fully optimized)
public void testDivideRemainderEuclideanProperty() {
// Use a seed so that the test is deterministic:
Random r = new Random(0L);
for (int i = 0; i < 1000000; i++) {
long dividend = r.nextLong();
long divisor = r.nextLong();
// Test that the Euclidean property is preserved:
assertEquals(
0,
dividend
- (divisor * UnsignedLongs.divide(dividend, divisor)
+ UnsignedLongs.remainder(dividend, divisor)));
}
}
public void testParseLong() {
assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("18446744073709551615"));
assertEquals(0x7fffffffffffffffL, UnsignedLongs.parseUnsignedLong("9223372036854775807"));
assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.parseUnsignedLong("18382112080831834642"));
assertEquals(0x5a4316b8c153ac4dL, UnsignedLongs.parseUnsignedLong("6504067269626408013"));
assertEquals(0x6cf78a4b139a4e2aL, UnsignedLongs.parseUnsignedLong("7851896530399809066"));
}
public void testParseLongEmptyString() {
try {
UnsignedLongs.parseUnsignedLong("");
fail("NumberFormatException should have been raised.");
} catch (NumberFormatException expected) {
}
}
public void testParseLongFails() {
try {
// One more than maximum value
UnsignedLongs.parseUnsignedLong("18446744073709551616");
fail();
} catch (NumberFormatException expected) {
}
}
public void testDecodeLong() {
assertEquals(0xffffffffffffffffL, UnsignedLongs.decode("0xffffffffffffffff"));
assertEquals(01234567, UnsignedLongs.decode("01234567")); // octal
assertEquals(0x1234567890abcdefL, UnsignedLongs.decode("#1234567890abcdef"));
assertEquals(987654321012345678L, UnsignedLongs.decode("987654321012345678"));
assertEquals(0x135791357913579L, UnsignedLongs.decode("0x135791357913579"));
assertEquals(0x135791357913579L, UnsignedLongs.decode("0X135791357913579"));
assertEquals(0L, UnsignedLongs.decode("0"));
}
public void testDecodeLongFails() {
try {
// One more than maximum value
UnsignedLongs.decode("0xfffffffffffffffff");
fail();
} catch (NumberFormatException expected) {
}
try {
UnsignedLongs.decode("-5");
fail();
} catch (NumberFormatException expected) {
}
try {
UnsignedLongs.decode("-0x5");
fail();
} catch (NumberFormatException expected) {
}
try {
UnsignedLongs.decode("-05");
fail();
} catch (NumberFormatException expected) {
}
}
public void testParseLongWithRadix() {
assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("ffffffffffffffff", 16));
assertEquals(0x1234567890abcdefL, UnsignedLongs.parseUnsignedLong("1234567890abcdef", 16));
}
public void testParseLongWithRadixLimits() {
BigInteger max = BigInteger.ZERO.setBit(64).subtract(ONE);
// loops through all legal radix values.
for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
// tests can successfully parse a number string with this radix.
String maxAsString = max.toString(radix);
assertEquals(max.longValue(), UnsignedLongs.parseUnsignedLong(maxAsString, radix));
try {
// tests that we get exception whre an overflow would occur.
BigInteger overflow = max.add(ONE);
String overflowAsString = overflow.toString(radix);
UnsignedLongs.parseUnsignedLong(overflowAsString, radix);
fail();
} catch (NumberFormatException expected) {
}
}
try {
UnsignedLongs.parseUnsignedLong("1234567890abcdef1", 16);
fail();
} catch (NumberFormatException expected) {
}
}
public void testParseLongThrowsExceptionForInvalidRadix() {
// Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, inclusive.
try {
UnsignedLongs.parseUnsignedLong("0", Character.MIN_RADIX - 1);
fail();
} catch (NumberFormatException expected) {
}
try {
UnsignedLongs.parseUnsignedLong("0", Character.MAX_RADIX + 1);
fail();
} catch (NumberFormatException expected) {
}
// The radix is used as an array index, so try a negative value.
try {
UnsignedLongs.parseUnsignedLong("0", -1);
fail();
} catch (NumberFormatException expected) {
}
}
public void testToString() {
String[] tests = {
"0",
"ffffffffffffffff",
"7fffffffffffffff",
"ff1a618b7f65ea12",
"5a4316b8c153ac4d",
"6cf78a4b139a4e2a"
};
int[] bases = {2, 5, 7, 8, 10, 16};
for (int base : bases) {
for (String x : tests) {
BigInteger xValue = new BigInteger(x, 16);
long xLong = xValue.longValue(); // signed
assertEquals(xValue.toString(base), UnsignedLongs.toString(xLong, base));
}
}
}
public void testJoin() {
assertEquals("", UnsignedLongs.join(","));
assertEquals("1", UnsignedLongs.join(",", 1));
assertEquals("1,2", UnsignedLongs.join(",", 1, 2));
assertEquals(
"18446744073709551615,9223372036854775808", UnsignedLongs.join(",", -1, Long.MIN_VALUE));
assertEquals("123", UnsignedLongs.join("", 1, 2, 3));
assertEquals(
"184467440737095516159223372036854775808", UnsignedLongs.join("", -1, Long.MIN_VALUE));
}
@GwtIncompatible // NullPointerTester
public void testNulls() {
new NullPointerTester().testAllPublicStaticMethods(UnsignedLongs.class);
}
}
|