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 207 208 209 210 211 212 213 214 215 216
|
// EXTRA_FILES: imports/testlambda1.d imports/testlambda2.d
module testlambdacomp;
void test1()
{
static assert(__traits(isSame, (a, b) => a + b, (c, d) => c + d));
static assert(__traits(isSame, a => ++a, b => ++b));
static assert(!__traits(isSame, (int a, int b) => a + b, (a, b) => a + b));
static assert(__traits(isSame, (a, b) => a + b + 10, (c, d) => c + d + 10));
}
class Y
{
static int r = 5;
int x;
this(int x)
{
this.x = x;
}
}
class A
{
Y a;
this(Y a)
{
this.a = a;
}
}
void foo3(alias pred)()
{
static assert(!__traits(isSame, pred, (A x, A y) => ++x.a.x + (--y.a.x)));
}
void test2()
{
int b;
static assert(!__traits(isSame, a => a + b, a => a + b));
int f() { return 3;}
static assert(__traits(isSame, a => a + f(), a => a + f()));
class A
{
Y a;
this(Y a)
{
this.a = a;
}
}
class B
{
int a;
this(int a)
{
this.a = a;
}
}
B q = new B(7);
alias pred = (A a, A b) => ++a.a.x + (--b.a.x);
foo3!pred();
static assert(!__traits(isSame, (A a) => ++a.a.x + 2, (A b) => ++b.a.x + 3));
static assert(__traits(isSame, pred, (A x, A y) => ++x.a.x + (--y.a.x)));
static assert(!__traits(isSame, (B a) => ++a.a + 2, (B b) => ++b.a + 3));
static assert(__traits(isSame, (B a) => ++a.a, (B a) => ++a.a));
B cl = new B(7);
static assert(!__traits(isSame, a => a + q.a, c => c + cl.a));
class C(G)
{
G a;
this(int a)
{
this.a = a;
}
}
static assert(!__traits(isSame, (C!int a) => ++a.a, (C!int a) => ++a.a));
struct X
{
int a;
}
static assert(__traits(isSame, (X a) => a.a + 2, (X b) => b.a + 2));
struct T(G)
{
G a;
}
static assert(!__traits(isSame, (T!int a) => ++a.a, (T!int a) => ++a.a));
}
void test3()
{
enum q = 10;
static assert(__traits(isSame, (a, b) => a + b + q, (c, d) => c + d + 10));
struct Bar
{
int a;
}
enum r1 = Bar(1);
enum r2 = Bar(1);
static assert(__traits(isSame, a => a + r1.a, b => b + r2.a));
enum X { A, B, C}
static assert(__traits(isSame, a => a + X.A, a => a + 0));
}
void foo(alias pred)()
{
static assert(__traits(isSame, pred, (c, d) => c + d));
static assert(__traits(isSame, (c, d) => c + d, pred));
}
void bar(alias pred)()
{
static assert(__traits(isSame, pred, (c, d) => c < d + 7));
enum q = 7;
static assert(__traits(isSame, pred, (c, d) => c < d + q));
int r = 7;
static assert(!__traits(isSame, pred, (c, d) => c < d + r));
}
void test4()
{
foo!((a, b) => a + b)();
bar!((a, b) => a < b + 7);
}
int bar()
{
return 2;
}
void testImportedFunctions(alias pred)()
{
// imports.testalambda1.bar != imports.testlambda2.bar
import imports.testlambda2 : bar;
static assert(!__traits(isSame, pred, (int a) => a + bar()));
}
void testLocalGlobalFunctionScopes(alias pred)()
{
// testlambdacomp.bar != testlambdacomp.test5.bar
static assert(!__traits(isSame, pred, (int a) => a + bar()));
// imports.testlambda1.bar != testlambdacomp.test5.bar
import imports.testlambda1 : bar;
static assert(!__traits(isSame, pred, (int a) => a + bar()));
// functions imported from different modules are not equal
testImportedFunctions!((int a) => a + bar())();
}
// lambda functions which contain function calls
void test5()
{
int bar()
{
return 3;
}
// functions in the same scope
alias pred = a => a + bar();
alias pred2 = b => b + bar();
static assert(__traits(isSame, pred, pred2));
// functions in different scopes
testLocalGlobalFunctionScopes!((int a) => a + bar())();
int foo(int a, int b)
{
return 2 + a + b;
}
// functions with different kind of parameters
alias preda23 = a => a + foo(2, 3);
alias predb23 = b => b + foo(2, 3);
alias predc24 = c => c + foo(2, 4);
alias predd23 = (int d) => d + foo(2, 3);
alias prede23 = (int e) => e + foo(2, 3);
alias predf24 = (int f) => f + foo(2, 4);
static assert(__traits(isSame, preda23, predb23));
static assert(!__traits(isSame, predc24, predd23));
static assert(__traits(isSame, predd23, prede23));
static assert(!__traits(isSame, prede23, predf24));
// functions with function calls as parameters
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))));
static assert(!__traits(isSame, (a, b) => foo(foo(1, a), foo(1, b)), (int a, int b) => foo(foo(1, a), foo(2, b))));
float floatFunc(float a, float b)
{
return a + b;
}
static assert(__traits(isSame, a => floatFunc(a, 1.0), b => floatFunc(b, 1.0)));
static assert(!__traits(isSame, a => floatFunc(a, 1.0), b => floatFunc(b, 2.0)));
}
void main()
{
test1();
test2();
test3();
test4();
test5();
}
|