Example #1
0
def test_parse_header():
    r"""Test parsing header."""
    header = [
        b"# name\tnumber\tvalue\tcomplex\n", b"# n/a\tg\tcm\tn/a\n",
        b"# %5s\t%ld\t%lf\t%g%+gj\n"
    ]
    res = dict(delimiter=b'\t',
               comment=b'# ',
               newline=b'\n',
               format_str=b"%5s\t%ld\t%lf\t%g%+gj\n",
               fmts=[b'%5s', b'%ld', b'%lf', b'%g%+gj'],
               field_names=[b'name', b'number', b'value', b'complex'],
               field_units=[b'n/a', b'g', b'cm', b'n/a'])
    assert_equal(serialize.parse_header(header), res)
    assert_equal(serialize.parse_header(header[::-1]), res)
    _empty = b''
    assert_equal(serialize.parse_header(_empty.join(header)), res)
    # Test without formats
    header2 = header[:2]
    res2 = dict(**res)
    del res2['format_str']
    res2['fmts'] = []
    assert_equal(serialize.parse_header(header2), res2)
    # Test with explicit line numbers
    assert_equal(
        serialize.parse_header(header, lineno_names=0, lineno_units=1), res)
    # Test errors
    header3 = [header[0], header[0]]
    assert_raises(RuntimeError, serialize.parse_header, header3)
    header4 = [header[1], header[1]]
    assert_raises(RuntimeError, serialize.parse_header, header4)
Example #2
0
def test_dict2list():
    r"""Test conversion of a dictionary to a list and back."""
    assert_raises(TypeError, serialize.dict2list, None)
    assert_raises(TypeError, serialize.list2dict, None)
    x = {'c': 0, 'b': 1, 'a': 2}
    y = [2, 1, 0]
    assert_equal(serialize.dict2list(x), y)
Example #3
0
def test_definition2dtype_errors():
    r"""Check that error raised if type not specified."""
    assert_raises(KeyError, ScalarMetaschemaProperties.definition2dtype, {})
    assert_raises(RuntimeError, ScalarMetaschemaProperties.definition2dtype,
                  {'type': 'float'})
    assert_equal(ScalarMetaschemaProperties.definition2dtype({'type': 'bytes'}),
                 np.dtype((ScalarMetaschemaProperties._valid_types['bytes'])))
Example #4
0
 def test_send_recv(self, msg_send=None):
     r"""Test routing of a short message between client and server."""
     try:
         if msg_send is None:
             msg_send = self.test_msg
         T = self.instance.start_timeout()
         while ((not T.is_out) and (not self.instance.is_valid)):
             self.instance.sleep()  # pragma: debug
         self.instance.stop_timeout()
         # Send a message to local output
         flag = self.send_comm.send(msg_send)
         assert(flag)
         # Receive on server side, then send back
         flag, srv_msg = self.recv_comm.recv(timeout=self.route_timeout)
         assert(flag)
         assert_equal(srv_msg, msg_send)
         self.instance.printStatus()
         flag = self.recv_comm.send(srv_msg)
         assert(flag)
         # Receive response on client side
         flag, cli_msg = self.send_comm.recv(timeout=self.route_timeout)
         assert(flag)
         assert_equal(cli_msg, msg_send)
     except BaseException:  # pragma: debug
         self.send_comm.printStatus()
         self.instance.printStatus(verbose=True)
         self.recv_comm.printStatus()
         raise
Example #5
0
    def test_send_recv_raw(self):
        r"""Test send/recv of a small message."""
        if self.comm in [
                'CommBase', 'AsyncComm', 'ValueComm', 'ForkComm', 'ServerComm',
                'ClientComm'
        ]:
            return

        def dummy(msg):
            print(msg)
            msg.msg = b''
            msg.args = b''
            msg.length = 0
            return msg

        assert (self.send_instance.send(self.test_msg))
        msg = self.recv_instance.recv(timeout=60.0,
                                      skip_deserialization=True,
                                      return_message_object=True,
                                      after_finalize_message=[dummy])
        assert (msg.finalized)
        assert_equal(self.recv_instance.finalize_message(msg), msg)
        msg.finalized = False
        assert (self.recv_instance.is_empty_recv(msg.args))
        msg = self.recv_instance.finalize_message(msg)
        assert_equal(msg.flag, CommBase.FLAG_EMPTY)
Example #6
0
    def check_received_data(cls, transform, x_recv):
        r"""Check that the received message is equivalent to the
        test data for the specified type.

        Args:
            transform (str): Name of transform being tested.
            x_recv (object): Received object.

        Raises:
            AssertionError: If the received message is not equivalent
                to the received message.

        """
        try:
            t = create_component('transform', subtype=transform)
        except ValueError:

            def t(x):
                return x

        x_sent = t(cls.get_test_data())
        print('RECEIVED:')
        tools.pprint_encoded(x_recv)
        print('EXPECTED:')
        tools.pprint_encoded(x_sent)
        assert_equal(x_recv, x_sent)
Example #7
0
def test_wait_for_creation():
    r"""Test FileComm waiting for creation."""
    msg_send = b'Test message\n'
    name = 'temp_file_create.txt'
    kwargs = {'in_temp': True, 'comm': 'FileComm', 'dont_open': True}
    # kwargs = {'wait_for_creation': 5, 'in_temp': True, comm='FileComm'}
    send_instance = new_comm(name, direction='send', **kwargs)
    recv_instance = new_comm(name, direction='recv',
                             wait_for_creation=5.0, **kwargs)
    if os.path.isfile(send_instance.address):
        os.remove(send_instance.address)
    
    def open_and_send(inst, msg):
        inst.open()
        flag = inst.send(msg)
        return flag
    
    send_instance.sched_task(0.5, open_and_send, args=[send_instance, msg_send],
                             store_output=True)
    recv_instance.open()
    T = recv_instance.start_timeout(recv_instance.wait_for_creation)
    while (not T.is_out) and (send_instance.sched_out is None):  # pragma: debug
        recv_instance.sleep()
    recv_instance.stop_timeout()
    assert(send_instance.sched_out)
    flag, msg_recv = recv_instance.recv()
    assert(flag)
    assert_equal(msg_recv, msg_send)
    send_instance.close()
    recv_instance.close()
    recv_instance.remove_file()
Example #8
0
def test_format_header():
    r"""Test formatting header."""
    kws_all = dict(field_names=['name', 'number', 'value', 'complex'],
                   field_units=['n/a', 'g', 'cm', 'n/a'])
    res_all = dict(names="# name\tnumber\tvalue\tcomplex\n",
                   units="# n/a\tg\tcm\tn/a\n")
    if platform._is_win:  # pragma: windows
        kws_all['format_str'] = "%5s\t%l64d\t%g\t%g%+gj\n"
        res_all['format'] = "# " + kws_all['format_str']
    else:
        kws_all['format_str'] = "%5s\t%ld\t%g\t%g%+gj\n"
        res_all['format'] = "# " + kws_all['format_str']
    kws_all['dtype'] = serialize.cformat2nptype(kws_all['format_str'],
                                                names=kws_all['field_names'])
    for x in [kws_all, res_all]:
        for k, v in x.items():
            if isinstance(v, str):
                x[k] = backwards.as_bytes(v)
            elif isinstance(v, list):
                x[k] = [backwards.as_bytes(iv) for iv in v]
    test_list = [(['format_str', 'field_names',
                   'field_units'], ['names', 'units', 'format']),
                 (['field_names', 'field_units',
                   'dtype'], ['names', 'units', 'format']),
                 (['field_units', 'dtype'], ['names', 'units', 'format']),
                 (['field_names'], ['names']), (['field_units'], ['units'])]
    for kws_keys, res_keys in test_list:
        kws = {k: kws_all[k] for k in kws_keys}
        res = b''.join([res_all[k] for k in res_keys])
        assert_equal(serialize.format_header(**kws), res)
    assert_raises(ValueError, serialize.format_header)
Example #9
0
def test_get_metaschema():
    r"""Test get_metaschema and ensure the metaschema is current."""
    temp = os.path.join(tempfile.gettempdir(), metaschema._metaschema_fbase)
    old_metaschema = metaschema.get_metaschema()
    try:
        shutil.move(metaschema._metaschema_fname, temp)
        metaschema._metaschema = None
        new_metaschema = metaschema.get_metaschema()
        new_id = new_metaschema.get('$id', new_metaschema.get('id', None))
        old_id = old_metaschema.get('$id', old_metaschema.get('id', None))
        assert (new_id is not None)
        assert (old_id is not None)
        if new_id != old_id:  # pragma: debug
            warnings.warn(
                ("The locally generated metaschema would have a different "
                 "id than the default (%s vs. %s). Check that your "
                 "installation of jsonschema is up to date.") %
                (new_id, old_id))
        else:
            try:
                assert_equal(new_metaschema, old_metaschema)
            except AssertionError:  # pragma: debug
                print("Old:\n%s" % pprint.pformat(old_metaschema))
                print("New:\n%s" % pprint.pformat(new_metaschema))
                raise
    except BaseException:  # pragma: debug
        shutil.move(temp, metaschema._metaschema_fname)
        raise
    shutil.move(temp, metaschema._metaschema_fname)
Example #10
0
def test_scanf():
    r"""Test scanf."""
    num_codes = 'dieEfgGouxX'
    fmt_list = [("%g%+gj", complex(1)), ("%s", "hello"), ("%5s", "the"),
                ("%5s\t%ld\t%lf\t%lf%+lfj\n", ("one", 1, 1.0, complex(1, 1)))]
    fmt_list += [("%" + s, 1) for s in num_codes]
    fmt_list += [("%5.2" + s, 1) for s in num_codes]
    fmt_list += [("%+5.2" + s, 1) for s in num_codes]
    fmt_list += [("%-5.2" + s, 1) for s in num_codes]
    fmt_list += [("% 5.2" + s, 1) for s in num_codes]
    fmt_list += [("%05.2" + s, 1) for s in num_codes]
    for fmt, val in fmt_list:
        if isinstance(val, tuple):
            val_tup = val
        else:
            val_tup = (val, )
        new_tup = []
        for v in val_tup:
            if isinstance(v, complex):
                new_tup += [v.real, v.imag]
            else:
                new_tup.append(v)
        val_str = fmt % tuple(new_tup)
        res = scanf.scanf(fmt, val_str)
        assert_equal(res, val_tup)
Example #11
0
def test_get_compilation_tool():
    r"""Test get_compilation_tool for different name variations."""
    from yggdrasil.drivers.CModelDriver import CModelDriver
    if CModelDriver.is_language_installed():
        tooltype = 'compiler'
        out = CModelDriver.get_tool('compiler').__class__
        toolname = out.toolname.lower()
        toolpath = os.path.join('somedir', toolname)
        toolfile = toolpath + '.exe'
        vals = [toolpath, toolfile]
        if platform._is_win:
            vals += [toolname.upper(), toolfile.upper()]
        for v in vals:
            assert_equal(CompiledModelDriver.get_compilation_tool(tooltype, v),
                         out)
        assert_raises(ValueError, CompiledModelDriver.get_compilation_tool,
                      'compiler', 'invalid')
    # else:
    #     assert_raises(NotImplementedError, CModelDriver.get_tool, 'compiler')
    #     assert_equal(CModelDriver.get_tool(
    #         'compiler', default='invalid'), 'invalid')
    assert_equal(
        CompiledModelDriver.get_compilation_tool('compiler',
                                                 'invalid',
                                                 default='invalid'), 'invalid')
def test_match_stype():
    r"""Test string type matching."""
    slist = ['hello', b'hello', u'hello', bytearray('hello', 'utf-8')]
    for s1 in slist:
        for s2 in slist:
            assert_equal(backwards.match_stype(s1, s2), s1)
    assert_raises(TypeError, backwards.match_stype, 1, 'hello')
Example #13
0
def test_encode_decode():
    r"""Test encode/decode for valid objects."""
    for x in _valid_objects.values():
        y = datatypes.encode(x)
        z = datatypes.decode(y)
        assert_equal(z, x)
        datatypes.encode_data(x)
Example #14
0
def test_guess_type_from_obj():
    r"""Test guess_type_from_obj."""
    invalid_objects = [object, object()]
    for t, x in _valid_objects.items():
        assert_equal(datatypes.guess_type_from_obj(x).name, t)
    for x in invalid_objects:
        assert_raises(datatypes.MetaschemaTypeError, datatypes.guess_type_from_obj, x)
Example #15
0
 def test_send_recv(self, msg_send=None):
     r"""Test routing of a short message between client and server."""
     if msg_send is None:
         msg_send = self.test_msg
     T = self.instance.start_timeout(self.timeout)
     while ((not T.is_out) and ((not self.instance.is_valid) or
                                (not self.srv_drv.is_valid))):
         self.instance.sleep()  # pragma: debug
     self.instance.stop_timeout()
     # Send a message to local output
     flag = self.send_comm.send(msg_send)
     assert (flag)
     # Receive on server side
     flag, srv_msg = self.recv_comm.recv(timeout=self.route_timeout)
     assert (flag)
     assert_equal(srv_msg, msg_send)
     self.instance.printStatus()
     self.srv_drv.printStatus()
     # Send reply back to client
     flag = self.recv_comm.send(srv_msg)
     assert (flag)
     # Receive response on client side
     flag, cli_msg = self.send_comm.recv(timeout=self.route_timeout)
     assert (flag)
     assert_equal(cli_msg, msg_send)
Example #16
0
def test_standardize():
    r"""Test standardize."""
    vals = [(False, ['inputs', 'outputs'], ['_file'], {
        'input': 'inputA',
        'output_file': 'outputA'
    }, {
        'inputs': [{
            'name': 'inputA'
        }],
        'outputs': [{
            'name': 'outputA'
        }]
    }),
            (True, ['input', 'output'], ['_file'], {
                'inputs': 'inputA',
                'output_files': 'outputA'
            }, {
                'input': [{
                    'name': 'inputA'
                }],
                'output': [{
                    'name': 'outputA'
                }]
            })]
    for is_singular, keys, suffixes, x, y in vals:
        schema.standardize(x, keys, suffixes=suffixes, is_singular=is_singular)
        assert_equal(x, y)
def test_singular2plural():
    r"""Test conversion from singular element names to plural ones and back."""
    pairs = [('face', 'faces'), ('vertex', 'vertices'),
             ('vertex_index', 'vertex_indices')]
    for s, p in pairs:
        assert_equal(PlyMetaschemaType.singular2plural(s), p)
        assert_equal(PlyMetaschemaType.plural2singular(p), s)
    assert_raises(ValueError, PlyMetaschemaType.plural2singular, 'invalid')
Example #18
0
def test_extract_formats():
    r"""Test extract_formats."""
    test_str = ['%10s\t%5.2f\t%4d\t%g%+gj']
    test_fmt = [['%10s', '%5.2f', '%4d', '%g%+gj']]
    for s, f in zip(test_str, test_fmt):
        assert_equal(serialize.extract_formats(s), f)
        assert_equal(serialize.extract_formats(backwards.as_bytes(s)),
                     [backwards.as_bytes(i) for i in f])
Example #19
0
def test_import_driver():
    r"""Check a few drivers."""
    drvs = [('Driver', Driver.Driver),
            ('ModelDriver', ModelDriver.ModelDriver),
            ('ConnectionDriver', ConnectionDriver.ConnectionDriver)]
    for n, dans in drvs:
        dres = drivers.import_driver(n)
        assert_equal(dres, dans)
Example #20
0
def test_extract_formats():
    r"""Test extract_formats."""
    test_str = ['%10s\t%5.2f\t%4d\t%g%+gj']
    test_fmt = [['%10s', '%5.2f', '%4d', '%g%+gj']]
    for s, f in zip(test_str, test_fmt):
        assert_equal(serialize.extract_formats(s), f)
        assert_equal(serialize.extract_formats(s.encode("utf-8")),
                     [i.encode("utf-8") for i in f])
def test_format_bytes():
    r"""Test formating of bytes string."""
    s0 = "%s, %s"
    ans = "one, one"
    arg0 = "one"
    args = (backwards.as_bytes(arg0), backwards.as_unicode(arg0))
    for cvt in [backwards.as_bytes, backwards.as_unicode]:
        res = backwards.format_bytes(cvt(s0), args)
        assert_equal(res, cvt(ans))
Example #22
0
def test_get_compatible_tool():
    r"""Test get_compatible_tool when default provided."""
    assert_raises(ValueError, CompiledModelDriver.get_compatible_tool,
                  'invalid', 'compiler', 'c')
    assert_equal(
        CompiledModelDriver.get_compatible_tool('invalid',
                                                'compiler',
                                                'c',
                                                default=None), None)
Example #23
0
def test_LockedBuffer_clear():
    r"""Test the clear method of the LockedBuffer class."""
    x = BufferComm.LockedBuffer()
    x.append('test')
    while x.empty():
        tools.sleep(0.1)
    assert (not x.empty())
    x.clear()
    assert_equal(len(x), 0)
Example #24
0
def test_SchemaRegistry():
    r"""Test schema registry."""
    assert_raises(ValueError, schema.SchemaRegistry, {})
    x = schema.SchemaRegistry()
    assert_equal(x == 0, False)
    fname = os.path.join(tempfile.gettempdir(), 'temp.yml')
    with open(fname, 'w') as fd:
        fd.write('')
    assert_raises(Exception, x.load, fname)
    os.remove(fname)
Example #25
0
def test_nptype2cformat():
    r"""Test conversion from numpy dtype to C format string."""
    for a, b in map_nptype2cformat:
        if isinstance(a, str):
            a = [a]
        for ia in a:
            assert_equal(serialize.nptype2cformat(ia), b)
    assert_raises(TypeError, serialize.nptype2cformat, 0)
    for a in unsupported_nptype:
        assert_raises(ValueError, serialize.nptype2cformat, a)
Example #26
0
def test_YggConfigParser():
    r"""Ensure that get returns proper defaults etc."""
    x = config.YggConfigParser()
    x.add_section('test_section')
    x.set('test_section', 'test_option', 'test_value')
    assert_equal(x.get('test_section', 'test_option'), 'test_value')
    assert_equal(x.get('test_section', 'fake_option'), None)
    assert_equal(x.get('test_section', 'fake_option', 5), 5)
    assert_equal(x.get('fake_section', 'fake_option'), None)
    assert_equal(x.get('fake_section', 'fake_option', 5), 5)
Example #27
0
def test_normalize():
    r"""Test normalization of legacy formats."""
    s = schema.get_schema()
    for x, y in _normalize_objects:
        a = s.normalize(x, backwards_compat=True)
        try:
            assert_equal(a, y)
        except BaseException:  # pragma: debug
            pprint.pprint(a)
            pprint.pprint(y)
            raise
Example #28
0
def test_encode_decode():
    r"""Test encode/decode for valid objects."""
    for x in _valid_objects.values():
        y = datatypes.encode(x)
        z = datatypes.decode(y)
        assert_equal(z, x)
        t = datatypes.encode_type(x)
        d = datatypes.encode_data(x)
        w = datatypes.decode_data(d, t)
        assert_equal(w, x)
    assert_raises(ValueError, datatypes.decode_data, b'', None)
Example #29
0
def test_normalize_instance():
    r"""Test normalize_instance."""
    for schema, x, y in _normalize_objects:
        z = metaschema.normalize_instance(x,
                                          schema,
                                          test_attr=1,
                                          show_errors=(x != y))
        try:
            assert_equal(z, y)
        except BaseException:  # pragma: debug
            print(schema, x, y, z)
            raise
Example #30
0
def do_send_recv(language='python',
                 fmt='%f\\n%d',
                 msg=[float(1.0), int(2)],
                 input_interface='YggInput',
                 output_interface='YggOutput'):
    r"""Function to perform simple send/receive between two comms using a
    language interface that calls the Python interface.
    
    Args:
        language (str, optional): Language that should be mimicked for the
            interface test. Defaults to 'python'.
        fmt (str, optional): Format string to use for the test. Defaults to
            '%f\\n%d'.
        msg (object, optional): Message object that should be used for the
            test. Defaults to [float(1.0), int(2)].
        input_interface (str, optional): Name of the interface function/class
            that should be used for the test. Defaults to 'YggInput'.
        output_interface (str, optional): Name of the interface function/class
            that should be used for the test. Defaults to 'YggOutput'.

    """
    name = 'test_%s' % language
    # Set converter based on language driver
    ldrv = import_component('model', language)
    converter = ldrv.python2language
    if converter is None:

        def converter(x):
            return x

    # Create and start drivers to transport messages
    odrv = OutputDriver.OutputDriver(name, 'link')
    odrv.start()
    os.environ.update(odrv.env)
    idrv = InputDriver.InputDriver(name, 'link', comm_env=odrv.comm_env)
    idrv.start()
    os.environ.update(idrv.env)
    # Connect and utilize interface under disguise as target language
    try:
        with ModelEnv(language=language):
            # Output
            o = YggInterface.YggInit(output_interface, (name, fmt))
            o.send(*msg)
            o.send_eof()
            o.close()
            # Input
            i = YggInterface.YggInit(input_interface, (name, fmt))
            assert_equal(i.recv(), (True, converter(msg)))
            assert_equal(i.recv(), (False, converter(YGG_MSG_EOF)))
    finally:
        odrv.terminate()
        idrv.terminate()