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.collect.testing.Helpers; 19 20 import junit.framework.TestCase; 21 22 import java.util.Arrays; 23 import java.util.Comparator; 24 import java.util.List; 25 26 /** 27 * Tests for UnsignedInts 28 * 29 * @author Louis Wasserman 30 */ 31 @GwtCompatible(emulated = true) 32 public class UnsignedIntsTest extends TestCase { 33 private static final long[] UNSIGNED_INTS = { 34 0L, 35 1L, 36 2L, 37 3L, 38 0x12345678L, 39 0x5a4316b8L, 40 0x6cf78a4bL, 41 0xff1a618bL, 42 0xfffffffdL, 43 0xfffffffeL, 44 0xffffffffL}; 45 46 private static final int LEAST = (int) 0L; 47 private static final int GREATEST = (int) 0xffffffffL; 48 49 public void testToLong() { 50 for (long a : UNSIGNED_INTS) { 51 assertEquals(a, UnsignedInts.toLong((int) a)); 52 } 53 } 54 55 public void testCompare() { 56 for (long a : UNSIGNED_INTS) { 57 for (long b : UNSIGNED_INTS) { 58 int cmpAsLongs = Longs.compare(a, b); 59 int cmpAsUInt = UnsignedInts.compare((int) a, (int) b); 60 assertEquals(Integer.signum(cmpAsLongs), Integer.signum(cmpAsUInt)); 61 } 62 } 63 } 64 65 public void testMax_noArgs() { 66 try { 67 UnsignedInts.max(); 68 fail(); 69 } catch (IllegalArgumentException expected) { 70 } 71 } 72 73 public void testMax() { 74 assertEquals(LEAST, UnsignedInts.max(LEAST)); 75 assertEquals(GREATEST, UnsignedInts.max(GREATEST)); 76 assertEquals((int) 0xff1a618bL, UnsignedInts.max( 77 (int) 8L, (int) 6L, (int) 7L, 78 (int) 0x12345678L, (int) 0x5a4316b8L, 79 (int) 0xff1a618bL, (int) 0L)); 80 } 81 82 public void testMin_noArgs() { 83 try { 84 UnsignedInts.min(); 85 fail(); 86 } catch (IllegalArgumentException expected) { 87 } 88 } 89 90 public void testMin() { 91 assertEquals(LEAST, UnsignedInts.min(LEAST)); 92 assertEquals(GREATEST, UnsignedInts.min(GREATEST)); 93 assertEquals((int) 0L, UnsignedInts.min( 94 (int) 8L, (int) 6L, (int) 7L, 95 (int) 0x12345678L, (int) 0x5a4316b8L, 96 (int) 0xff1a618bL, (int) 0L)); 97 } 98 99 public void testLexicographicalComparator() { 100 List<int[]> ordered = Arrays.asList( 101 new int[] {}, 102 new int[] {LEAST}, 103 new int[] {LEAST, LEAST}, 104 new int[] {LEAST, (int) 1L}, 105 new int[] {(int) 1L}, 106 new int[] {(int) 1L, LEAST}, 107 new int[] {GREATEST, (GREATEST - (int) 1L)}, 108 new int[] {GREATEST, GREATEST}, 109 new int[] {GREATEST, GREATEST, GREATEST} 110 ); 111 112 Comparator<int[]> comparator = UnsignedInts.lexicographicalComparator(); 113 Helpers.testComparator(comparator, ordered); 114 } 115 116 public void testDivide() { 117 for (long a : UNSIGNED_INTS) { 118 for (long b : UNSIGNED_INTS) { 119 try { 120 assertEquals((int) (a / b), UnsignedInts.divide((int) a, (int) b)); 121 assertFalse(b == 0); 122 } catch (ArithmeticException e) { 123 assertEquals(0, b); 124 } 125 } 126 } 127 } 128 129 public void testRemainder() { 130 for (long a : UNSIGNED_INTS) { 131 for (long b : UNSIGNED_INTS) { 132 try { 133 assertEquals((int) (a % b), UnsignedInts.remainder((int) a, (int) b)); 134 assertFalse(b == 0); 135 } catch (ArithmeticException e) { 136 assertEquals(0, b); 137 } 138 } 139 } 140 } 141 142 public void testParseInt() { 143 try { 144 for (long a : UNSIGNED_INTS) { 145 assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a))); 146 } 147 } catch (NumberFormatException e) { 148 fail(e.getMessage()); 149 } 150 151 try { 152 UnsignedInts.parseUnsignedInt(Long.toString(1L << 32)); 153 fail("Expected NumberFormatException"); 154 } catch (NumberFormatException expected) {} 155 } 156 157 public void testParseIntWithRadix() throws NumberFormatException { 158 for (long a : UNSIGNED_INTS) { 159 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { 160 assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a, radix), radix)); 161 } 162 } 163 164 // loops through all legal radix values. 165 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { 166 // tests can successfully parse a number string with this radix. 167 String maxAsString = Long.toString((1L << 32) - 1, radix); 168 assertEquals(-1, UnsignedInts.parseUnsignedInt(maxAsString, radix)); 169 170 try { 171 // tests that we get exception whre an overflow would occur. 172 long overflow = 1L << 32; 173 String overflowAsString = Long.toString(overflow, radix); 174 UnsignedInts.parseUnsignedInt(overflowAsString, radix); 175 fail(); 176 } catch (NumberFormatException expected) {} 177 } 178 } 179 180 public void testParseIntThrowsExceptionForInvalidRadix() { 181 // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX, 182 // inclusive. 183 try { 184 UnsignedInts.parseUnsignedInt("0", Character.MIN_RADIX - 1); 185 fail(); 186 } catch (NumberFormatException expected) {} 187 188 try { 189 UnsignedInts.parseUnsignedInt("0", Character.MAX_RADIX + 1); 190 fail(); 191 } catch (NumberFormatException expected) {} 192 193 // The radix is used as an array index, so try a negative value. 194 try { 195 UnsignedInts.parseUnsignedInt("0", -1); 196 fail(); 197 } catch (NumberFormatException expected) {} 198 } 199 200 public void testDecodeInt() { 201 assertEquals(0xffffffff, UnsignedInts.decode("0xffffffff")); 202 assertEquals(01234567, UnsignedInts.decode("01234567")); // octal 203 assertEquals(0x12345678, UnsignedInts.decode("#12345678")); 204 assertEquals(76543210, UnsignedInts.decode("76543210")); 205 assertEquals(0x13579135, UnsignedInts.decode("0x13579135")); 206 assertEquals(0x13579135, UnsignedInts.decode("0X13579135")); 207 assertEquals(0, UnsignedInts.decode("0")); 208 } 209 210 public void testDecodeIntFails() { 211 try { 212 // One more than maximum value 213 UnsignedInts.decode("0xfffffffff"); 214 fail(); 215 } catch (NumberFormatException expected) { 216 } 217 218 try { 219 UnsignedInts.decode("-5"); 220 fail(); 221 } catch (NumberFormatException expected) { 222 } 223 224 try { 225 UnsignedInts.decode("-0x5"); 226 fail(); 227 } catch (NumberFormatException expected) { 228 } 229 230 try { 231 UnsignedInts.decode("-05"); 232 fail(); 233 } catch (NumberFormatException expected) { 234 } 235 } 236 237 public void testToString() { 238 int[] bases = {2, 5, 7, 8, 10, 16}; 239 for (long a : UNSIGNED_INTS) { 240 for (int base : bases) { 241 assertEquals(UnsignedInts.toString((int) a, base), Long.toString(a, base)); 242 } 243 } 244 } 245 246 public void testJoin() { 247 assertEquals("", join()); 248 assertEquals("1", join(1)); 249 assertEquals("1,2", join(1, 2)); 250 assertEquals("4294967295,2147483648", join(-1, Integer.MIN_VALUE)); 251 252 assertEquals("123", UnsignedInts.join("", 1, 2, 3)); 253 } 254 255 private static String join(int... values) { 256 return UnsignedInts.join(",", values); 257 } 258 } 259 260