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
|
extern(C) int printf(const char*, ...);
/*******************************************/
interface D {
int foo();
}
class A : D {
int foo() { return 1; }
}
class B : A, D
{
override int foo() { return 2; }
}
class C : B, D {
override int foo() { return 3; }
}
void test1()
{
C c = new C;
A a = cast(A) c;
int j = a.foo();
printf("%d\n", j);
assert(j == 3);
B b = cast(B) c;
int k = b.foo();
printf("%d\n", k);
assert(k == 3);
D d1 = cast(D) c;
int l = d1.foo();
printf("%d\n", l);
assert(l == 3);
D d2 = cast(D) b;
int m = d2.foo();
printf("%d\n", m);
assert(m == 3);
}
/*******************************************/
interface D2 {
int foo();
}
class A2 : D2 {
int foo() { printf("A2\n"); return 1; }
}
class B2 : A2 {
override int foo() { printf("B2\n"); return 2; }
}
class C2 : B2, D2 {
override int foo() { printf("C2\n"); return 3; }
}
void test2()
{
int i;
C2 c = new C2;
D2 d = cast(D2)c;
i = c.foo();
assert(i == 3);
i = d.foo();
assert(i == 3);
B2 b = new B2;
if (cast(D2) b)
{
D2 e = cast(D2) b;
i = e.foo();
assert(i == 2);
}
else
assert(0);
A2 a;
if (cast(D2) a)
assert(0);
}
/*******************************************/
interface C3
{
int doLayout();
}
class A3
{
void print() { printf("A3::print\n"); }
}
class B3 : A3, C3
{
int doLayout() { printf( "B3::doLayout\n" ); return 17; }
}
void callLayout(A3 b)
{
printf("b = %p\n", b);
C3 cl = cast(C3)b;
printf("cl = %p\n", cl);
if (cl)
{ int i;
i = cl.doLayout();
assert(i == 17);
}
else
{ printf("the 'A3' you passed did not implement 'C3'\n" );
assert(0);
}
}
void test3()
{
callLayout(new B3());
}
/*******************************************/
template IContainer(T)
{
interface IContainer
{
alias Container!(int) selected_type;
bool isEmpty();
int enumerate();
}
}
template Container(T)
{
class Container : IContainer!(int)
{
bool isEmpty() { return false; }
int enumerate() { return 3; }
}
}
void Vector_test_IContainer_int()
{
alias IContainer!(int) icontainer_t;
}
void test4()
{
}
/*******************************************/
interface Italy(T)
{
alias Foo5!(int) selected_type;
bool isempty();
int enumerate();
}
class Foo5(T) : Italy!(int)
{
bool isempty() { return false; }
int enumerate() { return 3; }
}
void test5()
{
alias Italy!(int) Ic;
Foo5!(int) f = new Foo5!(int);
Ic i = cast(Ic)f;
assert(i.isempty() == false);
assert(i.enumerate() == 3);
}
/*******************************************/
int main (char[][] args)
{
test1();
test2();
test3();
test4();
test5();
printf("Success\n");
return 0;
}
|