test_filenames.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of hugin.
00003 
00004 hugin is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation, either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 hugin is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with hugin.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00027 #include "char_type.h"
00028 
00029 #include "Comment.h"
00030 #include "Variable.h"
00031 #include "VariableDef.h"
00032 #include "VariableRef.h"
00033 #include "MakefileItem.h"
00034 #include "Makefile.h"
00035 #include "AutoVariable.h"
00036 #include "Newline.h"
00037 #include "Rule.h"
00038 #include "Conditional.h"
00039 #include "StringAdapter.h"
00040 
00041 #include "test_util.h"
00042 
00043 #include <iostream>
00044 #include <vector>
00045 
00046 using namespace makefile;
00047 using namespace makefile::tester;
00048 namespace fs = boost::filesystem;
00049 
00050 #ifdef USE_WCHAR
00051 ostream& cout =  std::wcout;
00052 ostream& cerr =  std::wcerr;
00053 #else
00054 ostream& cout =  std::cout;
00055 ostream& cerr =  std::cerr;
00056 #endif
00057 
00058 #define START 0x20
00059 
00065 void printchars(ostream& out, wchar_t limit)
00066 {
00067         char_type c;
00068         for(c = 0x20; c < limit; c++)
00069         {
00070                 out << c;
00071         }
00072 
00073 }
00080 std::vector<path> createfiles_direct(const path dir, uchar_type limit)
00081 {
00082         std::vector<path> miss;
00083         char_type c[] = cstr("X.1");
00084         for(*c = START; static_cast<uchar_type>(*c) < limit; (*c)++)
00085         {
00086                 path filename(c);
00087                 ofstream file(dir / filename);
00088                 file.close();
00089                 if(!fs::exists(dir / filename))
00090                 {
00091                         miss.push_back(filename);
00092                 }
00093         }
00094         return miss;
00095 }
00111 std::vector<path> createfiles_make(const path dir, uchar_type limit)
00112 {
00113         const path makefile(cstr("makefile"));
00114         std::vector<path> miss;
00115         char_type c[] = cstr("X.1");
00116         for(*c = START; static_cast<uchar_type>(*c) < limit; (*c)++)
00117         {
00118                 path filename(c);
00119 
00120                 // If the filename cannot be stored in a Variable, theres no chance to bring it through.
00121                 try {
00122                         makefile::Variable mffilename(cstr("FILENAME"), (dir / filename).string(), makefile::Makefile::SHELL);
00123 
00124                         makefile::Rule touch;
00125                         touch.addTarget(cstr("all"));
00126                         touch.addCommand(cstr("@touch ") + mffilename.getRef().toString());
00127 
00128                         mffilename.getDef().add();
00129                         touch.add();
00130 
00131                         string dirstring = dir.string();
00132                         std::stringbuf makeout, makeerr;
00133                         int ret = exec_make(makeout, makeerr);
00134 
00135 
00136                         if(ret)
00137                         {
00138                                 std::cerr << "make returned " << ret << std::endl;
00139                                 std::cout << makeout.str();
00140                                 std::cerr << makeerr.str();
00141                         }
00142                         }
00143                 catch(std::exception& e) {
00144                         std::cerr << "Variable exception: " << e.what() << std::endl;
00145                 }
00146 
00147                 if(!fs::exists(dir / filename))
00148                 {
00149                         miss.push_back(filename);
00150                 }
00151         }
00152         return miss;
00153 }
00159 int cleandir(path dir)
00160 {
00161         if(fs::is_directory(dir))
00162                 fs::remove_all(dir);
00163         if(!fs::create_directories(dir))
00164         {
00165                 cerr << cstr("Error creating directory ") << dir.string() << std::endl;
00166                 return 1;
00167         }
00168         return 0;
00169 }
00170 
00171 void printmiss(std::vector<path>::iterator start, std::vector<path>::iterator end)
00172 {
00173         for(std::vector<path>::iterator i = start; i != end; i++)
00174         {
00175                 string s = i->string();
00176                 unsigned long first = 0;
00177                 first = static_cast<uchar_type>(s[0]);
00178                 cout << s << cstr("\t (0x") << std::hex << first << cstr(")\n");
00179         }
00180 }
00181 
00182 int main(int argc, char *argv[])
00183 {
00184         uchar_type limit;
00185         if(argc != 2)
00186         {
00187                 std::cerr << "Specify a limit as first argument" << std::endl;
00188                 return 1;
00189         }else{
00190                 limit = std::atoi(argv[1]);
00191         }
00192         std::cout << "Creating " << static_cast<unsigned long>(limit) - START << " files in" << std::endl;
00193 
00194         path basepath(fs::initial_path<path>() / cstr("chartest_direct"));
00195         path basepathmake(fs::initial_path<path>() / cstr("chartest_make"));
00196 
00197         cout << basepath.string() << std::endl;
00198         cout << basepathmake.string() << std::endl;
00199 
00200         if(cleandir(basepath) || cleandir(basepathmake))
00201                 return 1;
00202 
00203 //      ofstream outfile(basepath / cstr("tf.out"));
00204 //      printchars(outfile, limit);
00205 
00206         std::vector<path> miss_direct = createfiles_direct(basepath, limit);
00207         cout << cstr("Direct: Missing files ") << miss_direct.size() << std::endl;
00208         printmiss(miss_direct.begin(), miss_direct.end());
00209         cout << std::endl;
00210 
00211         std::vector<path> miss_make = createfiles_make(basepathmake, limit);
00212         cout << cstr("Make: Missing files ") << miss_make.size() << std::endl;
00213         printmiss(miss_make.begin(), miss_make.end());
00214         cout << std::endl;
00215         return 0;
00216 }

Generated on 28 Nov 2014 for Hugintrunk by  doxygen 1.4.7