aboutsummaryrefslogtreecommitdiff
path: root/libphobos/src/std/internal/test/range.d
blob: 4a5ff721d190adaacfef9b054d6d0778f28957f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
For testing only.
Contains tests related to member privacy that cannot be verified inside
std.range itself.
*/
module std.internal.test.range;

// Note: currently can't be @safe because RefCounted, which is used by chunks,
// isn't.
@system /*@safe*/ unittest
{
    import std.algorithm.comparison : equal;
    import std.range : chunks;

    struct R
    {
        int state = 0;
        @property bool empty() { return state >= 5; }
        @property int front() { return state; }
        void popFront() { state++; }
    }

    auto r = R().chunks(3);
    assert(r.equal!equal([[ 0, 1, 2 ], [ 3, 4 ]]));
}

// https://issues.dlang.org/show_bug.cgi?id=24415
@safe unittest
{
    import std.range : only;

    static struct S(T)
    {
        T i;

        this(ref return scope inout(S) rhs) scope @safe inout pure nothrow
        {
            i = rhs.i;
        }
    }
    {
        auto a = only(S!int(42));
        auto b = a;
        assert(!b.empty);
        assert(b.front == S!int(42));

        a.popFront();
        auto c = a;
        assert(c.empty);
    }
    {
        auto a = only(S!(const int)(42));
        auto b = a;
        assert(!b.empty);
        assert(b.front == S!(const int)(42));

        a.popFront();
        auto c = a;
        assert(c.empty);
    }
    {
        auto a = only(S!(immutable int)(42));
        auto b = a;
        assert(!b.empty);
        assert(b.front == S!(immutable int)(42));

        a.popFront();
        auto c = a;
        assert(c.empty);
    }
    {
        auto a = only(S!int(42), S!int(192));
        auto b = a;
        assert(!b.empty);
        assert(b.front == S!int(42));

        a.popFront();
        auto c = a;
        assert(!c.empty);
        assert(c.front == S!int(192));

        a.popFront();
        auto d = a;
        assert(d.empty);
    }
    {
        auto a = only(S!(const int)(42), S!(const int)(192));
        auto b = a;
        assert(!b.empty);
        assert(b.front == S!(const int)(42));

        a.popFront();
        auto c = a;
        assert(!c.empty);
        assert(c.front == S!(const int)(192));

        a.popFront();
        auto d = a;
        assert(d.empty);
    }
    {
        auto a = only(S!(immutable int)(42), S!(immutable int)(192));
        auto b = a;
        assert(!b.empty);
        assert(b.front == S!(immutable int)(42));

        a.popFront();
        auto c = a;
        assert(!c.empty);
        assert(c.front == S!(immutable int)(192));

        a.popFront();
        auto d = a;
        assert(d.empty);
    }
}