context: // **********************************************************************
  //
  // Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
  //
  // This copy of Ice is licensed to you under the terms described in the
  // ICE_LICENSE file included in this distribution.
  //
  // **********************************************************************
  
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Threading;
  using Test;
  
  #if SILVERLIGHT
  using System.Windows.Controls;
  #endif
  
  public class AllTests : TestCommon.TestApp
  {
      private class Callback
      {
          internal Callback()
          {
              _called = false;
          }
  
          public virtual void check()
          {
              _m.Lock();
              try
              {
                  while(!_called)
                  {
                      _m.Wait();
                  }
  
                  _called = false;
              }
              finally
              {
                  _m.Unlock();
              }
          }
  
          public virtual void called()
          {
              _m.Lock();
              try
              {
                  Debug.Assert(!_called);
                  _called = true;
                  _m.Notify();
              }
              finally
              {
                  _m.Unlock();
              }
          }
  
          private bool _called;
          private readonly IceUtilInternal.Monitor _m = new IceUtilInternal.Monitor();
      }
  
      private class AsyncCallback
      {
          public void response_SBaseAsObject(Ice.Object o)
          {
              AllTests.test(o != null);
              AllTests.test(o.ice_id().Equals("::Test::SBase"));
              SBase sb = (SBase) o;
              AllTests.test(sb != null);
              AllTests.test(sb.sb.Equals("SBase.sb"));
              callback.called();
          }
  
          public void response_SBaseAsSBase(SBase sb)
          {
              AllTests.test(sb.sb.Equals("SBase.sb"));
              callback.called();
          }
  
          public void response_SBSKnownDerivedAsSBase(SBase sb)
          {
              AllTests.test(sb.sb.Equals("SBSKnownDerived.sb"));
              SBSKnownDerived sbskd = (SBSKnownDerived) sb;
              AllTests.test(sbskd != null);
              AllTests.test(sbskd.sbskd.Equals("SBSKnownDerived.sbskd"));
              callback.called();
          }
  
          public void response_SBSKnownDerivedAsSBSKnownDerived(SBSKnownDerived sbskd)
          {
              AllTests.test(sbskd.sbskd.Equals("SBSKnownDerived.sbskd"));
              callback.called();
          }
  
          public void response_SBSUnknownDerivedAsSBase(SBase sb)
          {
              AllTests.test(sb.sb.Equals("SBSUnknownDerived.sb"));
              callback.called();
          }
  
          public void response_SBSUnknownDerivedAsSBaseCompact(SBase sb)
          {
              AllTests.test(false);
          }
  
          public void exception_SBSUnknownDerivedAsSBaseCompact(Ice.Exception ex)
          {
              AllTests.test(ex is Ice.NoObjectFactoryException);
              callback.called();
          }
  
          public void response_SUnknownAsObject1(Ice.Object o)
          {
              AllTests.test(false);
          }
  
          public void exception_SUnknownAsObject1(Ice.Exception exc)
          {
              AllTests.test(exc.GetType().FullName.Equals("Ice.NoObjectFactoryException"));
              callback.called();
          }
  
          public void response_SUnknownAsObject2(Ice.Object o)
          {
              AllTests.test(o is Ice.UnknownSlicedObject);
              AllTests.test((o as Ice.UnknownSlicedObject).getUnknownTypeId().Equals("::Test::SUnknown"));
              callback.called();
          }
  
          public void exception_SUnknownAsObject2(Ice.Exception exc)
          {
              AllTests.test(false);
          }
  
          public void response_oneElementCycle(B b)
          {
              AllTests.test(b != null);
              AllTests.test(b.ice_id().Equals("::Test::B"));
              AllTests.test(b.sb.Equals("B1.sb"));
              AllTests.test(b.pb == b);
              callback.called();
          }
  
          public void response_twoElementCycle(B b1)
          {
              AllTests.test(b1 != null);
              AllTests.test(b1.ice_id().Equals("::Test::B"));
              AllTests.test(b1.sb.Equals("B1.sb"));
  
              B b2 = b1.pb;
              AllTests.test(b2 != null);
              AllTests.test(b2.ice_id().Equals("::Test::B"));
              AllTests.test(b2.sb.Equals("B2.sb"));
              AllTests.test(b2.pb == b1);
              callback.called();
          }
  
          public void response_D1AsB(B b1)
          {
              AllTests.test(b1 != null);
              AllTests.test(b1.ice_id().Equals("::Test::D1"));
              AllTests.test(b1.sb.Equals("D1.sb"));
              AllTests.test(b1.pb != null);
              AllTests.test(b1.pb != b1);
              D1 d1 = (D1) b1;
              AllTests.test(d1 != null);
              AllTests.test(d1.sd1.Equals("D1.sd1"));
              AllTests.test(d1.pd1 != null);
              AllTests.test(d1.pd1 != b1);
              AllTests.test(b1.pb == d1.pd1);
  
              B b2 = b1.pb;
              AllTests.test(b2 != null);
              AllTests.test(b2.pb == b1);
              AllTests.test(b2.sb.Equals("D2.sb"));
              AllTests.test(b2.ice_id().Equals("::Test::B"));
              callback.called();
          }
  
          public void response_D1AsD1(D1 d1)
          {
              AllTests.test(d1 != null);
              AllTests.test(d1.ice_id().Equals("::Test::D1"));
              AllTests.test(d1.sb.Equals("D1.sb"));
              AllTests.test(d1.pb != null);
              AllTests.test(d1.pb != d1);
  
              B b2 = d1.pb;
              AllTests.test(b2 != null);
              AllTests.test(b2.ice_id().Equals("::Test::B"));
              AllTests.test(b2.sb.Equals("D2.sb"));
              AllTests.test(b2.pb == d1);
              callback.called();
          }
  
          public void response_D2AsB(B b2)
          {
              AllTests.test(b2 != null);
              AllTests.test(b2.ice_id().Equals("::Test::B"));
              AllTests.test(b2.sb.Equals("D2.sb"));
              AllTests.test(b2.pb != null);
              AllTests.test(b2.pb != b2);
  
              B b1 = b2.pb;
              AllTests.test(b1 != null);
              AllTests.test(b1.ice_id().Equals("::Test::D1"));
              AllTests.test(b1.sb.Equals("D1.sb"));
              AllTests.test(b1.pb == b2);
              D1 d1 = (D1) b1;
              AllTests.test(d1 != null);
              AllTests.test(d1.sd1.Equals("D1.sd1"));
              AllTests.test(d1.pd1 == b2);
              callback.called();
          }
  
          public void response_paramTest1(B b1, B b2)
          {
              AllTests.test(b1 != null);
              AllTests.test(b1.ice_id().Equals("::Test::D1"));
              AllTests.test(b1.sb.Equals("D1.sb"));
              AllTests.test(b1.pb == b2);
              D1 d1 = (D1) b1;
              AllTests.test(d1 != null);
              AllTests.test(d1.sd1.Equals("D1.sd1"));
              AllTests.test(d1.pd1 == b2);
  
              AllTests.test(b2 != null);
              AllTests.test(b2.ice_id().Equals("::Test::B")); // No factory, must be sliced
              AllTests.test(b2.sb.Equals("D2.sb"));
              AllTests.test(b2.pb == b1);
              callback.called();
          }
  
          public void response_paramTest2(B b2, B b1)
          {
              AllTests.test(b1 != null);
              AllTests.test(b1.ice_id().Equals("::Test::D1"));
              AllTests.test(b1.sb.Equals("D1.sb"));
              AllTests.test(b1.pb == b2);
              D1 d1 = (D1) b1;
              AllTests.test(d1 != null);
              AllTests.test(d1.sd1.Equals("D1.sd1"));
              AllTests.test(d1.pd1 == b2);
  
              AllTests.test(b2 != null);
              AllTests.test(b2.ice_id().Equals("::Test::B")); // No factory, must be sliced
              AllTests.test(b2.sb.Equals("D2.sb"));
              AllTests.test(b2.pb == b1);
              callback.called();
          }
  
          public void response_returnTest1(B r, B p1, B p2)
          {
              AllTests.test(r == p1);
              callback.called();
          }
  
          public void response_returnTest2(B r, B p1, B p2)
          {
              AllTests.test(r == p1);
              callback.called();
          }
  
          public void response_returnTest3(B b)
          {
              rb = b;
              callback.called();
          }
  
          public void response_paramTest3(B ret, B p1, B p2)
          {
              AllTests.test(p1 != null);
              AllTests.test(p1.sb.Equals("D2.sb (p1 1)"));
              AllTests.test(p1.pb == null);
              AllTests.test(p1.ice_id().Equals("::Test::B"));
  
              AllTests.test(p2 != null);
              AllTests.test(p2.sb.Equals("D2.sb (p2 1)"));
              AllTests.test(p2.pb == null);
              AllTests.test(p2.ice_id().Equals("::Test::B"));
  
              AllTests.test(ret != null);
              AllTests.test(ret.sb.Equals("D1.sb (p2 2)"));
              AllTests.test(ret.pb == null);
              AllTests.test(ret.ice_id().Equals("::Test::D1"));
              callback.called();
          }
  
          public void response_paramTest4(B ret, B b)
          {
              AllTests.test(b != null);
              AllTests.test(b.sb.Equals("D4.sb (1)"));
              AllTests.test(b.pb == null);
              AllTests.test(b.ice_id().Equals("::Test::B"));
  
              AllTests.test(ret != null);
              AllTests.test(ret.sb.Equals("B.sb (2)"));
              AllTests.test(ret.pb == null);
              AllTests.test(ret.ice_id().Equals("::Test::B"));
              callback.called();
          }
  
          public void response_sequenceTest(SS3 ss)
          {
              rss = ss;
              callback.called();
          }
  
          public void response_dictionaryTest(Dictionary<int, B> r, Dictionary<int, B> bout)
          {
              this.rbdict = (Dictionary<int, B>)r;
              this.obdict = (Dictionary<int, B>)bout;
              callback.called();
          }
  
          public void exception_throwBaseAsBase(Ice.Exception exc)
          {
              try
              {
                  BaseException e = (BaseException)exc;
                  AllTests.test(e.sbe.Equals("sbe"));
                  AllTests.test(e.pb != null);
                  AllTests.test(e.pb.sb.Equals("sb"));
                  AllTests.test(e.pb.pb == e.pb);
              }
              catch(Exception)
              {
                  AllTests.test(false);
              }
              callback.called();
          }
  
          public void exception_throwDerivedAsBase(Ice.Exception exc)
          {
              try
              {
                  DerivedException e = (DerivedException)exc;
                  AllTests.test(e.sbe.Equals("sbe"));
                  AllTests.test(e.pb != null);
                  AllTests.test(e.pb.sb.Equals("sb1"));
                  AllTests.test(e.pb.pb == e.pb);
                  AllTests.test(e.sde.Equals("sde1"));
                  AllTests.test(e.pd1 != null);
                  AllTests.test(e.pd1.sb.Equals("sb2"));
                  AllTests.test(e.pd1.pb == e.pd1);
                  AllTests.test(e.pd1.sd1.Equals("sd2"));
                  AllTests.test(e.pd1.pd1 == e.pd1);
              }
              catch(Exception)
              {
                  AllTests.test(false);
              }
              callback.called();
          }
  
          public void exception_throwDerivedAsDerived(Ice.Exception exc)
          {
              try
              {
                  DerivedException e = (DerivedException)exc;
                  AllTests.test(e.sbe.Equals("sbe"));
                  AllTests.test(e.pb != null);
                  AllTests.test(e.pb.sb.Equals("sb1"));
                  AllTests.test(e.pb.pb == e.pb);
                  AllTests.test(e.sde.Equals("sde1"));
                  AllTests.test(e.pd1 != null);
                  AllTests.test(e.pd1.sb.Equals("sb2"));
                  AllTests.test(e.pd1.pb == e.pd1);
                  AllTests.test(e.pd1.sd1.Equals("sd2"));
                  AllTests.test(e.pd1.pd1 == e.pd1);
              }
              catch(Exception)
              {
                  AllTests.test(false);
              }
              callback.called();
          }
  
          public void exception_throwUnknownDerivedAsBase(Ice.Exception exc)
          {
              try
              {
                  BaseException e = (BaseException)exc;
                  AllTests.test(e.sbe.Equals("sbe"));
                  AllTests.test(e.pb != null);
                  AllTests.test(e.pb.sb.Equals("sb d2"));
                  AllTests.test(e.pb.pb == e.pb);
              }
              catch(Exception)
              {
                  AllTests.test(false);
              }
              callback.called();
          }
  
          public void response_useForward(Forward f)
          {
              AllTests.test(f != null);
              callback.called();
          }
  
          public void response_exchangePBase1(PBase r)
          {
              PDerived p2 = (PDerived)r;
              AllTests.test(p2.pi == 3);
              AllTests.test(p2.ps.Equals("preserved"));
              AllTests.test(p2.pb == p2);
              callback.called();
          }
  
          public void response_exchangePBase2(PBase r)
          {
              AllTests.test(!(r is PCUnknown));
              AllTests.test(r.pi == 3);
              callback.called();
          }
  
          public void response_exchangePBase3(PBase r)
          {
              AllTests.test(!(r is PCDerived));
              AllTests.test(r.pi == 3);
              callback.called();
          }
  
          public void response_exchangePBase4(PBase r)
          {
              PCDerived p2 = r as PCDerived;
              AllTests.test(p2.pi == 3);
              AllTests.test(p2.pbs[0] == p2);
              callback.called();
          }
  
          public void response_exchangePBaseCompact1(PBase r)
          {
              AllTests.test(!(r is CompactPCDerived));
              AllTests.test(r.pi == 3);
              callback.called();
          }
  
          public void response_exchangePBaseCompact2(PBase r)
          {
              CompactPCDerived p2 = r as CompactPCDerived;
              AllTests.test(p2.pi == 3);
              AllTests.test(p2.pbs[0] == p2);
              callback.called();
          }
  
          public void response_exchangePBase5(PBase r)
          {
              AllTests.test(!(r is PCDerived3));
              AllTests.test(r is Preserved);
              AllTests.test(r.pi == 3);
              callback.called();
          }
  
          public void response_exchangePBase6(PBase r)
          {
              PCDerived3 p3 = r as PCDerived3;
              AllTests.test(p3.pi == 3);
              for(int i = 0; i < 300; ++i)
              {
                  PCDerived2 p2 = p3.pbs[i] as PCDerived2;
                  test(p2.pi == i);
                  test(p2.pbs.Length == 1);
                  test(p2.pbs[0] == null);
                  test(p2.pcd2 == i);
              }
              test(p3.pcd2 == p3.pi);
              test(p3.pcd3 == p3.pbs[10]);
              callback.called();
          }
  
          public void response()
          {
              AllTests.test(false);
          }
  
          public void exception(Ice.Exception exc)
          {
              if(exc is Ice.OperationNotExistException)
              {
                  callback.called();
                  return;
              }
              AllTests.test(false);
          }
  
          public virtual void check()
          {
              callback.check();
          }
  
          public B rb;
          public SS3 rss;
          public Dictionary<int, B> rbdict;
          public Dictionary<int, B> obdict;
  
          private Callback callback = new Callback();
      }
  
      private class PNodeI : PNode
      {
          public PNodeI()
          {
              ++counter;
          }
  
          internal static int counter = 0;
      }
  
      private class NodeFactoryI : Ice.ObjectFactory
      {
          public Ice.Object create(string id)
          {
              if(id.Equals(PNode.ice_staticId()))
              {
                  return new PNodeI();
              }
              return null;
          }
  
          public void destroy()
          {
          }
      }
  
      private class PreservedI : Preserved
      {
          public PreservedI()
          {
              ++counter;
          }
  
          internal static int counter = 0;
      }
  
      private class PreservedFactoryI : Ice.ObjectFactory
      {
          public Ice.Object create(string id)
          {
              if(id.Equals(Preserved.ice_staticId()))
              {
                  return new PreservedI();
              }
              return null;
          }
  
          public void destroy()
          {
          }
      }
  
  #if SILVERLIGHT
      public override Ice.InitializationData initData()
      {
          Ice.InitializationData initData = new Ice.InitializationData();
          initData.properties = Ice.Util.createProperties();
          initData.properties.setProperty("Ice.FactoryAssemblies", "objects,version=1.0.0.0");
          return initData;
      }
  
      override
      public void run(Ice.Communicator communicator)
  #else
      public static TestIntfPrx allTests(Ice.Communicator communicator, bool collocated)
  #endif
      {
          Write("testing stringToProxy... ");
          Flush();
          Ice.ObjectPrx basePrx = communicator.stringToProxy("Test:default -p 12010 -t 2000");
          test(basePrx != null);
          WriteLine("ok");
  
          Write("testing checked cast... ");
          Flush();
          TestIntfPrx testPrx = TestIntfPrxHelper.checkedCast(basePrx);
          test(testPrx != null);
          test(testPrx.Equals(basePrx));
          WriteLine("ok");
  
          Write("base as Object... ");
          Flush();
          {
              Ice.Object o;
              SBase sb = null;
              try
              {
                  o = testPrx.SBaseAsObject();
                  test(o != null);
                  test(o.ice_id().Equals("::Test::SBase"));
                  sb = (SBase) o;
              }
              catch(Exception)
              {
                  test(false);
              }
              test(sb != null);
              test(sb.sb.Equals("SBase.sb"));
          }
          WriteLine("ok");
  
          Write("base as Object (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBaseAsObject().whenCompleted(cb.response_SBaseAsObject, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("base as base... ");
          Flush();
          {
              SBase sb;
              try
              {
                  sb = testPrx.SBaseAsSBase();
                  test(sb.sb.Equals("SBase.sb"));
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("base as base (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBaseAsSBase().whenCompleted(cb.response_SBaseAsSBase, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("base with known derived as base... ");
          Flush();
          {
              SBase sb;
              SBSKnownDerived sbskd = null;
              try
              {
                  sb = testPrx.SBSKnownDerivedAsSBase();
                  test(sb.sb.Equals("SBSKnownDerived.sb"));
                  sbskd = (SBSKnownDerived) sb;
              }
              catch(Exception)
              {
                  test(false);
              }
              test(sbskd != null);
              test(sbskd.sbskd.Equals("SBSKnownDerived.sbskd"));
          }
          WriteLine("ok");
  
          Write("base with known derived as base (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBSKnownDerivedAsSBase().whenCompleted(cb.response_SBSKnownDerivedAsSBase, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("base with known derived as known derived... ");
          Flush();
          {
              SBSKnownDerived sbskd;
              try
              {
                  sbskd = testPrx.SBSKnownDerivedAsSBSKnownDerived();
                  test(sbskd.sbskd.Equals("SBSKnownDerived.sbskd"));
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("base with known derived as known derived (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBSKnownDerivedAsSBSKnownDerived().whenCompleted(
                          cb.response_SBSKnownDerivedAsSBSKnownDerived, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("base with unknown derived as base... ");
          Flush();
          {
              SBase sb;
              try
              {
                  sb = testPrx.SBSUnknownDerivedAsSBase();
                  test(sb.sb.Equals("SBSUnknownDerived.sb"));
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          if(testPrx.ice_getEncodingVersion().Equals(Ice.Util.Encoding_1_0))
          {
              try
              {
                  SBase sb = testPrx.SBSUnknownDerivedAsSBaseCompact();
                  test(sb.sb.Equals("SBSUnknownDerived.sb"));
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          else
          {
              try
              {
                  //
                  // This test fails when using the compact format because the instance cannot
                  // be sliced to a known type.
                  //
                  testPrx.SBSUnknownDerivedAsSBaseCompact();
                  test(false);
              }
              catch(Ice.NoObjectFactoryException)
              {
                  // Expected.
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("base with unknown derived as base (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBSUnknownDerivedAsSBase().whenCompleted(
                          cb.response_SBSUnknownDerivedAsSBase, cb.exception);
              cb.check();
          }
          if(testPrx.ice_getEncodingVersion().Equals(Ice.Util.Encoding_1_0))
          {
              //
              // This test succeeds for the 1.0 encoding.
              //
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBSUnknownDerivedAsSBaseCompact().whenCompleted(
                  cb.response_SBSUnknownDerivedAsSBase, cb.exception);
              cb.check();
          }
          else
          {
              //
              // This test fails when using the compact format because the instance cannot
              // be sliced to a known type.
              //
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_SBSUnknownDerivedAsSBaseCompact().whenCompleted(
                  cb.response_SBSUnknownDerivedAsSBaseCompact, cb.exception_SBSUnknownDerivedAsSBaseCompact);
              cb.check();
          }
          WriteLine("ok");
  
          Write("unknown with Object as Object... ");
          Flush();
          {
              try
              {
                  Ice.Object o = testPrx.SUnknownAsObject();
                  test(!testPrx.ice_getEncodingVersion().Equals(Ice.Util.Encoding_1_0));
                  test(o is Ice.UnknownSlicedObject);
                  test((o as Ice.UnknownSlicedObject).getUnknownTypeId().Equals("::Test::SUnknown"));
                  testPrx.checkSUnknown(o);
              }
              catch(Ice.NoObjectFactoryException)
              {
                  test(testPrx.ice_getEncodingVersion().Equals(Ice.Util.Encoding_1_0));
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("unknown with Object as Object (AMI)... ");
          Flush();
          {
              try
              {
                  AsyncCallback cb = new AsyncCallback();
                  if(testPrx.ice_getEncodingVersion().Equals(Ice.Util.Encoding_1_0))
                  {
                      testPrx.begin_SUnknownAsObject().whenCompleted(
                              cb.response_SUnknownAsObject1, cb.exception_SUnknownAsObject1);
                  }
                  else
                  {
                      testPrx.begin_SUnknownAsObject().whenCompleted(
                              cb.response_SUnknownAsObject2, cb.exception_SUnknownAsObject2);
                  }
                  cb.check();
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("one-element cycle... ");
          Flush();
          {
              try
              {
                  B b = testPrx.oneElementCycle();
                  test(b != null);
                  test(b.ice_id().Equals("::Test::B"));
                  test(b.sb.Equals("B1.sb"));
                  test(b.pb == b);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("one-element cycle (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_oneElementCycle().whenCompleted(
                          cb.response_oneElementCycle, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("two-element cycle... ");
          Flush();
          {
              try
              {
                  B b1 = testPrx.twoElementCycle();
                  test(b1 != null);
                  test(b1.ice_id().Equals("::Test::B"));
                  test(b1.sb.Equals("B1.sb"));
  
                  B b2 = b1.pb;
                  test(b2 != null);
                  test(b2.ice_id().Equals("::Test::B"));
                  test(b2.sb.Equals("B2.sb"));
                  test(b2.pb == b1);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("two-element cycle (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_twoElementCycle().whenCompleted(
                          cb.response_twoElementCycle, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("known derived pointer slicing as base... ");
          Flush();
          {
              try
              {
                  B b1;
                  b1 = testPrx.D1AsB();
                  test(b1 != null);
                  test(b1.ice_id().Equals("::Test::D1"));
                  test(b1.sb.Equals("D1.sb"));
                  test(b1.pb != null);
                  test(b1.pb != b1);
                  D1 d1 = (D1) b1;
                  test(d1 != null);
                  test(d1.sd1.Equals("D1.sd1"));
                  test(d1.pd1 != null);
                  test(d1.pd1 != b1);
                  test(b1.pb == d1.pd1);
  
                  B b2 = b1.pb;
                  test(b2 != null);
                  test(b2.pb == b1);
                  test(b2.sb.Equals("D2.sb"));
                  test(b2.ice_id().Equals("::Test::B"));
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("known derived pointer slicing as base (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_D1AsB().whenCompleted(cb.response_D1AsB, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("known derived pointer slicing as derived... ");
          Flush();
          {
              try
              {
                  D1 d1;
                  d1 = testPrx.D1AsD1();
                  test(d1 != null);
                  test(d1.ice_id().Equals("::Test::D1"));
                  test(d1.sb.Equals("D1.sb"));
                  test(d1.pb != null);
                  test(d1.pb != d1);
  
                  B b2 = d1.pb;
                  test(b2 != null);
                  test(b2.ice_id().Equals("::Test::B"));
                  test(b2.sb.Equals("D2.sb"));
                  test(b2.pb == d1);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("known derived pointer slicing as derived (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_D1AsD1().whenCompleted(cb.response_D1AsD1, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("unknown derived pointer slicing as base... ");
          Flush();
          {
              try
              {
                  B b2;
                  b2 = testPrx.D2AsB();
                  test(b2 != null);
                  test(b2.ice_id().Equals("::Test::B"));
                  test(b2.sb.Equals("D2.sb"));
                  test(b2.pb != null);
                  test(b2.pb != b2);
  
                  B b1 = b2.pb;
                  test(b1 != null);
                  test(b1.ice_id().Equals("::Test::D1"));
                  test(b1.sb.Equals("D1.sb"));
                  test(b1.pb == b2);
                  D1 d1 = (D1) b1;
                  test(d1 != null);
                  test(d1.sd1.Equals("D1.sd1"));
                  test(d1.pd1 == b2);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("unknown derived pointer slicing as base (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_D2AsB().whenCompleted(cb.response_D2AsB, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("param ptr slicing with known first... ");
          Flush();
          {
              try
              {
                  B b1;
                  B b2;
                  testPrx.paramTest1(out b1, out b2);
  
                  test(b1 != null);
                  test(b1.ice_id().Equals("::Test::D1"));
                  test(b1.sb.Equals("D1.sb"));
                  test(b1.pb == b2);
                  D1 d1 = (D1) b1;
                  test(d1 != null);
                  test(d1.sd1.Equals("D1.sd1"));
                  test(d1.pd1 == b2);
  
                  test(b2 != null);
                  test(b2.ice_id().Equals("::Test::B")); // No factory, must be sliced
                  test(b2.sb.Equals("D2.sb"));
                  test(b2.pb == b1);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("param ptr slicing with known first (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_paramTest1().whenCompleted(cb.response_paramTest1, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("param ptr slicing with unknown first... ");
          Flush();
          {
              try
              {
                  B b2;
                  B b1;
                  testPrx.paramTest2(out b2, out b1);
  
                  test(b1 != null);
                  test(b1.ice_id().Equals("::Test::D1"));
                  test(b1.sb.Equals("D1.sb"));
                  test(b1.pb == b2);
                  D1 d1 = (D1) b1;
                  test(d1 != null);
                  test(d1.sd1.Equals("D1.sd1"));
                  test(d1.pd1 == b2);
  
                  test(b2 != null);
                  test(b2.ice_id().Equals("::Test::B")); // No factory, must be sliced
                  test(b2.sb.Equals("D2.sb"));
                  test(b2.pb == b1);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("param ptr slicing with unknown first (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_paramTest2().whenCompleted(cb.response_paramTest2, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("return value identity with known first... ");
          Flush();
          {
              try
              {
                  B p1;
                  B p2;
                  B ret = testPrx.returnTest1(out p1, out p2);
                  test(ret == p1);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("return value identity with known first (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_returnTest1().whenCompleted(cb.response_returnTest1, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("return value identity with unknown first... ");
          Flush();
          {
              try
              {
                  B p1;
                  B p2;
                  B ret = testPrx.returnTest2(out p1, out p2);
                  test(ret == p1);
              }
              catch(Exception)
              {
                  test(false);
              }
          }
          WriteLine("ok");
  
          Write("return value identity with unknown first (AMI)... ");
          Flush();
          {
              AsyncCallback cb = new AsyncCallback();
              testPrx.begin_returnTest2().whenCompleted(cb.response_returnTest2, cb.exception);
              cb.check();
          }
          WriteLine("ok");
  
          Write("return value identity for input params known first... ");
          Flush();
          {
              try
              {
                  D1 d1 = new D1();
                  d1.sb = "D1.sb";
                  d1.sd1 = "D1.sd1";
                  D3 d3 = new D3();
                  d3.pb = d1;
                  d3.sb = "D3.sb";
                  d3.sd3 = "D3.sd3";
                  d3.pd3 = d1;
                  d1.pb = d3;
                  d1.pd1 = d3;
  
                  B b1 = testPrx.returnTest3(d1, d3);
  
                  test(b1 != null);
                  test(b1.sb.Equals("D1.sb"));
                  test(b1.ice_id().Equals("::Test::D1"));
                  D1 p1 = (D1) b1;
                  test(p1 != null);
                  test(p1.sd1.Equals("D1.sd1"));
                  test(p1.pd1 == b1.pb);
  
                  B b2 = b1.pb;
                  test(b2 != null);
                  test(b2.sb.Equals("D3.sb"));
                  test(b2.ice_id().Equals("::Test::B")); // Sliced by server
                  test(b2.pb == b1);
                  try
                  {
                      D3 p3 = (D3) b2;
                      test(false);
                      D3 tmp = p3; p3 = tmp; // Stop compiler warning about unused variable.
                  }
                  catch(InvalidCastException)
                  {
                  }
  
  
gt:                 test(b1 != d1);