Make objdetect/test C++11-compliant and reproducible

- Add conditional compilation directives to replace deprecated std::random_shuffle with new std::shuffle when C++11 is available.

- Set random seed to a fixed value before shuffling containers to ensure reproducibility.

Resolves opencv/opencv#22209.
This commit is contained in:
Rong Mantle Bao 2022-07-11 18:36:36 +08:00
parent 0a88f84847
commit 3135063100
No known key found for this signature in database
GPG Key ID: 419F9DDF521C96C1
2 changed files with 25 additions and 2 deletions

View File

@ -8,4 +8,10 @@
#include "opencv2/objdetect.hpp"
#include "opencv2/objdetect/objdetect_c.h"
#if defined CV_CXX11
#include <random>
#else
#include <cstdlib>
#endif
#endif

View File

@ -5,6 +5,16 @@
#include "test_precomp.hpp"
namespace opencv_test { namespace {
#if !defined CV_CXX11
// Wrapper for generating seeded random number via std::rand.
template<unsigned Seed>
class SeededRandFunctor {
public:
SeededRandFunctor() { std::srand(Seed); }
int operator()(int i) { return std::rand() % (i + 1); }
};
#endif
std::string encode_qrcode_images_name[] = {
"version1_mode1.png", "version1_mode2.png", "version1_mode4.png",
"version2_mode1.png", "version2_mode2.png", "version2_mode4.png",
@ -381,8 +391,15 @@ TEST(Objdetect_QRCode_Encode_Decode_Structured_Append, DISABLED_regression)
std::string symbol_set = config["symbols_set"];
std::string input_info = symbol_set;
std::random_shuffle(input_info.begin(), input_info.end());
#if defined CV_CXX11
// std::random_shuffle is deprecated since C++11 and removed in C++17.
// Use manually constructed RNG with a fixed seed and std::shuffle instead.
std::mt19937 rand_gen {1};
std::shuffle(input_info.begin(), input_info.end(), rand_gen);
#else
SeededRandFunctor<1> rand_gen;
std::random_shuffle(input_info.begin(), input_info.end(), rand_gen);
#endif
for (int j = min_stuctures_num; j < max_stuctures_num; j++)
{
QRCodeEncoder::Params params;