summaryrefslogtreecommitdiff
path: root/libjava/testsuite/libjava.lang/InterfaceDispatch.java
blob: 17f5c46946fe032c34432c6fd9485c5ad1eef957 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Test interface dispatch, type checking (instanceof), and casting. */

interface IA
{
  String a();
}

interface IB extends IA
{
  String b();
}

interface IC extends IB
{
  void c();
  int d();
  IB e(int i);
}

interface ID
{
  String z();
  String a();  
}

class CA
{
  String a()
  {
    return "CA a()";
  }
}

class CB implements IB
{
  public String a()
  {
    return "CB a()";
  }
  
  public String b()
  {
    return "CB b()";  
  }
}

class CC extends CB
{
  public int d()
  {
    return 99;
  }
}

class CD extends CC implements IC
{
  public String a()
  {
    return "CD a()";
  }
  
  public void c()
  {
    System.out.println("CD c()");
  }
  
  public int d()
  {
    return 6;
  }
  
  public IB e(int i)
  {
    if (i == 1)
      return new CB();
    else
      return new CD();
  }
}

class CE extends CB implements IB, ID
{
  public String a()
  {
    return ("CE a()");
  }
  
  public String b()
  {
    return ("CE b()");
  }
    
  public String z()
  {
    return("CE z()");  
  }
}


public class InterfaceDispatch
{
  public static void main(String args[])
  {
    new InterfaceDispatch();
  }
  
  public InterfaceDispatch()
  {
    /* _Jv_InstanceOf */
    
    /* Object instanceof CLASS */
    Object obj = new CA();
    
    if (obj instanceof CA)
      {
        System.out.println ("ok 1");
      }
    else
      {
        System.out.println ("FAIL 1");
      }

    obj = new CD();
    
    if (!(obj instanceof CA))
      {
        System.out.println ("ok 2a");
      }
    else
      {
        System.out.println ("FAIL 2a");
      }

    if (obj instanceof CB)
      {
        System.out.println ("ok 2b");
      }
    else
      {
        System.out.println ("FAIL 2b");
      }

    
    /* Object instanceof INTERFACE */
    obj = new CB();
    
    if (!(obj instanceof IC))
      {
        System.out.println("ok 3");
      }
    else
      {
        System.out.println ("FAIL 3");
      }
    
    if (obj instanceof IB)
      {
        System.out.println("ok 4");
      }
    else
      {
        System.out.println ("FAIL 4");
      }
    
    /* InterfaceRef instanceof INTERFACE */
    
    IA ia = new CB();
    
    if (ia instanceof IB)
      {
        System.out.println("ok 5");
      }
    else
      {
        System.out.println ("FAIL 5");
      }
    
    
    if (!(ia instanceof IC))
      {
        System.out.println("ok 6");
      }
    else
      {
        System.out.println ("FAIL 6");
      }
      
    /* InterfaceRef instanceof CLASS */
    
    if (ia instanceof CB)
      {
        System.out.println("ok 7");
      }
    else
      {
        System.out.println ("FAIL 7");
      }
      
    
    if (!(ia instanceof CD))
      {
        System.out.println("ok 8");
      }
    else
      {
        System.out.println ("FAIL 8");
      }    
    
    
    /* _Jv_CheckCast */
    Object obj_ca = new CA();
    Object obj_cc = new CC();    
    
    IA ia2;
    
    try
      {
        ia2 = (IA) obj_cc;
        System.out.println("ok 9");
      }
    catch (ClassCastException x)
      {
        System.out.println("FAIL 9");
      }
    
    CD cd;
    
    try
      {
        cd = (CD) obj_ca;
        System.out.println("FAIL 10");
      }
    catch (ClassCastException x)
      {
        System.out.println("ok 10");
      }
    
    IA ia3;
    
    try
      {
        ia3 = (IB) obj_ca;
        System.out.println("FAIL 11");
      }
    catch (ClassCastException x)
      {
        System.out.println("ok 11");
      }
      
    /* _Jv_LookupInterfaceMethod */
    Object obj_cb = new CB();
    
    IB ib = (IB) obj_cb;
    ib.b();
    if (ib.a().equalsIgnoreCase("CB a()"))
      System.out.println("ok 12");
    else
      System.out.println("FAIL 12");
      
    IC ic = new CD();
    if (ic.a().equalsIgnoreCase("CD a()"))
      System.out.println("ok 13");
    else
      System.out.println("FAIL 13");
          
    if (ic.d() == 6)
      System.out.println("ok 14");
    else
      System.out.println("FAIL 14");
      
    Object ce = new CE();
    
    ib = (IB) ce;
    ID id = (ID) ce;
    
    if (ib.b().equals("CE b()") && id.a().equals("CE a()"))
      System.out.println("ok 15");
    else
      System.out.println("FAIL 15");
    
    String t = ((ID)ce).z();
    
    if (t.equalsIgnoreCase("CE z()"))
      System.out.println("ok 16");
    else
      System.out.println("FAIL 16");
      
    /* Array types */
    
    Object[] obj_a = new CC[10];
    try
      {
        CB[] ca_a = (CB[]) obj_a;
        System.out.println("ok 17");
      }
    catch (ClassCastException x)
      {
        System.out.println("FAIL 17");
      }
    
    if (obj_a instanceof IB[])
      {
        System.out.println("ok 18");      
      }
    else
      {
        System.out.println("FAIL 18");      
      }
    
    IB[] ib_a = new CD[5];
    try 
      {
        CD[] cd_a = (CD[]) ib_a;
        System.out.println("ok 19");
      }
    catch (ClassCastException x)
      {
        System.out.println("FAIL 19");
      }
      
    CA[] ca_a;

    try 
      {
        ca_a = (CA[]) ib_a;
        System.out.println("FAIL 20");
      }
    catch (ClassCastException x)
      {
        System.out.println("ok 20");
      }

    
    /* Primitive types */
    
    short[] short_a = new short[100];
    
    try
      {
        obj = short_a;
        System.out.println("ok 21");
      }
    catch (ClassCastException x)
      {
        System.out.println("FAIL 21");      
      }

    try
      {
        short[] short_b = (short[]) obj;
        System.out.println("ok 22");
      }
    catch (ClassCastException x)
      {
        System.out.println("FAIL 22");      
      }

    int[] short_b;

    try
      {
        short_b = (int[]) obj;
        System.out.println("FAIL 23");
      }
    catch (ClassCastException x)
      {
        System.out.println("ok 23");      
      }

    Object obj1 = new int[25];
    
    if (obj1 instanceof short[])
      {
        System.out.println("FAIL 24");      
      }
    else
      {
        System.out.println("ok 24");
      }
    
    if (obj1 instanceof int[])
      {
        System.out.println("ok 25");
      }
    else
      {
        System.out.println("FAIL 25");
      }
      
    /* null assignment */
    
    CA obj_ca2 = null;
    
    if (obj_ca2 instanceof CA)
      {
        System.out.println("FAIL 26");        
      }
    else
      {
        System.out.println("ok 26");
      }
  }  
}