basic_token.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 template class basic_token.
00032   \author pt
00033 */
00034 #include <lestes/common.hh>
00035 #include <lestes/equality.hh>
00036 #include <lestes/lang/cplus/lex/basic_token.hh>
00037 
00038 package(lestes);
00039 package(lang);
00040 package(cplus);
00041 package(lex);
00042 
00043 using namespace ::std;
00044 
00045 /*!
00046   \brief Test location.
00047 
00048   Location only for testing purposes.
00049 */
00050 class loc: public ::lestes::std::object {
00051 public:
00052         //! Tests equality.
00053         bool equals(const ptr<loc> &other) const;
00054         //! Returns new location.
00055         static ptr<loc> create(ulint a_position);
00056 protected:
00057         //! Creates new location.
00058         loc(ulint a_position);
00059 private:
00060         //! The represented position.
00061         ulint position;
00062         //! Hides copy constructor.
00063         loc(const loc &);
00064         //! Hides assingment operator.
00065         loc &operator=(const loc &);
00066 };
00067 
00068 /*!
00069   Creates new location.
00070   \param a_position  The position to initialize wiht.
00071 */
00072 loc::loc(ulint a_position):
00073         position(a_position) 
00074 {
00075 }
00076 
00077 /*!
00078   Tests equality to other location.
00079   \param other  The location to compare to.
00080   \return  True if both positions are the same.
00081 */
00082 bool loc::equals(const ptr<loc> &rhs) const 
00083 {
00084         return rhs && is_equal(position,rhs->position);
00085 }
00086 
00087 /*! 
00088   Returns new location, initializes with position.
00089   \param a_position  The position to initialize with.
00090 */
00091 ptr<loc> loc::create(ulint a_position) {
00092         return new loc(a_position);
00093 }
00094 
00095 //! Token type.
00096 typedef enum { POSITIVE = 1, ZERO = 0, NEGATIVE = -1 } ttype;
00097 
00098 //! Helper typedef for basic_token template test.
00099 typedef basic_token<ttype,ptr<loc>,int> basic_testing_token;
00100 
00101 /*!
00102   \brief Testing token.
00103 
00104   Template instance to test basic_token.
00105 */
00106 class testing_token: public basic_testing_token {
00107 public:
00108         //! Clones the token.
00109         ptr<testing_token> clone(void) const;
00110         //! Returns new token.
00111         static ptr<testing_token> create(const location_type &a_location, const type_type &a_type,
00112                         const value_type &a_value);
00113 protected:
00114         //! Creates new token.
00115         testing_token(const location_type &a_location, const type_type &a_type,
00116                         const value_type &a_value);   
00117 };
00118 
00119 /*!
00120   Creates new token, initializes all fields.
00121   \param a_location  The initial location.
00122   \param a_type  The initial token type.
00123   \param a_value  The initial token value.
00124 */
00125 testing_token::testing_token(const location_type &a_location, const type_type &a_type,
00126                         const value_type &a_value):
00127         basic_testing_token(a_location,a_type,a_value)
00128 {   
00129 }
00130 
00131 /*!
00132   Clones the token.
00133   \post is_equal(this,returned)
00134   \return  New token with equal values.
00135 */
00136 ptr<testing_token> testing_token::clone(void) const
00137 {
00138         return new testing_token(location_get(),type_get(),value_get());
00139 }
00140 
00141 /*!
00142   Creates new token, initializes all fields.
00143   \param a_location  The initial location.
00144   \param a_type  The initial token type.
00145   \param a_value  The initial token value.
00146 */
00147 ptr<testing_token> testing_token::create(const location_type& a_location, const type_type &a_type,
00148                         const value_type &a_value)
00149 {
00150         return new testing_token(a_location,a_type,a_value);
00151 }
00152 
00153 /*!
00154   \brief Tests basic token.
00155 
00156   Performs testing of basic token.
00157 */
00158 void basic_token_test(void)
00159 {
00160         ptr<loc> el = loc::create(13);
00161         ptr<loc> em = loc::create(17);
00162         ptr<testing_token> a = testing_token::create(em,ZERO,0);
00163         
00164         lassert(is_equal(a,a));
00165         lassert(is_equal(a->location_get(),em));
00166         lassert(is_equal(a->type_get(),ZERO));
00167         lassert(is_equal(a->value_get(),0));
00168         
00169         a = testing_token::create(el,POSITIVE,13);
00170 
00171         lassert(is_equal(a,a));
00172         lassert(is_equal(a->location_get(),el));
00173         lassert(is_equal(a->type_get(),POSITIVE));
00174         lassert(is_equal(a->value_get(),13));
00175 
00176         ptr<testing_token> b = testing_token::create(em,NEGATIVE,-4);
00177         ptr<testing_token> c = b->clone();
00178 
00179         lassert(is_equal(c,b));
00180         lassert(is_equal(b,c));
00181         lassert(is_equal(b->type_get(),c->type_get()));
00182         lassert(is_equal(b->value_get(),c->value_get()));
00183         lassert(is_equal(b->location_get(),c->location_get()));
00184 
00185         ptr<testing_token> d;
00186 
00187         d = a;
00188 
00189         lassert(is_equal(d,a));
00190         lassert(is_equal(a,d));
00191         lassert(is_equal(d->type_get(),POSITIVE));
00192         lassert(is_equal(d->value_get(),13));
00193         lassert(is_equal(d->location_get(),el));
00194 }
00195 
00196 end_package(lex);
00197 end_package(cplus);
00198 end_package(lang);
00199 end_package(lestes);
00200 
00201 /*!
00202   \brief Main function.
00203 
00204   Runs the unit test in different namespace.
00205 */
00206 int main(void)
00207 {
00208 	::lestes::lang::cplus::lex::basic_token_test();
00209         return 0;
00210 }
00211 /* vim: set ft=lestes : */

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