/** @throws Exception If failed. */
  public void testRanges() throws Exception {
    IgfsFileMap map = new IgfsFileMap();

    IgniteUuid[] affKeys = new IgniteUuid[20];

    for (int i = 0; i < affKeys.length; i++) affKeys[i] = IgniteUuid.randomUuid();

    int numOfRanges = 0;

    do {
      for (int i = 0; i < 2 * numOfRanges + 1; i++) {
        long off1 = i * 10;
        long off2 = i * 10 + 5;
        long off3 = i * 10 + 8;

        IgniteUuid affKey = i % 2 == 0 ? null : affKeys[i / 2];

        assertEquals("For i: " + i, affKey, map.affinityKey(off1, false));
        assertEquals("For i: " + i, affKey, map.affinityKey(off2, false));
        assertEquals("For i: " + i, affKey, map.affinityKey(off3, false));
      }

      map.addRange(
          new IgfsFileAffinityRange(
              10 + 20 * numOfRanges, 19 + 20 * numOfRanges, affKeys[numOfRanges]));

      numOfRanges++;
    } while (numOfRanges < 20);
  }
  /** @throws Exception If failed. */
  public void testAddUpdateAdd() throws Exception {
    IgfsFileMap map = new IgfsFileMap();

    IgniteUuid affKey = IgniteUuid.randomUuid();

    map.addRange(new IgfsFileAffinityRange(0, 9, affKey));

    map.updateRangeStatus(new IgfsFileAffinityRange(0, 9, affKey), RANGE_STATUS_MOVING);

    map.addRange(new IgfsFileAffinityRange(10, 19, affKey));

    List<IgfsFileAffinityRange> ranges = map.ranges();

    assertEquals(2, ranges.size());

    assertEquals(RANGE_STATUS_MOVING, ranges.get(0).status());
    assertTrue(ranges.get(0).regionEqual(new IgfsFileAffinityRange(0, 9, affKey)));

    assertEquals(RANGE_STATUS_INITIAL, ranges.get(1).status());
    assertTrue(ranges.get(1).regionEqual(new IgfsFileAffinityRange(10, 19, affKey)));
  }
  /** @throws Exception If failed. */
  public void testRangeUpdate1() throws Exception {
    IgfsFileMap map = new IgfsFileMap();

    IgniteUuid affKey = IgniteUuid.randomUuid();

    for (int i = 0; i < 4; i++)
      map.addRange(new IgfsFileAffinityRange(i * 20 + 10, i * 20 + 19, affKey));

    // Middle, first, last.
    map.updateRangeStatus(new IgfsFileAffinityRange(30, 39, affKey), RANGE_STATUS_MOVING);
    map.updateRangeStatus(new IgfsFileAffinityRange(10, 19, affKey), RANGE_STATUS_MOVING);
    map.updateRangeStatus(new IgfsFileAffinityRange(70, 79, affKey), RANGE_STATUS_MOVING);

    List<IgfsFileAffinityRange> ranges = map.ranges();

    assertEquals(RANGE_STATUS_MOVING, ranges.get(0).status());
    assertEquals(RANGE_STATUS_MOVING, ranges.get(1).status());
    assertEquals(RANGE_STATUS_INITIAL, ranges.get(2).status());
    assertEquals(RANGE_STATUS_MOVING, ranges.get(3).status());

    // Middle, first, last.
    map.updateRangeStatus(new IgfsFileAffinityRange(30, 39, affKey), RANGE_STATUS_MOVED);
    map.updateRangeStatus(new IgfsFileAffinityRange(10, 19, affKey), RANGE_STATUS_MOVED);
    map.updateRangeStatus(new IgfsFileAffinityRange(70, 79, affKey), RANGE_STATUS_MOVED);

    ranges = map.ranges();

    assertEquals(RANGE_STATUS_MOVED, ranges.get(0).status());
    assertEquals(RANGE_STATUS_MOVED, ranges.get(1).status());
    assertEquals(RANGE_STATUS_INITIAL, ranges.get(2).status());
    assertEquals(RANGE_STATUS_MOVED, ranges.get(3).status());

    // Middle, first, last.
    map.deleteRange(new IgfsFileAffinityRange(30, 39, affKey));
    map.deleteRange(new IgfsFileAffinityRange(10, 19, affKey));
    map.deleteRange(new IgfsFileAffinityRange(70, 79, affKey));

    ranges = map.ranges();

    assertEquals(1, ranges.size());
    assertEquals(RANGE_STATUS_INITIAL, ranges.get(0).status());
    assertTrue(ranges.get(0).regionEqual(new IgfsFileAffinityRange(50, 59, affKey)));
  }
  /** @throws Exception If failed. */
  public void testInvalidRangeUpdates() throws Exception {
    final IgfsFileMap map = new IgfsFileMap();

    final IgniteUuid affKey1 = IgniteUuid.randomUuid();
    final IgniteUuid affKey2 = IgniteUuid.randomUuid();

    map.addRange(new IgfsFileAffinityRange(10, 19, affKey1));
    map.addRange(new IgfsFileAffinityRange(30, 39, affKey1));

    GridTestUtils.assertThrows(
        log,
        new Callable<Object>() {
          @Override
          public Object call() throws Exception {
            map.updateRangeStatus(new IgfsFileAffinityRange(0, 5, affKey1), RANGE_STATUS_MOVING);

            return null;
          }
        },
        IgfsInvalidRangeException.class,
        null);

    GridTestUtils.assertThrows(
        log,
        new Callable<Object>() {
          @Override
          public Object call() throws Exception {
            map.updateRangeStatus(new IgfsFileAffinityRange(15, 19, affKey1), RANGE_STATUS_MOVING);

            return null;
          }
        },
        IgfsInvalidRangeException.class,
        null);

    GridTestUtils.assertThrows(
        log,
        new Callable<Object>() {
          @Override
          public Object call() throws Exception {
            map.updateRangeStatus(new IgfsFileAffinityRange(10, 19, affKey2), RANGE_STATUS_MOVING);

            return null;
          }
        },
        AssertionError.class,
        null);

    GridTestUtils.assertThrows(
        log,
        new Callable<Object>() {
          @Override
          public Object call() throws Exception {
            map.updateRangeStatus(new IgfsFileAffinityRange(10, 22, affKey1), RANGE_STATUS_MOVING);

            return null;
          }
        },
        AssertionError.class,
        null);

    assertEquals(2, map.ranges().size());
  }