@Test
  public void testIntersectingRanges() {
    mts = new MerkleTrees(partitioner);

    boolean failure = true;
    mts.addMerkleTree(1, new Range<>(tok(1), tok(3)));

    try {
      mts.addMerkleTree(1, new Range<>(tok(2), tok(4)));
    } catch (AssertionError e) {
      failure = false;
    }

    assertFalse(failure);
  }
  @Test
  public void testDifference() {
    int maxsize = 16;
    mts = new MerkleTrees(partitioner);
    mts.addMerkleTree(32, fullRange());

    MerkleTrees mts2 = new MerkleTrees(partitioner);
    mts2.addMerkleTree(32, fullRange());

    mts.init();
    mts2.init();

    // add dummy hashes to both trees
    for (TreeRange range : mts.invalids()) range.addAll(new HIterator(range.right));
    for (TreeRange range : mts2.invalids()) range.addAll(new HIterator(range.right));

    TreeRange leftmost = null;
    TreeRange middle = null;

    mts.maxsize(fullRange(), maxsize + 2); // give some room for splitting

    // split the leftmost
    Iterator<TreeRange> ranges = mts.invalids();
    leftmost = ranges.next();
    mts.split(leftmost.right);

    // set the hashes for the leaf of the created split
    middle = mts.get(leftmost.right);
    middle.hash("arbitrary!".getBytes());
    mts.get(partitioner.midpoint(leftmost.left, leftmost.right))
        .hash("even more arbitrary!".getBytes());

    // trees should disagree for (leftmost.left, middle.right]
    List<Range<Token>> diffs = MerkleTrees.difference(mts, mts2);
    assertEquals(diffs + " contains wrong number of differences:", 1, diffs.size());
    assertTrue(diffs.contains(new Range<>(leftmost.left, middle.right)));
  }
  @Test
  public void testSplitLimitSize() {
    mts = new MerkleTrees(partitioner);

    mts.addMerkleTree(2, fullRange());

    assertTrue(mts.split(tok(4)));
    assertEquals(2, mts.size());

    // should fail to split above maxsize
    assertFalse(mts.split(tok(2)));
    assertEquals(2, mts.size());
    assertEquals(new Range<>(tok(4), tok(-1)), mts.get(tok(-1)));
    assertEquals(new Range<>(tok(-1), tok(4)), mts.get(tok(4)));
  }
  @Test
  public void testSplitLimitDepth() {
    mts = new MerkleTrees(partitioner);

    mts.addMerkleTree(Integer.MAX_VALUE, (byte) 2, fullRange());

    assertTrue(mts.split(tok(4)));
    assertTrue(mts.split(tok(2)));
    assertEquals(3, mts.size());

    // should fail to split below hashdepth
    assertFalse(mts.split(tok(1)));
    assertEquals(3, mts.size());
    assertEquals(new Range<>(tok(4), tok(-1)), mts.get(tok(-1)));
    assertEquals(new Range<>(tok(-1), tok(2)), mts.get(tok(2)));
    assertEquals(new Range<>(tok(2), tok(4)), mts.get(tok(4)));
  }
  /**
   * Generate two trees with different splits, but containing the same keys, and check that they
   * compare equally.
   *
   * <p>The set of keys used in this test is: #{2,4,6,8,12,14,0}
   */
  @Test
  public void testValidateTree() {
    TOKEN_SCALE = new BigInteger("16"); // this test needs slightly more resolution

    Range<Token> full = new Range<>(tok(-1), tok(-1));
    Iterator<TreeRange> ranges;
    MerkleTrees mts2 = new MerkleTrees(partitioner);
    mts2.addMerkleTree(Integer.MAX_VALUE, fullRange());

    mts.split(tok(8));
    mts.split(tok(4));
    mts.split(tok(12));
    mts.split(tok(6));
    mts.split(tok(10));

    ranges = mts.invalids();
    ranges.next().addAll(new HIterator(2, 4)); // (-1,4]: depth 2
    ranges.next().addAll(new HIterator(6)); // (4,6]
    ranges.next().addAll(new HIterator(8)); // (6,8]
    ranges.next().addAll(new HIterator(/*empty*/ new int[0])); // (8,10]
    ranges.next().addAll(new HIterator(12)); // (10,12]
    ranges.next().addAll(new HIterator(14, -1)); // (12,-1]: depth 2

    mts2.split(tok(8));
    mts2.split(tok(4));
    mts2.split(tok(12));
    mts2.split(tok(2));
    mts2.split(tok(10));
    mts2.split(tok(9));
    mts2.split(tok(11));

    ranges = mts2.invalids();
    ranges.next().addAll(new HIterator(2)); // (-1,2]
    ranges.next().addAll(new HIterator(4)); // (2,4]
    ranges.next().addAll(new HIterator(6, 8)); // (4,8]: depth 2
    ranges.next().addAll(new HIterator(/*empty*/ new int[0])); // (8,9]
    ranges.next().addAll(new HIterator(/*empty*/ new int[0])); // (9,10]
    ranges.next().addAll(new HIterator(/*empty*/ new int[0])); // (10,11]: depth 4
    ranges.next().addAll(new HIterator(12)); // (11,12]: depth 4
    ranges.next().addAll(new HIterator(14, -1)); // (12,-1]: depth 2

    byte[] mthash = mts.hash(full);
    byte[] mt2hash = mts2.hash(full);
    assertHashEquals("Tree hashes did not match: " + mts + " && " + mts2, mthash, mt2hash);
  }
  @Test
  public void testHashRandom() {
    int max = 1000000;
    TOKEN_SCALE = new BigInteger("" + max);

    mts = new MerkleTrees(partitioner);
    mts.addMerkleTree(32, fullRange());

    Random random = new Random();
    while (true) {
      if (!mts.split(tok(random.nextInt(max)))) break;
    }

    // validate the tree
    TreeRangeIterator ranges = mts.invalids();
    for (TreeRange range : ranges) range.addHash(new RowHash(range.right, new byte[0], 0));

    assert mts.hash(new Range<>(tok(-1), tok(-1))) != null : "Could not hash tree " + mts;
  }
 @Before
 public void clear() {
   TOKEN_SCALE = new BigInteger("8");
   mts = new MerkleTrees(partitioner);
   mts.addMerkleTree(Integer.MAX_VALUE, fullRange());
 }