Fix unittests in std.range.

This commit is contained in:
David Simcha 2010-06-23 13:07:31 +00:00
parent 6f1dc2378d
commit d6cefbcf7e

View file

@ -1486,9 +1486,10 @@ Repeat!(T) repeat(T)(T value) { return Repeat!(T)(value); }
unittest
{
enforce(equal(take(repeat(5), 4), [ 5, 5, 5, 5 ][]));
enforce(equal(take(repeat(5), 4), [ 5, 5, 5, 5 ][]));
static assert(isForwardRange!(Repeat!(uint)));
}
/**
Replicates $(D value) exactly $(D n) times. Equivalent to $(D
take(repeat(value), n)).
@ -1549,7 +1550,7 @@ struct Cycle(R) if (isForwardRange!(R) && !isInfinite!(R))
R _original, _current;
this(R input) { _original = input; _current = input.save; }
/// Range primitive implementations.
ref ElementType!(R) front() { return _current.front; }
auto ref front() { return _current.front; }
/// Ditto
static if (isBidirectionalRange!(R))
ref ElementType!(R) back() { return _current.back; }
@ -1563,15 +1564,18 @@ struct Cycle(R) if (isForwardRange!(R) && !isInfinite!(R))
}
@property Cycle!(R) save() {
return Cycle!(R)(this._original.save, this._current.save);
Cycle!(R) ret;
ret._original = this._original.save;
ret._current = this._current.save;
return ret;
}
}
}
/// Ditto
template Cycle(R) if (isInfinite!(R))
{
{
alias R Cycle;
}
@ -1632,12 +1636,24 @@ Cycle!(R) cycle(R)(ref R input, size_t index = 0) if (isStaticArray!R)
unittest
{
assert(equal(take(cycle([1, 2][]), 5), [ 1, 2, 1, 2, 1 ][]));
assert(equal(take(cycle([1, 2][]), 5), [ 1, 2, 1, 2, 1 ][]));
static assert(isForwardRange!(Cycle!(uint[])));
struct DummyRange {
uint num;
ref uint front() { return num; }
void popFront() { num++; }
@property bool empty() { return num >= 10; }
typeof(this) save() { return this; }
}
static assert(isForwardRange!(Cycle!(DummyRange)));
int[3] a = [ 1, 2, 3 ];
static assert(isStaticArray!(typeof(a)));
auto c = cycle(a);
assert(a.ptr == c._ptr);
assert(equal(take(cycle(a), 5), [ 1, 2, 3, 1, 2 ][]));
assert(equal(take(cycle(a), 5), [ 1, 2, 3, 1, 2 ][]));
static assert(isForwardRange!(typeof(c)));
}
unittest // For infinite ranges
@ -1648,12 +1664,12 @@ unittest // For infinite ranges
int front() { return 0; }
enum empty = false;
}
InfRange i;
auto c = cycle(i);
assert (c == i);
}
/**
Iterate several ranges in lockstep. The element type is a proxy tuple
that allows accessing the current element in the $(D n)th range by