1 module ut.ctfe.functions; 2 3 4 import ut.ctfe; 5 6 7 8 @("callMixin.add1") 9 unittest { 10 import std.traits: Unqual; 11 12 enum mod = module_!"modules.functions"; 13 enum add1 = mod.functionsByOverload[0]; 14 15 mixin(add1.importMixin); 16 17 mixin(add1.fullyQualifiedName, `(1, 2)`).should == 4; 18 mixin(add1.fullyQualifiedName, `(2, 3)`).should == 6; 19 20 // or, easier... 21 mixin(add1.callMixin(1, 2)).should == 4; 22 auto arg = 2; 23 mixin(add1.callMixin("arg", 2)).should == 5; 24 } 25 26 27 @("pointer.byOverload.add1.0") 28 unittest { 29 enum mod = module_!"modules.functions"; 30 enum add1 = mod.functionsByOverload[0]; 31 32 mixin(add1.importMixin); 33 34 auto ptr = pointer!add1; 35 static assert(is(typeof(ptr) == int function(int, int))); 36 37 ptr(1, 2).should == 4; 38 ptr(2, 3).should == 6; 39 } 40 41 @("pointer.byOverload.add1.1") 42 unittest { 43 enum mod = module_!"modules.functions"; 44 enum add1 = mod.functionsByOverload[1]; 45 46 mixin(add1.importMixin); 47 48 auto ptr = pointer!add1; 49 static assert(is(typeof(ptr) == double function(double, double))); 50 51 ptr(1.0, 2.0).should == 4.0; 52 ptr(2.0, 3.0).should == 6.0; 53 } 54 55 56 @("pointer.bySymbol.add1") 57 unittest { 58 enum mod = module_!"modules.functions"; 59 enum add1 = mod.functionsBySymbol[0]; 60 61 enum add1Int = add1.overloads[0]; 62 enum add1Double = add1.overloads[1]; 63 64 mixin(add1Int.importMixin); 65 66 auto ptrInt = pointer!add1Int; 67 static assert(is(typeof(ptrInt) == int function(int, int))); 68 ptrInt(1, 2).should == 4; 69 ptrInt(2, 3).should == 6; 70 71 auto ptrDouble = pointer!add1Double; 72 static assert(is(typeof(ptrDouble) == double function(double, double))); 73 ptrDouble(1.0, 2.0).should == 4.0; 74 ptrDouble(2.0, 3.0).should == 6.0; 75 } 76 77 78 @("pointer.byOverload.withDefault") 79 unittest { 80 static import modules.functions; 81 82 enum mod = module_!"modules.functions"; 83 enum withDefault = mod.functionsByOverload[2]; 84 static assert(withDefault.identifier == "withDefault"); 85 86 auto ptr = pointer!withDefault; 87 (ptr is &modules.functions.withDefault).should == true; 88 89 ptr(1.1, 2.2).should ~ 3.3; 90 ptr(1.1).should ~ 34.4; 91 } 92 93 94 @("pointerMixin.byOverload.add1.0") 95 unittest { 96 enum mod = module_!"modules.functions"; 97 enum add1 = mod.functionsByOverload[0]; 98 99 mixin(add1.importMixin); 100 101 auto ptr = mixin(add1.pointerMixin); 102 static assert(is(typeof(ptr) == int function(int, int))); 103 104 ptr(1, 2).should == 4; 105 ptr(2, 3).should == 6; 106 } 107 108 109 @("pointerMixin.byOverload.add1.1") 110 unittest { 111 enum mod = module_!"modules.functions"; 112 enum add1 = mod.functionsByOverload[1]; 113 114 mixin(add1.importMixin); 115 116 auto ptr = mixin(add1.pointerMixin); 117 static assert(is(typeof(ptr) == double function(double, double))); 118 119 ptr(1.0, 2.0).should == 4.0; 120 ptr(2.0, 3.0).should == 6.0; 121 } 122 123 124 @("pointerMixin.bySymbol.add1") 125 unittest { 126 enum mod = module_!"modules.functions"; 127 enum add1 = mod.functionsBySymbol[0]; 128 129 enum add1Int = add1.overloads[0]; 130 enum add1Double = add1.overloads[1]; 131 132 mixin(add1Int.importMixin); 133 134 auto ptrInt = mixin(add1Int.pointerMixin); 135 static assert(is(typeof(ptrInt) == int function(int, int))); 136 ptrInt(1, 2).should == 4; 137 ptrInt(2, 3).should == 6; 138 139 auto ptrDouble = mixin(add1Double.pointerMixin); 140 static assert(is(typeof(ptrDouble) == double function(double, double))); 141 ptrDouble(1.0, 2.0).should == 4.0; 142 ptrDouble(2.0, 3.0).should == 6.0; 143 } 144 145 146 @("pointerMixin.byOverload.withDefault") 147 unittest { 148 static import modules.functions; 149 150 enum mod = module_!"modules.functions"; 151 enum withDefault = mod.functionsByOverload[2]; 152 static assert(withDefault.identifier == "withDefault"); 153 154 auto ptr = mixin(withDefault.pointerMixin); 155 (ptr is &modules.functions.withDefault).should == true; 156 157 ptr(1.1, 2.2).should ~ 3.3; 158 ptr(1.1).should ~ 34.4; 159 } 160 161 162 @("functions.byOverload") 163 @safe pure unittest { 164 static import modules.functions; 165 import std.traits: PSC = ParameterStorageClass; 166 167 enum mod = module_!"modules.functions"; 168 mod.functionsByOverload[].shouldBeSameSetAs( 169 [ 170 Function( 171 "modules.functions", 172 0, 173 "add1", 174 Type("int"), 175 [ 176 Parameter("int", "i"), 177 Parameter("int", "j"), 178 ], 179 ), 180 Function( 181 "modules.functions", 182 1, 183 "add1", 184 Type("double"), 185 [ 186 Parameter("double", "d0"), 187 Parameter("double", "d1"), 188 ], 189 ), 190 Function( 191 "modules.functions", 192 0, 193 "withDefault", 194 Type("double"), 195 [ 196 Parameter("double", "fst"), 197 Parameter("double", "snd", "33.3"), 198 ], 199 ), 200 Function( 201 "modules.functions", 202 0, 203 "storageClasses", 204 Type("void"), 205 [ 206 Parameter("int", "normal", "", PSC.none), 207 Parameter("int*", "returnScope", "", PSC.return_ | PSC.scope_), 208 Parameter("int", "out_", "", PSC.out_), 209 Parameter("int", "ref_", "", PSC.ref_), 210 Parameter("int", "lazy_", "", PSC.lazy_), 211 ] 212 ), 213 Function( 214 "modules.functions", 215 0, 216 "exportedFunc", 217 Type("void"), 218 [], 219 ), 220 Function( 221 "modules.functions", 222 0, 223 "externC", 224 Type("void"), 225 [], 226 ), 227 Function( 228 "modules.functions", 229 0, 230 "externCpp", 231 Type("void"), 232 [], 233 ), 234 Function( 235 "modules.functions", 236 0, 237 "identityInt", 238 Type("int"), 239 [Parameter("int", "x", "", PSC.none)], 240 ), 241 Function( 242 "modules.functions", 243 0, 244 "voldermort", 245 Type("Voldermort"), 246 [Parameter("int", "i", "", PSC.none)], 247 ), 248 Function( 249 "modules.functions", 250 0, 251 "voldermortArray", 252 Type("DasVoldermort[]"), 253 [Parameter("int", "i", "", PSC.none)], 254 ), 255 Function( 256 "modules.functions", 257 0, 258 "concatFoo", 259 Type("string"), 260 [ 261 Parameter("string", "s0", "", PSC.none), 262 Parameter("int", "i", "", PSC.none), 263 Parameter("string", "s1", "", PSC.none), 264 ], 265 ), 266 ] 267 ); 268 } 269 270 271 @("functions.bySymbol") 272 @safe pure unittest { 273 static import modules.functions; 274 import std.traits: PSC = ParameterStorageClass; 275 276 enum mod = module_!"modules.functions"; 277 mod.functionsBySymbol[].shouldBeSameSetAs( 278 [ 279 OverloadSet( 280 "add1", 281 [ 282 Function( 283 "modules.functions", 284 0, 285 "add1", 286 Type("int"), 287 [ 288 Parameter("int", "i"), 289 Parameter("int", "j"), 290 ], 291 ), 292 Function( 293 "modules.functions", 294 1, 295 "add1", 296 Type("double"), 297 [ 298 Parameter("double", "d0"), 299 Parameter("double", "d1"), 300 ], 301 ), 302 ] 303 ), 304 OverloadSet( 305 "withDefault", 306 [ 307 Function( 308 "modules.functions", 309 0, 310 "withDefault", 311 Type("double"), 312 [ 313 Parameter("double", "fst"), 314 Parameter("double", "snd", "33.3"), 315 ], 316 ), 317 ] 318 ), 319 OverloadSet( 320 "storageClasses", 321 [ 322 Function( 323 "modules.functions", 324 0, 325 "storageClasses", 326 Type("void"), 327 [ 328 Parameter("int", "normal", "", PSC.none), 329 Parameter("int*", "returnScope", "", PSC.return_ | PSC.scope_), 330 Parameter("int", "out_", "", PSC.out_), 331 Parameter("int", "ref_", "", PSC.ref_), 332 Parameter("int", "lazy_", "", PSC.lazy_), 333 ] 334 ), 335 ] 336 ), 337 OverloadSet( 338 "exportedFunc", 339 [ 340 Function( 341 "modules.functions", 342 0, 343 "exportedFunc", 344 Type("void"), 345 [], 346 ), 347 ] 348 ), 349 OverloadSet( 350 "externC", 351 [ 352 Function( 353 "modules.functions", 354 0, 355 "externC", 356 Type("void"), 357 [], 358 ), 359 ] 360 ), 361 OverloadSet( 362 "externCpp", 363 [ 364 Function( 365 "modules.functions", 366 0, 367 "externCpp", 368 Type("void"), 369 [], 370 ), 371 ] 372 ), 373 OverloadSet( 374 "identityInt", 375 [ 376 Function( 377 "modules.functions", 378 0, 379 "identityInt", 380 Type("int"), 381 [Parameter("int", "x", "", PSC.none)], 382 ), 383 ] 384 ), 385 OverloadSet( 386 "voldermort", 387 [ 388 Function( 389 "modules.functions", 390 0, 391 "voldermort", 392 Type("Voldermort"), 393 [Parameter("int", "i", "", PSC.none)], 394 ), 395 ] 396 ), 397 OverloadSet( 398 "voldermortArray", 399 [ 400 Function( 401 "modules.functions", 402 0, 403 "voldermortArray", 404 Type("DasVoldermort[]"), 405 [Parameter("int", "i", "", PSC.none)], 406 ), 407 ] 408 ), 409 OverloadSet( 410 "concatFoo", 411 [ 412 Function( 413 "modules.functions", 414 0, 415 "concatFoo", 416 Type("string"), 417 [ 418 Parameter("string", "s0", "", PSC.none), 419 Parameter("int", "i", "", PSC.none), 420 Parameter("string", "s1", "", PSC.none), 421 ], 422 ), 423 424 ] 425 ), 426 ] 427 ); 428 } 429 430 431 @("functions.allAggregates") 432 @safe pure unittest { 433 import std.traits: PSC = ParameterStorageClass; 434 435 enum mod = module_!"modules.functions"; 436 mod.allAggregates[].shouldBeSameSetAs( 437 [ 438 Aggregate("Voldermort", Aggregate.Kind.struct_), 439 Aggregate("DasVoldermort", Aggregate.Kind.struct_), 440 ] 441 ); 442 }