~ubuntu-branches/ubuntu/trusty/hugin/trusty-proposed

« back to all changes in this revision

Viewing changes to src/hugin_base/makefilelib/test_filenames.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2011-01-06 14:28:24 UTC
  • mfrom: (1.1.9 upstream) (0.1.21 experimental)
  • Revision ID: james.westby@ubuntu.com-20110106142824-zn9lxylg5z44dynn
* Drop Cyril Brulebois from Uploaders. Thank you very much for your work.
* Bump package version. (rc3 was re-released as 2010.4.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This file is part of hugin.
 
3
 
 
4
hugin is free software: you can redistribute it and/or modify
 
5
it under the terms of the GNU General Public License as published by
 
6
the Free Software Foundation, either version 2 of the License, or
 
7
(at your option) any later version.
 
8
 
 
9
hugin is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU General Public License for more details.
 
13
 
 
14
You should have received a copy of the GNU General Public License
 
15
along with hugin.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
/**
 
19
 * @file test_filenames.cpp
 
20
 * @brief Tester that tries to feed a lot of possible characters
 
21
 * as filenames into the makefilelib, make, and the filesystem.
 
22
 *
 
23
 *  Created on: Jul 16, 2010
 
24
 * @author Florian Achleitner <florian.achleitner.2.6.31@gmail.com>
 
25
 */
 
26
 
 
27
#include "char_type.h"
 
28
 
 
29
#include "Comment.h"
 
30
#include "Variable.h"
 
31
#include "VariableDef.h"
 
32
#include "VariableRef.h"
 
33
#include "MakefileItem.h"
 
34
#include "Makefile.h"
 
35
#include "AutoVariable.h"
 
36
#include "Newline.h"
 
37
#include "Rule.h"
 
38
#include "Conditional.h"
 
39
#include "StringAdapter.h"
 
40
 
 
41
#include "test_util.h"
 
42
 
 
43
#include <iostream>
 
44
#include <vector>
 
45
 
 
46
using namespace makefile;
 
47
using namespace makefile::tester;
 
48
namespace fs = boost::filesystem;
 
49
 
 
50
#ifdef USE_WCHAR
 
51
ostream& cout =  std::wcout;
 
52
ostream& cerr =  std::wcerr;
 
53
#else
 
54
ostream& cout =  std::cout;
 
55
ostream& cerr =  std::cerr;
 
56
#endif
 
57
 
 
58
#define START 0x20
 
59
 
 
60
/**
 
61
 * Prints the tested characters.
 
62
 * @param out
 
63
 * @param limit
 
64
 */
 
65
void printchars(ostream& out, wchar_t limit)
 
66
{
 
67
        char_type c;
 
68
        for(c = 0x20; c < limit; c++)
 
69
        {
 
70
                out << c;
 
71
        }
 
72
 
 
73
}
 
74
/**
 
75
 * Tries to create files with names from 0x20 to limit using boost::filesystem.
 
76
 * @param dir
 
77
 * @param limit Upper limit for filenames
 
78
 * @return Filenames that couldn't be found after creation.
 
79
 */
 
80
std::vector<path> createfiles_direct(const path dir, uchar_type limit)
 
81
{
 
82
        std::vector<path> miss;
 
83
        char_type c[] = cstr("X.1");
 
84
        for(*c = START; static_cast<uchar_type>(*c) < limit; (*c)++)
 
85
        {
 
86
                path filename(c);
 
87
                ofstream file(dir / filename);
 
88
                file.close();
 
89
                if(!fs::exists(dir / filename))
 
90
                {
 
91
                        miss.push_back(filename);
 
92
                }
 
93
        }
 
94
        return miss;
 
95
}
 
96
/**
 
97
 * Tries to create files with names from 0x20 to limit by calling make.
 
98
 * It first writes a makefile of this structure:
 
99
@verbatim
 
100
FILENAME=X.1
 
101
 
 
102
all :
 
103
@touch $(FILENAME)
 
104
@endverbatim
 
105
 * Afterwards make is called with system() and creates the file by executing touch.
 
106
 *
 
107
 * @param dir
 
108
 * @param limit Upper limit for filenames
 
109
 * @return Filenames that couldn't be found after creation.
 
110
 */
 
111
std::vector<path> createfiles_make(const path dir, uchar_type limit)
 
112
{
 
113
        const path makefile(cstr("makefile"));
 
114
        std::vector<path> miss;
 
115
        char_type c[] = cstr("X.1");
 
116
        for(*c = START; static_cast<uchar_type>(*c) < limit; (*c)++)
 
117
        {
 
118
                path filename(c);
 
119
 
 
120
                // If the filename cannot be stored in a Variable, theres no chance to bring it through.
 
121
                try {
 
122
                        makefile::Variable mffilename(cstr("FILENAME"), (dir / filename).string(), makefile::Makefile::SHELL);
 
123
 
 
124
                        makefile::Rule touch;
 
125
                        touch.addTarget(cstr("all"));
 
126
                        touch.addCommand(cstr("@touch ") + mffilename.getRef().toString());
 
127
 
 
128
                        mffilename.getDef().add();
 
129
                        touch.add();
 
130
 
 
131
                        string dirstring = dir.string();
 
132
                        std::stringbuf makeout, makeerr;
 
133
                        int ret = exec_make(makeout, makeerr);
 
134
 
 
135
 
 
136
                        if(ret)
 
137
                        {
 
138
                                std::cerr << "make returned " << ret << std::endl;
 
139
                                std::cout << makeout.str();
 
140
                                std::cerr << makeerr.str();
 
141
                        }
 
142
                        }
 
143
                catch(std::exception& e) {
 
144
                        std::cerr << "Variable exception: " << e.what() << std::endl;
 
145
                }
 
146
 
 
147
                if(!fs::exists(dir / filename))
 
148
                {
 
149
                        miss.push_back(filename);
 
150
                }
 
151
        }
 
152
        return miss;
 
153
}
 
154
/**
 
155
 * Removes and recreates the output directories.
 
156
 * @param dir
 
157
 * @return 0 on success, 1 on error.
 
158
 */
 
159
int cleandir(path dir)
 
160
{
 
161
        if(fs::is_directory(dir))
 
162
                fs::remove_all(dir);
 
163
        if(!fs::create_directories(dir))
 
164
        {
 
165
                cerr << cstr("Error creating directory ") << dir.string() << std::endl;
 
166
                return 1;
 
167
        }
 
168
        return 0;
 
169
}
 
170
 
 
171
void printmiss(std::vector<path>::iterator start, std::vector<path>::iterator end)
 
172
{
 
173
        for(std::vector<path>::iterator i = start; i != end; i++)
 
174
        {
 
175
                string s = i->string();
 
176
                unsigned long first = 0;
 
177
                first = static_cast<uchar_type>(s[0]);
 
178
                cout << s << cstr("\t (0x") << std::hex << first << cstr(")\n");
 
179
        }
 
180
}
 
181
 
 
182
int main(int argc, char *argv[])
 
183
{
 
184
        uchar_type limit;
 
185
        if(argc != 2)
 
186
        {
 
187
                std::cerr << "Specify a limit as first argument" << std::endl;
 
188
                return 1;
 
189
        }else{
 
190
                limit = std::atoi(argv[1]);
 
191
        }
 
192
        std::cout << "Creating " << static_cast<unsigned long>(limit) - START << " files in" << std::endl;
 
193
 
 
194
        path basepath(fs::initial_path<path>() / cstr("chartest_direct"));
 
195
        path basepathmake(fs::initial_path<path>() / cstr("chartest_make"));
 
196
 
 
197
        cout << basepath.string() << std::endl;
 
198
        cout << basepathmake.string() << std::endl;
 
199
 
 
200
        if(cleandir(basepath) || cleandir(basepathmake))
 
201
                return 1;
 
202
 
 
203
//      ofstream outfile(basepath / cstr("tf.out"));
 
204
//      printchars(outfile, limit);
 
205
 
 
206
        std::vector<path> miss_direct = createfiles_direct(basepath, limit);
 
207
        cout << cstr("Direct: Missing files ") << miss_direct.size() << std::endl;
 
208
        printmiss(miss_direct.begin(), miss_direct.end());
 
209
        cout << std::endl;
 
210
 
 
211
        std::vector<path> miss_make = createfiles_make(basepathmake, limit);
 
212
        cout << cstr("Make: Missing files ") << miss_make.size() << std::endl;
 
213
        printmiss(miss_make.begin(), miss_make.end());
 
214
        cout << std::endl;
 
215
        return 0;
 
216
}