PLearn 0.1
TupleTest.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // TupleTest.cc
00004 //
00005 // Copyright (C) 2006 Pascal Vincent
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Pascal Vincent
00036 
00040 #include "TupleTest.h"
00041 #include <plearn/base/tuple.h>
00042 #include <plearn/io/openString.h>
00043 #include <plearn/base/tostring.h>
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00048 PLEARN_IMPLEMENT_OBJECT(
00049     TupleTest,
00050     "Simple test testing tuple i/o with PStreams",
00051     ""
00052 );
00053 
00055 // TupleTest //
00057 TupleTest::TupleTest()
00058     /* ### Initialize all fields to their default value */
00059 {}
00060 
00062 // build //
00064 void TupleTest::build()
00065 {
00066     inherited::build();
00067     build_();
00068 }
00069 
00071 // makeDeepCopyFromShallowCopy //
00073 void TupleTest::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00074 {
00075     inherited::makeDeepCopyFromShallowCopy(copies);
00076 
00077     // ### Call deepCopyField on all "pointer-like" fields
00078     // ### that you wish to be deepCopied rather than
00079     // ### shallow-copied.
00080     // ### ex:
00081     // deepCopyField(trainvec, copies);
00082 
00083     // ### Remove this line when you have fully implemented this method.
00084     PLERROR("TupleTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00085 }
00086 
00088 // declareOptions //
00090 void TupleTest::declareOptions(OptionList& ol)
00091 {
00092     // Now call the parent class' declareOptions
00093     inherited::declareOptions(ol);
00094 }
00095 
00097 // build_ //
00099 void TupleTest::build_()
00100 {}
00101 
00103 // perform //
00105 void TupleTest::perform()
00106 {
00107     string srepr;
00108     PStream fromsrepr;
00109 
00110     // first test pair
00111     
00112     pout << "\n***** Simple pair test *****" << endl;
00113     
00114     typedef pair<string, pair<int, string> > p1_t;
00115     p1_t p1("mot1",make_pair(3,string("mot2")));
00116     pout << "Type: " << TypeTraits<p1_t>::name() << endl;
00117     pout << "Value: " << p1 << endl;
00118 
00119     srepr = tostring(p1,PStream::plearn_ascii);
00120     fromsrepr = openString(srepr, PStream::plearn_ascii);
00121     p1_t p1_fromascii;
00122     fromsrepr >> p1_fromascii;
00123     pout << "Value reread from plearn_ascii serialization: " << p1_fromascii << endl;
00124     pout << "Same? " << (p1_fromascii==p1) << endl;
00125 
00126     srepr = tostring(p1,PStream::plearn_binary);
00127     fromsrepr = openString(srepr, PStream::plearn_binary);
00128     p1_t p1_frombinary;
00129     fromsrepr >> p1_frombinary;
00130     pout << "Value reread from plearn_binary serialization: " << p1_frombinary << endl;
00131     pout << "Same? " << (p1_frombinary==p1) << endl;
00132 
00133     // test a simple tuple
00134 
00135     pout << "\n***** Simple tuple test *****" << endl;
00136 
00137     typedef tuple<int, double, string, char> t1_t;
00138     t1_t t1(3,4.5,string("essai"),'X');
00139     pout << "Type: " << TypeTraits<t1_t>::name() << endl;
00140     pout << "Value: " << t1 << endl;
00141 
00142     srepr = tostring(t1,PStream::plearn_ascii);
00143     fromsrepr = openString(srepr, PStream::plearn_ascii);
00144     t1_t t1_fromascii;
00145     fromsrepr >> t1_fromascii;
00146     pout << "Value reread from plearn_ascii serialization: " << t1_fromascii << endl;
00147     pout << "Same? " << (t1_fromascii==t1) << endl;
00148 
00149     srepr = tostring(t1,PStream::plearn_binary);
00150     fromsrepr = openString(srepr, PStream::plearn_binary);
00151     t1_t t1_frombinary;
00152     fromsrepr >> t1_frombinary;
00153     pout << "Value reread from plearn_binary serialization: " << t1_frombinary << endl;
00154     pout << "Same? " << (t1_frombinary==t1) << endl;
00155 
00156     // test a complex tuple
00157 
00158     pout << "\n***** Complex tuple test *****" << endl;
00159 
00160     typedef tuple<int, 
00161         float, 
00162         tuple<string, pair<double, int>, map<string, float> >,
00163         char> t2_t;
00164     map<string, float> m;
00165     m["key1"] = 1.0;
00166     m["key2"] = 2.0;
00167     m["key3"] = 3.0;
00168     t2_t t2(36, 
00169             3.25,
00170             make_tuple(string("mot1"), make_pair(4.5,3), m),
00171             'Z');
00172     pout << "Type: " << TypeTraits<t2_t>::name() << endl;
00173     pout << "Value: " << t2 << endl;
00174 
00175     srepr = tostring(t2,PStream::plearn_ascii);
00176     fromsrepr = openString(srepr, PStream::plearn_ascii);
00177     t2_t t2_fromascii;
00178     fromsrepr >> t2_fromascii;
00179     pout << "Value reread from plearn_ascii serialization: " << t2_fromascii << endl;
00180     pout << "Same? " << (t2_fromascii==t2) << endl;
00181 
00182     srepr = tostring(t2,PStream::plearn_binary);
00183     fromsrepr = openString(srepr, PStream::plearn_binary);
00184     t2_t t2_frombinary;
00185     fromsrepr >> t2_frombinary;
00186     pout << "Value reread from plearn_binary serialization: " << t2_frombinary << endl;
00187     pout << "Same? " << (t2_frombinary==t2) << endl;
00188 
00189     pout << "\n**** ALL DONE *****" << endl;
00190 }
00191 
00192 } // end of namespace PLearn
00193 
00194 
00195 /*
00196   Local Variables:
00197   mode:c++
00198   c-basic-offset:4
00199   c-file-style:"stroustrup"
00200   c-file-offsets:((innamespace . 0)(inline-open . 0))
00201   indent-tabs-mode:nil
00202   fill-column:79
00203   End:
00204 */
00205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines