From ed1e439e8ddff68fea93a519c2d497df6e485484 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Fri, 8 Mar 2024 18:37:36 +0100 Subject: [PATCH 1/7] random edges generator --- .../include/gudhi/random_graph_generators.h | 67 +++++++++++++++++++ src/common/test/CMakeLists.txt | 2 + .../test/test_random_graph_generators.cpp | 64 ++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 src/common/include/gudhi/random_graph_generators.h create mode 100644 src/common/test/test_random_graph_generators.cpp diff --git a/src/common/include/gudhi/random_graph_generators.h b/src/common/include/gudhi/random_graph_generators.h new file mode 100644 index 0000000000..391d66f1c5 --- /dev/null +++ b/src/common/include/gudhi/random_graph_generators.h @@ -0,0 +1,67 @@ +/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. + * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. + * Author(s): Vincent Rouvreau + * + * Copyright (C) 2024 Inria + * + * Modification(s): + * - YYYY/MM Author: Description of the modification + */ + +#ifndef RANDOM_GRAPH_GENERATORS_H_ +#define RANDOM_GRAPH_GENERATORS_H_ + +#include // for std::prev_permutation +#include +#include // for std::pair +#include +#ifdef DEBUG_TRACES +#include +#endif // DEBUG_TRACES + +std::random_device rd; + +namespace Gudhi { + +template +std::vector> random_edges(Vertex_handle nb_vertices, double density = 0.15) { + std::vector> permutations; + if (nb_vertices < 2) + return permutations; + + std::uniform_real_distribution unif(0., 1.); + std::mt19937 rand_engine(rd()); + + std::vector to_permute(nb_vertices); + std::fill(to_permute.begin(), to_permute.begin() + 2, true); + + do { + // Keep only X% of the possible edges + if (unif(rand_engine) > density) + continue; + + std::pair permutation = {-1, -1}; + for (Vertex_handle idx = 0; idx < nb_vertices; ++idx) { + if (to_permute[idx]) { + if (permutation.first == -1) { + permutation.first = idx; + } else { + permutation.second = idx; + // No need to go further, only 2 'true' values + break; + } + } + } +#ifdef DEBUG_TRACES + std::cout << permutation.first << ", " << permutation.second << std::endl; +#endif // DEBUG_TRACES + permutations.push_back(permutation); + //std::cout << permutation.first << ", " << permutation.second << "\n"; + } while (std::prev_permutation(to_permute.begin(), to_permute.end())); + + return permutations; +} + +} // namespace Gudhi + +#endif // RANDOM_GRAPH_GENERATORS_H_ diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt index 34de73988a..8d99e8d745 100644 --- a/src/common/test/CMakeLists.txt +++ b/src/common/test/CMakeLists.txt @@ -5,6 +5,7 @@ include(GUDHI_boost_test) add_executable ( Common_test_points_off_reader test_points_off_reader.cpp ) add_executable ( Common_test_distance_matrix_reader test_distance_matrix_reader.cpp ) add_executable ( Common_test_persistence_intervals_reader test_persistence_intervals_reader.cpp ) +add_executable ( Common_test_random_test_generators test_random_graph_generators.cpp ) # Do not forget to copy test files in current binary dir file(COPY "${CMAKE_SOURCE_DIR}/data/points/alphacomplexdoc.off" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) @@ -17,3 +18,4 @@ file(COPY "${CMAKE_SOURCE_DIR}/src/common/test/persistence_intervals_without_dim gudhi_add_boost_test(Common_test_points_off_reader) gudhi_add_boost_test(Common_test_distance_matrix_reader) gudhi_add_boost_test(Common_test_persistence_intervals_reader) +gudhi_add_boost_test(Common_test_random_test_generators) diff --git a/src/common/test/test_random_graph_generators.cpp b/src/common/test/test_random_graph_generators.cpp new file mode 100644 index 0000000000..712172b890 --- /dev/null +++ b/src/common/test/test_random_graph_generators.cpp @@ -0,0 +1,64 @@ +/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. + * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. + * Author(s): Vincent Rouvreau + * + * Copyright (C) 2024 Inria + * + * Modification(s): + * - YYYY/MM Author: Description of the modification + */ + +#include + +#include +#include +#include + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "random_graph_generators" +#include + +using Edge = std::pair; +using Edge_range = std::vector; + +BOOST_AUTO_TEST_CASE( random_edges_limits ) +{ + Edge_range edges = Gudhi::random_edges(0); + std::cout << "Gudhi::random_edges(0).size() = " << edges.size() << std::endl; + BOOST_CHECK(edges.size() == 0); + + edges = Gudhi::random_edges(1); + std::cout << "Gudhi::random_edges(1).size() = " << edges.size() << std::endl; + BOOST_CHECK(edges.size() == 0); + + edges = Gudhi::random_edges(2, 1.); + std::cout << "Gudhi::random_edges(2).size() = " << edges.size() << std::endl; + BOOST_CHECK(edges.size() == 1); + + edges = Gudhi::random_edges(15, 0.); + std::cout << "Gudhi::random_edges(15, 0.).size() = " << edges.size() << std::endl; + BOOST_CHECK(edges.size() == 0); + + edges = Gudhi::random_edges(15, 1.); + std::cout << "Gudhi::random_edges(15, 1.).size() = " << edges.size() << std::endl; + BOOST_CHECK(edges.size() == 105); +} + +BOOST_AUTO_TEST_CASE( random_edges_mean ) +{ + int nb_edges {0}; + for (int idx = 0; idx < 100; ++idx) { + Edge_range edges = Gudhi::random_edges(15); + nb_edges += edges.size(); +#ifdef DEBUG_TRACES + std::cout << edges.size() << ", "; +#endif // DEBUG_TRACES + } +#ifdef DEBUG_TRACES + std::cout << "\n"; +#endif // DEBUG_TRACES + std::cout << "Total number of edges for 100 x Gudhi::random_edges(15) [aka. 15% of 105] = " << nb_edges << std::endl; + // 1575 +/- 10% + BOOST_CHECK(nb_edges < 1733); + BOOST_CHECK(nb_edges > 1417); +} From 7376b45d3f7215d25192baead1d926ad483c8f5f Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Fri, 8 Mar 2024 19:48:05 +0100 Subject: [PATCH 2/7] simplex_tree_random_flag_complex --- .../include/gudhi/random_graph_generators.h | 37 +++++++++++++---- .../test/test_random_graph_generators.cpp | 40 +++++++++++++++---- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/common/include/gudhi/random_graph_generators.h b/src/common/include/gudhi/random_graph_generators.h index 391d66f1c5..6348036893 100644 --- a/src/common/include/gudhi/random_graph_generators.h +++ b/src/common/include/gudhi/random_graph_generators.h @@ -11,10 +11,14 @@ #ifndef RANDOM_GRAPH_GENERATORS_H_ #define RANDOM_GRAPH_GENERATORS_H_ +#include + + #include // for std::prev_permutation #include -#include // for std::pair +#include #include +#include // for std::iota #ifdef DEBUG_TRACES #include #endif // DEBUG_TRACES @@ -24,8 +28,8 @@ std::random_device rd; namespace Gudhi { template -std::vector> random_edges(Vertex_handle nb_vertices, double density = 0.15) { - std::vector> permutations; +std::vector> random_edges(Vertex_handle nb_vertices, double density = 0.15) { + std::vector> permutations; if (nb_vertices < 2) return permutations; @@ -40,13 +44,13 @@ std::vector> random_edges(Vertex_handle if (unif(rand_engine) > density) continue; - std::pair permutation = {-1, -1}; + std::array permutation = {-1, -1}; for (Vertex_handle idx = 0; idx < nb_vertices; ++idx) { if (to_permute[idx]) { - if (permutation.first == -1) { - permutation.first = idx; + if (permutation[0] == -1) { + permutation[0] = idx; } else { - permutation.second = idx; + permutation[1] = idx; // No need to go further, only 2 'true' values break; } @@ -62,6 +66,25 @@ std::vector> random_edges(Vertex_handle return permutations; } +template +void simplex_tree_random_flag_complex( + Simplex_tree& st, + typename Simplex_tree::Vertex_handle nb_vertices, + double density = 0.15) { + using Vertex_handle = typename Simplex_tree::Vertex_handle; + std::vector vertices(nb_vertices); + std::iota(vertices.begin(), vertices.end(), 0); // vertices is { 0, 1, 2, ..., 99 } when nb_vertices is 100 + st.insert_batch_vertices(vertices); + + std::uniform_real_distribution unif(0., 1.); + std::mt19937 rand_engine(rd()); + + for (auto edge : random_edges(nb_vertices, density)) { + st.insert_simplex(edge, unif(rand_engine)); + } + +} + } // namespace Gudhi #endif // RANDOM_GRAPH_GENERATORS_H_ diff --git a/src/common/test/test_random_graph_generators.cpp b/src/common/test/test_random_graph_generators.cpp index 712172b890..e6e2d12fef 100644 --- a/src/common/test/test_random_graph_generators.cpp +++ b/src/common/test/test_random_graph_generators.cpp @@ -9,20 +9,21 @@ */ #include +#include #include -#include +#include #include #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE "random_graph_generators" #include -using Edge = std::pair; +using Edge = std::array; using Edge_range = std::vector; -BOOST_AUTO_TEST_CASE( random_edges_limits ) -{ +BOOST_AUTO_TEST_CASE( random_edges_limits ) { + std::cout << " ## BOOST_AUTO_TEST_CASE( random_edges_limits )" << std::endl; Edge_range edges = Gudhi::random_edges(0); std::cout << "Gudhi::random_edges(0).size() = " << edges.size() << std::endl; BOOST_CHECK(edges.size() == 0); @@ -44,8 +45,8 @@ BOOST_AUTO_TEST_CASE( random_edges_limits ) BOOST_CHECK(edges.size() == 105); } -BOOST_AUTO_TEST_CASE( random_edges_mean ) -{ +BOOST_AUTO_TEST_CASE( random_edges_mean ) { + std::cout << " ## BOOST_AUTO_TEST_CASE( random_edges_mean )" << std::endl; int nb_edges {0}; for (int idx = 0; idx < 100; ++idx) { Edge_range edges = Gudhi::random_edges(15); @@ -55,10 +56,35 @@ BOOST_AUTO_TEST_CASE( random_edges_mean ) #endif // DEBUG_TRACES } #ifdef DEBUG_TRACES - std::cout << "\n"; + std::cout << "\n"; #endif // DEBUG_TRACES std::cout << "Total number of edges for 100 x Gudhi::random_edges(15) [aka. 15% of 105] = " << nb_edges << std::endl; // 1575 +/- 10% BOOST_CHECK(nb_edges < 1733); BOOST_CHECK(nb_edges > 1417); } + +BOOST_AUTO_TEST_CASE( simplex_tree_random_flag_complex_test ) { + std::cout << " ## BOOST_AUTO_TEST_CASE( simplex_tree_random_flag_complex_test )" << std::endl; + Gudhi::Simplex_tree<> stree; + // Insert 100% of the possible edges, with 10 vertices + simplex_tree_random_flag_complex(stree, 10, 1.); + + std::cout << "Random flag complex with " << stree.num_vertices() << " vertices and " << stree.num_simplices() << std::endl; + BOOST_CHECK(stree.num_vertices() == 10); + BOOST_CHECK(stree.num_simplices() == 55); +} + +BOOST_AUTO_TEST_CASE( pouet ) { + std::cout << " ## BOOST_AUTO_TEST_CASE( pouet )" << std::endl; + Gudhi::Simplex_tree<> stree; + // Insert 100% of the possible edges, with 10 vertices + simplex_tree_random_flag_complex(stree, 1000); + + std::cout << "Random flag complex with " << stree.num_vertices() << " vertices and " << stree.num_simplices() << std::endl; + + // 15 % of 499500 = 74925 + // 74925 +/- 5% = [71178.75, 78671.25] + BOOST_CHECK(stree.num_simplices() < 78671); + BOOST_CHECK(stree.num_simplices() > 71178); +} From 8450dbdc54d5368e3d1bce0a12866f2a9e2ced7e Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Fri, 8 Mar 2024 20:12:03 +0100 Subject: [PATCH 3/7] typos in test --- src/common/test/test_random_graph_generators.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/common/test/test_random_graph_generators.cpp b/src/common/test/test_random_graph_generators.cpp index e6e2d12fef..bef330d917 100644 --- a/src/common/test/test_random_graph_generators.cpp +++ b/src/common/test/test_random_graph_generators.cpp @@ -70,18 +70,20 @@ BOOST_AUTO_TEST_CASE( simplex_tree_random_flag_complex_test ) { // Insert 100% of the possible edges, with 10 vertices simplex_tree_random_flag_complex(stree, 10, 1.); - std::cout << "Random flag complex with " << stree.num_vertices() << " vertices and " << stree.num_simplices() << std::endl; + std::cout << "Random flag complex with " << stree.num_vertices() << " vertices and " << stree.num_simplices() << + " simplices" << std::endl; BOOST_CHECK(stree.num_vertices() == 10); BOOST_CHECK(stree.num_simplices() == 55); } -BOOST_AUTO_TEST_CASE( pouet ) { - std::cout << " ## BOOST_AUTO_TEST_CASE( pouet )" << std::endl; +BOOST_AUTO_TEST_CASE( simplex_tree_random_flag_complex_test_1000 ) { + std::cout << " ## BOOST_AUTO_TEST_CASE( simplex_tree_random_flag_complex_test_1000 )" << std::endl; Gudhi::Simplex_tree<> stree; - // Insert 100% of the possible edges, with 10 vertices + // Insert 15% of the possible edges, with 1000 vertices simplex_tree_random_flag_complex(stree, 1000); - std::cout << "Random flag complex with " << stree.num_vertices() << " vertices and " << stree.num_simplices() << std::endl; + std::cout << "Random flag complex with " << stree.num_vertices() << " vertices and " << stree.num_simplices() << + " simplices" << std::endl; // 15 % of 499500 = 74925 // 74925 +/- 5% = [71178.75, 78671.25] From 82201e4e335214da8997b6c07c658999d4dcca5b Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Mon, 11 Mar 2024 09:47:13 +0100 Subject: [PATCH 4/7] Vectorize random values, but same timings --- .../include/gudhi/random_graph_generators.h | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/common/include/gudhi/random_graph_generators.h b/src/common/include/gudhi/random_graph_generators.h index 6348036893..7046fc1cf1 100644 --- a/src/common/include/gudhi/random_graph_generators.h +++ b/src/common/include/gudhi/random_graph_generators.h @@ -19,6 +19,7 @@ #include #include #include // for std::iota +#include // for std::size_t #ifdef DEBUG_TRACES #include #endif // DEBUG_TRACES @@ -27,22 +28,35 @@ std::random_device rd; namespace Gudhi { +template +std::vector N_random_values(std::size_t nb_values, Floating_type min = 0., Floating_type max = 1.) { + std::uniform_real_distribution unif(0., 1.); + std::mt19937 rand_engine(rd()); + std::vector random_filtrations(nb_values); + std::generate(random_filtrations.begin(), random_filtrations.end(), + [&unif, &rand_engine]() { return unif(rand_engine); }); + return random_filtrations; +} + template std::vector> random_edges(Vertex_handle nb_vertices, double density = 0.15) { std::vector> permutations; if (nb_vertices < 2) return permutations; - std::uniform_real_distribution unif(0., 1.); - std::mt19937 rand_engine(rd()); - std::vector to_permute(nb_vertices); std::fill(to_permute.begin(), to_permute.begin() + 2, true); + std::size_t nb_permutations = (nb_vertices * (nb_vertices - 1)) / 2; + auto random_values = N_random_values(nb_permutations); + std::size_t idx = 0; do { // Keep only X% of the possible edges - if (unif(rand_engine) > density) + if (random_values[idx] > density) { + idx++; continue; + } + idx++; std::array permutation = {-1, -1}; for (Vertex_handle idx = 0; idx < nb_vertices; ++idx) { @@ -76,11 +90,14 @@ void simplex_tree_random_flag_complex( std::iota(vertices.begin(), vertices.end(), 0); // vertices is { 0, 1, 2, ..., 99 } when nb_vertices is 100 st.insert_batch_vertices(vertices); - std::uniform_real_distribution unif(0., 1.); - std::mt19937 rand_engine(rd()); + auto edges = random_edges(nb_vertices, density); + + auto random_filtrations = N_random_values(edges.size()); - for (auto edge : random_edges(nb_vertices, density)) { - st.insert_simplex(edge, unif(rand_engine)); + std::size_t idx = 0; + for (auto edge : edges) { + st.insert_simplex(edge, random_filtrations[idx]); + idx++; } } From cbe4c49258db631e12254e5de0c7b8e6acafe599 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Mon, 11 Mar 2024 10:30:19 +0100 Subject: [PATCH 5/7] Fix TBB as simplex tree requires it --- src/common/test/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt index 8d99e8d745..abd6b9361b 100644 --- a/src/common/test/CMakeLists.txt +++ b/src/common/test/CMakeLists.txt @@ -6,6 +6,9 @@ add_executable ( Common_test_points_off_reader test_points_off_reader.cpp ) add_executable ( Common_test_distance_matrix_reader test_distance_matrix_reader.cpp ) add_executable ( Common_test_persistence_intervals_reader test_persistence_intervals_reader.cpp ) add_executable ( Common_test_random_test_generators test_random_graph_generators.cpp ) +if(TARGET TBB::tbb) + target_link_libraries(Common_test_random_test_generators TBB::tbb) +endif() # Do not forget to copy test files in current binary dir file(COPY "${CMAKE_SOURCE_DIR}/data/points/alphacomplexdoc.off" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) From e772ab7656afeb2e29a0ef20507d3d60d12e614e Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Wed, 15 May 2024 08:16:09 +0200 Subject: [PATCH 6/7] New Gudhi::Random class --- src/common/include/gudhi/Random.h | 56 ++++++++++++++++ .../include/gudhi/random_graph_generators.h | 16 ++--- src/common/test/CMakeLists.txt | 8 ++- src/common/test/test_random.cpp | 65 +++++++++++++++++++ 4 files changed, 130 insertions(+), 15 deletions(-) create mode 100644 src/common/include/gudhi/Random.h create mode 100644 src/common/test/test_random.cpp diff --git a/src/common/include/gudhi/Random.h b/src/common/include/gudhi/Random.h new file mode 100644 index 0000000000..589b179bf2 --- /dev/null +++ b/src/common/include/gudhi/Random.h @@ -0,0 +1,56 @@ +/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. + * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. + * Author(s): Vincent Rouvreau + * + * Copyright (C) 2024 Inria + * + * Modification(s): + * - YYYY/MM Author: Description of the modification + */ + +#ifndef RANDOM__H_ +#define RANDOM__H_ + +#include // for std::random_device, std::mt19937, std::uniform_real_distribution +#include +#include // for std::generate +#include // for std::size_t + +namespace Gudhi { + std::random_device rd; + + class Random { + public: + Random() : gen_(rd()) {} + Random(std::uint_fast32_t seed) : gen_(seed) {} + + template + Type get(const Type& min = 0, const Type& max = 1) { + if constexpr (std::is_floating_point_v) { + std::uniform_real_distribution dis(min, max); + return dis(gen_); + } else if constexpr (std::is_integral_v) { + std::uniform_int_distribution dis(min, max); + return dis(gen_); + } + } + + template + std::vector get_range(std::size_t nbr, const Type& min = 0, const Type& max = 1) { + std::vector result(nbr); + if constexpr (std::is_floating_point_v) { + std::uniform_real_distribution dis(min, max); + std::generate(result.begin(), result.end(), [&]() { return dis(gen_); }); + } else if constexpr (std::is_integral_v) { + std::uniform_int_distribution dis(min, max); + std::generate(result.begin(), result.end(), [&]() { return dis(gen_); }); + } + return result; + } + + private: + std::mt19937 gen_; + }; +} // namespace Gudhi + +#endif // RANDOM__H_ \ No newline at end of file diff --git a/src/common/include/gudhi/random_graph_generators.h b/src/common/include/gudhi/random_graph_generators.h index 7046fc1cf1..125917bb21 100644 --- a/src/common/include/gudhi/random_graph_generators.h +++ b/src/common/include/gudhi/random_graph_generators.h @@ -12,6 +12,7 @@ #define RANDOM_GRAPH_GENERATORS_H_ #include +#include #include // for std::prev_permutation @@ -20,6 +21,7 @@ #include #include // for std::iota #include // for std::size_t +#include // for std::is_floating_point_v #ifdef DEBUG_TRACES #include #endif // DEBUG_TRACES @@ -28,16 +30,6 @@ std::random_device rd; namespace Gudhi { -template -std::vector N_random_values(std::size_t nb_values, Floating_type min = 0., Floating_type max = 1.) { - std::uniform_real_distribution unif(0., 1.); - std::mt19937 rand_engine(rd()); - std::vector random_filtrations(nb_values); - std::generate(random_filtrations.begin(), random_filtrations.end(), - [&unif, &rand_engine]() { return unif(rand_engine); }); - return random_filtrations; -} - template std::vector> random_edges(Vertex_handle nb_vertices, double density = 0.15) { std::vector> permutations; @@ -48,7 +40,7 @@ std::vector> random_edges(Vertex_handle nb_vertices std::fill(to_permute.begin(), to_permute.begin() + 2, true); std::size_t nb_permutations = (nb_vertices * (nb_vertices - 1)) / 2; - auto random_values = N_random_values(nb_permutations); + auto random_values = Gudhi::Random().get_range(nb_permutations); std::size_t idx = 0; do { // Keep only X% of the possible edges @@ -92,7 +84,7 @@ void simplex_tree_random_flag_complex( auto edges = random_edges(nb_vertices, density); - auto random_filtrations = N_random_values(edges.size()); + auto random_filtrations = Gudhi::Random().get_range(edges.size()); std::size_t idx = 0; for (auto edge : edges) { diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt index abd6b9361b..af9a91a197 100644 --- a/src/common/test/CMakeLists.txt +++ b/src/common/test/CMakeLists.txt @@ -5,10 +5,11 @@ include(GUDHI_boost_test) add_executable ( Common_test_points_off_reader test_points_off_reader.cpp ) add_executable ( Common_test_distance_matrix_reader test_distance_matrix_reader.cpp ) add_executable ( Common_test_persistence_intervals_reader test_persistence_intervals_reader.cpp ) -add_executable ( Common_test_random_test_generators test_random_graph_generators.cpp ) +add_executable ( Common_test_random_graph_generators test_random_graph_generators.cpp ) if(TARGET TBB::tbb) - target_link_libraries(Common_test_random_test_generators TBB::tbb) + target_link_libraries(Common_test_random_graph_generators TBB::tbb) endif() +add_executable ( Common_test_random test_random.cpp ) # Do not forget to copy test files in current binary dir file(COPY "${CMAKE_SOURCE_DIR}/data/points/alphacomplexdoc.off" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/) @@ -21,4 +22,5 @@ file(COPY "${CMAKE_SOURCE_DIR}/src/common/test/persistence_intervals_without_dim gudhi_add_boost_test(Common_test_points_off_reader) gudhi_add_boost_test(Common_test_distance_matrix_reader) gudhi_add_boost_test(Common_test_persistence_intervals_reader) -gudhi_add_boost_test(Common_test_random_test_generators) +gudhi_add_boost_test(Common_test_random_graph_generators) +gudhi_add_boost_test(Common_test_random) diff --git a/src/common/test/test_random.cpp b/src/common/test/test_random.cpp new file mode 100644 index 0000000000..c911d8ee3a --- /dev/null +++ b/src/common/test/test_random.cpp @@ -0,0 +1,65 @@ +/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. + * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. + * Author(s): Vincent Rouvreau + * + * Copyright (C) 2024 Inria + * + * Modification(s): + * - YYYY/MM Author: Description of the modification + */ + +#include + +#include +#include + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE "random" +#include +#include + +using list_of_rnd_types = boost::mpl::list; + +BOOST_AUTO_TEST_CASE_TEMPLATE(random_same_seed, RndType, list_of_rnd_types) { + std::cout << " ## BOOST_AUTO_TEST_CASE( random_same_seed )" << std::endl; + + RndType min{0}; + RndType max{100}; + RndType first = Gudhi::Random(1).get(min, max); + RndType second = Gudhi::Random(1).get(min, max); + + std::clog << "First random number: " << first << " - Second random number: " << second << std::endl; + BOOST_CHECK(first >= min); + BOOST_CHECK(first <= max); + BOOST_CHECK(first == second); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(random_different_seed, RndType, list_of_rnd_types) { + std::cout << " ## BOOST_AUTO_TEST_CASE( random_different_seed )" << std::endl; + + RndType min{0}; + RndType max{100}; + RndType first = Gudhi::Random().get(min, max); + RndType second = Gudhi::Random().get(min, max); + + std::clog << "First random number: " << first << " - Second random number: " << second << std::endl; + BOOST_CHECK(first >= min); + BOOST_CHECK(first <= max); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(random_range, RndType, list_of_rnd_types) { + std::cout << " ## BOOST_AUTO_TEST_CASE( random_range )" << std::endl; + + RndType min{0}; + RndType max{100}; + const std::size_t N{20}; + std::vector range = Gudhi::Random().get_range(N, min, max); + + BOOST_CHECK(range.size() == N); + + for (const auto& value : range) { + std::clog << "Random number: " << value << std::endl; + BOOST_CHECK(value >= min); + BOOST_CHECK(value <= max); + } +} From 5be23381964bdcd1e60e7c4edd769e41deba0512 Mon Sep 17 00:00:00 2001 From: Vincent Rouvreau Date: Thu, 16 May 2024 09:28:26 +0200 Subject: [PATCH 7/7] Simplex tree is not needed, it is given as a template --- src/common/include/gudhi/random_graph_generators.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/include/gudhi/random_graph_generators.h b/src/common/include/gudhi/random_graph_generators.h index 125917bb21..38fbea4f96 100644 --- a/src/common/include/gudhi/random_graph_generators.h +++ b/src/common/include/gudhi/random_graph_generators.h @@ -11,7 +11,6 @@ #ifndef RANDOM_GRAPH_GENERATORS_H_ #define RANDOM_GRAPH_GENERATORS_H_ -#include #include