Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions std/algorithm/iteration.d
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,18 @@ private struct _Cache(R, bool bidir)

E front() @property
{
version(assert) if (empty) throw new RangeError();
assert(!empty);
return caches[0];
}
static if (bidir) E back() @property
{
version(assert) if (empty) throw new RangeError();
assert(!empty);
return caches[1];
}

void popFront()
{
version(assert) if (empty) throw new RangeError();
assert(!empty);
source.popFront();
if (!source.empty)
caches[0] = source.front;
Expand All @@ -374,7 +374,7 @@ private struct _Cache(R, bool bidir)
}
static if (bidir) void popBack()
{
version(assert) if (empty) throw new RangeError();
assert(!empty);
source.popBack();
if (!source.empty)
caches[1] = source.back;
Expand Down Expand Up @@ -2631,13 +2631,12 @@ template reduce(fun...) if (fun.length >= 1)
auto reduce(R)(R r)
if (isIterable!R)
{
import std.exception : enforce;
alias E = Select!(isInputRange!R, ElementType!R, ForeachType!R);
alias Args = staticMap!(ReduceSeedType!E, binfuns);

static if (isInputRange!R)
{
enforce(!r.empty, "Cannot reduce an empty input range w/o an explicit seed value.");
assert(!r.empty, "Cannot reduce an empty input range w/o an explicit seed value.");
Args result = r.front;
r.popFront();
return reduceImpl!false(r, result);
Expand Down Expand Up @@ -2727,8 +2726,7 @@ template reduce(fun...) if (fun.length >= 1)
args[i] = f(args[i], e);
}
static if (mustInitialize)
if (!initialized)
throw new Exception("Cannot reduce an empty iterable w/o an explicit seed value.");
assert(initialized, "Cannot reduce an empty iterable w/o an explicit seed value.");

static if (Args.length == 1)
return args[0];
Expand Down Expand Up @@ -2852,6 +2850,8 @@ The number of seeds must be correspondingly increased.
{
import std.algorithm.comparison : max, min;
import std.exception : assertThrown;
import core.exception : AssertError;

import std.range : iota;
import std.typecons : tuple, Tuple;

Expand Down Expand Up @@ -2882,10 +2882,10 @@ The number of seeds must be correspondingly increased.
assert(reduce!("a + b", max)(tuple(5, 0), oa) == tuple(hundredSum + 5, 99));

// Test for throwing on empty range plus no seed.
assertThrown(reduce!"a + b"([1, 2][0..0]));
assertThrown!AssertError(reduce!"a + b"([1, 2][0..0]));

oa.actEmpty = true;
assertThrown(reduce!"a + b"(oa));
assertThrown!AssertError(reduce!"a + b"(oa));
}

@safe unittest
Expand Down Expand Up @@ -4151,12 +4151,7 @@ private struct SplitterResult(alias isTerminator, Range)

@property auto front()
{
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
}
assert(!empty);
static if (fullSlicing)
return _input[0 .. _end];
else
Expand All @@ -4168,12 +4163,8 @@ private struct SplitterResult(alias isTerminator, Range)

void popFront()
{
version(assert)
{
import core.exception : RangeError;
if (empty)
throw new RangeError();
}
import core.exception : RangeError;
assert(!empty);

static if (fullSlicing)
{
Expand Down Expand Up @@ -4347,14 +4338,14 @@ if (isSomeChar!C)

@property C[] front() pure @safe
{
version(assert) if (empty) throw new RangeError();
assert(!empty);
return _s[0 .. _frontLength];
}

void popFront() pure @safe
{
import std.string : stripLeft;
version(assert) if (empty) throw new RangeError();
assert(!empty);
_s = _s[_frontLength .. $].stripLeft();
getFirst();
}
Expand Down
29 changes: 12 additions & 17 deletions std/algorithm/mutation.d
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,7 @@ void fill(InputRange, ForwardRange)(InputRange range, ForwardRange filler)
}
else
{
import std.exception : enforce;

enforce(!filler.empty, "Cannot fill range with an empty filler");
assert(!filler.empty, "Cannot fill range with an empty filler");

static if (hasLength!InputRange && hasLength!ForwardRange
&& is(typeof(range.length > filler.length)))
Expand Down Expand Up @@ -672,9 +670,10 @@ void fill(InputRange, ForwardRange)(InputRange range, ForwardRange filler)
assert(a == [ 8, 9, 8, 9, 8 ]);
}

@safe unittest
unittest
{
import std.exception : assertThrown;
import core.exception : AssertError;
import std.internal.test.dummyrange;

debug(std_algorithm) scope(success)
Expand All @@ -701,7 +700,7 @@ void fill(InputRange, ForwardRange)(InputRange range, ForwardRange filler)
assert(a == [0, 1, 2, 3, 4]);

//empty filler test
assertThrown(fill(a, a[$..$]));
assertThrown!AssertError(fill(a, a[$..$]));
}

/**
Expand Down Expand Up @@ -1394,8 +1393,6 @@ unittest
private InputRange2 moveAllImpl(alias moveOp, InputRange1, InputRange2)(
ref InputRange1 src, ref InputRange2 tgt)
{
import std.exception : enforce;

static if (isRandomAccessRange!InputRange1 && hasLength!InputRange1 && hasLength!InputRange2
&& hasSlicing!InputRange2 && isRandomAccessRange!InputRange2)
{
Expand Down Expand Up @@ -1649,9 +1646,7 @@ if (s != SwapStrategy.stable
}
static if (i > 0)
{
import std.exception : enforce;

enforce(blackouts[i - 1].pos + blackouts[i - 1].len
assert(blackouts[i - 1].pos + blackouts[i - 1].len
<= blackouts[i].pos,
"remove(): incorrect ordering of elements to remove");
}
Expand Down Expand Up @@ -1730,8 +1725,7 @@ if (s == SwapStrategy.stable

static if (pass > 0)
{
import std.exception : enforce;
enforce(pos <= from,
assert(pos <= from,
"remove(): incorrect ordering of elements to remove");

for (; pos < from; ++pos, src.popFront(), tgt.popFront())
Expand All @@ -1755,17 +1749,18 @@ if (s == SwapStrategy.stable
return result;
}

@safe unittest
unittest
{
import std.exception : assertThrown;
import core.exception : AssertError;
import std.range;

// http://d.puremagic.com/issues/show_bug.cgi?id=10173
int[] test = iota(0, 10).array();
assertThrown(remove!(SwapStrategy.stable)(test, tuple(2, 4), tuple(1, 3)));
assertThrown(remove!(SwapStrategy.unstable)(test, tuple(2, 4), tuple(1, 3)));
assertThrown(remove!(SwapStrategy.stable)(test, 2, 4, 1, 3));
assertThrown(remove!(SwapStrategy.unstable)(test, 2, 4, 1, 3));
assertThrown!AssertError(remove!(SwapStrategy.stable)(test, tuple(2, 4), tuple(1, 3)));
assertThrown!AssertError(remove!(SwapStrategy.unstable)(test, tuple(2, 4), tuple(1, 3)));
assertThrown!AssertError(remove!(SwapStrategy.stable)(test, 2, 4, 1, 3));
assertThrown!AssertError(remove!(SwapStrategy.unstable)(test, 2, 4, 1, 3));
}

@safe unittest
Expand Down
6 changes: 3 additions & 3 deletions std/algorithm/searching.d
Original file line number Diff line number Diff line change
Expand Up @@ -3043,7 +3043,6 @@ if (isInputRange!Range && !isInfinite!Range &&
is(typeof(binaryFun!pred(range.front, range.front))))
{
import std.algorithm.internal : algoFormat;
import std.exception : enforce;

alias T = ElementType!Range;
alias UT = Unqual!T;
Expand All @@ -3053,7 +3052,7 @@ if (isInputRange!Range && !isInfinite!Range &&
algoFormat("Error: Cannot call minCount on a %s, because it is not possible "~
"to copy the result value (a %s) into a Tuple.", Range.stringof, T.stringof));

enforce(!range.empty, "Can't count elements from an empty range");
assert(!range.empty, "Can't count elements from an empty range");
size_t occurrences = 1;

static if (isForwardRange!Range)
Expand Down Expand Up @@ -3152,6 +3151,7 @@ unittest
{
import std.conv : text;
import std.exception : assertThrown;
import core.exception : AssertError;
import std.internal.test.dummyrange;

debug(std_algorithm) scope(success)
Expand All @@ -3162,7 +3162,7 @@ unittest
assert(c == tuple([2, 4], 1), text(c[0]));

//Test empty range
assertThrown(minCount(b[$..$]));
assertThrown!AssertError(minCount(b[$..$]));

//test with reference ranges. Test both input and forward.
assert(minCount(new ReferenceInputRange!int([1, 2, 1, 0, 2, 0])) == tuple(0, 2));
Expand Down
11 changes: 4 additions & 7 deletions std/algorithm/sorting.d
Original file line number Diff line number Diff line change
Expand Up @@ -953,13 +953,12 @@ makeIndex(
&& is(ElementType!(RangeIndex) : ElementType!(Range)*))
{
import std.algorithm.internal : addressOf;
import std.exception : enforce;

// assume collection already ordered
size_t i;
for (; !r.empty; r.popFront(), ++i)
index[i] = addressOf(r.front);
enforce(index.length == i);
assert(index.length == i);
// sort the index
sort!((a, b) => binaryFun!less(*a, *b), ss)(index);
return typeof(return)(index);
Expand All @@ -976,15 +975,14 @@ if (isRandomAccessRange!Range && !isInfinite!Range &&
isRandomAccessRange!RangeIndex && !isInfinite!RangeIndex &&
isIntegral!(ElementType!RangeIndex))
{
import std.exception : enforce;
import std.conv : to;

alias IndexType = Unqual!(ElementType!RangeIndex);
enforce(r.length == index.length,
assert(r.length == index.length,
"r and index must be same length for makeIndex.");
static if (IndexType.sizeof < size_t.sizeof)
{
enforce(r.length <= IndexType.max, "Cannot create an index with " ~
assert(r.length <= IndexType.max, "Cannot create an index with " ~
"element type " ~ IndexType.stringof ~ " with length " ~
to!string(r.length) ~ ".");
}
Expand Down Expand Up @@ -3531,10 +3529,9 @@ void topNIndex(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
"Stable swap strategy not implemented yet.");

import std.container : BinaryHeap;
import std.exception : enforce;

if (index.empty) return;
enforce(ElementType!(RangeIndex).max >= index.length,
assert(ElementType!(RangeIndex).max >= index.length,
"Index type too small");
bool indirectLess(ElementType!(RangeIndex) a, ElementType!(RangeIndex) b)
{
Expand Down