Skip to content

Commit

Permalink
Add test/get_integral_result_4.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Nov 16, 2024
1 parent 1a00a20 commit 97c52f5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ run has_constant_size.cpp ;
run get_integral_result.cpp ;
run get_integral_result_2.cpp ;
run get_integral_result_3.cpp ;
run get_integral_result_4.cpp ;

# digest

Expand Down
86 changes: 86 additions & 0 deletions test/get_integral_result_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2017, 2018, 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/hash2/get_integral_result.hpp>
#include <boost/core/lightweight_test.hpp>
#include <set>
#include <limits>
#include <cstdint>

template<class T> void test_identity()
{
using boost::hash2::get_integral_result;

using R = T;

for( unsigned i = 0; i <= std::numeric_limits<T>::max(); ++i )
{
R r = static_cast<R>( i );
T t = get_integral_result<T>( r );

BOOST_TEST_EQ( t, r );
}
}

template<class T, class R> void test_permutation( R offset = 0, R scale = 1 )
{
using boost::hash2::get_integral_result;

std::set<T> dist;

for( unsigned i = 0; i <= std::numeric_limits<T>::max(); ++i )
{
R r = static_cast<R>( i * scale + offset );
T t = get_integral_result<T>( r );

dist.insert( t );
}

BOOST_TEST_EQ( dist.size(), std::numeric_limits<T>::max() + 1u );
}

template<class T, class R> void test_roundtrip()
{
using boost::hash2::get_integral_result;

std::set<T> dist;

for( unsigned i = 0; i <= std::numeric_limits<T>::max(); ++i )
{
T t1 = static_cast<T>( i );
R r = get_integral_result<R>( t1 );
T t2 = get_integral_result<T>( r );

dist.insert( t2 );
}

BOOST_TEST_EQ( dist.size(), std::numeric_limits<T>::max() + 1u );
}

int main()
{
test_identity<std::uint8_t>();

test_permutation<std::uint8_t, std::uint8_t>();
test_permutation<std::uint8_t, std::uint16_t>();
test_permutation<std::uint8_t, std::uint32_t>();
test_permutation<std::uint8_t, std::uint64_t>();

test_roundtrip<std::uint8_t, std::uint8_t>();
test_roundtrip<std::uint8_t, std::uint16_t>();
test_roundtrip<std::uint8_t, std::uint32_t>();
test_roundtrip<std::uint8_t, std::uint64_t>();

test_identity<std::uint16_t>();

test_permutation<std::uint16_t, std::uint16_t>();
test_permutation<std::uint16_t, std::uint32_t>();
test_permutation<std::uint16_t, std::uint64_t>();

test_roundtrip<std::uint16_t, std::uint16_t>();
test_roundtrip<std::uint16_t, std::uint32_t>();
test_roundtrip<std::uint16_t, std::uint64_t>();

return boost::report_errors();
}

0 comments on commit 97c52f5

Please sign in to comment.