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 java.math.BigInteger.ONE; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.collect.testing.Helpers; 21 22 import junit.framework.TestCase; 23 24 import java.math.BigInteger; 25 import java.util.Arrays; 26 import java.util.Comparator; 27 import java.util.List; 28 29 /** 30 * Tests for UnsignedLongs 31 * 32 * @author Brian Milch 33 * @author Louis Wasserman 34 */ 35 @GwtCompatible(emulated = true) 36 public class UnsignedLongsTest extends TestCase { 37 private static final long LEAST = 0L; 38 private static final long GREATEST = 0xffffffffffffffffL; 39 40 public void testCompare() { 41 // max value 42 assertTrue(UnsignedLongs.compare(0, 0xffffffffffffffffL) < 0); 43 assertTrue(UnsignedLongs.compare(0xffffffffffffffffL, 0) > 0); 44 45 // both with high bit set 46 assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xffffffffffffffffL) < 0); 47 assertTrue(UnsignedLongs.compare(0xffffffffffffffffL, 0xff1a618b7f65ea12L) > 0); 48 49 // one with high bit set 50 assertTrue(UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0xff1a618b7f65ea12L) < 0); 51 assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0x5a4316b8c153ac4dL) > 0); 52 53 // neither with high bit set 54 assertTrue(UnsignedLongs.compare(0x5a4316b8c153ac4dL, 0x6cf78a4b139a4e2aL) < 0); 55 assertTrue(UnsignedLongs.compare(0x6cf78a4b139a4e2aL, 0x5a4316b8c153ac4dL) > 0); 56 57 // same value 58 assertTrue(UnsignedLongs.compare(0xff1a618b7f65ea12L, 0xff1a618b7f65ea12L) == 0); 59 } 60 61 public void testMax_noArgs() { 62 try { 63 UnsignedLongs.max(); 64 fail(); 65 } catch (IllegalArgumentException expected) { 66 } 67 } 68 69 public void testMax() { 70 assertEquals(LEAST, UnsignedLongs.max(LEAST)); 71 assertEquals(GREATEST, UnsignedLongs.max(GREATEST)); 72 assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.max( 73 0x5a4316b8c153ac4dL, 8L, 100L, 74 0L, 0x6cf78a4b139a4e2aL, 0xff1a618b7f65ea12L)); 75 } 76 77 public void testMin_noArgs() { 78 try { 79 UnsignedLongs.min(); 80 fail(); 81 } catch (IllegalArgumentException expected) { 82 } 83 } 84 85 public void testMin() { 86 assertEquals(LEAST, UnsignedLongs.min(LEAST)); 87 assertEquals(GREATEST, UnsignedLongs.min(GREATEST)); 88 assertEquals(0L, UnsignedLongs.min( 89 0x5a4316b8c153ac4dL, 8L, 100L, 90 0L, 0x6cf78a4b139a4e2aL, 0xff1a618b7f65ea12L)); 91 } 92 93 public void testLexicographicalComparator() { 94 List<long[]> ordered = Arrays.asList( 95 new long[] {}, 96 new long[] {LEAST}, 97 new long[] {LEAST, LEAST}, 98 new long[] {LEAST, (long) 1}, 99 new long[] {(long) 1}, 100 new long[] {(long) 1, LEAST}, 101 new long[] {GREATEST, GREATEST - (long) 1}, 102 new long[] {GREATEST, GREATEST}, 103 new long[] {GREATEST, GREATEST, GREATEST}); 104 105 Comparator<long[]> comparator = UnsignedLongs.lexicographicalComparator(); 106 Helpers.testComparator(comparator, ordered); 107 } 108 109 public void testDivide() { 110 assertEquals(2, UnsignedLongs.divide(14, 5)); 111 assertEquals(0, UnsignedLongs.divide(0, 50)); 112 assertEquals(1, UnsignedLongs.divide(0xfffffffffffffffeL, 0xfffffffffffffffdL)); 113 assertEquals(0, UnsignedLongs.divide(0xfffffffffffffffdL, 0xfffffffffffffffeL)); 114 assertEquals(281479271743488L, UnsignedLongs.divide(0xfffffffffffffffeL, 65535)); 115 assertEquals(0x7fffffffffffffffL, UnsignedLongs.divide(0xfffffffffffffffeL, 2)); 116 assertEquals(3689348814741910322L, UnsignedLongs.divide(0xfffffffffffffffeL, 5)); 117 } 118 119 public void testRemainder() { 120 assertEquals(4, UnsignedLongs.remainder(14, 5)); 121 assertEquals(0, UnsignedLongs.remainder(0, 50)); 122 assertEquals(1, UnsignedLongs.remainder(0xfffffffffffffffeL, 0xfffffffffffffffdL)); 123 assertEquals(0xfffffffffffffffdL, 124 UnsignedLongs.remainder(0xfffffffffffffffdL, 0xfffffffffffffffeL)); 125 assertEquals(65534L, UnsignedLongs.remainder(0xfffffffffffffffeL, 65535)); 126 assertEquals(0, UnsignedLongs.remainder(0xfffffffffffffffeL, 2)); 127 assertEquals(4, UnsignedLongs.remainder(0xfffffffffffffffeL, 5)); 128 } 129 130 public void testParseLong() { 131 assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("18446744073709551615")); 132 assertEquals(0x7fffffffffffffffL, UnsignedLongs.parseUnsignedLong("9223372036854775807")); 133 assertEquals(0xff1a618b7f65ea12L, UnsignedLongs.parseUnsignedLong("18382112080831834642")); 134 assertEquals(0x5a4316b8c153ac4dL, UnsignedLongs.parseUnsignedLong("6504067269626408013")); 135 assertEquals(0x6cf78a4b139a4e2aL, UnsignedLongs.parseUnsignedLong("7851896530399809066")); 136 137 try { 138 // One more than maximum value 139 UnsignedLongs.parseUnsignedLong("18446744073709551616"); 140 fail(); 141 } catch (NumberFormatException expected) { 142 } 143 } 144 145 public void testDecodeLong() { 146 assertEquals(0xffffffffffffffffL, UnsignedLongs.decode("0xffffffffffffffff")); 147 assertEquals(01234567, UnsignedLongs.decode("01234567")); // octal 148 assertEquals(0x1234567890abcdefL, UnsignedLongs.decode("#1234567890abcdef")); 149 assertEquals(987654321012345678L, UnsignedLongs.decode("987654321012345678")); 150 assertEquals(0x135791357913579L, UnsignedLongs.decode("0x135791357913579")); 151 assertEquals(0x135791357913579L, UnsignedLongs.decode("0X135791357913579")); 152 assertEquals(0L, UnsignedLongs.decode("0")); 153 } 154 155 public void testDecodeLongFails() { 156 try { 157 // One more than maximum value 158 UnsignedLongs.decode("0xfffffffffffffffff"); 159 fail(); 160 } catch (NumberFormatException expected) { 161 } 162 163 try { 164 UnsignedLongs.decode("-5"); 165 fail(); 166 } catch (NumberFormatException expected) { 167 } 168 169 try { 170 UnsignedLongs.decode("-0x5"); 171 fail(); 172 } catch (NumberFormatException expected) { 173 } 174 175 try { 176 UnsignedLongs.decode("-05"); 177 fail(); 178 } catch (NumberFormatException expected) { 179 } 180 } 181 182 public void testParseLongWithRadix() { 183 assertEquals(0xffffffffffffffffL, UnsignedLongs.parseUnsignedLong("ffffffffffffffff", 16)); 184 assertEquals(0x1234567890abcdefL, UnsignedLongs.parseUnsignedLong("1234567890abcdef", 16)); 185 186 BigInteger max = BigInteger.ZERO.setBit(64).subtract(ONE); 187 // loops through all legal radix values. 188 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { 189 // tests can successfully parse a number string with this radix. 190 String maxAsString = max.toString(radix); 191 assertEquals(max.longValue(), UnsignedLongs.parseUnsignedLong(maxAsString, radix)); 192 193 try { 194 // tests that we get exception whre an overflow would occur. 195 BigInteger overflow = max.add(ONE); 196 String overflowAsString = overflow.toString(radix); 197 UnsignedLongs.parseUnsignedLong(overflowAsString, radix); 198 fail(); 199 } catch (NumberFormatException expected) { 200 } 201 } 202 203 try { 204 UnsignedLongs.parseUnsignedLong("1234567890abcdef1", 16); 205 fail(); 206 } catch (NumberFormatException expected) { 207 } 208 } 209 210 public void testParseLongThrowsExceptionForInvalidRadix() { 211 // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, inclusive. 212 try { 213 UnsignedLongs.parseUnsignedLong("0", Character.MIN_RADIX - 1); 214 fail(); 215 } catch (NumberFormatException expected) { 216 } 217 218 try { 219 UnsignedLongs.parseUnsignedLong("0", Character.MAX_RADIX + 1); 220 fail(); 221 } catch (NumberFormatException expected) { 222 } 223 224 // The radix is used as an array index, so try a negative value. 225 try { 226 UnsignedLongs.parseUnsignedLong("0", -1); 227 fail(); 228 } catch (NumberFormatException expected) { 229 } 230 } 231 232 public void testToString() { 233 String[] tests = { 234 "ffffffffffffffff", 235 "7fffffffffffffff", 236 "ff1a618b7f65ea12", 237 "5a4316b8c153ac4d", 238 "6cf78a4b139a4e2a" 239 }; 240 int[] bases = { 2, 5, 7, 8, 10, 16 }; 241 for (int base : bases) { 242 for (String x : tests) { 243 BigInteger xValue = new BigInteger(x, 16); 244 long xLong = xValue.longValue(); // signed 245 assertEquals(xValue.toString(base), UnsignedLongs.toString(xLong, base)); 246 } 247 } 248 } 249 250 public void testJoin() { 251 assertEquals("", UnsignedLongs.join(",")); 252 assertEquals("1", UnsignedLongs.join(",", 1)); 253 assertEquals("1,2", UnsignedLongs.join(",", 1, 2)); 254 assertEquals("18446744073709551615,9223372036854775808", 255 UnsignedLongs.join(",", -1, Long.MIN_VALUE)); 256 assertEquals("123", UnsignedLongs.join("", 1, 2, 3)); 257 assertEquals("184467440737095516159223372036854775808", 258 UnsignedLongs.join("", -1, Long.MIN_VALUE)); 259 } 260 } 261 262