mirror of
https://github.com/dlang/phobos.git
synced 2025-05-05 01:20:40 +03:00
std.algorithm.reduce() should throw on empty ranges and no explicit seed instead of silently returning bogus results.
This commit is contained in:
parent
f9e540579b
commit
1725ddc8ed
1 changed files with 21 additions and 0 deletions
|
@ -420,6 +420,8 @@ template reduce(fun...)
|
|||
}
|
||||
else
|
||||
{
|
||||
enforce(!args[$ - 1].empty,
|
||||
"Cannot reduce an empty range w/o an explicit seed value.");
|
||||
alias args[0] r;
|
||||
static if (fun.length == 1)
|
||||
{
|
||||
|
@ -504,6 +506,9 @@ template reduce(fun...)
|
|||
}
|
||||
}
|
||||
|
||||
enforce(initialized,
|
||||
"Cannot reduce an empty iterable w/o an explicit seed value.");
|
||||
|
||||
static if(fun.length == 1)
|
||||
{
|
||||
return result.field[0];
|
||||
|
@ -544,9 +549,13 @@ unittest
|
|||
// Test the opApply case.
|
||||
struct OpApply
|
||||
{
|
||||
bool actEmpty;
|
||||
|
||||
int opApply(int delegate(ref int) dg)
|
||||
{
|
||||
int res;
|
||||
if(actEmpty) return res;
|
||||
|
||||
foreach(i; 0..100)
|
||||
{
|
||||
res = dg(i);
|
||||
|
@ -562,6 +571,18 @@ unittest
|
|||
assert(reduce!"a + b"(oa) == hundredSum);
|
||||
assert(reduce!("a + b", max)(oa) == tuple(hundredSum, 99));
|
||||
assert(reduce!("a + b", max)(tuple(5, 0), oa) == tuple(hundredSum + 5, 99));
|
||||
|
||||
// Test for throwing on empty range plus no seed.
|
||||
try {
|
||||
reduce!"a + b"([1, 2][0..0]);
|
||||
assert(0);
|
||||
} catch(Exception) {}
|
||||
|
||||
oa.actEmpty = true;
|
||||
try {
|
||||
reduce!"a + b"(oa);
|
||||
assert(0);
|
||||
} catch(Exception) {}
|
||||
}
|
||||
|
||||
unittest
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue