Exemplo n.º 1
0
    def test_yanny(self):
        """Used to test the yanny class.
        """
        #
        # Describe what should be in the object
        #
        pair_data = self.test_data["test.par"]["pairs"]
        struct_data = self.test_data["test.par"]["structures"]
        symbols = [
"""typedef struct {
    float mag[5];
    char b[5][];
    char foo[25];
    double c;
    int flags[2];
    BOOLEAN new_flag;
} MYSTRUCT;""",
"""typedef struct {
    float foo<3>; # This is archaic array notation, strongly deprecated,
    char bar<10>; # but still technically supported.
} OLD;""",
"""typedef struct {
    STATUS state;
    char timestamp[]; #UTC timestamp in format 2008-06-21T00:27:33
} STATUS_UPDATE;""",
            ]
        enum = [
"""typedef enum {
    FALSE,
    TRUE
} BOOLEAN;""",
"""typedef enum {
    FAILURE,
    INCOMPLETE,
    SUCCESS
} STATUS;""",
            ]
        #
        # Open the object
        #
        par = yanny(self.data('test.par'))
        #
        # Test the pairs
        #
        assert set(par.pairs()) == set(pair_data)
        #
        # Test the pair values
        #
        for p in par.pairs():
            assert par[p] == pair_data[p]
        #
        # Test the structure of the object
        #
        assert (set(par) == set(list(pair_data) +
                list(struct_data)))
        assert (set(par._symbols) == set(list(struct_data) +
                ['struct', 'enum']))
        assert set(par._symbols['struct']) == set(symbols)
        assert set(par._symbols['enum']) == set(enum)
        assert set(par.tables()) == set(struct_data)
        for t in par.tables():
            assert (par.dtype(t) ==
                    self.json2dtype(struct_data[t]['dtype']))
            assert par.size(t) == struct_data[t]['size']
            assert (set(par.columns(t)) ==
                    set(struct_data[t]['columns']))
            for c in par.columns(t):
                assert (par.type(t, c) ==
                        struct_data[t]['columns'][c])
                # print(par[t][c])
        assert par.isenum('MYSTRUCT', 'new_flag')
        assert par._enum_cache['BOOLEAN'] == ['FALSE', 'TRUE']
        assert (par._enum_cache['STATUS'] ==
                ['FAILURE', 'INCOMPLETE', 'SUCCESS'])
        #
        # Test values
        #
        assert np.allclose(par['MYSTRUCT'].mag[0],
                            np.array([17.5, 17.546, 17.4, 16.1, 16.0]))
        assert np.allclose(par['MYSTRUCT'].mag[5],
                            np.array([19.3, 18.2, 17.1, 16.0, 15.9]))
        assert par['MYSTRUCT'].foo[1] == six.b("My dog has no nose.")
        assert np.allclose(par['MYSTRUCT'].c[2], 7.24345567)
        assert (par['MYSTRUCT']['flags'][2] == np.array([123123, 0])).all()
        #
        # Test expected write failures.
        #
        # This should fail, since test.par already exists.
        with raises(PydlutilsException):
            par.write()
        datatable = {'status_update': {'state': ['SUCCESS', 'SUCCESS'],
            'timestamp': ['2008-06-22 01:27:33', '2008-06-22 01:27:36']},
            'new_keyword': 'new_value'}
        par.filename = self.data('test_append.par')
        # This should also fail, because test_append.par does not exist.
        with raises(PydlutilsException):
            par.append(datatable)
        return
Exemplo n.º 2
0
    def test_yanny(self):
        """Used to test the yanny class.
        """
        #
        # Describe what should be in the object
        #
        pair_data = self.test_data["test.par"]["pairs"]
        struct_data = self.test_data["test.par"]["structures"]
        symbols = [
            """typedef struct {
    float mag[5];
    char b[5][];
    char foo[25];
    double c;
    int flags[2];
    BOOLEAN new_flag;
} MYSTRUCT;""",
            """typedef struct {
    float foo<3>; # This is archaic array notation, strongly deprecated,
    char bar<10>; # but still technically supported.
} OLD;""",
            """typedef struct {
    STATUS state;
    char timestamp[]; #UTC timestamp in format 2008-06-21T00:27:33
} STATUS_UPDATE;""",
        ]
        enum = [
            """typedef enum {
    FALSE,
    TRUE
} BOOLEAN;""",
            """typedef enum {
    FAILURE,
    INCOMPLETE,
    SUCCESS
} STATUS;""",
        ]
        with open(self.data('test.par')) as f:
            file_data = f.read()
        #
        # Open the object
        #
        par = yanny(self.data('test.par'))
        #
        # Test some static methods, etc.
        #
        assert par.get_token('abcd') == ('abcd', '')
        assert str(par) == file_data
        assert repr(par) == file_data
        par2 = yanny(self.data('test_table.par'))
        assert par != par2
        #
        # Test types
        #
        assert par.type('MYSTRUCT', 'c') == 'double'
        assert par.type('FOOBAR', 'd') is None
        assert par.type('MYSTRUCT', 'foobar') is None
        assert not par2.isenum('TEST', 'a')
        assert par.array_length('MYSTRUCT', 'c') == 1
        assert par.char_length('MYSTRUCT', 'c') is None
        #
        # Test the pairs
        #
        assert set(par.pairs()) == set(pair_data)
        #
        # Test the pair values
        #
        for p in par.pairs():
            assert par[p] == pair_data[p]
        #
        # Test the structure of the object
        #
        assert (set(par) == set(list(pair_data) + list(struct_data)))
        assert (set(
            par._symbols) == set(list(struct_data) + ['struct', 'enum']))
        assert set(par._symbols['struct']) == set(symbols)
        assert set(par._symbols['enum']) == set(enum)
        assert set(par.tables()) == set(struct_data)
        for t in par.tables():
            assert (par.dtype(t) == self.json2dtype(struct_data[t]['dtype']))
            assert par.size(t) == struct_data[t]['size']
            assert (set(par.columns(t)) == set(struct_data[t]['columns']))
            for c in par.columns(t):
                assert (par.type(t, c) == struct_data[t]['columns'][c])
                # print(par[t][c])
        assert par.isenum('MYSTRUCT', 'new_flag')
        assert par._enum_cache['BOOLEAN'] == ['FALSE', 'TRUE']
        assert (par._enum_cache['STATUS'] == [
            'FAILURE', 'INCOMPLETE', 'SUCCESS'
        ])
        #
        # Test values
        #
        assert np.allclose(par['MYSTRUCT'].mag[0],
                           np.array([17.5, 17.546, 17.4, 16.1, 16.0]))
        assert np.allclose(par['MYSTRUCT'].mag[5],
                           np.array([19.3, 18.2, 17.1, 16.0, 15.9]))
        assert par['MYSTRUCT'].foo[1] == six.b("My dog has no nose.")
        assert np.allclose(par['MYSTRUCT'].c[2], 7.24345567)
        assert (par['MYSTRUCT']['flags'][2] == np.array([123123, 0])).all()
        #
        # Test expected write failures.
        #
        # This should fail, since test.par already exists.
        with raises(PydlutilsException):
            par.write()
        with catch_warnings(PydlutilsUserWarning) as w:
            par.append({})
        datatable = {
            'status_update': {
                'state': ['SUCCESS', 'SUCCESS'],
                'timestamp': ['2008-06-22 01:27:33', '2008-06-22 01:27:36']
            },
            'new_keyword': 'new_value'
        }
        par.filename = self.temp('test_append.par')
        # This should also fail, because test_append.par does not exist.
        with raises(PydlutilsException):
            par.append(datatable)
        return
Exemplo n.º 3
0
def test_yanny():
    """Used to test the yanny class.
    """
    import os
    from .. import yanny
    from ... import PydlutilsException
    from numpy import allclose, array, dtype
    from astropy.tests.helper import raises
    from astropy.extern import six
    #
    # Describe what should be in the object
    #
    pair_dict = {'mjd':'54579','alpha':'beta gamma delta','semicolon':'This pair contains a semicolon;'}
    struct_dict = {
        'MYSTRUCT':{
            'dtype':[('mag', '<f4', (5,)), ('b', 'S33', (5,)), ('foo', 'S25'), ('c', '<f8'), ('flags', '<i4', (2,)), ('new_flag', 'S5')],
            'size':7,
            'columns':{'mag':'float[5]','b':'char[5][]','foo':'char[25]','c':'double','flags':'int[2]','new_flag':'BOOLEAN'},
            },
        'OLD':{
            'dtype':[('foo', '<f4', (3,)), ('bar', 'S10')],
            'size':2,
            'columns':{'foo':'float[3]','bar':'char[10]'},
            },
        'STATUS_UPDATE':{
            'dtype':[('state', 'S10'), ('timestamp', 'S19')],
            'size':11,
            'columns':{'state':'STATUS','timestamp':'char[]'},
            },
        }
    symbols = [
"""typedef struct {
    float mag[5];
    char b[5][];
    char foo[25];
    double c;
    int flags[2];
    BOOLEAN new_flag;
} MYSTRUCT;""",
"""typedef struct {
    float foo<3>; # This is archaic array notation, strongly deprecated,
    char bar<10>; # but still technically supported.
} OLD;""",
"""typedef struct {
    STATUS state;
    char timestamp[]; #UTC timestamp in format 2008-06-21T00:27:33
} STATUS_UPDATE;""",
        ]
    enum = [
"""typedef enum {
    FALSE,
    TRUE
} BOOLEAN;""",
"""typedef enum {
    FAILURE,
    INCOMPLETE,
    SUCCESS
} STATUS;""",
        ]
    #
    # Open the object
    #
    par = yanny(os.path.join(os.path.dirname(__file__),'t','test.par'),np=True)
    #
    # Test the pairs
    #
    assert set(par.pairs()) == set(pair_dict)
    #
    # Test the pair values
    #
    for p in par.pairs():
        assert par[p] == pair_dict[p]
    #
    # Test the structure of the object
    #
    assert set(par) == set(list(pair_dict) + list(struct_dict) + ['symbols'])
    assert set(par['symbols']) == set(list(struct_dict) + ['struct','enum'])
    assert set(par['symbols']['struct']) == set(symbols)
    assert set(par['symbols']['enum']) == set(enum)
    assert set(par.tables()) == set(struct_dict)
    for t in par.tables():
        assert par.dtype(t) == dtype(struct_dict[t]['dtype'])
        assert par.size(t) == struct_dict[t]['size']
        assert set(par.columns(t)) == set(struct_dict[t]['columns'])
        for c in par.columns(t):
            assert par.type(t,c) == struct_dict[t]['columns'][c]
            #print(par[t][c])
    assert par.isenum('MYSTRUCT','new_flag')
    assert par._enum_cache['BOOLEAN'] == ['FALSE','TRUE']
    assert par._enum_cache['STATUS'] == ['FAILURE','INCOMPLETE','SUCCESS']
    #
    # Test values
    #
    assert allclose(par['MYSTRUCT'].mag[0], array([17.5, 17.546, 17.4, 16.1, 16.0]))
    assert allclose(par['MYSTRUCT'].mag[5], array([19.3, 18.2, 17.1, 16.0, 15.9]))
    assert par['MYSTRUCT'].foo[1] == six.b("My dog has no nose.")
    assert allclose(par['MYSTRUCT'].c[2], 7.24345567)
    assert (par['MYSTRUCT']['flags'][2] == array([123123, 0])).all()
    #
    # Test expected write failures.
    #
    with raises(PydlutilsException):
        par.write() # This should fail, since test.par already exists.
    datatable = {'status_update': {'state':['SUCCESS', 'SUCCESS'],
        'timestamp':['2008-06-22 01:27:33','2008-06-22 01:27:36']},
        'new_keyword':'new_value'}
    par.filename = os.path.join(os.path.dirname(__file__),'t','test_append.par')
    with raises(PydlutilsException):
        par.append(datatable) # This should also fail, because test_append.par does not exist
    return