| 1 |
/* |
| 2 |
RUN_OUTPUT: |
| 3 |
--- |
| 4 |
Success |
| 5 |
--- |
| 6 |
*/ |
| 7 |
extern(C) int printf(const char*, ...); |
| 8 |
|
| 9 |
alias TypeTuple(T...) = T; |
| 10 |
|
| 11 |
/***************************************************/ |
| 12 |
// https://issues.dlang.org/show_bug.cgi?id=13336 |
| 13 |
|
| 14 |
struct S13336 |
| 15 |
{ |
| 16 |
int opApply(scope int delegate(int) dg) |
| 17 |
{ |
| 18 |
return dg(0); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
double result13336; |
| 23 |
|
| 24 |
enum fbody13336 = |
| 25 |
q{ |
| 26 |
static if (n == 1) |
| 27 |
{ |
| 28 |
if (f) |
| 29 |
return sx; |
| 30 |
return sy; |
| 31 |
} |
| 32 |
static if (n == 2) |
| 33 |
{ |
| 34 |
foreach (e; S13336()) |
| 35 |
{ |
| 36 |
if (f) |
| 37 |
return sx; |
| 38 |
return sy; |
| 39 |
} |
| 40 |
assert(0); |
| 41 |
} |
| 42 |
static if (n == 3) |
| 43 |
{ |
| 44 |
if (f) |
| 45 |
return sx; |
| 46 |
foreach (e; S13336()) |
| 47 |
{ |
| 48 |
return sy; |
| 49 |
} |
| 50 |
assert(0); |
| 51 |
} |
| 52 |
static if (n == 4) |
| 53 |
{ |
| 54 |
foreach (e; S13336()) |
| 55 |
{ |
| 56 |
if (f) |
| 57 |
return sx; |
| 58 |
} |
| 59 |
return sy; |
| 60 |
} |
| 61 |
static if (n == 5) |
| 62 |
{ |
| 63 |
if (false) |
| 64 |
return 99; |
| 65 |
foreach (e; S13336()) |
| 66 |
{ |
| 67 |
if (f) |
| 68 |
return sx; |
| 69 |
return sy; |
| 70 |
} |
| 71 |
assert(0); |
| 72 |
} |
| 73 |
static if (n == 6) |
| 74 |
{ |
| 75 |
foreach (e; S13336()) |
| 76 |
{ |
| 77 |
if (f) |
| 78 |
return sx; |
| 79 |
return sy; |
| 80 |
} |
| 81 |
return 99; |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
// auto ref without out contract |
| 86 |
auto ref f13336a(int n, alias sx, alias sy)(bool f) |
| 87 |
{ |
| 88 |
mixin(fbody13336); |
| 89 |
} |
| 90 |
|
| 91 |
// auto without out contract |
| 92 |
auto f13336b(int n, alias sx, alias sy)(bool f) |
| 93 |
{ |
| 94 |
mixin(fbody13336); |
| 95 |
} |
| 96 |
|
| 97 |
// auto ref with out contract |
| 98 |
auto ref f13336c(int n, alias sx, alias sy)(bool f) |
| 99 |
out(r) |
| 100 |
{ |
| 101 |
static assert(is(typeof(r) == const double)); |
| 102 |
assert(r == (f ? sx : sy)); |
| 103 |
result13336 = r; |
| 104 |
} |
| 105 |
do |
| 106 |
{ |
| 107 |
mixin(fbody13336); |
| 108 |
} |
| 109 |
|
| 110 |
// auto with out contract |
| 111 |
auto f13336d(int n, alias sx, alias sy)(bool f) |
| 112 |
out(r) |
| 113 |
{ |
| 114 |
static assert(is(typeof(r) == const double)); |
| 115 |
assert(r == (f ? sx : sy)); |
| 116 |
result13336 = r; |
| 117 |
} |
| 118 |
do |
| 119 |
{ |
| 120 |
mixin(fbody13336); |
| 121 |
} |
| 122 |
|
| 123 |
void test13336() |
| 124 |
{ |
| 125 |
static int sx = 1; |
| 126 |
static double sy = 2.5; |
| 127 |
|
| 128 |
foreach (num; TypeTuple!(1, 2, 3, 4, 5, 6)) |
| 129 |
{ |
| 130 |
foreach (foo; TypeTuple!(f13336a, f13336b)) |
| 131 |
{ |
| 132 |
alias fooxy = foo!(num, sx, sy); |
| 133 |
static assert(is(typeof(&fooxy) : double function(bool))); |
| 134 |
assert(fooxy(1) == 1.0); |
| 135 |
assert(fooxy(0) == 2.5); |
| 136 |
|
| 137 |
alias fooyx = foo!(num, sy, sx); |
| 138 |
static assert(is(typeof(&fooyx) : double function(bool))); |
| 139 |
assert(fooyx(1) == 2.5); |
| 140 |
assert(fooyx(0) == 1.0); |
| 141 |
} |
| 142 |
|
| 143 |
foreach (foo; TypeTuple!(f13336c, f13336d)) |
| 144 |
{ |
| 145 |
alias fooxy = foo!(num, sx, sy); |
| 146 |
static assert(is(typeof(&fooxy) : double function(bool))); |
| 147 |
assert(fooxy(1) == 1.0 && result13336 == 1.0); |
| 148 |
assert(fooxy(0) == 2.5 && result13336 == 2.5); |
| 149 |
|
| 150 |
alias fooyx = foo!(num, sy, sx); |
| 151 |
static assert(is(typeof(&fooyx) : double function(bool))); |
| 152 |
assert(fooyx(1) == 2.5 && result13336 == 2.5); |
| 153 |
assert(fooyx(0) == 1.0 && result13336 == 1.0); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/***************************************************/ |
| 159 |
// https://issues.dlang.org/show_bug.cgi?id=15018 |
| 160 |
|
| 161 |
struct S15018(int n) |
| 162 |
{ |
| 163 |
short[n] m; |
| 164 |
} |
| 165 |
|
| 166 |
S15018!n f15018(int n)() |
| 167 |
{ |
| 168 |
S15018!n s; |
| 169 |
foreach(i; 0..n) |
| 170 |
s.m[i] = cast(short)(i * i + 3); |
| 171 |
return s; |
| 172 |
} |
| 173 |
|
| 174 |
void test15018() |
| 175 |
{ |
| 176 |
// size 4 |
| 177 |
S15018!2[3] s3; |
| 178 |
s3[] = f15018!2(); |
| 179 |
foreach (int i; 0..3) |
| 180 |
{ |
| 181 |
assert(s3[i].m[0] == 3); |
| 182 |
assert(s3[i].m[1] == 4); |
| 183 |
} |
| 184 |
|
| 185 |
// size 4-18 |
| 186 |
foreach (n; TypeTuple!(2, 3, 4, 5, 6, 7, 8, 9)) |
| 187 |
{ |
| 188 |
S15018!n[5] i5; |
| 189 |
i5[] = f15018!n(); |
| 190 |
foreach (int j; 0..5) |
| 191 |
foreach(k; 0..n) |
| 192 |
if (i5[j].m[k] != k * k + 3) |
| 193 |
assert(false); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/***************************************************/ |
| 198 |
|
| 199 |
int main() |
| 200 |
{ |
| 201 |
test13336(); |
| 202 |
test15018(); |
| 203 |
|
| 204 |
printf("Success\n"); |
| 205 |
return 0; |
| 206 |
} |