@Test
  public void testSerialization() throws Exception {
    Range<Token> first = new Range<>(tok(3), tok(4));

    Collection<Range<Token>> ranges = new ArrayList<>();

    ranges.add(first);
    ranges.add(new Range<Token>(tok(5), tok(2)));

    mts = new MerkleTrees(partitioner);
    mts.addMerkleTrees(256, ranges);

    // populate and validate the tree
    mts.init();
    for (TreeRange range : mts.invalids()) range.addAll(new HIterator(range.right));

    byte[] initialhash = mts.hash(first);

    long serializedSize =
        MerkleTrees.serializer.serializedSize(mts, MessagingService.current_version);
    DataOutputBuffer out = new DataOutputBuffer();
    MerkleTrees.serializer.serialize(mts, out, MessagingService.current_version);
    byte[] serialized = out.toByteArray();

    assertEquals(serializedSize, serialized.length);

    DataInputBuffer in = new DataInputBuffer(serialized);
    MerkleTrees restored = MerkleTrees.serializer.deserialize(in, MessagingService.current_version);

    assertHashEquals(initialhash, restored.hash(first));
  }
  @Test
  public void testHashRandom() {
    int max = 1000000;
    TOKEN_SCALE = new BigInteger("" + max);

    mt = new MerkleTree(partitioner, RECOMMENDED_DEPTH, 32);
    Random random = new Random();
    while (true) {
      if (!mt.split(tok(random.nextInt(max)))) break;
    }

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

    assert null != mt.hash(new Range(tok(-1), tok(-1))) : "Could not hash tree " + mt;
  }
  @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;
  }
  @Test
  public void testSerialization() throws Exception {
    Range full = new Range(tok(-1), tok(-1));
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);

    // populate and validate the tree
    mt.maxsize(256);
    mt.init();
    for (TreeRange range : mt.invalids(full)) range.addAll(new HIterator(range.right));

    byte[] initialhash = mt.hash(full);
    oout.writeObject(mt);
    oout.close();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream oin = new ObjectInputStream(bin);
    MerkleTree restored = (MerkleTree) oin.readObject();

    // restore partitioner after serialization
    restored.partitioner(partitioner);

    assertHashEquals(initialhash, restored.hash(full));
  }
  @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 testDifference() {
    Range full = new Range(tok(-1), tok(-1));
    int maxsize = 16;
    mt.maxsize(maxsize);
    MerkleTree mt2 = new MerkleTree(partitioner, RECOMMENDED_DEPTH, maxsize);
    mt.init();
    mt2.init();

    TreeRange leftmost = null;
    TreeRange middle = null;
    TreeRange rightmost = null;

    // compact the leftmost, and split the rightmost
    Iterator<TreeRange> ranges = mt.invalids(full);
    leftmost = ranges.next();
    rightmost = null;
    while (ranges.hasNext()) rightmost = ranges.next();
    mt.compact(leftmost.right);
    leftmost = mt.get(leftmost.right); // leftmost is now a larger range
    mt.split(rightmost.right);

    // set the hash for the left neighbor of rightmost
    middle = mt.get(rightmost.left);
    middle.hash("arbitrary!".getBytes());
    byte depth = middle.depth;

    // add dummy hashes to the rest of both trees
    for (TreeRange range : mt.invalids(full)) range.addAll(new HIterator(range.right));
    for (TreeRange range : mt2.invalids(full)) range.addAll(new HIterator(range.right));

    // trees should disagree for leftmost, (middle.left, rightmost.right]
    List<TreeRange> diffs = MerkleTree.difference(mt, mt2);
    assertEquals(diffs + " contains wrong number of differences:", 2, diffs.size());
    assertTrue(diffs.contains(leftmost));
    assertTrue(diffs.contains(new Range(middle.left, rightmost.right)));
  }