Пример #1
0
  public void testDecodeIntFails() {
    try {
      // One more than maximum value
      int unused = UnsignedInts.decode("0xfffffffff");
      fail();
    } catch (NumberFormatException expected) {
    }

    try {
      int unused = UnsignedInts.decode("-5");
      fail();
    } catch (NumberFormatException expected) {
    }

    try {
      int unused = UnsignedInts.decode("-0x5");
      fail();
    } catch (NumberFormatException expected) {
    }

    try {
      int unused = UnsignedInts.decode("-05");
      fail();
    } catch (NumberFormatException expected) {
    }
  }
Пример #2
0
 public void testMin() {
   assertEquals(LEAST, UnsignedInts.min(LEAST));
   assertEquals(GREATEST, UnsignedInts.min(GREATEST));
   assertEquals(
       (int) 0L,
       UnsignedInts.min(
           (int) 8L,
           (int) 6L,
           (int) 7L,
           (int) 0x12345678L,
           (int) 0x5a4316b8L,
           (int) 0xff1a618bL,
           (int) 0L));
 }
Пример #3
0
  public void testParseInt() {
    try {
      for (long a : UNSIGNED_INTS) {
        assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a)));
      }
    } catch (NumberFormatException e) {
      fail(e.getMessage());
    }

    try {
      UnsignedInts.parseUnsignedInt(Long.toString(1L << 32));
      fail("Expected NumberFormatException");
    } catch (NumberFormatException expected) {
    }
  }
Пример #4
0
 @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++) {
     int dividend = r.nextInt();
     int divisor = r.nextInt();
     // Test that the Euclidean property is preserved:
     assertTrue(
         dividend
                 - (divisor * UnsignedInts.divide(dividend, divisor)
                     + UnsignedInts.remainder(dividend, divisor))
             == 0);
   }
 }
Пример #5
0
 public void testParseIntWithRadix() {
   for (long a : UNSIGNED_INTS) {
     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
       assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a, radix), radix));
     }
   }
 }
Пример #6
0
 public void testParseIntFail() {
   try {
     int unused = UnsignedInts.parseUnsignedInt(Long.toString(1L << 32));
     fail("Expected NumberFormatException");
   } catch (NumberFormatException expected) {
   }
 }
Пример #7
0
 public void testMin_noArgs() {
   try {
     int unused = UnsignedInts.min();
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Пример #8
0
 public void testToString() {
   int[] bases = {2, 5, 7, 8, 10, 16};
   for (long a : UNSIGNED_INTS) {
     for (int base : bases) {
       assertEquals(UnsignedInts.toString((int) a, base), Long.toString(a, base));
     }
   }
 }
Пример #9
0
  public void testParseIntWithRadixLimits() {
    // 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 = Long.toString((1L << 32) - 1, radix);
      assertEquals(-1, UnsignedInts.parseUnsignedInt(maxAsString, radix));

      try {
        // tests that we get exception whre an overflow would occur.
        long overflow = 1L << 32;
        String overflowAsString = Long.toString(overflow, radix);
        int unused = UnsignedInts.parseUnsignedInt(overflowAsString, radix);
        fail();
      } catch (NumberFormatException expected) {
      }
    }
  }
Пример #10
0
  public void testJoin() {
    assertEquals("", join());
    assertEquals("1", join(1));
    assertEquals("1,2", join(1, 2));
    assertEquals("4294967295,2147483648", join(-1, Integer.MIN_VALUE));

    assertEquals("123", UnsignedInts.join("", 1, 2, 3));
  }
Пример #11
0
 public void testCompare() {
   for (long a : UNSIGNED_INTS) {
     for (long b : UNSIGNED_INTS) {
       int cmpAsLongs = Longs.compare(a, b);
       int cmpAsUInt = UnsignedInts.compare((int) a, (int) b);
       assertEquals(Integer.signum(cmpAsLongs), Integer.signum(cmpAsUInt));
     }
   }
 }
Пример #12
0
 @Override
 public int compare(int[] left, int[] right) {
   int minLength = Math.min(left.length, right.length);
   for (int i = 0; i < minLength; i++) {
     if (left[i] != right[i]) {
       return UnsignedInts.compare(left[i], right[i]);
     }
   }
   return left.length - right.length;
 }
Пример #13
0
 public void testDecodeInt() {
   assertEquals(0xffffffff, UnsignedInts.decode("0xffffffff"));
   assertEquals(01234567, UnsignedInts.decode("01234567")); // octal
   assertEquals(0x12345678, UnsignedInts.decode("#12345678"));
   assertEquals(76543210, UnsignedInts.decode("76543210"));
   assertEquals(0x13579135, UnsignedInts.decode("0x13579135"));
   assertEquals(0x13579135, UnsignedInts.decode("0X13579135"));
   assertEquals(0, UnsignedInts.decode("0"));
 }
Пример #14
0
 public void testRemainder() {
   for (long a : UNSIGNED_INTS) {
     for (long b : UNSIGNED_INTS) {
       try {
         assertEquals((int) (a % b), UnsignedInts.remainder((int) a, (int) b));
         assertFalse(b == 0);
       } catch (ArithmeticException e) {
         assertEquals(0, b);
       }
     }
   }
 }
Пример #15
0
  public void testParseIntThrowsExceptionForInvalidRadix() {
    // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX,
    // inclusive.
    try {
      int unused = UnsignedInts.parseUnsignedInt("0", Character.MIN_RADIX - 1);
      fail();
    } catch (NumberFormatException expected) {
    }

    try {
      int unused = UnsignedInts.parseUnsignedInt("0", Character.MAX_RADIX + 1);
      fail();
    } catch (NumberFormatException expected) {
    }

    // The radix is used as an array index, so try a negative value.
    try {
      int unused = UnsignedInts.parseUnsignedInt("0", -1);
      fail();
    } catch (NumberFormatException expected) {
    }
  }
Пример #16
0
  public void testLexicographicalComparator() {
    List<int[]> ordered =
        Arrays.asList(
            new int[] {},
            new int[] {LEAST},
            new int[] {LEAST, LEAST},
            new int[] {LEAST, (int) 1L},
            new int[] {(int) 1L},
            new int[] {(int) 1L, LEAST},
            new int[] {GREATEST, (GREATEST - (int) 1L)},
            new int[] {GREATEST, GREATEST},
            new int[] {GREATEST, GREATEST, GREATEST});

    Comparator<int[]> comparator = UnsignedInts.lexicographicalComparator();
    Helpers.testComparator(comparator, ordered);
  }
Пример #17
0
  private static int compare(
      @NotNull final Buffer left,
      long leftFrom,
      final long leftLength,
      @NotNull final Buffer right,
      long rightFrom,
      final long rightLength) {
    assert leftLength > 0;
    assert rightLength > 0;

    // Adapted from Guava UnsignedBytes

    long length = Math.min(leftLength, rightLength);

    for (;
        length >= Longs.BYTES;
        leftFrom += Longs.BYTES, rightFrom += Longs.BYTES, length -= Longs.BYTES) {
      final long lw = left.getLong(leftFrom);
      final long rw = right.getLong(rightFrom);
      if (lw != rw) {
        return UnsignedLongs.compare(lw, rw);
      }
    }

    if (length >= Ints.BYTES) {
      final int lw = left.getInt(leftFrom);
      final int rw = right.getInt(rightFrom);
      if (lw != rw) {
        return UnsignedInts.compare(lw, rw);
      }
      leftFrom += Ints.BYTES;
      rightFrom += Ints.BYTES;
      length -= Ints.BYTES;
    }

    for (; length > 0; leftFrom++, rightFrom++, length--) {
      final int result = UnsignedBytes.compare(left.get(leftFrom), right.get(rightFrom));

      if (result != 0) {
        return result;
      }
    }

    return (leftLength < rightLength) ? -1 : ((leftLength == rightLength) ? 0 : 1);
  }
Пример #18
0
 protected final int compare(int index1, int index2) {
   final boolean n1 = nulls.get(index1);
   final boolean n2 = nulls.get(index2);
   if (n1 && n2) {
     return 0;
   }
   if (n1 ^ n2) {
     return nullsFirst ? (n1 ? -1 : 1) : (n1 ? 1 : -1);
   }
   int compare;
   switch (type) {
     case 0:
       compare = Booleans.compare(booleans[index1], booleans[index2]);
       break;
     case 1:
       compare = bits[index1] - bits[index2];
       break;
     case 2:
       compare = Shorts.compare(shorts[index1], shorts[index2]);
       break;
     case 3:
       compare = Ints.compare(ints[index1], ints[index2]);
       break;
     case 4:
       compare = Longs.compare(longs[index1], longs[index2]);
       break;
     case 5:
       compare = Floats.compare(floats[index1], floats[index2]);
       break;
     case 6:
       compare = Doubles.compare(doubles[index1], doubles[index2]);
       break;
     case 7:
       compare = TextDatum.COMPARATOR.compare(bytes[index1], bytes[index2]);
       break;
     case 8:
       compare = UnsignedInts.compare(ints[index1], ints[index2]);
       break;
     default:
       throw new IllegalArgumentException();
   }
   return ascending ? compare : -compare;
 }
Пример #19
0
 public void testParseInt() {
   for (long a : UNSIGNED_INTS) {
     assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a)));
   }
 }
Пример #20
0
 public void testToLong() {
   for (long a : UNSIGNED_INTS) {
     assertEquals(a, UnsignedInts.toLong((int) a));
   }
 }
Пример #21
0
 private static String join(int... values) {
   return UnsignedInts.join(",", values);
 }