1 extern(C) int printf(const char*, ...);
TypeTuple(TL...)2 template TypeTuple(TL...) { alias TypeTuple = TL; }
3
4 import core.stdc.math : isnan;
5
6 /********************************************/
7 // 9112
8
test9112a()9 void test9112a() // T() and T(v)
10 {
11 void test(T)(T v)
12 {
13 foreach (string qual; TypeTuple!("", "const ", "immutable "))
14 {
15 mixin("alias U = "~qual~T.stringof~";");
16 //pragma(msg, U);
17
18 mixin("auto x1 = "~qual~T.stringof~"();"); // U() default construction syntax
19 mixin("auto x2 = "~qual~T.stringof~"(v);"); // U(v)
20 static assert(!__traits(compiles, mixin(qual~T.stringof~"(v, v)"))); // U(v, v)
21 static assert(is(typeof(x1) == U));
22 static assert(is(typeof(x2) == U));
23 static if ( is(typeof(U.nan) : real)) assert( isnan(x1.re) && !isnan(x1.im), U.stringof);
24 static if ( is(typeof(U.nan) : ireal)) assert(!isnan(x1.re) && isnan(x1.im), U.stringof);
25 static if ( is(typeof(U.nan) : creal)) assert( isnan(x1.re) && isnan(x1.im), U.stringof);
26 static if (!is(typeof(U.nan))) assert( x1 == U.init, U.stringof);
27 assert(x2 == v, U.stringof);
28 }
29 }
30 static assert(!__traits(compiles, { auto x1 = void(); }));
31 static assert(!__traits(compiles, { auto x2 = void(1); }));
32 test!( byte )(10);
33 test!(ubyte )(10);
34 test!( short )(10);
35 test!(ushort )(10);
36 test!( int )(10);
37 test!(uint )(10);
38 test!( long )(10);
39 test!(ulong )(10);
40 test!( float )(3.14);
41 test!( double)(3.14);
42 test!( real )(3.14);
43 test!(ifloat )(1.4142i);
44 test!(idouble)(1.4142i);
45 test!(ireal )(1.4142i);
46 test!(cfloat )(1.2+3.4i);
47 test!(cdouble)(1.2+3.4i);
48 test!(creal )(1.2+3.4i);
49 test!( char )('A');
50 test!(wchar )('A');
51 test!(dchar )('A');
52 test!(bool )(true);
53
54 static assert(!__traits(compiles, int(1.42))); // in curre,t this is disallowed
55 static assert(!__traits(compiles, double(3.14i)));
56
57 {
58 int x;
59 alias T = int*;
60 //auto p = int*(&x); // Error: found '*' when expecting '.' following int
61 //auto p = (int*)(&x); // Error: C style cast illegal, use cast(int*)&x
62 auto p = T(&x);
63 assert( p == &x);
64 assert(*p == x);
65 }
66 }
67
68 enum Enum : long { a = 10, b = 20 }
69
test9112b()70 void test9112b() // new T(v)
71 {
72 void test(T)(T v)
73 {
74 foreach (string qual; TypeTuple!("", "const ", "immutable "))
75 {
76 mixin("alias U = "~qual~T.stringof~";");
77 //pragma(msg, U);
78
79 mixin("auto p1 = new "~qual~T.stringof~"();"); // U() default construction syntax
80 mixin("auto p2 = new "~qual~T.stringof~"(v);"); // U(v)
81 static assert(!__traits(compiles, mixin("new "~qual~T.stringof~"(v, v)"))); // U(v, v)
82 static assert(is(typeof(p1) == U*));
83 static assert(is(typeof(p2) == U*));
84 assert( p1 !is null);
85 assert( p2 !is null);
86 auto x1 = *p1;
87 auto x2 = *p2;
88 static if ( is(typeof(U.nan) : real)) assert( isnan(x1.re) && !isnan(x1.im), U.stringof);
89 static if ( is(typeof(U.nan) : ireal)) assert(!isnan(x1.re) && isnan(x1.im), U.stringof);
90 static if ( is(typeof(U.nan) : creal)) assert( isnan(x1.re) && isnan(x1.im), U.stringof);
91 static if (!is(typeof(U.nan))) assert( x1 == U.init, U.stringof);
92 assert(x2 == v, U.stringof);
93 }
94 }
95
96 static assert(!__traits(compiles, { auto x1 = new void(); }));
97 static assert(!__traits(compiles, { auto x2 = new void(1); }));
98 static assert(!__traits(compiles, { auto x2 = new void(1, 2); }));
99 test!( byte )(10);
100 test!(ubyte )(10);
101 test!( short )(10);
102 test!(ushort )(10);
103 test!( int )(10);
104 test!(uint )(10);
105 test!( long )(10);
106 test!(ulong )(10);
107 test!( float )(3.14);
108 test!( double)(3.14);
109 test!( real )(3.14);
110 test!(ifloat )(1.4142i);
111 test!(idouble)(1.4142i);
112 test!(ireal )(1.4142i);
113 test!(cfloat )(1.2+3.4i);
114 test!(cdouble)(1.2+3.4i);
115 test!(creal )(1.2+3.4i);
116 test!( char )('A');
117 test!(wchar )('A');
118 test!(dchar )('A');
119 test!(bool )(true);
120 test!(Enum )(Enum.a);
121
122 void testPtr(T)(T v)
123 {
124 T* pv = &v;
125 T** ppv = new T*(pv);
126 assert( *ppv == pv);
127 assert(**ppv == v);
128 }
129 foreach (T; TypeTuple!(int, const long, immutable double))
130 {
131 testPtr!T(10);
132 }
133 foreach (T; TypeTuple!(Enum, const Enum, immutable Enum))
134 {
135 testPtr!T(Enum.a);
136 }
137
138 static assert(!__traits(compiles, new const int(1, 2)));
139
140 static assert(!__traits(compiles, new int(1.42))); // in curre,t this is disallowed
141 static assert(!__traits(compiles, new double(3.14i)));
142
143 // int(1) in directly on statement scope should be parsed as an expression, but
144 // would fail to compile because of "has no effect" error.
145 static assert(!__traits(compiles, { int(1); }));
146 }
147
148 /********************************************/
149
main()150 int main()
151 {
152 test9112a();
153 test9112b();
154
155 printf("Success\n");
156 return 0;
157 }
158