Debian Codegraph @

test/regression/ufctest.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2013 Anders Logg, Kristian B. Oelgaard and Marie E. Rognes
3 #
4 # This file is part of FFC.
5 #
6 # FFC is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # FFC is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with FFC. If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Modified by Martin Sandve Alnæs, 2013-2017
20
21 _test_code = """\
22 #include "../../ufctest.h"
23 #include "{prefix}.h"
24 #include <fstream>
25
26 int main(int argc, char * argv[])
27 {{
28 const char jsonfilename[] = "{prefix}.json";
29 std::ofstream jsonfile(jsonfilename);
30 Printer printer(jsonfile);
31 printer.begin();
32
33 {benchline}
34 {tests}
35
36 printer.end();
37 return 0;
38 }}
39 """
40
41
42 def generate_test_code(header_file):
43 "Generate test code for given header file."
44
45 # Count the number of forms and elements
46 prefix = header_file.split(".h")[0]
47 generated_code = open(header_file).read()
48 num_forms = generated_code.count("class %s_form_" % prefix.lower())
49 num_elements = generated_code.count("class %s_finite_element_" % prefix.lower())
50
51 # Generate tests, either based on forms or elements
52 if num_forms > 0:
53 benchline = " bool bench = (argc > 1) && argv[1][0] == 'b';\n"
54 tests = [' {prefix}_form_{i} f{i}; test_form(f{i}, bench, {i}, printer);'.format(prefix=prefix.lower(), i=i)
55 for i in range(num_forms)]
56 else:
57 benchline = ""
58 tests = [' {prefix}_finite_element_{i} e{i}; test_finite_element(e{i}, {i}, printer);'.format(prefix=prefix.lower(), i=i)
59 for i in range(num_elements)]
60
61 # Write file
62 test_file = open(prefix + ".cpp", "w")
63 test_file.write(_test_code.format(prefix=prefix, benchline=benchline, tests="\n".join(tests)))
64 test_file.close()