Update the hash function tests to use a base class.

The base class performs the standard signature test that all hash
functions should pass.
This commit is contained in:
aherbert 2020-03-12 15:13:54 +00:00
parent a51c96520a
commit 9f2271334d
5 changed files with 70 additions and 58 deletions

View File

@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.commons.collections4.bloomfilter.hasher.function;
import static org.junit.Assert.assertEquals;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunctionIdentity;
import org.junit.Test;
/**
* Tests the signature of a hash function.
*/
public abstract class AbstractHashFunctionTest {
/**
* Test that the signature is properly generated.
*/
@Test
public void signatureTest() {
final HashFunction hf = createHashFunction();
final long expected = hf.apply(HashFunctionIdentity.prepareSignatureBuffer(hf), 0);
assertEquals(expected, hf.getSignature());
// Should be repeatable
final long expected2 = hf.apply(HashFunctionIdentity.prepareSignatureBuffer(hf), 0);
assertEquals(expected, expected2);
assertEquals("Apache Commons Collections", hf.getProvider());
}
/**
* Creates the hash function.
*
* @return the hash function
*/
protected abstract HashFunction createHashFunction();
}

View File

@ -18,15 +18,13 @@ package org.apache.commons.collections4.bloomfilter.hasher.function;
import static org.junit.Assert.assertEquals;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.junit.Test;
/**
* Tests the MD5 cyclic hash function.
*/
public class MD5CyclicTest {
public class MD5CyclicTest extends AbstractHashFunctionTest {
/**
* Test that the apply function returns the proper values.
@ -46,16 +44,8 @@ public class MD5CyclicTest {
assertEquals(l1 + l2 + l2, l);
}
/**
* Test that the signature is properly generated.
*/
@Test
public void signatureTest() {
final MD5Cyclic md5 = new MD5Cyclic();
final String arg = String.format("%s-%s-%s", md5.getName().toUpperCase(Locale.ROOT), md5.getSignedness(),
md5.getProcessType());
final long expected = md5.apply(arg.getBytes(StandardCharsets.UTF_8), 0);
assertEquals(expected, md5.getSignature());
assertEquals("Apache Commons Collections", md5.getProvider());
@Override
protected HashFunction createHashFunction() {
return new MD5Cyclic();
}
}

View File

@ -19,14 +19,13 @@ package org.apache.commons.collections4.bloomfilter.hasher.function;
import static org.junit.Assert.assertEquals;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.junit.Test;
/**
* Test that the Murmur3 128 x86 hash function works correctly.
*/
public class Murmur128x86CyclicTest {
public class Murmur128x86CyclicTest extends AbstractHashFunctionTest {
/**
* Test that the apply function returns the proper values.
@ -48,16 +47,8 @@ public class Murmur128x86CyclicTest {
assertEquals(l1 + l2 + l2, l);
}
/**
* Test that the signature is properly generated.
*/
@Test
public void signatureTest() {
final Murmur128x86Cyclic murmur = new Murmur128x86Cyclic();
final String arg = String.format("%s-%s-%s", murmur.getName().toUpperCase(Locale.ROOT), murmur.getSignedness(),
murmur.getProcessType());
final long expected = murmur.apply(arg.getBytes(StandardCharsets.UTF_8), 0);
assertEquals(expected, murmur.getSignature());
assertEquals("Apache Commons Collections", murmur.getProvider());
@Override
protected HashFunction createHashFunction() {
return new Murmur128x86Cyclic();
}
}

View File

@ -19,14 +19,13 @@ package org.apache.commons.collections4.bloomfilter.hasher.function;
import static org.junit.Assert.assertEquals;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.junit.Test;
/**
* Test that the Murmur3 32 x86 hash function works correctly.
*/
public class Murmur32x86IterativeTest {
public class Murmur32x86IterativeTest extends AbstractHashFunctionTest {
/**
* Test that the apply function returns the proper values.
@ -46,16 +45,8 @@ public class Murmur32x86IterativeTest {
assertEquals(-1561435247, l);
}
/**
* Test that the signature is properly generated.
*/
@Test
public void signatureTest() {
final Murmur32x86Iterative murmur = new Murmur32x86Iterative();
final String arg = String.format("%s-%s-%s", murmur.getName().toUpperCase(Locale.ROOT), murmur.getSignedness(),
murmur.getProcessType());
final long expected = murmur.apply(arg.getBytes(StandardCharsets.UTF_8), 0);
assertEquals(expected, murmur.getSignature());
assertEquals("Apache Commons Collections", murmur.getProvider());
@Override
protected HashFunction createHashFunction() {
return new Murmur32x86Iterative();
}
}

View File

@ -20,13 +20,13 @@ import static org.junit.Assert.assertEquals;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
import org.apache.commons.collections4.bloomfilter.hasher.HashFunction;
import org.junit.Test;
/**
* Tests that the Objects hash works correctly.
*/
public class ObjectsHashIterativeTest {
public class ObjectsHashIterativeTest extends AbstractHashFunctionTest {
/**
* Test that the apply function returns the proper values.
@ -49,18 +49,8 @@ public class ObjectsHashIterativeTest {
assertEquals(Arrays.deepHashCode(new Object[] {prev, buffer}), l);
}
/**
* Test that the signature is properly generated.
*/
@Test
public void signatureTest() {
final ObjectsHashIterative obj = new ObjectsHashIterative();
final String arg = String.format("%s-%s-%s", obj.getName().toUpperCase(Locale.ROOT), obj.getSignedness(),
obj.getProcessType());
final long expected = obj.apply(arg.getBytes(StandardCharsets.UTF_8), 0);
final long expected2 = obj.apply(arg.getBytes(StandardCharsets.UTF_8), 0);
assertEquals(expected, expected2);
assertEquals(expected, obj.getSignature());
assertEquals("Apache Commons Collections", obj.getProvider());
@Override
protected HashFunction createHashFunction() {
return new ObjectsHashIterative();
}
}