@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 testHashFull() {
    byte[] val = DUMMY;
    Range<Token> range = new Range<>(tok(-1), tok(-1));

    // (zero, zero]
    assertNull(mts.hash(range));

    // validate the range
    mts.get(tok(-1)).hash(val);

    assertHashEquals(val, mts.hash(range));
  }
  @Test
  public void testHashPartial() {
    byte[] val = DUMMY;
    byte[] leftval = hashed(val, 1, 1);
    byte[] partialval = hashed(val, 1);
    Range<Token> left = new Range<>(tok(-1), tok(4));
    Range<Token> partial = new Range<>(tok(2), tok(4));
    Range<Token> right = new Range<>(tok(4), tok(-1));
    Range<Token> linvalid = new Range<>(tok(1), tok(4));
    Range<Token> rinvalid = new Range<>(tok(4), tok(6));

    // (zero,two] (two,four] (four, zero]
    mts.split(tok(4));
    mts.split(tok(2));
    assertNull(mts.hash(left));
    assertNull(mts.hash(partial));
    assertNull(mts.hash(right));
    assertNull(mts.hash(linvalid));
    assertNull(mts.hash(rinvalid));

    // validate the range
    mts.get(tok(2)).hash(val);
    mts.get(tok(4)).hash(val);
    mts.get(tok(-1)).hash(val);

    assertHashEquals(leftval, mts.hash(left));
    assertHashEquals(partialval, mts.hash(partial));
    assertHashEquals(val, mts.hash(right));
    assertNull(mts.hash(linvalid));
    assertNull(mts.hash(rinvalid));
  }
  /**
   * 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 testHashDegenerate() {
    TOKEN_SCALE = new BigInteger("32");

    byte[] val = DUMMY;
    byte[] childfullval = hashed(val, 5, 5, 4);
    byte[] fullval = hashed(val, 5, 5, 4, 3, 2, 1);
    Range<Token> childfull = new Range<>(tok(-1), tok(4));
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    Range<Token> invalid = new Range<>(tok(4), tok(-1));

    mts.split(tok(16));
    mts.split(tok(8));
    mts.split(tok(4));
    mts.split(tok(2));
    mts.split(tok(1));
    assertNull(mts.hash(full));
    assertNull(mts.hash(childfull));
    assertNull(mts.hash(invalid));

    // validate the range
    mts.get(tok(1)).hash(val);
    mts.get(tok(2)).hash(val);
    mts.get(tok(4)).hash(val);
    mts.get(tok(8)).hash(val);
    mts.get(tok(16)).hash(val);
    mts.get(tok(-1)).hash(val);

    assertHashEquals(fullval, mts.hash(full));
    assertHashEquals(childfullval, mts.hash(childfull));
    assertNull(mts.hash(invalid));
  }
  @Test
  public void testHashInner() {
    byte[] val = DUMMY;
    byte[] lchildval = hashed(val, 3, 3, 2);
    byte[] rchildval = hashed(val, 2, 2);
    byte[] fullval = hashed(val, 3, 3, 2, 2, 2);
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    Range<Token> lchild = new Range<>(tok(-1), tok(4));
    Range<Token> rchild = new Range<>(tok(4), tok(-1));
    Range<Token> invalid = new Range<>(tok(1), tok(-1));

    // (zero,one] (one, two] (two,four] (four, six] (six, zero]
    mts.split(tok(4));
    mts.split(tok(2));
    mts.split(tok(6));
    mts.split(tok(1));
    assertNull(mts.hash(full));
    assertNull(mts.hash(lchild));
    assertNull(mts.hash(rchild));
    assertNull(mts.hash(invalid));

    // validate the range
    mts.get(tok(1)).hash(val);
    mts.get(tok(2)).hash(val);
    mts.get(tok(4)).hash(val);
    mts.get(tok(6)).hash(val);
    mts.get(tok(-1)).hash(val);

    assertHashEquals(fullval, mts.hash(full));
    assertHashEquals(lchildval, mts.hash(lchild));
    assertHashEquals(rchildval, mts.hash(rchild));
    assertNull(mts.hash(invalid));
  }
  @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;
  }