1
0
Fork 0
forked from lthn/blockchain

unit_tests: median_helper_tests improved to make sure new optimized version of median() works exactly the same as the old one

This commit is contained in:
sowle 2024-02-04 23:52:56 +01:00
parent 1ef05b36e1
commit 01adc5087c
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC

View file

@ -9,6 +9,35 @@
#include "misc_language.h"
namespace epee::misc_utils
{
// old impelementation that uses std::sort
template<class type_vec_type>
type_vec_type old_median(std::vector<type_vec_type> &v)
{
//CRITICAL_REGION_LOCAL(m_lock);
if(v.empty())
return boost::value_initialized<type_vec_type>();
if(v.size() == 1)
return v[0];
size_t n = (v.size()) / 2;
std::sort(v.begin(), v.end());
//nth_element(v.begin(), v.begin()+n-1, v.end());
if(v.size()%2)
{//1, 3, 5...
return v[n];
}else
{//2, 4, 6...
return (v[n-1] + v[n])/2;
}
}
} // namespace epee::misc_utils
//------------------------------------------------------------------------------
bool test_median(const std::vector<uint64_t>& v_)
{
std::vector<uint64_t> v(v_);
@ -21,8 +50,9 @@ bool test_median(const std::vector<uint64_t>& v_)
}
uint64_t m1 = epee::misc_utils::median(v);
uint64_t m2 = mh.get_median();
if (m1 != m2)
return false;
uint64_t m3 = epee::misc_utils::old_median(v);
CHECK_AND_ASSERT_MES(m1 == m2, false, "m1 != m2");
CHECK_AND_ASSERT_MES(m2 == m3, false, "m2 != m3");
return true;
}
@ -100,4 +130,4 @@ TEST(median_helper_test, median_helper_test)
mh.scan_items(cb, cb_finalizer);
}
}