test_gl_space.py 2.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

18
19
import itertools

Jait Dixit's avatar
Jait Dixit committed
20
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
21
import pytest
22
23
from numpy.testing import (assert_, assert_almost_equal, assert_equal,
                           assert_raises)
Jait Dixit's avatar
Jait Dixit committed
24

Martin Reinecke's avatar
5->6    
Martin Reinecke committed
25
from nifty6 import GLSpace
Philipp Arras's avatar
Philipp Arras committed
26
27
28

pmp = pytest.mark.parametrize

Martin Reinecke's avatar
Martin Reinecke committed
29
# [nlat, nlon, expected]
Philipp Arras's avatar
Philipp Arras committed
30
31
32
33
34
35
36
37
38
39
40
CONSTRUCTOR_CONFIGS = [[
    2, None, {
        'nlat': 2,
        'nlon': 3,
        'harmonic': False,
        'shape': (6,),
        'size': 6,
    }
], [0, None, {
    'error': ValueError
}]]
Jait Dixit's avatar
Jait Dixit committed
41
42


43
def get_dvol_configs():
Philipp Arras's avatar
Philipp Arras committed
44
    wgt = [2.0943951, 2.0943951]
Martin Reinecke's avatar
Martin Reinecke committed
45
    # for GLSpace(nlat=2, nlon=3)
Philipp Arras's avatar
Philipp Arras committed
46
47
48
49
    dvol_0 = np.array(
        list(
            itertools.chain.from_iterable(
                itertools.repeat(x, 3) for x in wgt)))
Jait Dixit's avatar
Jait Dixit committed
50
    return [
51
        [1, dvol_0],
Philipp Arras's avatar
Philipp Arras committed
52
53
    ]

Jait Dixit's avatar
Jait Dixit committed
54

Philipp Arras's avatar
Philipp Arras committed
55
56
57
58
@pmp('attribute', ['nlat', 'nlon'])
def test_property_ret_type(attribute):
    g = GLSpace(2)
    assert_(isinstance(getattr(g, attribute), int))
Jait Dixit's avatar
Jait Dixit committed
59
60


Philipp Arras's avatar
Philipp Arras committed
61
62
63
@pmp('nlat, nlon, expected', CONSTRUCTOR_CONFIGS)
def test_constructor(nlat, nlon, expected):
    g = GLSpace(4)
Jait Dixit's avatar
Jait Dixit committed
64

Philipp Arras's avatar
Philipp Arras committed
65
66
67
68
69
70
71
    if 'error' in expected:
        with assert_raises(expected['error']):
            GLSpace(nlat, nlon)
    else:
        g = GLSpace(nlat, nlon)
        for key, value in expected.items():
            assert_equal(getattr(g, key), value)
Theo Steininger's avatar
Theo Steininger committed
72

Jait Dixit's avatar
Jait Dixit committed
73

Philipp Arras's avatar
Philipp Arras committed
74
75
76
@pmp('power, expected', get_dvol_configs())
def test_dvol(power, expected):
    assert_almost_equal(GLSpace(2).dvol, expected)