summaryrefslogblamecommitdiff
path: root/tools/debug-debug-scripts
blob: e5eeb6c2ce3ac70f71d5a7e99250aa98ba3977d1 (plain) (tree)
1
2
3
4
                     
                 

                                     


















                                                                           






















































                                                                  

                                                 
                              



                                                                                  





































                                                                                     
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function

copyright = '''
//    Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import itertools
import os
import subprocess
import sys
import tempfile

error = False

def eprint(s):
    print('Error:', s, file=sys.stderr)

def get_classes_from_file(a):
    global error
    d = {}
    execfile(a, d)
    for (k, v) in sorted(d.items()):
        try:
            name = v.name
        except AttributeError:
            if hasattr(v, 'enabled') and hasattr(v, 'tests'):
                name = 'None::' + k
            else:
                continue
        else:
            if name.split('::')[-1] != k:
                eprint('Mismatch: class %s is for %s' % (k, name))
                error = True
        if not v.enabled:
            eprint('Disabled: %s' % name)
            continue

        try:
            tests = v.tests
        except AttributeError:
            eprint('Unimplemented tests for %s' % name)
            error = True
            continue
        extra = getattr(v, 'test_extra', '').rstrip(' ')
        yield (k, tests, extra)

def c_quote(s):
    s = s.replace('\\', '\\\\')
    s = s.replace('"', '\\"')
    return '"' + s + '"'

def gen_test(name, expr, expected):
    print('static')
    print('void %s()' % name)
    print('{')
    print('    auto value = %s;' % expr)
    print('    const char *expected = %s;' % c_quote(expected))
    print('    do_breakpoint(value, expected);')
    print('}')

def main(args):
    print('// test.cpp - generated by', __file__)
    print(copyright)
    print('#include <cstdio>')
    print('// just mention "fwd.hpp" and "../poison.hpp" to make formatter happy')
    print('namespace tmwa')
    print('{')
    print('} // namespace tmwa')
    print()
    print('template<class T>')
    print('__attribute__((noinline))')
    print('void do_breakpoint(const T& value, const char *expected)')
    print('{')
    print('    (void)value;')
    print('    (void)expected;')
    print('    if (!expected) printf("printer test: %p = %s\\n", &value, expected);')
    print('}')

    names = []
    for a in args:
        basename, ext = os.path.splitext(a)
        assert ext == '.py'
        print()
        print('// Tests from', a)
        header = basename + '.hpp'
        print('#include "%s"' % header)

        for (k, tests, extra) in get_classes_from_file(a):
            print()
            print('// Tests for', k)
            print(extra)
            for (i, (expr, expected)) in enumerate(tests):
                name = 'testset_%s_subtest_%d' % (k, i)
                gen_test(name, expr, expected)
                names.append(name)
    print('int main()')
    print('{')
    for n in names:
        print('    %s();' % n)
    print('}')
    if error and not os.getenv('TMWA_FORCE_GENERATE'):
        sys.exit(1)


if __name__ == '__main__':
    main(sys.argv[1:])