From 141cddb5f8c21101035d766c85a3ef5c32923f8d Mon Sep 17 00:00:00 2001 From: Arnaud Becheler <8360330+Becheler@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:26:15 +0200 Subject: [PATCH 1/2] test: add Boost 1.90 serialization gold archives, fails since 1.92 --- test/Jamfile.v2 | 4 +- test/serialize.cpp | 185 ++++++++++++++++++++++++- test/serialize_archives/boost_1_90.bin | Bin 0 -> 473 bytes test/serialize_archives/boost_1_90.txt | 1 + test/serialize_archives/boost_1_90.xml | 146 +++++++++++++++++++ 5 files changed, 333 insertions(+), 3 deletions(-) create mode 100644 test/serialize_archives/boost_1_90.bin create mode 100644 test/serialize_archives/boost_1_90.txt create mode 100644 test/serialize_archives/boost_1_90.xml diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7287f460f..577f1ceb1 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -24,6 +24,8 @@ path-constant METIS_INPUT_FILE : ./weighted_graph.gr ; path-constant WEIGHTED_MATCHING_INPUT_FILE : ./weighted_matching.dat ; +path-constant SERIALIZE_ARCHIVES : ./serialize_archives ; + project : requirements /boost/graph//boost_graph ; alias graph_test_regular : @@ -87,7 +89,7 @@ alias graph_test_regular : [ run serialize.cpp /boost/serialization//boost_serialization - : : : ] + : $(SERIALIZE_ARCHIVES) : : ] [ compile reverse_graph_cc.cpp ] [ run sequential_vertex_coloring.cpp ] diff --git a/test/serialize.cpp b/test/serialize.cpp index b609238d6..8f2c0fc5e 100644 --- a/test/serialize.cpp +++ b/test/serialize.cpp @@ -1,4 +1,5 @@ // Copyright (C) 2006 Trustees of Indiana University +// Copyright (c) 2026 Arnaud Becheler // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -17,6 +18,20 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include struct vertex_properties { @@ -55,8 +70,174 @@ static_assert(boost::serialization::tracking_level< Graph >::value typedef adjacency_list< vecS, vecS, undirectedS, vertex_properties > Graph_no_edge_property; -int main() +using ListGraph = boost::adjacency_list< boost::listS, boost::listS, boost::bidirectionalS, vertex_properties, edge_properties >; + +using PlainGraph = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS >; + +using PropertyGraph = boost::adjacency_list< boost::vecS, boost::listS, boost::directedS, boost::property< boost::vertex_name_t, std::string >, boost::property< boost::edge_name_t, std::string, boost::property< boost::edge_weight_t, int > > >; + +using EdgeRecord = std::tuple< std::size_t, std::size_t, std::string >; + +void check_list_round_trip() +{ + ListGraph g; + const auto a = boost::add_vertex(g); + const auto b = boost::add_vertex(g); + g[a].name = "A"; + g[b].name = "B"; + const auto ab = boost::add_edge(a, b, g).first; + g[ab].name = "ab"; + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + { + boost::archive::binary_oarchive oa(ss); + oa << g; + } + ListGraph loaded; + boost::archive::binary_iarchive ia(ss); + ia >> loaded; + constexpr std::size_t expected_num_vertices = 2; + constexpr std::size_t expected_num_edges = 1; + BOOST_TEST_EQ(boost::num_vertices(loaded), expected_num_vertices); + BOOST_TEST_EQ(boost::num_edges(loaded), expected_num_edges); + const auto first_vertex = *boost::vertices(loaded).first; + const auto first_edge = *boost::edges(loaded).first; + BOOST_TEST_EQ(loaded[first_vertex].name, "A"); + BOOST_TEST_EQ(loaded[first_edge].name, "ab"); +} + +std::size_t binary_archive_size(std::size_t num_edges) +{ + PlainGraph g(2); + for (std::size_t i = 0; i < num_edges; ++i) + { + boost::add_edge(0, 1, g); + } + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + { + boost::archive::binary_oarchive oa(ss); + oa << g; + } + return ss.str().size(); +} + +// Each edge costs two 4 byte indices, as in archives from Boost 1.91 and earlier. +void check_binary_layout() +{ + constexpr std::size_t expected_edge_size = 2 * sizeof(unsigned int); + const auto one_edge_size = binary_archive_size(1); + const auto two_edges_size = binary_archive_size(2); + BOOST_TEST_EQ(two_edges_size - one_edge_size, expected_edge_size); +} + +// Gold archives hold A, B, C with edges ab1 and ab2 from A to B, bc from B to C and cc from C to C. +template < class G, class VertexTag, class EdgeTag > +void check_gold_graph(const G& g, VertexTag vertex_tag, EdgeTag edge_tag) +{ + using Vertex = typename boost::graph_traits< G >::vertex_descriptor; + const std::vector< std::string > expected_vertex_names = { "A", "B", "C" }; + const std::vector< EdgeRecord > expected_edges = { EdgeRecord(0, 1, "ab1"), EdgeRecord(0, 1, "ab2"), EdgeRecord(1, 2, "bc"), EdgeRecord(2, 2, "cc") }; + const auto vertex_name_map = boost::get(vertex_tag, g); + const auto edge_name_map = boost::get(edge_tag, g); + std::unordered_map< Vertex, std::size_t > index_map; + std::vector< std::string > vertex_names; + for (const auto v : boost::make_iterator_range(boost::vertices(g))) + { + index_map[v] = vertex_names.size(); + vertex_names.push_back(boost::get(vertex_name_map, v)); + } + std::vector< EdgeRecord > edges; + for (const auto e : boost::make_iterator_range(boost::edges(g))) + { + const auto u = index_map[boost::source(e, g)]; + const auto v = index_map[boost::target(e, g)]; + edges.emplace_back(u, v, boost::get(edge_name_map, e)); + } + std::sort(edges.begin(), edges.end()); + BOOST_TEST(vertex_names == expected_vertex_names); + BOOST_TEST(edges == expected_edges); +} + +void check_gold_weights(const PropertyGraph& g) { + const std::map< std::string, int > expected_weights = { { "ab1", 1 }, { "ab2", 2 }, { "bc", 3 }, { "cc", 4 } }; + const auto edge_name_map = boost::get(boost::edge_name, g); + const auto weight_map = boost::get(boost::edge_weight, g); + for (const auto e : boost::make_iterator_range(boost::edges(g))) + { + const auto& name = boost::get(edge_name_map, e); + BOOST_TEST_EQ(boost::get(weight_map, e), expected_weights.at(name)); + } +} + +void check_gold_plain(const PlainGraph& g) +{ + using PlainEdge = std::pair< std::size_t, std::size_t >; + constexpr std::size_t expected_num_vertices = 3; + const std::vector< PlainEdge > expected_edges = { PlainEdge(0, 1), PlainEdge(0, 1), PlainEdge(1, 2), PlainEdge(2, 2) }; + std::vector< PlainEdge > edges; + for (const auto e : boost::make_iterator_range(boost::edges(g))) + { + edges.emplace_back(boost::source(e, g), boost::target(e, g)); + } + std::sort(edges.begin(), edges.end()); + BOOST_TEST_EQ(boost::num_vertices(g), expected_num_vertices); + BOOST_TEST(edges == expected_edges); +} + +template < class IArchive > +void check_gold_archive(const std::string& path, std::ios::openmode mode) +{ + std::ifstream ifs(path, mode); + BOOST_TEST(ifs.good()); + if (!ifs) + { + return; + } + IArchive ia(ifs); + Graph vec_vec_undirected; + ListGraph list_list_bidirectional; + PropertyGraph vec_list_directed_property; + PlainGraph vec_vec_directed_plain; + ia >> boost::make_nvp("vec_vec_undirected", vec_vec_undirected); + ia >> boost::make_nvp("list_list_bidirectional", list_list_bidirectional); + ia >> boost::make_nvp("vec_list_directed_property", vec_list_directed_property); + ia >> boost::make_nvp("vec_vec_directed_plain", vec_vec_directed_plain); + check_gold_graph(vec_vec_undirected, &vertex_properties::name, &edge_properties::name); + check_gold_graph(list_list_bidirectional, &vertex_properties::name, &edge_properties::name); + check_gold_graph(vec_list_directed_property, boost::vertex_name, boost::edge_name); + check_gold_weights(vec_list_directed_property); + check_gold_plain(vec_vec_directed_plain); +} + +// Native binary archives only load where sizes and byte order match the 64 bit little endian writer. +bool is_gold_binary_platform() +{ + constexpr unsigned int one = 1; + unsigned char first_byte = 0; + std::memcpy(&first_byte, &one, 1); + return sizeof(int) == 4 && sizeof(long) == 8 && sizeof(std::size_t) == 8 && first_byte == 1; +} + +void check_gold_archives(const std::string& directory) +{ + check_gold_archive< boost::archive::text_iarchive >(directory + "/boost_1_90.txt", std::ios::in); + check_gold_archive< boost::archive::xml_iarchive >(directory + "/boost_1_90.xml", std::ios::in); + if (is_gold_binary_platform()) + { + check_gold_archive< boost::archive::binary_iarchive >(directory + "/boost_1_90.bin", std::ios::in | std::ios::binary); + } +} + +int main(int argc, char* argv[]) +{ + check_list_round_trip(); + check_binary_layout(); + BOOST_TEST_GE(argc, 2); + if (argc >= 2) + { + check_gold_archives(argv[1]); + } + { std::ofstream ofs("./kevin-bacon2.dat"); archive::xml_oarchive oa(ofs); @@ -89,5 +270,5 @@ int main() Graph_no_edge_property g_n; ia >> BOOST_SERIALIZATION_NVP(g_n); } - return 0; + return boost::report_errors(); } diff --git a/test/serialize_archives/boost_1_90.bin b/test/serialize_archives/boost_1_90.bin new file mode 100644 index 0000000000000000000000000000000000000000..4509150dfa5117b8730b17a93588de066f75c040 GIT binary patch literal 473 zcmWe*fPmuEqRhmc%&Nqa%=|nntHh$@jLfoB5e60x77j+J8U|({g9XNci8{g#x+5x~Z7B>I@ literal 0 HcmV?d00001 diff --git a/test/serialize_archives/boost_1_90.txt b/test/serialize_archives/boost_1_90.txt new file mode 100644 index 000000000..488cd6143 --- /dev/null +++ b/test/serialize_archives/boost_1_90.txt @@ -0,0 +1 @@ +22 serialization::archive 20 0 0 3 4 0 0 1 A 1 B 1 C 0 1 0 0 3 ab1 0 1 3 ab2 1 2 2 bc 2 2 2 cc 0 0 0 0 3 4 1 A 1 B 1 C 0 1 3 ab1 0 1 3 ab2 1 2 2 bc 2 2 2 cc 0 0 3 4 0 0 1 A 1 B 1 C 0 1 0 0 3 ab1 0 0 1 0 1 3 ab2 2 1 2 2 bc 3 2 2 2 cc 4 0 0 3 4 0 1 0 1 1 2 2 2 diff --git a/test/serialize_archives/boost_1_90.xml b/test/serialize_archives/boost_1_90.xml new file mode 100644 index 000000000..d6643ca62 --- /dev/null +++ b/test/serialize_archives/boost_1_90.xml @@ -0,0 +1,146 @@ + + + + + 3 + 4 + + A + + + B + + + C + + 0 + 1 + + ab1 + + 0 + 1 + + ab2 + + 1 + 2 + + bc + + 2 + 2 + + cc + + + + + 3 + 4 + + A + + + B + + + C + + 0 + 1 + + ab1 + + 0 + 1 + + ab2 + + 1 + 2 + + bc + + 2 + 2 + + cc + + + + + 3 + 4 + + A + + + + B + + + + C + + + 0 + 1 + + ab1 + + 1 + + + + 0 + 1 + + ab2 + + 2 + + + + 1 + 2 + + bc + + 3 + + + + 2 + 2 + + cc + + 4 + + + + + + + 3 + 4 + + + + 0 + 1 + + 0 + 1 + + 1 + 2 + + 2 + 2 + + + + + From 14c5394cf3be97fd4d1c4271196e526b4f7ea2d6 Mon Sep 17 00:00:00 2001 From: Arnaud Becheler <8360330+Becheler@users.noreply.github.com> Date: Mon, 19 Oct 2026 21:32:56 +0200 Subject: [PATCH 2/2] fix: restore adjacency_list serialization layout of Boost 1.91 --- include/boost/graph/adj_list_serialize.hpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/include/boost/graph/adj_list_serialize.hpp b/include/boost/graph/adj_list_serialize.hpp index c1bdeebf9..e3b3543d3 100644 --- a/include/boost/graph/adj_list_serialize.hpp +++ b/include/boost/graph/adj_list_serialize.hpp @@ -1,5 +1,6 @@ //======================================================================= // Copyright 2005 Jeremy G. Siek +// Copyright 2026 Arnaud Becheler // Authors: Jeremy G. Siek // // Distributed under the Boost Software License, Version 1.0. (See @@ -50,14 +51,15 @@ namespace serialization using Graph = adjacency_list< OEL, VL, D, VP, EP, GP, EL >; using Vertex = typename graph_traits< Graph >::vertex_descriptor; - const auto V = num_vertices(graph); - const auto E = num_edges(graph); + // Fixed 4 byte integers keep binary archives readable across releases. + const auto V = static_cast< unsigned int >(num_vertices(graph)); + const auto E = static_cast< unsigned int >(num_edges(graph)); ar << BOOST_NVP(V); ar << BOOST_NVP(E); // assign indices to vertices - std::unordered_map< Vertex, Vertex > indices(V); - Vertex num = 0; + std::unordered_map< Vertex, unsigned int > indices(V); + unsigned int num = 0; BGL_FORALL_VERTICES_T(v, graph, Graph) { indices[v] = num++; @@ -88,16 +90,15 @@ namespace serialization using Graph = adjacency_list< OEL, VL, D, VP, EP, GP, EL >; using Vertex = typename graph_traits< Graph >::vertex_descriptor; using Edge = typename graph_traits< Graph >::edge_descriptor; - using VertexSizeType = typename graph_traits< Graph >::vertices_size_type; - VertexSizeType V; + unsigned int V; ar >> BOOST_NVP(V); - VertexSizeType E; + unsigned int E; ar >> BOOST_NVP(E); std::vector< Vertex > verts(V); - size_t i = 0; + unsigned int i = 0; while (V-- > 0) { const auto v = add_vertex(graph); @@ -108,8 +109,8 @@ namespace serialization while (E-- > 0) { - Vertex u; - Vertex v; + unsigned int u; + unsigned int v; ar >> BOOST_NVP(u); ar >> BOOST_NVP(v);