/** @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 testRangeSplit() throws Exception {
    IgniteUuid affKey = IgniteUuid.randomUuid();

    IgfsFileAffinityRange range = new IgfsFileAffinityRange(0, 9999, affKey);

    Collection<IgfsFileAffinityRange> split = range.split(10000);

    assertEquals(1, split.size());
    assertTrue(range.regionEqual(F.first(split)));

    split = range.split(5000);

    assertEquals(2, split.size());

    Iterator<IgfsFileAffinityRange> it = split.iterator();

    IgfsFileAffinityRange part = it.next();

    assertTrue(part.regionEqual(new IgfsFileAffinityRange(0, 4999, affKey)));

    part = it.next();

    assertTrue(part.regionEqual(new IgfsFileAffinityRange(5000, 9999, affKey)));

    split = range.split(3000);

    assertEquals(4, split.size());

    it = split.iterator();

    part = it.next();

    assertTrue(part.regionEqual(new IgfsFileAffinityRange(0, 2999, affKey)));

    part = it.next();

    assertTrue(part.regionEqual(new IgfsFileAffinityRange(3000, 5999, affKey)));

    part = it.next();

    assertTrue(part.regionEqual(new IgfsFileAffinityRange(6000, 8999, affKey)));

    part = it.next();

    assertTrue(part.regionEqual(new IgfsFileAffinityRange(9000, 9999, 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 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 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());
  }