From c60ca94cdd836aac8bcee93b01f541bab24de7a5 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 15 Feb 2022 20:23:05 -0800 Subject: [PATCH] tests: Fix MSVC 2022 build Instead of trying to detect if we can safely use random shuffle simply reimplement it ourselves. The quality of the RNG is not essential for these tests. --- tests/test_xpath.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/test_xpath.cpp b/tests/test_xpath.cpp index b057c57..0fbe392 100644 --- a/tests/test_xpath.cpp +++ b/tests/test_xpath.cpp @@ -10,14 +10,6 @@ #include #include -// std::random_shuffle is deprecated in c++14, is removed in c++17. -#if defined(__cplusplus) && (__cplusplus >= 201402L) -# include -# define PUGIXML_SHUFFLE(rng) std::shuffle(rng.begin(), rng.end(), std::default_random_engine{std::random_device{}()}) -#else -# define PUGIXML_SHUFFLE(rng) std::random_shuffle(rng.begin(), rng.end()) -#endif - using namespace pugi; static void load_document_copy(xml_document& doc, const char_t* text) @@ -28,6 +20,22 @@ static void load_document_copy(xml_document& doc, const char_t* text) doc.append_copy(source.first_child()); } +template +static void random_shuffle(std::vector& v) +{ + size_t rng = 2147483647; + + for (size_t i = v.size() - 1; i > 0; --i) + { + // Fisher-Yates shuffle + size_t j = rng % (i + 1); + std::swap(v[j], v[i]); + + // LCG RNG, constants from Numerical Recipes + rng = rng * 1664525 + 1013904223; + } +} + TEST(xpath_allocator_many_pages) { std::basic_string query = STR("0"); @@ -163,7 +171,7 @@ TEST(xpath_sort_random_medium) xpath_node_set ns = doc.select_nodes(STR("//node() | //@*")); std::vector nsv(ns.begin(), ns.end()); - PUGIXML_SHUFFLE(nsv); + random_shuffle(nsv); xpath_node_set copy(&nsv[0], &nsv[0] + nsv.size()); copy.sort(); @@ -192,7 +200,7 @@ TEST(xpath_sort_random_large) xpath_node_set ns = doc.select_nodes(STR("//node() | //@*")); std::vector nsv(ns.begin(), ns.end()); - PUGIXML_SHUFFLE(nsv); + random_shuffle(nsv); xpath_node_set copy(&nsv[0], &nsv[0] + nsv.size()); copy.sort();