macro_body.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   Unit test for class macro_body.
00030   \author pt
00031 */
00032 #include <lestes/common.hh>
00033 #include <lestes/std/source_location.hh>
00034 #include <lestes/std/ucn_string.hh>
00035 #include <lestes/lang/cplus/lex/macro_body.hh>
00036 #include <lestes/lang/cplus/lex/macro_head.hh>
00037 #include <lestes/lang/cplus/lex/token_sequence.hh>
00038 #include <lestes/lang/cplus/lex/token_value.hh>
00039 #include <lestes/lang/cplus/lex/pp_token.hh>
00040 #include <string>
00041 
00042 package(lestes);
00043 package(lang);
00044 package(cplus);
00045 package(lex);
00046 
00047 using namespace ::std;
00048 
00049 void macro_body_test(void)
00050 {
00051         ptr<file_info> fi = file_info::create(string("abc"),NULL);
00052         ptr<source_location> loc = source_location::create(fi,0,0);
00053 
00054         ptr<token_sequence> mhts = token_sequence::create();
00055         mhts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("abc")) );
00056         mhts->add_back( pp_token::create(loc,pp_token::TOK_COMMA) );
00057         mhts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("def")) );
00058         mhts->add_back( pp_token::create(loc,pp_token::TOK_RIGHT_PAR) );
00059         
00060         ptr<macro_head> mh = macro_head::create();
00061         lassert(is_equal(mh->parse(mhts),true));
00062         
00063         ptr<token_sequence> ts = token_sequence::create();
00064         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00065         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00066         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00067                 
00068         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00069         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00070                 
00071         ts->add_back( pp_token::create(loc,pp_token::TOK_GT) );
00072         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00073         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00074         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00075 
00076         ts->add_back( pp_token::create(loc,pp_token::TOK_AMP) );
00077         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00078         ts->add_back( pp_token::create(loc,pp_token::TOK_HAT) );
00079         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00080 
00081         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00082         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00083         ts->add_back( pp_token::create(loc,pp_token::TOK_AMP) );
00084         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00085         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00086         ts->add_back( pp_token::create(loc,pp_token::TOK_HAT) );
00087         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00088         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00089         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00090 
00091         ts->add_back( pp_token::create(loc,pp_token::TOK_AMP) );
00092         ts->add_back( pp_token::create(loc,pp_token::TOK_HAT) );
00093         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00094 
00095         ts->add_back( pp_token::create(loc,pp_token::TOK_EMARK) );
00096         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00097         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00098         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00099         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00100         ts->add_back( pp_token::create(loc,pp_token::TOK_EQ) );
00101         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00102 
00103         ts->add_back( pp_token::create(loc,pp_token::TOK_EMARK) );
00104         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00105         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00106         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00107         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00108         ts->add_back( pp_token::create(loc,pp_token::TOK_EQ) );
00109         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00110 
00111         ts->add_back( pp_token::create(loc,pp_token::TOK_LT) );
00112         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00113         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00114         ts->add_back( pp_token::create(loc,pp_token::TOK_EQ) );
00115         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00116 
00117                 // function-like
00118         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00119         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00120         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00121                 
00122         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00123         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00124                 
00125         ts->add_back( pp_token::create(loc,pp_token::TOK_GT) );
00126         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00127         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00128         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00129 
00130         ts->add_back( pp_token::create(loc,pp_token::TOK_AMP) );
00131         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00132         ts->add_back( pp_token::create(loc,pp_token::TOK_HAT) );
00133         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00134 
00135         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00136         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00137         ts->add_back( pp_token::create(loc,pp_token::TOK_AMP) );
00138         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00139         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00140         ts->add_back( pp_token::create(loc,pp_token::TOK_HAT) );
00141         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00142         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00143         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00144 
00145         ts->add_back( pp_token::create(loc,pp_token::TOK_AMP) );
00146         ts->add_back( pp_token::create(loc,pp_token::TOK_HAT) );
00147         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00148 
00149         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00150         ts->add_back( pp_token::create(loc,pp_token::TOK_EQ) );
00151         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00152         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("iii")) );
00153         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00154         ts->add_back( pp_token::create(loc,pp_token::TOK_PLUS) );
00155         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("abc")) );
00156         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00157         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00158 
00159         ts->add_back( pp_token::create(loc,pp_token::TOK_EQ) );
00160         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00161         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00162         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("iii")) );
00163         ts->add_back( pp_token::create(loc,pp_token::TOK_PLUS) );
00164         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("abc")) );
00165         ts->add_back( pp_token::create(loc,pp_token::TOK_BLANK) );
00166         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00167 
00168         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH) );
00169         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("abc")) );
00170         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00171 
00172         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH) );
00173         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("iii")) );
00174         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00175 
00176         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("iii")) );
00177         ts->add_back( pp_token::create(loc,pp_token::TOK_HASH_HASH) );
00178         ts->add_back( pp_token::create(loc,pp_token::TOK_IDENT,token_value::create("abc")) );
00179         ts->add_back( pp_token::create(loc,pp_token::TOK_LINE_END) );
00180 
00181         ptr<macro_body> mb0 = macro_body::create();
00182         // created object
00183         lassert(is_equal(mb0,mb0));
00184         lassert(is_equal(mb0->state_get(),macro_body::BEGIN));
00185         
00186         // object-like macro
00187 
00188         // eol
00189         lassert(is_equal(mb0->parse(ts),true));
00190         lassert(is_equal(mb0->state_get(),macro_body::PARSED));
00191         lassert(is_equal(mb0,mb0));
00192 
00193         ptr<macro_body> mb1 = macro_body::create();
00194         // ## at beginning 
00195         lassert(is_equal(mb1->parse(ts),false));
00196         lassert(is_equal(mb1->state_get(),macro_body::DEAD));
00197         lassert(is_equal(mb1,mb1));
00198         lassert(!is_equal(mb1,mb0));
00199         
00200         ptr<macro_body> mb2 = macro_body::create();
00201         // ## at the end
00202         lassert(is_equal(mb2->parse(ts),false));
00203         lassert(is_equal(mb2->state_get(),macro_body::DEAD));
00204         lassert(is_equal(mb2,mb1));
00205 
00206         ptr<macro_body> mb3 = macro_body::create();
00207         // normal tokens 
00208         lassert(is_equal(mb3->parse(ts),true));
00209         lassert(is_equal(mb3->state_get(),macro_body::PARSED));
00210 
00211         ptr<macro_body> mb4 = macro_body::create();
00212         // the same tokens with front and back ws and extended internal ws
00213         lassert(is_equal(mb4->parse(ts),true));
00214         lassert(is_equal(mb4->state_get(),macro_body::PARSED));
00215         lassert(is_equal(mb4,mb4));
00216         lassert(is_equal(mb4,mb3));
00217 
00218         ptr<macro_body> mb5 = macro_body::create();
00219         // the same tokens with different ws
00220         lassert(is_equal(mb5->parse(ts),true));
00221         lassert(is_equal(mb5->state_get(),macro_body::PARSED));
00222         lassert(!is_equal(mb5,mb4));
00223 
00224         ptr<macro_body> mb6 = macro_body::create();
00225         // tokens with ## inside and ws
00226         lassert(is_equal(mb6->parse(ts),true));
00227         lassert(is_equal(mb6->state_get(),macro_body::PARSED));
00228         lassert(is_equal(mb6,mb6));
00229 
00230         ptr<macro_body> mb7 = macro_body::create();
00231         // tokens with ## inside and equivalent ws around
00232         lassert(is_equal(mb7->parse(ts),true));
00233         lassert(is_equal(mb7->state_get(),macro_body::PARSED));
00234         lassert(is_equal(mb7,mb7));
00235         lassert(is_equal(mb7,mb6));
00236 
00237         ptr<macro_body> mb8 = macro_body::create();
00238         // tokens with two ## successive ##
00239         lassert(is_equal(mb8->parse(ts),true));
00240         lassert(is_equal(mb8->state_get(),macro_body::PARSED));
00241         lassert(is_equal(mb8,mb8));
00242 
00243         // function-like macro
00244         ptr<macro_body> mb9 = macro_body::create();
00245 
00246         // eol
00247         lassert(is_equal(mb9->parse(ts,mh),true));
00248         lassert(is_equal(mb9->state_get(),macro_body::PARSED));
00249         lassert(is_equal(mb9,mb9));
00250 
00251         ptr<macro_body> mb10 = macro_body::create();
00252         // ## at the beginning
00253         lassert(is_equal(mb10->parse(ts,mh),false));
00254         lassert(is_equal(mb10->state_get(),macro_body::DEAD));
00255         lassert(!is_equal(mb10,mb9));
00256 
00257         ptr<macro_body> mb11 = macro_body::create();
00258         // ## at the end
00259         lassert(is_equal(mb11->parse(ts,mh),false));
00260         lassert(is_equal(mb11->state_get(),macro_body::DEAD));
00261         lassert(is_equal(mb11,mb10));
00262 
00263         ptr<macro_body> mb12 = macro_body::create();
00264         // ordinary tokens
00265         lassert(is_equal(mb12->parse(ts,mh),true));
00266         lassert(is_equal(mb12->state_get(),macro_body::PARSED));
00267         lassert(is_equal(mb12,mb12));
00268 
00269         ptr<macro_body> mb13 = macro_body::create();
00270         // ordinary tokens with equivalent ws
00271         lassert(is_equal(mb13->parse(ts,mh),true));
00272         lassert(is_equal(mb13->state_get(),macro_body::PARSED));
00273         lassert(is_equal(mb13,mb12));
00274 
00275         ptr<macro_body> mb14 = macro_body::create();
00276         // ordinary tokens with nonequivalent ws
00277         lassert(is_equal(mb14->parse(ts,mh),true));
00278         lassert(is_equal(mb14->state_get(),macro_body::PARSED));
00279         lassert(!is_equal(mb14,mb13));
00280 
00281         ptr<macro_body> mb15 = macro_body::create();
00282         // identifier and parameter
00283         lassert(is_equal(mb15->parse(ts,mh),true));
00284         lassert(is_equal(mb15->state_get(),macro_body::PARSED));
00285         lassert(is_equal(mb15,mb15));
00286 
00287         ptr<macro_body> mb16 = macro_body::create();
00288         // identifier and parameter with equivalent ws
00289         lassert(is_equal(mb16->parse(ts,mh),true));
00290         lassert(is_equal(mb16->state_get(),macro_body::PARSED));
00291         lassert(is_equal(mb16,mb16));
00292 
00293         ptr<macro_body> mb17 = macro_body::create();
00294         // # and parameter
00295         lassert(is_equal(mb17->parse(ts,mh),true));
00296         lassert(is_equal(mb17->state_get(),macro_body::PARSED));
00297         lassert(is_equal(mb17,mb17));
00298 
00299         ptr<macro_body> mb18 = macro_body::create();
00300         // # and identifier
00301         lassert(is_equal(mb18->parse(ts,mh),false));
00302         lassert(is_equal(mb18->state_get(),macro_body::DEAD));
00303 
00304         ptr<macro_body> mb19 = macro_body::create();
00305         // identifier ## and parameter
00306         lassert(is_equal(mb19->parse(ts,mh),true));
00307         lassert(is_equal(mb19->state_get(),macro_body::PARSED));
00308         lassert(is_equal(mb19,mb19));
00309 }
00310 
00311 end_package(lex);
00312 end_package(cplus);
00313 end_package(lang);
00314 end_package(lestes);
00315 
00316 int main(void)
00317 {
00318 	::lestes::lang::cplus::lex::macro_body_test();
00319         return 0;
00320 }
00321 /* vim: set ft=lestes : */
00322 

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