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

« back to all changes in this revision

Viewing changes to src/hugin_base/makefilelib/test_makefilelib.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_makefilelib.cpp
 
20
 * @brief Tests all features of the lib.
 
21
 *  Created on: Jul 24, 2010
 
22
 * @author Florian Achleitner <florian.achleitner.2.6.31@gmail.com>
 
23
 */
 
24
 
 
25
#include "char_type.h"
 
26
#include <iostream>
 
27
#include <stdexcept>
 
28
#include <cstdio>
 
29
#include <fstream>
 
30
#include "Comment.h"
 
31
#include "Variable.h"
 
32
#include "VariableDef.h"
 
33
#include "VariableRef.h"
 
34
#include "MakefileItem.h"
 
35
#include "Makefile.h"
 
36
#include "AutoVariable.h"
 
37
#include "Newline.h"
 
38
#include "Rule.h"
 
39
#include "Conditional.h"
 
40
#include "StringAdapter.h"
 
41
#include "Manager.h"
 
42
 
 
43
#include "test_util.h"
 
44
 
 
45
#include <boost/iostreams/stream.hpp>
 
46
#include <boost/iostreams/device/file_descriptor.hpp>
 
47
 
 
48
using namespace makefile;
 
49
namespace fs = boost::filesystem;
 
50
namespace io = boost::iostreams;
 
51
 
 
52
namespace makefile { namespace tester {
 
53
 
 
54
#ifdef USE_WCHAR
 
55
ostream& cout =  std::wcout;
 
56
ostream& cerr =  std::wcerr;
 
57
#else
 
58
ostream& cout =  std::cout;
 
59
ostream& cerr =  std::cerr;
 
60
#endif
 
61
 
 
62
struct TestComment : public Test
 
63
{
 
64
        Manager mgr;
 
65
        TestComment()
 
66
        :Test("Comment", "")
 
67
        {
 
68
                Comment* comment = mgr.own(new Comment(cstr("First line")));
 
69
                comment->appendLine(cstr("second line"));
 
70
                comment->appendLine(cstr("third line\nfourth\r line"));
 
71
                comment->add();
 
72
        }
 
73
};
 
74
 
 
75
struct TestRule : public Test
 
76
{
 
77
        Manager mgr;
 
78
        TestRule()
 
79
        :Test("Rule", "cp 1.in 1.out\ncp 2.in 2.out\nHello Make\n")
 
80
        {
 
81
                Rule& rule = *mgr.own(new Rule());
 
82
                Rule& rule2 = *mgr.own(new Rule());
 
83
                rule.addTarget(cstr("all"));
 
84
                rule.addPrereq(cstr("1.out"));
 
85
                rule.addPrereq(cstr("2.out"));
 
86
                rule.addCommand(cstr("@echo Hello Make"));
 
87
                rule.add();
 
88
 
 
89
                rule2.addTarget(cstr("%.out"));
 
90
                rule2.addPrereq(cstr("%.in"));
 
91
                rule2.addCommand(cstr("cp $*.in $@"));
 
92
                rule2.add();
 
93
 
 
94
                std::ofstream in1("1.in"); in1.close();
 
95
                std::ofstream in2("2.in"); in2.close();
 
96
        }
 
97
 
 
98
        bool precond()
 
99
        {
 
100
                return fs::exists("1.out") && fs::exists("2.out");
 
101
        }
 
102
        ~TestRule()
 
103
        {
 
104
                fs::remove("1.in"); fs::remove("2.in"); fs::remove("1.out"); fs::remove("2.out");
 
105
        }
 
106
};
 
107
 
 
108
struct TestConditional : public Test
 
109
{
 
110
        Manager mgr;
 
111
 
 
112
        TestConditional()
 
113
        :Test("Conditional",
 
114
                        "Results:\n"
 
115
                        "cond1 is true\n"
 
116
                        "cond2 is false\n"
 
117
                        "cond3 is true\n"
 
118
                        "cond4 is false\n")
 
119
        {
 
120
                Variable& var1 = *mgr.own(new Variable(cstr("VAR1"), cstr("equal")));
 
121
                Variable& var2 = *mgr.own(new Variable(cstr("VAR2"), cstr("equal")));
 
122
                Variable& var3 = *mgr.own(new Variable(cstr("VAR3"), cstr("nequal")));
 
123
                Variable& ifvar1 = *mgr.own(new Variable(cstr("TESTVAR1"), cstr("cond1 is true")));
 
124
                Variable& ifvar2 = *mgr.own(new Variable(cstr("TESTVAR2"), cstr("cond2 is true")));
 
125
                Variable& ifvar3 = *mgr.own(new Variable(cstr("TESTVAR3"), cstr("cond3 is true")));
 
126
                Variable& ifvar4 = *mgr.own(new Variable(cstr("TESTVAR4"), cstr("cond4 is true")));
 
127
                Variable& elsevar1 = *mgr.own(new Variable(ifvar1.getName(), cstr("cond1 is false")));
 
128
                Variable& elsevar2 = *mgr.own(new Variable(ifvar2.getName(), cstr("cond2 is false")));
 
129
                Variable& elsevar3 = *mgr.own(new Variable(ifvar3.getName(), cstr("cond3 is false")));
 
130
                Variable& elsevar4 = *mgr.own(new Variable(ifvar4.getName(), cstr("cond4 is false")));
 
131
                ConditionalDEF& cdef1 = *mgr.own(new ConditionalDEF(var1.getName()));
 
132
                ConditionalDEF& cdef2 = *mgr.own(new ConditionalDEF(cstr("THISISNOTDEFINED")));
 
133
                ConditionalEQ& ceq1 = *mgr.own(new ConditionalEQ(var1.getRef(), var2.getRef()));
 
134
                ConditionalEQ& ceq2 = *mgr.own(new ConditionalEQ(var1.getRef(), var3.getRef()));
 
135
                Rule& rule = *mgr.own(new Rule());
 
136
 
 
137
                var1.getDef().add();
 
138
                var2.getDef().add();
 
139
                var3.getDef().add();
 
140
                rule.addTarget(cstr("all"));
 
141
                rule.addCommand(cstr("@echo Results:"));
 
142
                rule.addCommand(cstr("@echo ") + ifvar1.getRef());
 
143
                rule.addCommand(cstr("@echo ") + ifvar2.getRef());
 
144
                rule.addCommand(cstr("@echo ") + ifvar3.getRef());
 
145
                rule.addCommand(cstr("@echo ") + ifvar4.getRef());
 
146
 
 
147
                cdef1.addToIf(ifvar1.getDef());
 
148
                cdef1.addToElse(elsevar1.getDef());
 
149
                cdef2.addToIf(ifvar2.getDef());
 
150
                cdef2.addToElse(elsevar2.getDef());
 
151
 
 
152
                ceq1.addToIf(ifvar3.getDef());
 
153
                ceq1.addToElse(elsevar3.getDef());
 
154
                ceq2.addToIf(ifvar4.getDef());
 
155
                ceq2.addToElse(elsevar4.getDef());
 
156
 
 
157
                cdef1.add();
 
158
                cdef2.add();
 
159
                ceq1.add();
 
160
                ceq2.add();
 
161
                rule.add();
 
162
 
 
163
//              ofstream mf("test.mk");
 
164
//              Makefile::getSingleton().writeMakefile(mf);
 
165
        }
 
166
};
 
167
 
 
168
struct TestVariable : public Test
 
169
{
 
170
        Manager mgr;
 
171
        const std::vector<string> values;
 
172
        TestVariable()
 
173
        : Test("Variable",
 
174
                "a single value\n"
 
175
                "3.14592\n"
 
176
                "a single word__another word__value3\n")
 
177
        {
 
178
                Variable* v1 = mgr.own(new Variable(cstr("VAR1"), cstr("a single value")));
 
179
                Variable* v2 = mgr.own(new Variable(cstr("VAR2"), 3.14592));
 
180
                std::vector<string> values;
 
181
                values.push_back("a single word");
 
182
                values.push_back("another word");
 
183
                values.push_back("value3");
 
184
                Variable* v3 = mgr.own(new Variable(cstr("VAR3"), values.begin(), values.end(),
 
185
                                Makefile::SHELL, cstr("__")));
 
186
 
 
187
                v1->getDef().add(); v2->getDef().add(); v3->getDef().add();
 
188
 
 
189
                Rule* rule = mgr.own_add(new Rule());
 
190
                rule->addTarget(cstr("all"));
 
191
                rule->addCommand(cstr("@echo ") + v1->getRef());
 
192
                rule->addCommand(cstr("@echo ") + v2->getRef());
 
193
                rule->addCommand(cstr("@echo ") + v3->getRef());
 
194
        }
 
195
};
 
196
 
 
197
void do_test(bool& result, Test* test)
 
198
{
 
199
        result &= test->run();
 
200
        delete test;
 
201
}
 
202
}} // namespace
 
203
using namespace makefile::tester;
 
204
 
 
205
int main(int argc, char *argv[])
 
206
{
 
207
        bool result = true;
 
208
        do_test(result, new TestComment);
 
209
        do_test(result, new TestRule);
 
210
        do_test(result, new TestConditional);
 
211
        do_test(result, new TestVariable);
 
212
        return !result;         // return 0 on success (= !true)
 
213
}
 
214
 
 
215