Skip to content

Commit

Permalink
Fix Issue #12835: open lower bound for integral-type uniform()
Browse files Browse the repository at this point in the history
Added a cast to ensure the setting of lower bound can handle character
types and integral types smaller than int.  The additional unittests
should prevent such issues from arising again.
  • Loading branch information
WebDrake committed Jul 11, 2014
1 parent 20d2e6c commit 88d86ec
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions std/random.d
Expand Up @@ -1333,7 +1333,7 @@ if ((isIntegral!(CommonType!(T1, T2)) || isSomeChar!(CommonType!(T1, T2))) &&
{
enforce(a < ResultType.max,
text("std.random.uniform(): invalid left bound ", a));
ResultType lower = a + 1;
ResultType lower = cast(ResultType) (a + 1);
}
else
{
Expand Down Expand Up @@ -1399,10 +1399,38 @@ unittest
int, uint, long, ulong, float, double, real))
{
T lo = 0, hi = 100;
T init = uniform(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);

// Try tests with each of the possible bounds
{
T init = uniform(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"[)"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"(]"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"()"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}
{
T init = uniform!"[]"(lo, hi);
size_t i = 50;
while (--i && uniform(lo, hi) == init) {}
assert(i > 0);
}

/* Test case with closed boundaries covering whole range
* of integral type
Expand Down

0 comments on commit 88d86ec

Please sign in to comment.