File: ufctest.py

package info (click to toggle)
ffc 2018.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,172 kB
  • sloc: cpp: 45,285; python: 27,853; sh: 11,393; ansic: 1,069; makefile: 499
file content (64 lines) | stat: -rw-r--r-- 2,210 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2013 Anders Logg, Kristian B. Oelgaard and Marie E. Rognes
#
# This file is part of FFC.
#
# FFC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFC. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Martin Sandve Alnæs, 2013-2017

_test_code = """\
#include "../../ufctest.h"
#include "{prefix}.h"
#include <fstream>

int main(int argc, char * argv[])
{{
  const char jsonfilename[] = "{prefix}.json";
  std::ofstream jsonfile(jsonfilename);
  Printer printer(jsonfile);
  printer.begin();

{benchline}
{tests}

  printer.end();
  return 0;
}}
"""


def generate_test_code(header_file):
    "Generate test code for given header file."

    # Count the number of forms and elements
    prefix = header_file.split(".h")[0]
    generated_code = open(header_file).read()
    num_forms = generated_code.count("class %s_form_" % prefix.lower())
    num_elements = generated_code.count("class %s_finite_element_" % prefix.lower())

    # Generate tests, either based on forms or elements
    if num_forms > 0:
        benchline = "  bool bench = (argc > 1) && argv[1][0] == 'b';\n"
        tests = ['  {prefix}_form_{i} f{i}; test_form(f{i}, bench, {i}, printer);'.format(prefix=prefix.lower(), i=i)
                 for i in range(num_forms)]
    else:
        benchline = ""
        tests = ['  {prefix}_finite_element_{i} e{i}; test_finite_element(e{i}, {i}, printer);'.format(prefix=prefix.lower(), i=i)
                 for i in range(num_elements)]

    # Write file
    test_file = open(prefix + ".cpp", "w")
    test_file.write(_test_code.format(prefix=prefix, benchline=benchline, tests="\n".join(tests)))
    test_file.close()