[PATCH 1/2] libstdc++: Expand serialization test for piecewise distributions.
Jonathan Wakely
jwakely@redhat.com
Thu May 28 11:05:05 GMT 2026
On Tue, 26 May 2026 at 09:57, Tomasz Kamiński <tkaminsk@redhat.com> wrote:
>
> Due the viariability of the resutls, the test are currently limited
> to x86_64 architectures. float/double test are disabled for -m32
> as I was getting unstable result.
>
> libstdc++-v3/ChangeLog:
>
> * testsuite/26_numerics/random/piecewise_constant_distribution/operators/serialize2.cc:
> New test.
> * testsuite/26_numerics/random/piecewise_linear_distribution/operators/serialize2.cc:
> New test.
> ---
> I have realized, that changing the return type of densities,
> will change the ouput of distributions. This commits add
> actual test of the serialization.
>
> They are limited to x86_64 as they turned out to be unstable,
> and not roundriping, and I do not want to correct them on every
> architecture.
>
> Testing on x86_64-linux. All *piecewise* test already passed.
> OK for trunk when all test passes?
OK
>
> .../operators/serialize2.cc | 109 +++++++++++++++++
> .../operators/serialize2.cc | 112 ++++++++++++++++++
> 2 files changed, 221 insertions(+)
> create mode 100644 libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/operators/serialize2.cc
> create mode 100644 libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/operators/serialize2.cc
>
> diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/operators/serialize2.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/operators/serialize2.cc
> new file mode 100644
> index 00000000000..60838d7396b
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/operators/serialize2.cc
> @@ -0,0 +1,109 @@
> +// { dg-do run { target c++11 } }
> +// { dg-require-effective-target x86 }
> +// { dg-require-cstdint "" }
> +
> +#include <random>
> +#include <sstream>
> +#include <testsuite_hooks.h>
> +
> +template<typename RealType>
> +void
> +test_default()
> +{
> + std::stringstream str;
> + std::piecewise_constant_distribution<RealType>
> + u(1, RealType(0), RealType(1), [](RealType) -> RealType { return 1.0; });
> + std::minstd_rand0 rng;
> +
> + str << u;
> + std::string res = str.str();
> +
> + std::piecewise_constant_distribution<RealType> v;
> + str >> v;
> + VERIFY( u == v );
> +
> + if (!std::numeric_limits<RealType>::is_iec559)
> + return;
> +
> + char const* expected = nullptr;
> + switch (std::numeric_limits<RealType>::digits)
> + {
> + case 24: // ieee32
> + expected =
> + "1 0.000000000e+00 1.000000000e+00 1.000000000e+00";
> + VERIFY( res == expected );
> + break;
> + case 53: // ieee64
> + expected =
> + "1 0.00000000000000000e+00 1.00000000000000000e+00 1.00000000000000000e+00";
> + VERIFY( res == expected );
> + break;
> + case 64: // ieee80
> + expected =
> + "1 0.000000000000000000000e+00 1.000000000000000000000e+00 1.000000000000000000000e+00";
> + VERIFY( res == expected );
> + break;
> + default:
> + break;
> + };
> +}
> +
> +template<typename RealType>
> +void
> +test_custom()
> +{
> + std::stringstream str;
> + std::piecewise_constant_distribution<RealType>
> + u(3, RealType(0), RealType(1), [](RealType r) -> RealType { return 1.0 + r; });
> + std::minstd_rand0 rng;
> +
> + str << u;
> + std::string res = str.str();
> +
> + std::piecewise_constant_distribution<RealType> v;
> + str >> v;
> + // This does not hold currently.
> + // VERIFY( u == v );
> +
> + if (!std::numeric_limits<RealType>::is_iec559)
> + return;
> +
> + char const* expected = nullptr;
> + switch (std::numeric_limits<RealType>::digits)
> + {
> + case 24: // ieee32
> + expected =
> + "3 0.000000000e+00 3.333333433e-01 6.666666865e-01 1.000000000e+00"
> + " 7.777777281e-01 9.999999702e-01 1.222222322e+00";
> + VERIFY( res == expected );
> + break;
> + case 53: // ieee64
> + expected =
> + "3 0.00000000000000000e+00 3.33333333333333315e-01 6.66666666666666630e-01 1.00000000000000000e+00"
> + " 7.77777777777777901e-01 1.00000000000000000e+00 1.22222222222222210e+00";
> + VERIFY( res == expected );
> + break;
> + case 64: // ieee80
> + expected =
> + "3 0.000000000000000000000e+00 3.333333333333333333424e-01 6.666666666666666666847e-01 1.000000000000000000000e+00"
> + " 7.777777777777779011359e-01 1.000000000000000000000e+00 1.222222222222222098864e+00";
> + VERIFY( res == expected );
> + break;
> + default:
> + break;
> + };
> +}
> +
> +int main()
> +{
> + test_default<float>();
> + test_default<double>();
> + test_default<long double>();
> +
> +#ifdef __x86_64__
> + test_custom<float>();
> +#endif
> + test_custom<double>();
> + test_custom<long double>();
> + return 0;
> +}
> diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/operators/serialize2.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/operators/serialize2.cc
> new file mode 100644
> index 00000000000..e8ab174eb70
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/operators/serialize2.cc
> @@ -0,0 +1,112 @@
> +// { dg-do run { target c++11 } }
> +// { dg-require-effective-target x86 }
> +// { dg-require-cstdint "" }
> +
> +#include <random>
> +#include <sstream>
> +#include <testsuite_hooks.h>
> +
> +template<typename RealType>
> +void
> +test_default()
> +{
> + std::stringstream str;
> + std::piecewise_linear_distribution<RealType>
> + u(1, RealType(0), RealType(1), [](RealType) -> RealType { return 1.0; });
> + std::minstd_rand0 rng;
> +
> + str << u;
> + std::string res = str.str();
> +
> + std::piecewise_linear_distribution<RealType> v;
> + str >> v;
> + VERIFY( u == v );
> +
> + if (!std::numeric_limits<RealType>::is_iec559)
> + return;
> +
> + char const* expected = nullptr;
> + switch (std::numeric_limits<RealType>::digits)
> + {
> + case 24: // ieee32
> + expected =
> + "1 0.000000000e+00 1.000000000e+00"
> + " 1.000000000e+00 1.000000000e+00";
> + VERIFY( res == expected );
> + break;
> + case 53: // ieee64
> + expected =
> + "1 0.00000000000000000e+00 1.00000000000000000e+00"
> + " 1.00000000000000000e+00 1.00000000000000000e+00";
> + VERIFY( res == expected );
> + break;
> + case 64: // ieee80
> + expected =
> + "1 0.000000000000000000000e+00 1.000000000000000000000e+00"
> + " 1.000000000000000000000e+00 1.000000000000000000000e+00";
> + VERIFY( res == expected );
> + break;
> + default:
> + break;
> + };
> +}
> +
> +template<typename RealType>
> +void
> +test_custom()
> +{
> + std::stringstream str;
> + std::piecewise_linear_distribution<RealType>
> + u(3, RealType(0), RealType(1), [](RealType r) -> RealType { return 1.0 + r; });
> + std::minstd_rand0 rng;
> +
> + str << u;
> + std::string res = str.str();
> +
> + std::piecewise_constant_distribution<RealType> v;
> + str >> v;
> + // This does not hold currently
> + // VERIFY( u == v );
> +
> + if (!std::numeric_limits<RealType>::is_iec559)
> + return;
> +
> + char const* expected = nullptr;
> + switch (std::numeric_limits<RealType>::digits)
> + {
> + case 24: // ieee32
> + expected =
> + "3 0.000000000e+00 3.333333433e-01 6.666666865e-01 1.000000000e+00"
> + " 7.272727292e-01 9.090909278e-01 1.090909061e+00 1.272727325e+00";
> + VERIFY( res == expected );
> + break;
> + case 53: // ieee64
> + expected =
> + "3 0.00000000000000000e+00 3.33333333333333315e-01 6.66666666666666630e-01 1.00000000000000000e+00"
> + " 7.27272727272727182e-01 9.09090909090908950e-01 1.09090909090909083e+00 1.27272727272727249e+00";
> + VERIFY( res == expected );
> + break;
> + case 64: // ieee80
> + expected =
> + "3 0.000000000000000000000e+00 3.333333333333333333424e-01 6.666666666666666666847e-01 1.000000000000000000000e+00"
> + " 7.272727272727271818908e-01 9.090909090909090606303e-01 1.090909090909090828347e+00 1.272727272727272707087e+00";
> + VERIFY( res == expected );
> + break;
> + default:
> + break;
> + };
> +}
> +
> +int main()
> +{
> + test_default<float>();
> + test_default<double>();
> + test_default<long double>();
> +
> +#ifdef __x86_64__
> + test_custom<float>();
> + test_custom<double>();
> +#endif
> + test_custom<long double>();
> + return 0;
> +}
> --
> 2.54.0
>
More information about the Libstdc++
mailing list