Skip to content

Commit

Permalink
Make objdetect/test C++11-compliant and reproducible
Browse files Browse the repository at this point in the history
- 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#22209.
  • Loading branch information
CSharperMantle committed Jul 12, 2022
1 parent 0a88f84 commit 3135063
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions modules/objdetect/test/test_precomp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 19 additions & 2 deletions modules/objdetect/test/test_qrcode_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3135063

Please sign in to comment.