diff --git a/examples/5_stream_failtest.cpp b/examples/5_stream_failtest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdbb7ce10259c0a8d4d3611a3ecfbb6225d1312d --- /dev/null +++ b/examples/5_stream_failtest.cpp @@ -0,0 +1,128 @@ + +#include <iostream> +#include <sstream> +#include <cassert> +#include <tscpp.h> +#include "types.h" + +using namespace std; +using namespace tscpp; + +int main() +{ + //eof during type name in InputArchive + { + stringstream ss; + InputArchive ia(ss); + Point3d p; + try { + ia>>p; + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + assert(p.x==0 && p.y==0 && p.z==0); + } + } + + //wrong type in InputArchive + { + stringstream ss; + OutputArchive oa(ss); + Point3d p(1,2,3); + oa<<p; + + InputArchive ia(ss); + Point2d q; + try { + ia>>q; + assert(false); + } catch(TscppException& ex) { + assert(ss.tellg()==0); + assert(string(ex.what())=="wrong type"); + assert(ex.name()==typeid(p).name()); + assert(q.x==0 && q.y==0); + } + } + + //eof after type name in InputArchive + { + stringstream ss; + OutputArchive oa(ss); + Point3d p(1,2,3); + oa<<p; + + string s=ss.str(); + s.pop_back(); //Remove one char + ss.str(s); + + InputArchive ia(ss); + Point3d q; + try { + ia>>q; + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + } + } + + TypePool tp; + tp.registerType<Point2d>([&](Point2d& t) { assert(false); }); + tp.registerType<Point3d>([&](Point3d& t) { assert(false); }); + + //eof during type name in UnknownInputArchive + { + + stringstream ss; + UnknownInputArchive ia(ss,tp); + try { + ia.unserialize(); + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + } + } + + //unknown type in UnknownInputArchive + { + stringstream ss; + OutputArchive oa(ss); + MiscData md; + oa<<md; + + UnknownInputArchive ia(ss,tp); + try { + ia.unserialize(); + assert(false); + } catch(TscppException& ex) { + assert(ss.tellg()==0); + assert(string(ex.what())=="unknown type"); + assert(ex.name()==typeid(md).name()); + } + } + + //eof after type name in UnknownInputArchive + { + stringstream ss; + OutputArchive oa(ss); + Point3d p(1,2,3); + oa<<p; + + string s=ss.str(); + s.pop_back(); //Remove one char + ss.str(s); + + UnknownInputArchive ia(ss,tp); + try { + ia.unserialize(); + assert(false); + } catch(TscppException& ex) { + assert(string(ex.what())=="eof"); + assert(ex.name()==""); + } + } + + cout<<"Test passed"<<endl; +} diff --git a/examples/Makefile b/examples/Makefile index 5dc24567ba941c1cbe8e04970adfc86001cb3ef7..2f227a461da343a7e73710e3d27c980eec665107 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,14 +3,17 @@ CXX = g++ CXXFLAGS = -std=c++11 -O2 -Wall -I.. all: - $(CXX) $(CXXFLAGS) 1_stream_known.cpp ../tscpp.cpp -o 1_stream_known - $(CXX) $(CXXFLAGS) 2_stream_unknown.cpp ../tscpp.cpp -o 2_stream_unknown - $(CXX) $(CXXFLAGS) 3_buffer_known.cpp ../tscpp.cpp -o 3_buffer_known - $(CXX) $(CXXFLAGS) 4_buffer_unknown.cpp ../tscpp.cpp -o 4_buffer_unknown + $(CXX) $(CXXFLAGS) 1_stream_known.cpp ../tscpp.cpp -o 1_stream_known + $(CXX) $(CXXFLAGS) 2_stream_unknown.cpp ../tscpp.cpp -o 2_stream_unknown + $(CXX) $(CXXFLAGS) 3_buffer_known.cpp ../tscpp.cpp -o 3_buffer_known + $(CXX) $(CXXFLAGS) 4_buffer_unknown.cpp ../tscpp.cpp -o 4_buffer_unknown + $(CXX) $(CXXFLAGS) 5_stream_failtest.cpp ../tscpp.cpp -o 5_stream_failtest ./1_stream_known ./2_stream_unknown ./3_buffer_known ./4_buffer_unknown + ./5_stream_failtest clean: - rm -f 1_stream_known 2_stream_unknown 3_buffer_known 4_buffer_unknown + rm -f 1_stream_known 2_stream_unknown 3_buffer_known 4_buffer_unknown \ + 5_stream_failtest