import source from 1.3.40
[external/swig.git] / Examples / test-suite / java / virtual_poly_runme.java
1 // virtual_poly test
2
3 import virtual_poly.*;
4
5 public class virtual_poly_runme {
6
7   static {
8     try {
9         System.loadLibrary("virtual_poly");
10     } catch (UnsatisfiedLinkError e) {
11       System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
12       System.exit(1);
13     }
14   }
15
16   public static void main(String argv[]) {
17
18     NDouble d = new NDouble(3.5);
19     NInt i = new NInt(2);
20
21     //
22     // These two natural 'copy' forms fail because no covariant (polymorphic) return types 
23     // are supported in Java. 
24     //
25     // NDouble dc = d.copy();
26     // NInt ic = i.copy();
27
28     //
29     // Unlike C++, we have to downcast instead.
30     //
31     NDouble dc = (NDouble)d.copy();
32     NInt ic = (NInt)i.copy();
33
34     NDouble ddc = NDouble.narrow(dc);
35     NInt dic = NInt.narrow(ic);
36
37     virtual_poly.incr(ic);
38     if ( (i.get() + 1) != ic.get() )
39       throw new RuntimeException("incr test failed");
40
41     //
42     // Checking a pure user downcast
43     //
44     NNumber n1 = d.copy();
45     NNumber n2 = d.nnumber();
46     NDouble dn1 = NDouble.narrow(n1);
47     NDouble dn2 = NDouble.narrow(n2);
48
49     if ( (dn1.get()) != dn2.get() )
50       throw new RuntimeException("copy/narrow test failed");
51
52     //
53     // Checking the ref polymorphic case
54     //
55     NNumber nr = d.ref_this();
56     NDouble dr1 = NDouble.narrow(nr);
57     NDouble dr2 = (NDouble)d.ref_this();
58     if ( dr1.get() != dr2.get() )
59       throw new RuntimeException("copy/narrow test failed");
60   }
61 }