Skip to content

Commit

Permalink
Fix range violation for cartesianProduct of empty ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
H. S. Teoh committed Jul 12, 2014
1 parent ec51209 commit 6e51fdf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions std/algorithm.d
Expand Up @@ -13002,7 +13002,11 @@ auto cartesianProduct(RR...)(RR ranges)
{
ranges = _ranges;
foreach (i, r; ranges)
{
current[i] = r.save;
if (current[i].empty)
empty = true;
}
}
@property auto front()
{
Expand Down Expand Up @@ -13038,6 +13042,22 @@ auto cartesianProduct(RR...)(RR ranges)
return Result(ranges);
}

unittest
{
// Issue 10693: cartesian product of empty ranges should be empty.
int[] a, b, c, d, e;
auto cprod = cartesianProduct(a,b,c,d,e);
assert(cprod.empty);
foreach (_; cprod) {} // should not crash

// Test case where only one of the ranges is empty: the result should still
// be empty.
int[] p=[1], q=[];
auto cprod2 = cartesianProduct(p,p,p,q,p);
assert(cprod2.empty);
foreach (_; cprod2) {} // should not crash
}

/// ditto
auto cartesianProduct(R1, R2, RR...)(R1 range1, R2 range2, RR otherRanges)
if (!allSatisfy!(isForwardRange, R1, R2, RR) ||
Expand Down

0 comments on commit 6e51fdf

Please sign in to comment.