encoder_factory.test.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 /*! \file
00029   \brief Unit test.
00030   
00031   Unit test for class encoder_factory.
00032   \author pt
00033 */
00034 #include <lestes/common.hh>
00035 #include <lestes/equality.hh>
00036 #include <lestes/lang/cplus/lex/encoder.hh>
00037 #include <lestes/lang/cplus/lex/encoder_factory.hh>
00038 
00039 package(lestes);
00040 package(lang);
00041 package(cplus);
00042 package(lex);
00043 
00044 using namespace ::std;
00045 
00046 /*!
00047   \brief Test encoder.
00048 
00049   Simple encoder to test the encoder_factory.
00050   Returns specific token according to the template parameter.
00051   \param Discriminator  The value to 
00052 */
00053 template <ulint Discriminator>
00054 class encoder_test: public encoder {
00055 public:
00056         //! Reads next token.
00057         ptr<ucn_token> read(void);
00058         //! Returns new instance.
00059         static ptr< encoder_test<Discriminator> > create(void);
00060 protected:
00061         //! Creates the object.
00062         encoder_test(void);
00063 private:
00064         //! Hides copy constructor.
00065         encoder_test(const encoder_test<Discriminator> &);
00066         //! Hides assignment operator.
00067         encoder_test<Discriminator> &operator=(const encoder_test<Discriminator> &);
00068 };
00069 
00070 /*!
00071   Creates the encoder.
00072 */
00073 template <ulint Discriminator>
00074 encoder_test<Discriminator>::encoder_test<Discriminator>(void)
00075 {
00076 }
00077 
00078 /*!
00079   Returns token to distinguish instances.
00080   The token value corresponds to Discriminator.
00081   \return  The token with value set to the discriminator.
00082 */
00083 template <ulint Discriminator>
00084 ptr<ucn_token> encoder_test<Discriminator>::read(void)
00085 {
00086         return ucn_token::create(ucn_token::TOK_NOT_EOF,Discriminator);
00087 }
00088 
00089 /*!
00090   Returns new instance of first type.
00091   \return  The new instance.
00092 */
00093 template <ulint Discriminator>
00094 ptr< encoder_test<Discriminator> > encoder_test<Discriminator>::create(void)
00095 {
00096         return new encoder_test<Discriminator>();
00097 }
00098 
00099 //! Helper typedef for test encoder.
00100 typedef encoder_test<1> enc1;
00101 //! Helper typedef for test encoder.
00102 typedef encoder_test<2> enc2;
00103 //! Helper typedef for test encoder.
00104 typedef encoder_test<3> enc3;
00105 
00106 /*!
00107   \brief Tests encoder_factory class.
00108 
00109   Performs testing of encoder_factory class.
00110 */
00111 void encoder_factory_test(void)
00112 {
00113         ptr<encoder_factory> ef = encoder_factory::instance();
00114         
00115         // instance exists
00116         lassert(ef);
00117 
00118         // the factory is empty
00119         lassert(is_equal(ef->summon("first"),ptr<encoder>(NULL)));
00120         lassert(is_equal(ef->summon("second"),ptr<encoder>(NULL)));
00121         lassert(is_equal(ef->summon("third"),ptr<encoder>(NULL)));
00122         
00123         // inserted first
00124         ef->insert("first",encoder_factory::encoder_create_adaptor<enc1>);
00125         lassert(!is_equal(ef->summon("first"),ptr<encoder>(NULL)));
00126         lassert(is_equal(ef->summon("second"),ptr<encoder>(NULL)));
00127         lassert(is_equal(ef->summon("third"),ptr<encoder>(NULL)));
00128         lassert(is_equal(ef->summon("first")->read()->value_get(),1U));
00129         
00130         // inserted second
00131         ef->insert("second",&encoder_factory::encoder_create_adaptor<enc2>);
00132         lassert(!is_equal(ef->summon("first"),ptr<encoder>(NULL)));
00133         lassert(!is_equal(ef->summon("second"),ptr<encoder>(NULL)));
00134         lassert(is_equal(ef->summon("third"),ptr<encoder>(NULL)));
00135         lassert(is_equal(ef->summon("first")->read()->value_get(),1U));
00136         lassert(is_equal(ef->summon("second")->read()->value_get(),2U));
00137         
00138         // inserted third
00139         ef->insert("third",&encoder_factory::encoder_create_adaptor<enc3>);
00140         lassert(!is_equal(ef->summon("first"),ptr<encoder>(NULL)));
00141         lassert(!is_equal(ef->summon("second"),ptr<encoder>(NULL)));
00142         lassert(!is_equal(ef->summon("third"),ptr<encoder>(NULL)));
00143         lassert(is_equal(ef->summon("first")->read()->value_get(),1U));
00144         lassert(is_equal(ef->summon("second")->read()->value_get(),2U));
00145         lassert(is_equal(ef->summon("third")->read()->value_get(),3U));
00146         
00147         // removed first
00148         ef->remove("first");
00149         lassert(is_equal(ef->summon("first"),ptr<encoder>(NULL)));
00150         lassert(!is_equal(ef->summon("second"),ptr<encoder>(NULL)));
00151         lassert(!is_equal(ef->summon("third"),ptr<encoder>(NULL)));
00152         lassert(is_equal(ef->summon("second")->read()->value_get(),2U));
00153         lassert(is_equal(ef->summon("third")->read()->value_get(),3U));
00154         
00155 }
00156 
00157 end_package(lex);
00158 end_package(cplus);
00159 end_package(lang);
00160 end_package(lestes);
00161 
00162 /*!
00163   \brief Main function.
00164 
00165   Runs the unit test in different namespace.
00166 */
00167 int main(void)
00168 {
00169 	::lestes::lang::cplus::lex::encoder_factory_test();
00170         return 0;
00171 }
00172 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:22:33 2007 for lestes by doxygen 1.5.1-20070107