File: test_Units.py

package info (click to toggle)
cf-python 1.3.2+dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 7,996 kB
  • sloc: python: 51,733; ansic: 2,736; makefile: 78; sh: 2
file content (142 lines) | stat: -rw-r--r-- 6,319 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import cf
import math
import os
import unittest

class UnitsTest(unittest.TestCase):
    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            'test_file.nc')
    chunk_sizes = (17, 34, 300, 100000)[::-1]

    def test_Units___eq__(self):
        self.assertTrue(cf.Units('')==cf.Units(''))
        self.assertTrue(cf.Units('18')==cf.Units('18'))
        self.assertTrue(cf.Units('1')==cf.Units('1'))
        self.assertTrue(cf.Units('m')==cf.Units('m'))
        self.assertTrue(cf.Units('m')==cf.Units('metres'))
        self.assertTrue(cf.Units('m')==cf.Units('meTRES'))

        self.assertTrue(cf.Units('days since 2000-1-1')==cf.Units('d since 2000-1-1 0:0'))
        self.assertTrue(cf.Units('days since 2000-1-1')!=cf.Units('h since 1234-1-1 0:0'))
        
        self.assertTrue(cf.Units('days since 2000-1-1')==cf.Units('d since 2000-1-1 0:0', calendar='gregorian'))
        self.assertTrue(cf.Units('days since 2000-1-1')==cf.Units('d since 2000-1-1 0:0', calendar='standard'))
        
        self.assertTrue(cf.Units(calendar='noleap')==cf.Units(calendar='noleap'))
        self.assertTrue(cf.Units(calendar='noleap')==cf.Units(calendar='365_day'))
        self.assertTrue(cf.Units(calendar='nOLEAP')==cf.Units(calendar='365_dAY'))
        
        self.assertTrue(cf.Units('days since 2000-1-1', calendar='all_leap')==cf.Units('d since 2000-1-1 0:0', calendar='366_day'))
        self.assertTrue(cf.Units('days since 2000-1-1', calendar='all_leap')!=cf.Units('h since 2000-1-1 0:0', calendar='366_day'))    
    #--- End: def
        
    def test_Units_equivalent(self):
        self.assertTrue(cf.Units('').equivalent(cf.Units('')))
        self.assertTrue(cf.Units('').equivalent(cf.Units('1')))
        self.assertTrue(cf.Units('').equivalent(cf.Units('18')))
        self.assertTrue(cf.Units('18').equivalent(cf.Units('1')))
        self.assertTrue(cf.Units('18').equivalent(cf.Units('18')))
        self.assertTrue(cf.Units('1)').equivalent(cf.Units('1')))

        self.assertTrue(cf.Units('m').equivalent(cf.Units('m')))
        self.assertTrue(cf.Units('meter').equivalent(cf.Units('km')))
        self.assertTrue(cf.Units('metre').equivalent(cf.Units('mile')))

        self.assertTrue(cf.Units('s').equivalent(cf.Units('h')))
        self.assertTrue(cf.Units('s').equivalent(cf.Units('day')))
        self.assertTrue(cf.Units('second').equivalent(cf.Units('month')) )   

        self.assertTrue(cf.Units(calendar='noleap').equivalent(cf.Units(calendar='noleap')))
        self.assertTrue(cf.Units(calendar='noleap').equivalent(cf.Units(calendar='365_day')))
        self.assertTrue(cf.Units(calendar='nOLEAP').equivalent(cf.Units(calendar='365_dAY')))

        self.assertTrue(cf.Units('days since 2000-1-1').equivalent(cf.Units('d since 2000-1-1 0:0')))
        self.assertTrue(cf.Units('days since 2000-1-1').equivalent(cf.Units('h since 1234-1-1 0:0')))
        self.assertTrue(cf.Units('days since 2000-1-1').equivalent(cf.Units('d since 2000-1-1 0:0', calendar='gregorian')))
        self.assertTrue(cf.Units('days since 2000-1-1').equivalent(cf.Units('h since 1234-1-1 0:0', calendar='standard')))

        self.assertTrue(cf.Units('days since 2000-1-1', calendar='all_leap').equivalent(cf.Units('d since 2000-1-1 0:0', calendar='366_day')))
        self.assertTrue(cf.Units('days since 2000-1-1', calendar='all_leap').equivalent(cf.Units('h since 1234-1-1 0:0', calendar='366_day')))    
    #--- End: def 

    def test_Units_BINARY_AND_UNARY_OPERATORS(self):
        self.assertTrue((cf.Units('m')*2)    ==cf.Units('2m'))
        self.assertTrue((cf.Units('m')/2)    ==cf.Units('0.5m'))
        self.assertTrue((cf.Units('m')//2)   ==cf.Units('0.5m'))
        self.assertTrue((cf.Units('m')+2)    ==cf.Units('m @ -2'))
        self.assertTrue((cf.Units('m')-2)    ==cf.Units('m @ 2'))
        self.assertTrue((cf.Units('m')**2)   ==cf.Units('m2'))
        self.assertTrue((cf.Units('m')**-2)  ==cf.Units('m-2'))
        self.assertTrue((cf.Units('m2')**0.5)==cf.Units('m'))
    
        u = cf.Units('m')
        v = u
        u *= 2
        self.assertTrue(u==cf.Units('2m'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u /= 2
        self.assertTrue(u==cf.Units('0.5m'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u //= 2
        self.assertTrue(u==cf.Units('0.5m'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u += 2
        self.assertTrue(u==cf.Units('m @ -2'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u -= 2
        self.assertTrue(u==cf.Units('m @ 2'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u **= 2
        self.assertTrue(u==cf.Units('m2'))
        self.assertTrue(u!=v)
    
        self.assertTrue((2*cf.Units('m')) ==cf.Units('2m'))
        self.assertTrue((2/cf.Units('m')) ==cf.Units('2 m-1'))
        self.assertTrue((2//cf.Units('m'))==cf.Units('2 m-1'))
        self.assertTrue((2+cf.Units('m')) ==cf.Units('m @ -2'))
        self.assertTrue((2-cf.Units('m')) ==cf.Units('-1 m @ -2'))
    
        self.assertTrue((cf.Units('m')*cf.Units('2m')) ==cf.Units('2 m2'))
        self.assertTrue((cf.Units('m')/cf.Units('2m')) ==cf.Units('0.5'))
        self.assertTrue((cf.Units('m')//cf.Units('2m'))==cf.Units('0.5'))
    
        u = cf.Units('m')
        v = u
        u *= u
        self.assertTrue(u==cf.Units('m2'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u /= u
        self.assertTrue(u==cf.Units('1'))
        self.assertTrue(u!=v)
        u = cf.Units('m')
        v = u
        u //= u
        self.assertTrue(u==cf.Units('1'))
        self.assertTrue(u!=v)
    
        self.assertTrue(cf.Units('m').log(10)    ==cf.Units('lg(re 1 m)'))
        self.assertTrue(cf.Units('m').log(2)     ==cf.Units('lb(re 1 m)'))
        self.assertTrue(cf.Units('m').log(math.e)==cf.Units('ln(re 1 m)'))
        self.assertTrue(cf.Units('m').log(1.5)   ==cf.Units('2.46630346237643 ln(re 1 m)'))    
    #--- End: def

#--- End: class

if __name__ == '__main__':
    print 'cf-python version:', cf.__version__
    print 'cf-python path:'   , os.path.abspath(cf.__file__)
    print ''
    unittest.main(verbosity=2)