diff mbox

[v3] Fix default-constructed piecewise_linear_distribution

Message ID 4CB46488.7060103@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Oct. 12, 2010, 1:37 p.m. UTC
Hi,

tested x86_64-linux, will go in 4_5-branch too.

Paolo.

//////////////////////////////
2010-10-12  Paolo Carlini  <paolo.carlini@oracle.com>

	* include/bits/random.tcc (piecewise_linear_distribution<>::
	operator()): Don't crash when the dist is default-constructed.
	* testsuite/26_numerics/random/piecewise_linear_distribution/
	operators/call-default.cc: New.
diff mbox

Patch

Index: include/bits/random.tcc
===================================================================
--- include/bits/random.tcc	(revision 165351)
+++ include/bits/random.tcc	(working copy)
@@ -2623,6 +2623,9 @@ 
 	  __aurng(__urng);
 
 	const double __p = __aurng();
+	if (__param._M_m.empty())
+	  return __p;
+
 	auto __pos = std::lower_bound(__param._M_cp.begin(),
 				      __param._M_cp.end(), __p);
 	const size_t __i = __pos - __param._M_cp.begin();
Index: testsuite/26_numerics/random/piecewise_linear_distribution/operators/call-default.cc
===================================================================
--- testsuite/26_numerics/random/piecewise_linear_distribution/operators/call-default.cc	(revision 0)
+++ testsuite/26_numerics/random/piecewise_linear_distribution/operators/call-default.cc	(revision 0)
@@ -0,0 +1,41 @@ 
+// { dg-options "-std=c++0x" }
+// { dg-require-cstdint "" }
+//
+// 2010-10-12  Paolo Carlini  <paolo.carlini@oracle.com>
+//
+// Copyright (C) 2010 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// 26.5.8.5.3 Class template piecewise_linear_distribution
+//   [rand.dist.samp.plinear]
+
+#include <random>
+
+void
+test01()
+{
+  std::piecewise_linear_distribution<> u;
+  std::minstd_rand0 rng;
+
+  u(rng);
+}
+
+int main()
+{
+  test01();
+  return 0;
+}