summaryrefslogtreecommitdiff
path: root/cpp/test/Ice/stream/Client.cpp
diff options
context:
space:
mode:
authorMark Spruiell <mes@zeroc.com>2004-10-19 02:27:18 +0000
committerMark Spruiell <mes@zeroc.com>2004-10-19 02:27:18 +0000
commit4ee06da3e23c279897cef5b2b51487875b4486b7 (patch)
treee2080691778256ebf32ceabc7d8edce6e869fbad /cpp/test/Ice/stream/Client.cpp
parentupdating dependencies (diff)
downloadice-4ee06da3e23c279897cef5b2b51487875b4486b7.tar.bz2
ice-4ee06da3e23c279897cef5b2b51487875b4486b7.tar.xz
ice-4ee06da3e23c279897cef5b2b51487875b4486b7.zip
adding streaming API
Diffstat (limited to 'cpp/test/Ice/stream/Client.cpp')
-rw-r--r--cpp/test/Ice/stream/Client.cpp487
1 files changed, 487 insertions, 0 deletions
diff --git a/cpp/test/Ice/stream/Client.cpp b/cpp/test/Ice/stream/Client.cpp
new file mode 100644
index 00000000000..ad02e6f6975
--- /dev/null
+++ b/cpp/test/Ice/stream/Client.cpp
@@ -0,0 +1,487 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2004 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.
+//
+// **********************************************************************
+
+#include <Ice/Ice.h>
+#include <TestCommon.h>
+#include <Test.h>
+
+using namespace std;
+
+class TestObjectWriter : public Ice::ObjectWriter
+{
+public:
+
+ TestObjectWriter(const Test::MyClassPtr& p)
+ {
+ obj = p;
+ called = false;
+ }
+
+ virtual void
+ write(const Ice::OutputStreamPtr& out) const
+ {
+ obj->__write(out);
+ const_cast<TestObjectWriter*>(this)->called = true;
+ }
+
+ Test::MyClassPtr obj;
+ bool called;
+};
+typedef IceUtil::Handle<TestObjectWriter> TestObjectWriterPtr;
+
+class TestObjectReader : public Ice::ObjectReader
+{
+public:
+
+ TestObjectReader()
+ {
+ called = false;
+ }
+
+ virtual void
+ read(const Ice::InputStreamPtr& in, bool rid)
+ {
+ obj = new Test::MyClass;
+ obj->__read(in, rid);
+ called = true;
+ }
+
+ Test::MyClassPtr obj;
+ bool called;
+};
+typedef IceUtil::Handle<TestObjectReader> TestObjectReaderPtr;
+
+class TestObjectFactory : public Ice::ObjectFactory
+{
+public:
+
+ virtual Ice::ObjectPtr
+ create(const string& type)
+ {
+ assert(type == Test::MyClass::ice_staticId());
+ return new TestObjectReader;
+ }
+
+ virtual void
+ destroy()
+ {
+ }
+};
+
+class TestReadObjectCallback : public Ice::ReadObjectCallback
+{
+public:
+
+ virtual void
+ invoke(const Ice::ObjectPtr& p)
+ {
+ obj = p;
+ }
+
+ Ice::ObjectPtr obj;
+};
+typedef IceUtil::Handle<TestReadObjectCallback> TestReadObjectCallbackPtr;
+
+int
+run(int argc, char** argv, const Ice::CommunicatorPtr& communicator)
+{
+ Ice::InputStreamPtr in;
+ Ice::OutputStreamPtr out;
+ vector<Ice::Byte> data;
+
+ cout << "testing primitive types... " << flush;
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeBool(true);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readBool());
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeByte(1);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readByte() == 1);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeShort(2);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readShort() == 2);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeInt(3);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readInt() == 3);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeLong(4);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readLong() == 4);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeFloat(5.0);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readFloat() == 5.0);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeDouble(6.0);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readDouble() == 6.0);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ out->writeString("hello world");
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ test(in->readString() == "hello world");
+ in->finished();
+ }
+
+ cout << "ok" << endl;
+
+ cout << "testing constructed types... " << flush;
+
+ {
+ out = Ice::createOutputStream(communicator);
+ Test::ice_writeMyEnum(out, Test::enum3);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::MyEnum e;
+ Test::ice_readMyEnum(in, e);
+ test(e == Test::enum3);
+ in->finished();
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ Test::SmallStruct s;
+ s.bo = true;
+ s.by = 1;
+ s.sh = 2;
+ s.i = 3;
+ s.l = 4;
+ s.f = 5.0;
+ s.d = 6.0;
+ s.str = "7";
+ s.e = Test::enum2;
+ s.p = Test::MyClassPrx::uncheckedCast(communicator->stringToProxy("test:default"));
+ Test::ice_writeSmallStruct(out, s);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::SmallStruct s2;
+ Test::ice_readSmallStruct(in, s2);
+ in->finished();
+ test(s2 == s);
+ }
+
+ {
+ Test::BoolS arr;
+ arr.push_back(true);
+ arr.push_back(false);
+ arr.push_back(true);
+ arr.push_back(false);
+
+ out = Ice::createOutputStream(communicator);
+ out->writeBoolSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::BoolS arr2 = in->readBoolSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::ByteS arr;
+ arr.push_back(0x01);
+ arr.push_back(0x11);
+ arr.push_back(0x12);
+ arr.push_back(0x22);
+
+ out = Ice::createOutputStream(communicator);
+ out->writeByteSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::ByteS arr2 = in->readByteSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::ShortS arr;
+ arr.push_back(0x01);
+ arr.push_back(0x11);
+ arr.push_back(0x12);
+ arr.push_back(0x22);
+ out = Ice::createOutputStream(communicator);
+ out->writeShortSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::ShortS arr2 = in->readShortSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::IntS arr;
+ arr.push_back(0x01);
+ arr.push_back(0x11);
+ arr.push_back(0x12);
+ arr.push_back(0x22);
+ out = Ice::createOutputStream(communicator);
+ out->writeIntSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::IntS arr2 = in->readIntSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::LongS arr;
+ arr.push_back(0x01);
+ arr.push_back(0x11);
+ arr.push_back(0x12);
+ arr.push_back(0x22);
+ out = Ice::createOutputStream(communicator);
+ out->writeLongSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::LongS arr2 = in->readLongSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::FloatS arr;
+ arr.push_back(1);
+ arr.push_back(2);
+ arr.push_back(3);
+ arr.push_back(4);
+ out = Ice::createOutputStream(communicator);
+ out->writeFloatSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::FloatS arr2 = in->readFloatSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::DoubleS arr;
+ arr.push_back(1);
+ arr.push_back(2);
+ arr.push_back(3);
+ arr.push_back(4);
+ out = Ice::createOutputStream(communicator);
+ out->writeDoubleSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::DoubleS arr2 = in->readDoubleSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::StringS arr;
+ arr.push_back("string1");
+ arr.push_back("string2");
+ arr.push_back("string3");
+ arr.push_back("string4");
+ out = Ice::createOutputStream(communicator);
+ out->writeStringSeq(arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::StringS arr2 = in->readStringSeq();
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::MyEnumS arr;
+ arr.push_back(Test::enum3);
+ arr.push_back(Test::enum2);
+ arr.push_back(Test::enum1);
+ arr.push_back(Test::enum2);
+
+ out = Ice::createOutputStream(communicator);
+ Test::ice_writeMyEnumS(out, arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::MyEnumS arr2;
+ Test::ice_readMyEnumS(in, arr2);
+ in->finished();
+ test(arr2 == arr);
+ }
+
+ {
+ Test::MyClassS arr;
+ for(int i = 0; i < 4; ++i)
+ {
+ Test::MyClassPtr c = new Test::MyClass;
+ c->c = c;
+ c->o = c;
+ c->s.e = Test::enum2;
+
+ c->seq1.push_back(true);
+ c->seq1.push_back(false);
+ c->seq1.push_back(true);
+ c->seq1.push_back(false);
+
+ c->seq2.push_back(1);
+ c->seq2.push_back(2);
+ c->seq2.push_back(3);
+ c->seq2.push_back(4);
+
+ c->seq3.push_back(1);
+ c->seq3.push_back(2);
+ c->seq3.push_back(3);
+ c->seq3.push_back(4);
+
+ c->seq4.push_back(1);
+ c->seq4.push_back(2);
+ c->seq4.push_back(3);
+ c->seq4.push_back(4);
+
+ c->seq5.push_back(1);
+ c->seq5.push_back(2);
+ c->seq5.push_back(3);
+ c->seq5.push_back(4);
+
+ c->seq6.push_back(1);
+ c->seq6.push_back(2);
+ c->seq6.push_back(3);
+ c->seq6.push_back(4);
+
+ c->seq7.push_back(1);
+ c->seq7.push_back(2);
+ c->seq7.push_back(3);
+ c->seq7.push_back(4);
+
+ c->seq8.push_back("string1");
+ c->seq8.push_back("string2");
+ c->seq8.push_back("string3");
+ c->seq8.push_back("string4");
+
+ c->seq9.push_back(Test::enum3);
+ c->seq9.push_back(Test::enum2);
+ c->seq9.push_back(Test::enum1);
+
+ c->d["hi"] = c;
+ }
+ out = Ice::createOutputStream(communicator);
+ Test::ice_writeMyClassS(out, arr);
+ out->finished(data);
+ in = Ice::createInputStream(communicator, data);
+ Test::MyClassS arr2;
+ Test::ice_readMyClassS(in, arr2);
+ in->finished();
+ test(arr2.size() == arr.size());
+ for(Test::MyClassS::size_type i = 0; i < arr2.size(); ++i)
+ {
+ test(arr2[i]);
+ test(arr2[i]->c == arr2[i]);
+ test(arr2[i]->o == arr2[i]);
+ test(arr2[i]->s.e == Test::enum2);
+ test(arr2[i]->seq1 == arr[i]->seq1);
+ test(arr2[i]->seq2 == arr[i]->seq2);
+ test(arr2[i]->seq3 == arr[i]->seq3);
+ test(arr2[i]->seq4 == arr[i]->seq4);
+ test(arr2[i]->seq5 == arr[i]->seq5);
+ test(arr2[i]->seq6 == arr[i]->seq6);
+ test(arr2[i]->seq7 == arr[i]->seq7);
+ test(arr2[i]->seq8 == arr[i]->seq8);
+ test(arr2[i]->seq9 == arr[i]->seq9);
+ test(arr2[i]->d["hi"] == arr2[i]);
+ }
+ }
+
+ {
+ out = Ice::createOutputStream(communicator);
+ Test::MyClassPtr obj = new Test::MyClass;
+ obj->s.e = Test::enum2;
+ TestObjectWriterPtr writer = new TestObjectWriter(obj);
+ out->writeObject(writer);
+ out->finished(data);
+ test(writer->called);
+ communicator->addObjectFactory(new TestObjectFactory, Test::MyClass::ice_staticId());
+ in = Ice::createInputStream(communicator, data);
+ TestReadObjectCallbackPtr cb = new TestReadObjectCallback;
+ in->readObject(cb);
+ in->finished();
+ test(cb->obj);
+ TestObjectReaderPtr reader = TestObjectReaderPtr::dynamicCast(cb->obj);
+ test(reader);
+ test(reader->called);
+ test(reader->obj);
+ test(reader->obj->s.e == Test::enum2);
+ }
+
+ cout << "ok" << endl;
+
+ return 0;
+}
+
+int
+main(int argc, char* argv[])
+{
+ int status;
+ Ice::CommunicatorPtr communicator;
+
+ try
+ {
+ communicator = Ice::initialize(argc, argv);
+ status = run(argc, argv, communicator);
+ }
+ catch(const Ice::Exception& ex)
+ {
+ cerr << ex << endl;
+ status = EXIT_FAILURE;
+ }
+
+ if(communicator)
+ {
+ try
+ {
+ communicator->destroy();
+ }
+ catch(const Ice::Exception& ex)
+ {
+ cerr << ex << endl;
+ status = EXIT_FAILURE;
+ }
+ }
+
+ return status;
+}