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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/* Written by Artur Biesiadowski <abies@pg.gda.pl> */
/*
This class test basic 4 conversion types and compares results to ready ones, done
on sure VM (suns JDK). Conversions are
(obj instanceof clazz)
(clazz)obj
clazz.isInstance(obj)
clazz1.isAssignableFrom(clazz2);
Hopefully all needed cases are covered. If you want to add object just put it
into objs table. If you want to add class, you need to add it to both cls and to
testCode method. Of course you need to regenerate results after that.
*/
/*
You can copy/modify/use this file for any purposes, as long as you do not delete
my name from top of that file. Of course you can add your own below that :)
*/
import java.io.*;
interface I1 {}
interface I2 {}
interface I3 extends I2{}
class A1 implements I1 {}
class AB12 extends A1 implements I2 {}
class ABC12 extends AB12 {}
class D3 implements I3 {}
public class TestCasts
{
public Object objs[] =
{
null,
new Object(),
new A1(),
new AB12(),
new ABC12(),
new D3(),
new A1[1],
new AB12[1],
new ABC12[1],
new D3[1],
new I1[1],
new I2[1],
new I3[1],
new int[1],
new A1[1][1],
new AB12[1][1],
new I1[1][1]
};
public Class cls[] =
{
Object.class,
A1.class,
AB12.class,
ABC12.class,
D3.class,
I1.class,
I2.class,
I3.class,
Cloneable.class,
Serializable.class,
A1[].class,
AB12[].class,
ABC12[].class,
D3[].class,
I1[].class,
I2[].class,
I3[].class,
int[].class,
A1[][].class,
AB12[][].class,
I1[][].class
};
java.util.Vector results = new java.util.Vector(1000);
boolean verbose = false;
boolean generate = false;
String filename = "TestCasts-results.txt";
public static void main(String argv[] )
{
TestCasts tc = new TestCasts();
if ( argv.length > 0 )
{
int i;
for ( i =0; i < argv.length;i++ )
{
if ( argv[i].equals("-g") )
{
tc.generate = true;
}
else if ( argv[i].equals("-v") )
{
tc.verbose = true;
}
else if ( argv[i].equals("-f") )
{
i++;
if ( i > argv.length )
{
System.out.println("You need to specify filename after -f");
System.exit(1);
}
tc.filename = argv[i];
}
else
{
System.out.println( "Options are: -v -g -f file");
System.out.println( "[-v] verbose ");
System.out.println( "[-g] generate result table");
System.out.println( "[-f file] read/write tests from/to file (default "+tc.filename+")");
System.exit(1);
}
}
}
tc.test();
//System.out.println(tc.results);
System.out.println( "Performed " + tc.counter + " tests");
if ( tc.generate )
System.out.println( "True: " + tc.genTrue + "\tfalse: " + tc.genFalse);
else
{
System.out.println( "Passed: " + tc.passed + "\tfailed: " + tc.failed);
if (tc.failed == 0 )
System.out.println("PASSED: all cast tests");
}
}
public final void test()
{
if (!generate)
readResultsFromFile();
int i;
int j;
for ( i=0; i < objs.length; i++ )
{
for ( j=0; j < cls.length; j++ )
{
reportClIsInst(objs[i], cls[j], cls[j].isInstance(objs[i]) );
}
}
for (i=0; i < objs.length; i++ )
{
testCode(objs[i]);
}
for ( i=0; i < cls.length; i++ )
{
for ( j=0; j < cls.length; j++ )
{
reportClIsAssign(cls[i], cls[j], cls[i].isAssignableFrom(cls[j]));
}
}
if ( generate )
writeResultsToFile();
}
public final void testCode(Object o)
{
reportInstanceof(o, Object.class, (o instanceof Object) );
try
{
Object r1 = (Object) o;
reportCast(o, Object.class, true );
} catch (ClassCastException e) {
reportCast(o,Object.class, false );
}
reportInstanceof(o, A1.class, (o instanceof A1) );
try
{
A1 r1 = (A1) o;
reportCast(o, A1.class, true );
} catch (ClassCastException e) {
reportCast(o,A1.class, false );
}
reportInstanceof(o, AB12.class, (o instanceof AB12) );
try
{
AB12 r1 = (AB12) o;
reportCast(o, AB12.class, true );
} catch (ClassCastException e) {
reportCast(o,AB12.class, false );
}
reportInstanceof(o, ABC12.class, (o instanceof ABC12) );
try
{
ABC12 r1 = (ABC12) o;
reportCast(o, ABC12.class, true );
} catch (ClassCastException e) {
reportCast(o,ABC12.class, false );
}
reportInstanceof(o, D3.class, (o instanceof D3) );
try
{
D3 r1 = (D3) o;
reportCast(o, D3.class, true );
} catch (ClassCastException e) {
reportCast(o,D3.class, false );
}
reportInstanceof(o, I1.class, (o instanceof I1) );
try
{
I1 r1 = (I1) o;
reportCast(o, I1.class, true );
} catch (ClassCastException e) {
reportCast(o,I1.class, false );
}
reportInstanceof(o, I2.class, (o instanceof I2) );
try
{
I2 r1 = (I2) o;
reportCast(o, I2.class, true );
} catch (ClassCastException e) {
reportCast(o,I2.class, false );
}
reportInstanceof(o, I3.class, (o instanceof I3) );
try
{
I3 r1 = (I3) o;
reportCast(o, I3.class, true );
} catch (ClassCastException e) {
reportCast(o,I3.class, false );
}
reportInstanceof(o, Cloneable.class, (o instanceof Cloneable) );
try
{
Cloneable r1 = (Cloneable) o;
reportCast(o, Cloneable.class, true );
} catch (ClassCastException e) {
reportCast(o,Cloneable.class, false );
}
reportInstanceof(o, Serializable.class, (o instanceof Serializable) );
try
{
Serializable r1 = (Serializable) o;
reportCast(o, Serializable.class, true );
} catch (ClassCastException e) {
reportCast(o,Serializable.class, false );
}
reportInstanceof(o, A1[].class, (o instanceof A1[]) );
try
{
A1[] r1 = (A1[]) o;
reportCast(o, A1[].class, true );
} catch (ClassCastException e) {
reportCast(o,A1[].class, false );
}
reportInstanceof(o, AB12[].class, (o instanceof AB12[]) );
try
{
AB12[] r1 = (AB12[]) o;
reportCast(o, AB12[].class, true );
} catch (ClassCastException e) {
reportCast(o,AB12[].class, false );
}
reportInstanceof(o, ABC12[].class, (o instanceof ABC12[]) );
try
{
ABC12[] r1 = (ABC12[]) o;
reportCast(o, ABC12[].class, true );
} catch (ClassCastException e) {
reportCast(o,ABC12[].class, false );
}
reportInstanceof(o, D3[].class, (o instanceof D3[]) );
try
{
D3[] r1 = (D3[]) o;
reportCast(o, D3[].class, true );
} catch (ClassCastException e) {
reportCast(o,D3[].class, false );
}
reportInstanceof(o, I1[].class, (o instanceof I1[]) );
try
{
I1[] r1 = (I1[]) o;
reportCast(o, I1[].class, true );
} catch (ClassCastException e) {
reportCast(o,I1[].class, false );
}
reportInstanceof(o, I2[].class, (o instanceof I2[]) );
try
{
I2[] r1 = (I2[]) o;
reportCast(o, I2[].class, true );
} catch (ClassCastException e) {
reportCast(o,I2[].class, false );
}
reportInstanceof(o, I3[].class, (o instanceof I3[]) );
try
{
I3[] r1 = (I3[]) o;
reportCast(o, I3[].class, true );
} catch (ClassCastException e) {
reportCast(o,I3[].class, false );
}
reportInstanceof(o, int[].class, (o instanceof int[]) );
try
{
int[] r1 = (int[]) o;
reportCast(o, int[].class, true );
} catch (ClassCastException e) {
reportCast(o,int[].class, false );
}
reportInstanceof(o, A1[][].class, (o instanceof A1[][]) );
try
{
A1[][] r1 = (A1[][]) o;
reportCast(o, A1[][].class, true );
} catch (ClassCastException e) {
reportCast(o,A1[][].class, false );
}
reportInstanceof(o, AB12[][].class, (o instanceof AB12[][]) );
try
{
AB12[][] r1 = (AB12[][]) o;
reportCast(o, AB12[][].class, true );
} catch (ClassCastException e) {
reportCast(o,AB12[][].class, false );
}
reportInstanceof(o, I1[][].class, (o instanceof I1[][]) );
try
{
I1[][] r1 = (I1[][]) o;
reportCast(o, I1[][].class, true );
} catch (ClassCastException e) {
reportCast(o,I1[][].class, false );
}
}
int counter = 0;
int passed = 0;
int failed = 0;
int genTrue = 0;
int genFalse =0;
public final boolean result(boolean b )
{
counter++;
if ( generate )
{
if (b )
{
genTrue++;
results.addElement(Boolean.TRUE);
}
else
{
genFalse++;
results.addElement(Boolean.FALSE);
}
return true;
}
else
{
if ( ((Boolean)results.elementAt(counter-1)).booleanValue() != b )
{
failed++;
return false;
}
else
{
passed++;
return true;
}
}
}
public final void reportClIsInst(Object obj, Class cl, boolean b )
{
if ( result(b) )
{
if ( verbose )
System.out.println("PASSED: "+obj +"\tis\t"+ cl + "\t?" + b);
}
else
{
System.out.println("FAILED: " + cl + ".isInstance(" + obj + ") is\t" + b );
}
}
public final void reportClIsAssign( Class c1, Class c2, boolean b )
{
if ( result(b) )
{
if (verbose)
System.out.println("PASSED: "+c1 + "\tisAssignableFrom\t" + c2 + "\t?\t" + b);
}
else
{
System.out.println("FAILED: " + c1 + ".isAssigableFrom(" + c2 + ") is " + b);
}
}
public final void reportInstanceof( Object obj, Class cl, boolean b )
{
if ( result(b) )
{
if ( verbose )
System.out.println("PASSED: "+obj +"\tinstanceof\t"+ cl + "\t?" + b);
}
else
{
System.out.println("FAILED: (" + obj + "instanceof\t" + cl + ")\tis\t" + b );
}
}
public final void reportCast( Object obj, Class cl, boolean b )
{
if ( result(b) )
{
if ( verbose )
System.out.println("PASSED: "+obj +"\tcastto \t"+ cl + "\t?" + b);
}
else
{
System.out.println("FAILED: " + obj + "\tcastto \t" + cl + "\tis\t" + b );
}
}
public final void readResultsFromFile()
{
try{
int i;
FileInputStream fin = new FileInputStream(filename);
while ( (i=fin.read()) != -1 )
{
results.addElement( i==1 ? Boolean.TRUE : Boolean.FALSE );
}
} catch (IOException e )
{
System.out.println("Cannot read from file " + filename);
System.out.println(e);
System.exit(1);
}
}
public final void writeResultsToFile()
{
try{
int i;
FileOutputStream fos = new FileOutputStream(filename);
for ( i=0; i < counter; i++ )
{
fos.write( ((Boolean)results.elementAt(i)).booleanValue() ? 1 : 0 );
}
fos.close();
} catch (IOException e )
{
System.out.println("Cannot read from file " + filename);
System.out.println(e);
System.exit(1);
}
}
}
|