| 1 |
// EXTRA_FILES: imports/testlambda1.d imports/testlambda2.d |
| 2 |
module testlambdacomp; |
| 3 |
|
| 4 |
void test1() |
| 5 |
{ |
| 6 |
static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d)); |
| 7 |
static assert(__traits(isSame, a => ++a, b => ++b)); |
| 8 |
static assert(!__traits(isSame, (int a, int b) => a + b, (a, b) => a + b)); |
| 9 |
static assert(__traits(isSame, (a, b) => a + b + 10, (c, d) => c + d + 10)); |
| 10 |
} |
| 11 |
|
| 12 |
class Y |
| 13 |
{ |
| 14 |
static int r = 5; |
| 15 |
int x; |
| 16 |
this(int x) |
| 17 |
{ |
| 18 |
this.x = x; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
class A |
| 23 |
{ |
| 24 |
Y a; |
| 25 |
this(Y a) |
| 26 |
{ |
| 27 |
this.a = a; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
void foo3(alias pred)() |
| 32 |
{ |
| 33 |
static assert(!__traits(isSame, pred, (A x, A y) => ++x.a.x + (--y.a.x))); |
| 34 |
} |
| 35 |
|
| 36 |
void test2() |
| 37 |
{ |
| 38 |
|
| 39 |
int b; |
| 40 |
static assert(!__traits(isSame, a => a + b, a => a + b)); |
| 41 |
|
| 42 |
int f() { return 3;} |
| 43 |
static assert(__traits(isSame, a => a + f(), a => a + f())); |
| 44 |
|
| 45 |
class A |
| 46 |
{ |
| 47 |
Y a; |
| 48 |
this(Y a) |
| 49 |
{ |
| 50 |
this.a = a; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
class B |
| 55 |
{ |
| 56 |
int a; |
| 57 |
this(int a) |
| 58 |
{ |
| 59 |
this.a = a; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
B q = new B(7); |
| 64 |
alias pred = (A a, A b) => ++a.a.x + (--b.a.x); |
| 65 |
foo3!pred(); |
| 66 |
static assert(!__traits(isSame, (A a) => ++a.a.x + 2, (A b) => ++b.a.x + 3)); |
| 67 |
static assert(__traits(isSame, pred, (A x, A y) => ++x.a.x + (--y.a.x))); |
| 68 |
static assert(!__traits(isSame, (B a) => ++a.a + 2, (B b) => ++b.a + 3)); |
| 69 |
static assert(__traits(isSame, (B a) => ++a.a, (B a) => ++a.a)); |
| 70 |
|
| 71 |
B cl = new B(7); |
| 72 |
static assert(!__traits(isSame, a => a + q.a, c => c + cl.a)); |
| 73 |
|
| 74 |
class C(G) |
| 75 |
{ |
| 76 |
G a; |
| 77 |
this(int a) |
| 78 |
{ |
| 79 |
this.a = a; |
| 80 |
} |
| 81 |
} |
| 82 |
static assert(!__traits(isSame, (C!int a) => ++a.a, (C!int a) => ++a.a)); |
| 83 |
|
| 84 |
struct X |
| 85 |
{ |
| 86 |
int a; |
| 87 |
} |
| 88 |
static assert(__traits(isSame, (X a) => a.a + 2, (X b) => b.a + 2)); |
| 89 |
|
| 90 |
struct T(G) |
| 91 |
{ |
| 92 |
G a; |
| 93 |
} |
| 94 |
static assert(!__traits(isSame, (T!int a) => ++a.a, (T!int a) => ++a.a)); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
void test3() |
| 99 |
{ |
| 100 |
enum q = 10; |
| 101 |
static assert(__traits(isSame, (a, b) => a + b + q, (c, d) => c + d + 10)); |
| 102 |
|
| 103 |
struct Bar |
| 104 |
{ |
| 105 |
int a; |
| 106 |
} |
| 107 |
enum r1 = Bar(1); |
| 108 |
enum r2 = Bar(1); |
| 109 |
static assert(__traits(isSame, a => a + r1.a, b => b + r2.a)); |
| 110 |
|
| 111 |
enum X { A, B, C} |
| 112 |
static assert(__traits(isSame, a => a + X.A, a => a + 0)); |
| 113 |
} |
| 114 |
|
| 115 |
void foo(alias pred)() |
| 116 |
{ |
| 117 |
static assert(__traits(isSame, pred, (c, d) => c + d)); |
| 118 |
static assert(__traits(isSame, (c, d) => c + d, pred)); |
| 119 |
} |
| 120 |
|
| 121 |
void bar(alias pred)() |
| 122 |
{ |
| 123 |
static assert(__traits(isSame, pred, (c, d) => c < d + 7)); |
| 124 |
|
| 125 |
enum q = 7; |
| 126 |
static assert(__traits(isSame, pred, (c, d) => c < d + q)); |
| 127 |
|
| 128 |
int r = 7; |
| 129 |
static assert(!__traits(isSame, pred, (c, d) => c < d + r)); |
| 130 |
} |
| 131 |
void test4() |
| 132 |
{ |
| 133 |
foo!((a, b) => a + b)(); |
| 134 |
bar!((a, b) => a < b + 7); |
| 135 |
} |
| 136 |
|
| 137 |
int bar() |
| 138 |
{ |
| 139 |
return 2; |
| 140 |
} |
| 141 |
|
| 142 |
void testImportedFunctions(alias pred)() |
| 143 |
{ |
| 144 |
// imports.testalambda1.bar != imports.testlambda2.bar |
| 145 |
import imports.testlambda2 : bar; |
| 146 |
static assert(!__traits(isSame, pred, (int a) => a + bar())); |
| 147 |
} |
| 148 |
|
| 149 |
void testLocalGlobalFunctionScopes(alias pred)() |
| 150 |
{ |
| 151 |
// testlambdacomp.bar != testlambdacomp.test5.bar |
| 152 |
static assert(!__traits(isSame, pred, (int a) => a + bar())); |
| 153 |
|
| 154 |
// imports.testlambda1.bar != testlambdacomp.test5.bar |
| 155 |
import imports.testlambda1 : bar; |
| 156 |
static assert(!__traits(isSame, pred, (int a) => a + bar())); |
| 157 |
|
| 158 |
// functions imported from different modules are not equal |
| 159 |
testImportedFunctions!((int a) => a + bar())(); |
| 160 |
} |
| 161 |
|
| 162 |
// lambda functions which contain function calls |
| 163 |
void test5() |
| 164 |
{ |
| 165 |
|
| 166 |
int bar() |
| 167 |
{ |
| 168 |
return 3; |
| 169 |
} |
| 170 |
|
| 171 |
// functions in the same scope |
| 172 |
alias pred = a => a + bar(); |
| 173 |
alias pred2 = b => b + bar(); |
| 174 |
static assert(__traits(isSame, pred, pred2)); |
| 175 |
|
| 176 |
// functions in different scopes |
| 177 |
testLocalGlobalFunctionScopes!((int a) => a + bar())(); |
| 178 |
|
| 179 |
int foo(int a, int b) |
| 180 |
{ |
| 181 |
return 2 + a + b; |
| 182 |
} |
| 183 |
|
| 184 |
// functions with different kind of parameters |
| 185 |
alias preda23 = a => a + foo(2, 3); |
| 186 |
alias predb23 = b => b + foo(2, 3); |
| 187 |
alias predc24 = c => c + foo(2, 4); |
| 188 |
alias predd23 = (int d) => d + foo(2, 3); |
| 189 |
alias prede23 = (int e) => e + foo(2, 3); |
| 190 |
alias predf24 = (int f) => f + foo(2, 4); |
| 191 |
static assert(__traits(isSame, preda23, predb23)); |
| 192 |
static assert(!__traits(isSame, predc24, predd23)); |
| 193 |
static assert(__traits(isSame, predd23, prede23)); |
| 194 |
static assert(!__traits(isSame, prede23, predf24)); |
| 195 |
|
| 196 |
// functions with function calls as parameters |
| 197 |
static assert(!__traits(isSame, (int a, int b) => foo(foo(1, a), foo(1, b)), (int a, int b) => foo(foo(1, a), foo(2, b)))); |
| 198 |
static assert(!__traits(isSame, (a, b) => foo(foo(1, a), foo(1, b)), (int a, int b) => foo(foo(1, a), foo(2, b)))); |
| 199 |
|
| 200 |
float floatFunc(float a, float b) |
| 201 |
{ |
| 202 |
return a + b; |
| 203 |
} |
| 204 |
|
| 205 |
static assert(__traits(isSame, a => floatFunc(a, 1.0), b => floatFunc(b, 1.0))); |
| 206 |
static assert(!__traits(isSame, a => floatFunc(a, 1.0), b => floatFunc(b, 2.0))); |
| 207 |
} |
| 208 |
|
| 209 |
void main() |
| 210 |
{ |
| 211 |
test1(); |
| 212 |
test2(); |
| 213 |
test3(); |
| 214 |
test4(); |
| 215 |
test5(); |
| 216 |
} |