/*
 * Copyright 2018 Patrick Moor <patrick@moor.ws>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ws.moor.gletscher.util;

import com.google.common.collect.Sets;
import com.google.common.hash.HashCode;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import static com.google.common.truth.Truth.assertThat;

@RunWith(JUnit4.class)
public class StreamSplitterTest {

  @Test
  public void findsCommonBlocksIfOffsetByOne() throws IOException {
    Random rnd = new Random(0);

    byte[] fileA = MoreArrays.randomBytes(rnd, 4 << 20);
    byte[] fileB = Arrays.copyOfRange(fileA, 1, fileA.length);

    StreamSplitter splitter = StreamSplitter.rollingHashSplitter(32 << 20);

    Set<HashCode> fileAHashes = new HashSet<>();
    splitter
        .split(new ByteArrayInputStream(fileA))
        .forEachRemaining(block -> fileAHashes.add(LegacyHashing.md5().hashBytes(block)));

    Set<HashCode> fileBHashes = new HashSet<>();
    splitter
        .split(new ByteArrayInputStream(fileB))
        .forEachRemaining(block -> fileBHashes.add(LegacyHashing.md5().hashBytes(block)));

    assertThat(Sets.union(fileAHashes, fileBHashes).size())
        .isLessThan(fileAHashes.size() + fileBHashes.size());
  }
}

@@ -718,16 +718,55 @@
 shCode;%0A
+import com.google.common.hash.Hashing;%0A
 import o
@@ -1590,38 +1590,32 @@
 AHashes.add(
-Legacy
 Hashing.
 md5().hashBy
@@ -1594,35 +1594,38 @@
 hes.add(Hashing.
-md5
+sha256
 ().hashBytes(blo
@@ -1797,22 +1797,16 @@
 add(
-Legacy
 Hashing.
 md5(
@@ -1805,11 +1805,14 @@
 ing.
-md5
+sha256
 ().h

use sha256 rather than md5 in test (the former is deprecated)