def test_get_language_ext(): r"""Test the get_language_ext method.""" test_lang = tools.get_supported_lang() for lang in test_lang: if (lang == 'executable') and (not platform._is_win): assert_raises(ValueError, languages.get_language_ext, lang) languages.get_language_ext(lang, default='') else: languages.get_language_ext(lang)
def test_get_language_ext(): r"""Test the get_language_ext method.""" test_lang = constants.LANGUAGES_WITH_ALIASES['all'] for lang in test_lang: if ((((lang == 'executable') and (not platform._is_win)) or (lang in ['function']))): assert_raises(ValueError, languages.get_language_ext, lang) languages.get_language_ext(lang, default='') else: languages.get_language_ext(lang)
def setup_model(cls, language, transform, language_ext=None, **kwargs): r"""Write the model file for the specified combination of language and type. Args: language (str): Language that model should be written in. transform (str): Transformation that should be performed. language_ext (str, optional): Extension that should be used for the model file. If not provided, the extension is determined from the specified language. **kwargs: Additional keyword arguments are passed to the write_function_def class method of the language driver. Returns: tuple(str, dict): Full path to the file that was written and the environment variables that should be set before running the integration. """ if language_ext is None: language_ext = get_language_ext(language) modelfile = os.path.join(_example_dir, cls.example_name, 'src', 'model' + language_ext) drv = import_component('model', language) testdata = cls.get_test_data() testtype = encode_type(testdata) inputs = [{'name': 'x', 'datatype': copy.deepcopy(testtype)}] outputs = [{'name': 'y', 'datatype': copy.deepcopy(testtype)}] # Write the model function_contents = [] for i, o in zip(inputs, outputs): function_contents += drv.write_assign_to_output( o, i, outputs_in_inputs=drv.outputs_in_inputs) lines = drv.write_function_def('model', function_contents=function_contents, inputs=copy.deepcopy(inputs), outputs=copy.deepcopy(outputs), outputs_in_inputs=drv.outputs_in_inputs, opening_msg='IN MODEL', closing_msg='MODEL EXIT', print_inputs=True, print_outputs=True, **kwargs) with open(modelfile, 'w') as fd: print(modelfile) print('\n'.join(lines)) fd.write('\n'.join(lines)) env = {} env['TEST_LANGUAGE'] = language env['TEST_LANGUAGE_EXT'] = language_ext env['TEST_TRANSFORM'] = transform if transform == 'table': env['TEST_MODEL_IO'] = ('outputs:\n' + ' - name: ' + language + '_model:output\n' + ' format_str: "%s\\t%d\\t%f\\n"') return modelfile, env
def test_get_language_ext(): r"""Test the get_language_ext method.""" test_lang = constants.LANGUAGES_WITH_ALIASES['all'] for lang in test_lang: if ((((lang == 'executable') and (not platform._is_win)) or (lang in ['dummy', 'mpi']))): with pytest.raises(ValueError): languages.get_language_ext(lang) languages.get_language_ext(lang, default='') else: languages.get_language_ext(lang)
def env(self, example_name, language, transform, example_module): r"""dict: Environment variables set for the test.""" language_ext = get_language_ext(language) modelfile = os.path.join(_example_dir, example_name, 'src', 'model' + language_ext) drv = import_component('model', language) testdata = example_module.get_test_data(transform) testtype = encode_type(testdata) inputs = [{'name': 'x', 'datatype': copy.deepcopy(testtype)}] outputs = [{'name': 'y', 'datatype': copy.deepcopy(testtype)}] # Write the model function_contents = [] for i, o in zip(inputs, outputs): function_contents += drv.write_assign_to_output( o, i, outputs_in_inputs=drv.outputs_in_inputs) lines = drv.write_function_def('model', function_contents=function_contents, inputs=copy.deepcopy(inputs), outputs=copy.deepcopy(outputs), outputs_in_inputs=drv.outputs_in_inputs, opening_msg='IN MODEL', closing_msg='MODEL EXIT', print_inputs=True, print_outputs=True) with open(modelfile, 'w') as fd: print(modelfile) print('\n'.join(lines)) fd.write('\n'.join(lines)) env = {} env['TEST_LANGUAGE'] = language env['TEST_LANGUAGE_EXT'] = language_ext env['TEST_TRANSFORM'] = transform if transform == 'table': env['TEST_MODEL_IO'] = ('outputs:\n' + ' - name: ' + language + '_model:output\n' + ' format_str: "%s\\t%d\\t%f\\n"') try: yield env finally: if os.path.isfile(modelfile): os.remove(modelfile)
import argparse from yggdrasil.examples.tests.test_transforms import TestExampleTransforms from yggdrasil.languages import get_language_ext from yggdrasil.runner import run _this_dir = os.path.dirname(__file__) def main(language, transform, language_ext=None): yamlfile = os.path.join(_this_dir, 'transforms.yml') modelfile, env = TestExampleTransforms.setup_model( language, transform, language_ext=language_ext) os.environ.update(env) try: run(yamlfile) finally: if os.path.isfile(modelfile): os.remove(modelfile) if __name__ == "__main__": parser = argparse.ArgumentParser( "Test application of a transform for the specified language.") # TODO: Set to list of installed languages/transforms? parser.add_argument('language', help='Language that should be tested.') parser.add_argument('transform', help='Name of data transform that should be tested.') args = parser.parse_args() args.language_ext = get_language_ext(args.language) main(args.language, args.transform, args.language_ext)
import os import glob import logging from yggdrasil import tools, languages, serialize, constants # TODO: This can be generated from the drivers ext_map = dict(constants.LANG2EXT, executable='', make='.cpp', cmake='.cpp', function='', osr='.xml', sbml='.xml') for lang in tools.get_supported_lang(): if lang.lower() not in ext_map: ext_map[lang.lower()] = languages.get_language_ext(lang) _example_dir = os.path.dirname(__file__) def register_example(example_dir): r"""Register an example based on the contents of the director. Args: example_dir (str): Full path to a directory potentially containing an example. Returns: tuple (list, dict, dict): A list of available languages, a dictionary mapping from language to YAML specification files for the example, and a dictionary mapping from language to source files for the example.
def setup_model(cls, language, typename, language_ext=None, using_pointers=False, using_generics=False, split_array=False, dont_add_lengths=False, length_prefix=False, assign_kws=None, **kwargs): r"""Write the model file for the specified combination of language and type. Args: language (str): Language that model should be written in. typename (str): Type that should be expected by the model. language_ext (str, optional): Extension that should be used for the model file. If not provided, the extension is determined from the specified language. using_pointers (bool, optional): If True and the tested language supports pointers, pointers will be used rather than explicit arrays. Defaults to False. using_generics (bool, optional): If True and the tested language has a dedicated generic class, the generic type will be used rather than explict types. Defaults to False. split_array (bool, optional): If True and the tested datatype is an array, the variables will be split and specified explicitly in the yaml. Defaults to False. dont_add_lengths (bool, optional): If True, lengths will not be added to the definition or assignments. Defaults to False. length_prefix (bool, optional): If True, the length variables will be given prefixes instead of suffixes. Defaults to False. assign_kws (dict, optional): Keyword arguments for the calls to write_assign_to_output. Defaults to {}. **kwargs: Additional keyword arguments are passed to the write_function_def class method of the language driver. Returns: str: Full path to the file that was written. """ if assign_kws is None: assign_kws = {} if language in ['c', 'c++', 'cpp']: # dont_add_lengths is only valid for C/C++ kwargs['dont_add_lengths'] = dont_add_lengths kwargs['use_length_prefix'] = length_prefix assign_kws.setdefault('dont_add_lengths', dont_add_lengths) assign_kws.setdefault('use_length_prefix', length_prefix) yaml_fields = {'vars': False, 'dtype': False} if language_ext is None: language_ext = get_language_ext(language) modelfile = os.path.join(_example_dir, cls.example_name, 'src', 'model' + language_ext) drv = import_component('model', language) if using_generics and drv.is_typed: testtype = {'type': 'any'} else: testdata = cls.get_test_data(typename) testtype = encode_type(testdata) using_generics = False if split_array and (typename == 'array'): inputs = [{'name': 'x%d' % i, 'datatype': x} for i, x in enumerate(copy.deepcopy(testtype['items']))] outputs = [{'name': 'y%d' % i, 'datatype': x} for i, x in enumerate(copy.deepcopy(testtype['items']))] else: inputs = [{'name': 'x', 'datatype': copy.deepcopy(testtype)}] outputs = [{'name': 'y', 'datatype': copy.deepcopy(testtype)}] # Write the model function_contents = [] for i, o in zip(inputs, outputs): if using_pointers and drv.is_typed: for k in ['shape', 'length']: i['datatype'].pop(k, None) o['datatype'].pop(k, None) function_contents += drv.write_assign_to_output( o, i, outputs_in_inputs=drv.outputs_in_inputs, **assign_kws) lines = drv.write_function_def( 'model', function_contents=function_contents, inputs=copy.deepcopy(inputs), outputs=copy.deepcopy(outputs), outputs_in_inputs=drv.outputs_in_inputs, opening_msg='IN MODEL', closing_msg='MODEL EXIT', print_inputs=True, print_outputs=True, **kwargs) with open(modelfile, 'w') as fd: print(modelfile) print('\n'.join(lines)) fd.write('\n'.join(lines)) os.environ['TEST_LANGUAGE'] = language os.environ['TEST_LANGUAGE_EXT'] = language_ext os.environ['TEST_TYPENAME'] = typename if (language in ['c', 'fortran']) and (not using_generics): yaml_fields['vars'] = True if typename in ['array', 'object']: yaml_fields['dtype'] = True if any(list(yaml_fields.values())): lines = [] for io, io_vars in zip(['input', 'output'], [inputs, outputs]): lines += [io + 's:', ' name: %s' % io] if yaml_fields['vars']: lines.append( ' vars: %s' % cls.get_varstr( io_vars, language, using_pointers=using_pointers, length_prefix=length_prefix, dont_add_lengths=dont_add_lengths)) if yaml_fields['dtype']: if len(io_vars) == 1: dtype = io_vars[0]['datatype'] else: dtype = {'type': 'array', 'items': [x['datatype'] for x in io_vars]} lines.append(' datatype:') for x in yaml.dump(dtype).splitlines(): if "units: ''" in x: continue lines.append(' ' + x) os.environ['TEST_MODEL_IO'] = '\n '.join(lines) + '\n' else: os.environ['TEST_MODEL_IO'] = '' return modelfile
def env(self, example_name, language, typename, using_pointers, using_generics, split_array, dont_add_lengths, length_prefix, example_module): r"""dict: Environment variables set for the test.""" kwargs = {} assign_kws = {} if language in ['c', 'c++', 'cpp']: # dont_add_lengths is only valid for C/C++ kwargs['dont_add_lengths'] = dont_add_lengths kwargs['use_length_prefix'] = length_prefix assign_kws.setdefault('dont_add_lengths', dont_add_lengths) assign_kws.setdefault('use_length_prefix', length_prefix) yaml_fields = {'vars': False, 'dtype': False} language_ext = get_language_ext(language) modelfile = os.path.join(os.path.dirname(__file__), example_name, 'src', 'model' + language_ext) drv = import_component('model', language) if using_generics and drv.is_typed: testtype = {'type': 'any'} else: testdata = example_module.get_test_data(typename) testtype = encode_type(testdata) using_generics = False if split_array and (typename == 'array'): inputs = [{ 'name': 'x%d' % i, 'datatype': x } for i, x in enumerate(copy.deepcopy(testtype['items']))] outputs = [{ 'name': 'y%d' % i, 'datatype': x } for i, x in enumerate(copy.deepcopy(testtype['items']))] else: inputs = [{'name': 'x', 'datatype': copy.deepcopy(testtype)}] outputs = [{'name': 'y', 'datatype': copy.deepcopy(testtype)}] # Write the model function_contents = [] for i, o in zip(inputs, outputs): if using_pointers and drv.is_typed: for k in ['shape', 'length']: i['datatype'].pop(k, None) o['datatype'].pop(k, None) function_contents += drv.write_assign_to_output( o, i, outputs_in_inputs=drv.outputs_in_inputs, **assign_kws) lines = drv.write_function_def('model', function_contents=function_contents, inputs=copy.deepcopy(inputs), outputs=copy.deepcopy(outputs), outputs_in_inputs=drv.outputs_in_inputs, opening_msg='IN MODEL', closing_msg='MODEL EXIT', print_inputs=True, print_outputs=True, **kwargs) with open(modelfile, 'w') as fd: print(modelfile) print('\n'.join(lines)) fd.write('\n'.join(lines)) env = {} env['TEST_LANGUAGE'] = language env['TEST_LANGUAGE_EXT'] = language_ext env['TEST_TYPENAME'] = typename if (language in ['c', 'fortran']) and (not using_generics): yaml_fields['vars'] = True if typename in ['array', 'object']: yaml_fields['dtype'] = True if any(list(yaml_fields.values())): lines = [] for io, io_vars in zip(['input', 'output'], [inputs, outputs]): lines += [io + 's:', ' name: %s' % io] if yaml_fields['vars']: lines.append( ' vars: %s' % self.get_varstr(io_vars, language, using_pointers=using_pointers, length_prefix=length_prefix, dont_add_lengths=dont_add_lengths)) if yaml_fields['dtype']: if len(io_vars) == 1: dtype = io_vars[0]['datatype'] else: dtype = { 'type': 'array', 'items': [x['datatype'] for x in io_vars] } lines.append(' datatype:') for x in yaml.dump(dtype).splitlines(): if "units: ''" in x: continue lines.append(' ' + x) env['TEST_MODEL_IO'] = '\n '.join(lines) + '\n' else: env['TEST_MODEL_IO'] = '' try: yield env finally: if os.path.isfile(modelfile): os.remove(modelfile)