www.digitalmars.com Sargon Component Library for D




sargon.array.asforwardrange

This module contains asForwardRange().

Authors:
Walter Bright

License:
Boost License 1.0

Source:
src/sargon/array/asforwardrange.d

ref auto asForwardRange(E)(E[] a);
Turn an array into a ForwardRange, useful for unittesting.

Parameters:
E[] a is an array of elements of type E

Returns:
a ForwardRange

Examples:
import std.range;

static assert(isForwardRange!(typeof(asForwardRange("hello"))));

void testrange(string f, string result)
{
    char[50] s;
    int i;
    foreach (c; f.asForwardRange())
    {
        s[i++] = c;
    }
    assert(s[0 .. i] == result);
}
testrange("file", "file");

{   // various boundary conditions
    auto r = "foo".asForwardRange;
    auto s = r.save;
    assert(!r.empty);
    assert(!r.empty);
    r.popFront();
    r.popFront();
    r.popFront();
    assert(r.empty);

    assert(!s.empty);
    assert(!s.empty);
    s.popFront();
    s.popFront();
    s.popFront();
    assert(s.empty);
}

void testForwardRange(R)(R r) if (isForwardRange!R);
Takes an ForwardRange as input and verifies that it can be iterated following protocol.

Parameters:
R r is a ForwardRange

Examples:
testForwardRange(asForwardRange("betty"));