From 432ba90f48ef33fc168b2b92f3f5e5593dce9ef6 Mon Sep 17 00:00:00 2001 From: Bryce McKinlay Date: Wed, 16 Feb 2000 05:13:46 +0000 Subject: [PATCH] InterfaceDispatch.java: New file. 2000-02-16 Bryce McKinlay * libjava.lang/InterfaceDispatch.java: New file. * libjava.lang/InterfaceDispatch.out: New file. From-SVN: r32001 --- libjava/testsuite/ChangeLog | 5 + .../libjava.lang/InterfaceDispatch.java | 403 ++++++++++++++++++ .../libjava.lang/InterfaceDispatch.out | 27 ++ 3 files changed, 435 insertions(+) create mode 100644 libjava/testsuite/libjava.lang/InterfaceDispatch.java create mode 100644 libjava/testsuite/libjava.lang/InterfaceDispatch.out diff --git a/libjava/testsuite/ChangeLog b/libjava/testsuite/ChangeLog index c162f7c00a1..4d8e534ac14 100644 --- a/libjava/testsuite/ChangeLog +++ b/libjava/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2000-02-16 Bryce McKinlay + + * libjava.lang/InterfaceDispatch.java: New file. + * libjava.lang/InterfaceDispatch.out: New file. + 2000-02-15 Tom Tromey * libjava.jni/virtual.java: New file. diff --git a/libjava/testsuite/libjava.lang/InterfaceDispatch.java b/libjava/testsuite/libjava.lang/InterfaceDispatch.java new file mode 100644 index 00000000000..17f5c46946f --- /dev/null +++ b/libjava/testsuite/libjava.lang/InterfaceDispatch.java @@ -0,0 +1,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"); + } + } +} diff --git a/libjava/testsuite/libjava.lang/InterfaceDispatch.out b/libjava/testsuite/libjava.lang/InterfaceDispatch.out new file mode 100644 index 00000000000..1582e3afa04 --- /dev/null +++ b/libjava/testsuite/libjava.lang/InterfaceDispatch.out @@ -0,0 +1,27 @@ +ok 1 +ok 2a +ok 2b +ok 3 +ok 4 +ok 5 +ok 6 +ok 7 +ok 8 +ok 9 +ok 10 +ok 11 +ok 12 +ok 13 +ok 14 +ok 15 +ok 16 +ok 17 +ok 18 +ok 19 +ok 20 +ok 21 +ok 22 +ok 23 +ok 24 +ok 25 +ok 26 -- 2.30.2