Utilities - Tuple hash computations

This commit is contained in:
Emmanuel BENOîT 2020-09-26 11:14:14 +02:00
parent 3889efde67
commit 8a19374d3f
2 changed files with 77 additions and 3 deletions

View file

@ -14,6 +14,11 @@ class ComputeHashTest : public CppUnit::TestFixture
CPPUNIT_TEST( testInt64 );
CPPUNIT_TEST( testStructDefault );
CPPUNIT_TEST( testPair );
CPPUNIT_TEST( testTupleEmpty );
CPPUNIT_TEST( testTupleOne );
CPPUNIT_TEST( testTuplePair );
CPPUNIT_TEST( testTupleMore );
CPPUNIT_TEST_SUITE_END( );
public:
@ -23,6 +28,11 @@ public:
void testInt64( );
void testStructDefault( );
void testPair( );
void testTupleEmpty( );
void testTupleOne( );
void testTuplePair( );
void testTupleMore( );
};
@ -79,10 +89,38 @@ void ComputeHashTest::testStructDefault( )
void ComputeHashTest::testPair( )
{
auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
const auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
uint32_t hv = 0x9e3779b9 + 12;
hv = 128 + 0x9e3779b9 + ( hv << 6 ) + ( hv >> 2 );
CPPUNIT_ASSERT_EQUAL( hv , ComputeHash( p ) );
}
/*----------------------------------------------------------------------------*/
void ComputeHashTest::testTupleEmpty( )
{
const std::tuple< > t{};
CPPUNIT_ASSERT_EQUAL( 0u , ComputeHash( t ) );
}
void ComputeHashTest::testTupleOne( )
{
const std::tuple< uint8_t > t{ 12 };
CPPUNIT_ASSERT_EQUAL( 0x9e3779b9 + 12u , ComputeHash( t ) );
}
void ComputeHashTest::testTuplePair( )
{
const auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
const auto t{ std::make_tuple< uint8_t , uint32_t >( 12 , 128 ) };
CPPUNIT_ASSERT_EQUAL( ComputeHash( p ) , ComputeHash( t ) );
}
void ComputeHashTest::testTupleMore( )
{
const auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
const auto t{ std::make_tuple< uint8_t , uint32_t , uint8_t >( 12 , 128 , 92 ) };
uint32_t hv = ComputeHash( p );
CombineHash( hv , uint8_t( 92 ) );
CPPUNIT_ASSERT_EQUAL( hv , ComputeHash( t ) );
}