File: testreturn.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A12.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 959,712 kB
  • sloc: cpp: 3,275,382; ansic: 2,061,766; ada: 840,956; f90: 208,513; makefile: 76,132; asm: 73,433; xml: 50,448; exp: 34,146; sh: 32,436; objc: 15,637; fortran: 14,012; python: 11,991; pascal: 6,787; awk: 4,779; perl: 3,054; yacc: 338; ml: 285; lex: 201; haskell: 122
file content (206 lines) | stat: -rw-r--r-- 3,980 bytes parent folder | download | duplicates (6)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
RUN_OUTPUT:
---
Success
---
*/
extern(C) int printf(const char*, ...);

alias TypeTuple(T...) = T;

/***************************************************/
// https://issues.dlang.org/show_bug.cgi?id=13336

struct S13336
{
    int opApply(scope int delegate(int) dg)
    {
        return dg(0);
    }
}

double result13336;

enum fbody13336 =
q{
    static if (n == 1)
    {
        if (f)
            return sx;
        return sy;
    }
    static if (n == 2)
    {
        foreach (e; S13336())
        {
            if (f)
                return sx;
            return sy;
        }
        assert(0);
    }
    static if (n == 3)
    {
        if (f)
            return sx;
        foreach (e; S13336())
        {
            return sy;
        }
        assert(0);
    }
    static if (n == 4)
    {
        foreach (e; S13336())
        {
            if (f)
                return sx;
        }
        return sy;
    }
    static if (n == 5)
    {
        if (false)
            return 99;
        foreach (e; S13336())
        {
            if (f)
                return sx;
            return sy;
        }
        assert(0);
    }
    static if (n == 6)
    {
        foreach (e; S13336())
        {
            if (f)
                return sx;
            return sy;
        }
        return 99;
    }
};

// auto ref without out contract
auto ref f13336a(int n, alias sx, alias sy)(bool f)
{
    mixin(fbody13336);
}

// auto without out contract
auto f13336b(int n, alias sx, alias sy)(bool f)
{
    mixin(fbody13336);
}

// auto ref with out contract
auto ref f13336c(int n, alias sx, alias sy)(bool f)
out(r)
{
    static assert(is(typeof(r) == const double));
    assert(r == (f ? sx : sy));
    result13336 = r;
}
do
{
    mixin(fbody13336);
}

// auto with out contract
auto f13336d(int n, alias sx, alias sy)(bool f)
out(r)
{
    static assert(is(typeof(r) == const double));
    assert(r == (f ? sx : sy));
    result13336 = r;
}
do
{
    mixin(fbody13336);
}

void test13336()
{
    static int sx = 1;
    static double sy = 2.5;

    foreach (num; TypeTuple!(1, 2, 3, 4, 5, 6))
    {
        foreach (foo; TypeTuple!(f13336a, f13336b))
        {
            alias fooxy = foo!(num, sx, sy);
            static assert(is(typeof(&fooxy) : double function(bool)));
            assert(fooxy(1) == 1.0);
            assert(fooxy(0) == 2.5);

            alias fooyx = foo!(num, sy, sx);
            static assert(is(typeof(&fooyx) : double function(bool)));
            assert(fooyx(1) == 2.5);
            assert(fooyx(0) == 1.0);
        }

        foreach (foo; TypeTuple!(f13336c, f13336d))
        {
            alias fooxy = foo!(num, sx, sy);
            static assert(is(typeof(&fooxy) : double function(bool)));
            assert(fooxy(1) == 1.0 && result13336 == 1.0);
            assert(fooxy(0) == 2.5 && result13336 == 2.5);

            alias fooyx = foo!(num, sy, sx);
            static assert(is(typeof(&fooyx) : double function(bool)));
            assert(fooyx(1) == 2.5 && result13336 == 2.5);
            assert(fooyx(0) == 1.0 && result13336 == 1.0);
        }
    }
}

/***************************************************/
// https://issues.dlang.org/show_bug.cgi?id=15018

struct S15018(int n)
{
    short[n] m;
}

S15018!n f15018(int n)()
{
    S15018!n s;
    foreach(i; 0..n)
        s.m[i] = cast(short)(i * i + 3);
    return s;
}

void test15018()
{
    // size 4
    S15018!2[3] s3;
    s3[] = f15018!2();
    foreach (int i; 0..3)
    {
        assert(s3[i].m[0] == 3);
        assert(s3[i].m[1] == 4);
    }

    // size 4-18
    foreach (n; TypeTuple!(2, 3, 4, 5, 6, 7, 8, 9))
    {
        S15018!n[5] i5;
        i5[] = f15018!n();
        foreach (int j; 0..5)
            foreach(k; 0..n)
                if (i5[j].m[k] != k * k + 3)
                    assert(false);
    }
}

/***************************************************/

int main()
{
    test13336();
    test15018();

    printf("Success\n");
    return 0;
}