| 1 |
|
| 2 |
extern(C) int printf(const char*, ...); |
| 3 |
|
| 4 |
/*******************************************/ |
| 5 |
|
| 6 |
interface D { |
| 7 |
int foo(); |
| 8 |
} |
| 9 |
|
| 10 |
class A : D { |
| 11 |
int foo() { return 1; } |
| 12 |
} |
| 13 |
|
| 14 |
class B : A, D |
| 15 |
{ |
| 16 |
override int foo() { return 2; } |
| 17 |
} |
| 18 |
|
| 19 |
class C : B, D { |
| 20 |
override int foo() { return 3; } |
| 21 |
} |
| 22 |
|
| 23 |
void test1() |
| 24 |
{ |
| 25 |
C c = new C; |
| 26 |
A a = cast(A) c; |
| 27 |
int j = a.foo(); |
| 28 |
printf("%d\n", j); |
| 29 |
assert(j == 3); |
| 30 |
|
| 31 |
B b = cast(B) c; |
| 32 |
int k = b.foo(); |
| 33 |
printf("%d\n", k); |
| 34 |
assert(k == 3); |
| 35 |
|
| 36 |
D d1 = cast(D) c; |
| 37 |
int l = d1.foo(); |
| 38 |
printf("%d\n", l); |
| 39 |
assert(l == 3); |
| 40 |
|
| 41 |
D d2 = cast(D) b; |
| 42 |
int m = d2.foo(); |
| 43 |
printf("%d\n", m); |
| 44 |
assert(m == 3); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/*******************************************/ |
| 49 |
|
| 50 |
interface D2 { |
| 51 |
int foo(); |
| 52 |
} |
| 53 |
|
| 54 |
class A2 : D2 { |
| 55 |
int foo() { printf("A2\n"); return 1; } |
| 56 |
} |
| 57 |
|
| 58 |
class B2 : A2 { |
| 59 |
override int foo() { printf("B2\n"); return 2; } |
| 60 |
} |
| 61 |
|
| 62 |
class C2 : B2, D2 { |
| 63 |
override int foo() { printf("C2\n"); return 3; } |
| 64 |
} |
| 65 |
|
| 66 |
void test2() |
| 67 |
{ |
| 68 |
int i; |
| 69 |
|
| 70 |
C2 c = new C2; |
| 71 |
D2 d = cast(D2)c; |
| 72 |
i = c.foo(); |
| 73 |
assert(i == 3); |
| 74 |
|
| 75 |
i = d.foo(); |
| 76 |
assert(i == 3); |
| 77 |
|
| 78 |
B2 b = new B2; |
| 79 |
if (cast(D2) b) |
| 80 |
{ |
| 81 |
D2 e = cast(D2) b; |
| 82 |
i = e.foo(); |
| 83 |
assert(i == 2); |
| 84 |
} |
| 85 |
else |
| 86 |
assert(0); |
| 87 |
|
| 88 |
A2 a; |
| 89 |
if (cast(D2) a) |
| 90 |
assert(0); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/*******************************************/ |
| 95 |
|
| 96 |
interface C3 |
| 97 |
{ |
| 98 |
int doLayout(); |
| 99 |
} |
| 100 |
|
| 101 |
class A3 |
| 102 |
{ |
| 103 |
void print() { printf("A3::print\n"); } |
| 104 |
} |
| 105 |
|
| 106 |
class B3 : A3, C3 |
| 107 |
{ |
| 108 |
int doLayout() { printf( "B3::doLayout\n" ); return 17; } |
| 109 |
} |
| 110 |
|
| 111 |
void callLayout(A3 b) |
| 112 |
{ |
| 113 |
printf("b = %p\n", b); |
| 114 |
C3 cl = cast(C3)b; |
| 115 |
printf("cl = %p\n", cl); |
| 116 |
if (cl) |
| 117 |
{ int i; |
| 118 |
|
| 119 |
i = cl.doLayout(); |
| 120 |
assert(i == 17); |
| 121 |
} |
| 122 |
else |
| 123 |
{ printf("the 'A3' you passed did not implement 'C3'\n" ); |
| 124 |
assert(0); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void test3() |
| 129 |
{ |
| 130 |
callLayout(new B3()); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
/*******************************************/ |
| 135 |
|
| 136 |
|
| 137 |
template IContainer(T) |
| 138 |
{ |
| 139 |
interface IContainer |
| 140 |
{ |
| 141 |
alias Container!(int) selected_type; |
| 142 |
bool isEmpty(); |
| 143 |
int enumerate(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
template Container(T) |
| 148 |
{ |
| 149 |
class Container : IContainer!(int) |
| 150 |
{ |
| 151 |
bool isEmpty() { return false; } |
| 152 |
int enumerate() { return 3; } |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void Vector_test_IContainer_int() |
| 157 |
{ |
| 158 |
alias IContainer!(int) icontainer_t; |
| 159 |
} |
| 160 |
|
| 161 |
void test4() |
| 162 |
{ |
| 163 |
} |
| 164 |
|
| 165 |
/*******************************************/ |
| 166 |
|
| 167 |
|
| 168 |
interface Italy(T) |
| 169 |
{ |
| 170 |
alias Foo5!(int) selected_type; |
| 171 |
bool isempty(); |
| 172 |
int enumerate(); |
| 173 |
} |
| 174 |
|
| 175 |
class Foo5(T) : Italy!(int) |
| 176 |
{ |
| 177 |
bool isempty() { return false; } |
| 178 |
int enumerate() { return 3; } |
| 179 |
} |
| 180 |
|
| 181 |
void test5() |
| 182 |
{ |
| 183 |
alias Italy!(int) Ic; |
| 184 |
Foo5!(int) f = new Foo5!(int); |
| 185 |
Ic i = cast(Ic)f; |
| 186 |
assert(i.isempty() == false); |
| 187 |
assert(i.enumerate() == 3); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/*******************************************/ |
| 192 |
|
| 193 |
int main (char[][] args) |
| 194 |
{ |
| 195 |
test1(); |
| 196 |
test2(); |
| 197 |
test3(); |
| 198 |
test4(); |
| 199 |
test5(); |
| 200 |
|
| 201 |
printf("Success\n"); |
| 202 |
return 0; |
| 203 |
} |