Files
core/o3tl/qa/test-hash_combine.cxx
Tomaž Vajngerl 794e86b99d o3tl: Add concepts for 32/64 bit types, use in hash_combine
This simplifies determining what the byte size of the template type
is, so we can write a specific implementation for 32 and 64 bit
seeds for hash_combine.

Change-Id: Icd68e4618151957ecc00585dd521adf11fe3931a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/182266
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
2025-03-03 09:43:18 +01:00

74 lines
2.0 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <sal/types.h>
#include <rtl/ustring.hxx>
#include <o3tl/hash_combine.hxx>
namespace
{
struct TestStruct
{
};
class HashCombineTest : public CppUnit::TestFixture
{
public:
void testBaseUsage()
{
TestStruct objectA;
TestStruct objectB;
{
sal_uInt64 seed(0);
o3tl::hash_combine(seed, OUString("Hmm-.."));
o3tl::hash_combine(seed, 24);
o3tl::hash_combine(seed, 42u);
o3tl::hash_combine(seed, 5.1);
o3tl::hash_combine(seed, &objectA);
o3tl::hash_combine(seed, &objectB);
CPPUNIT_ASSERT(seed != sal_uInt64(0));
static_assert(sizeof(seed) == 8);
}
{
sal_uInt32 seed(0);
o3tl::hash_combine(seed, OUString("Hmm-.."));
o3tl::hash_combine(seed, 24);
o3tl::hash_combine(seed, 42u);
o3tl::hash_combine(seed, 5.1);
o3tl::hash_combine(seed, &objectA);
o3tl::hash_combine(seed, &objectB);
CPPUNIT_ASSERT(seed != sal_uInt32(0));
static_assert(sizeof(seed) == 4);
}
// No assert of the actual value - we just check this compiles and runs. Hash algorithm is probably
// also implementation specific.
// Using sal_uInt16 for seed doesn't compile as there is not compatible hash_combine override for seed that is 16 bit.
}
CPPUNIT_TEST_SUITE(HashCombineTest);
CPPUNIT_TEST(testBaseUsage);
CPPUNIT_TEST_SUITE_END();
};
}
CPPUNIT_TEST_SUITE_REGISTRATION(HashCombineTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */