test_GObservation.py
4.24 KB
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
143
144
145
146
147
148
149
150
151
152
153
# ==========================================================================
# This module performs unit tests for the GammaLib observation module.
#
# Copyright (C) 2012-2016 Juergen Knoedlseder
#
# 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/>.
#
# ==========================================================================
import gammalib
# ========================================== #
# Test class for GammaLib observation module #
# ========================================== #
class Test(gammalib.GPythonTestSuite):
"""
Test class for GammaLib observation module.
"""
# Constructor
def __init__(self):
"""
Constructor.
"""
# Call base class constructor
gammalib.GPythonTestSuite.__init__(self)
# Return
return
# Set test functions
def set(self):
"""
Set all test functions.
"""
# Set test name
self.name("obs")
# Append tests
self.append(self.test_energy, "Test GEnergy class")
self.append(self.test_energies, "Test GEnergies class")
self.append(self.test_time, "Test GTime class")
# Return
return
# Test GEnergy class
def test_energy(self):
"""
Test GEnergy class.
"""
# Setup energies
energy = gammalib.GEnergy(3.0, "TeV")
energy_b = energy.copy()
# Unary addition operator
energy += energy_b
self.test_value(energy.TeV(), 6.0);
# Unary subtraction operator
energy -= energy_b
self.test_value(energy.TeV(), 3.0);
# Unary multiplication operator
energy *= 2.0
self.test_value(energy.TeV(), 6.0);
# Unary division operator
energy /= 2.0
self.test_value(energy.TeV(), 3.0);
# Binary addition operator
energy = energy + energy_b
self.test_value(energy.TeV(), 6.0);
# Binary subtraction operator
energy = energy - energy_b
self.test_value(energy.TeV(), 3.0);
# Scalar multiplication operator
energy = energy * 2.0
self.test_value(energy.TeV(), 6.0);
# Scalar division operator
energy = energy / 2.0
self.test_value(energy.TeV(), 3.0);
# Return
return
# Test GEnergies class
def test_energies(self):
"""
Test GEnergies class.
"""
# Setup 3 logarithmic energies
energies = gammalib.GEnergies(3, gammalib.GEnergy(1.0, "MeV"),
gammalib.GEnergy(100.0, "MeV"))
self.test_value(energies[0].MeV(), 1.0);
self.test_value(energies[1].MeV(), 10.0);
self.test_value(energies[2].MeV(), 100.0);
# Return
return
# Test GTime class
def test_time(self):
"""
Test GTime class.
"""
# Setup times
time_a = gammalib.GTime(9.0)
time_b = gammalib.GTime(3.0)
secs_b = 2.0
# Unary addition operator
time = time_a.copy()
time += secs_b
self.test_value(time.secs(), 11.0);
# Unary subtraction operator
time = time_a.copy()
time -= secs_b
self.test_value(time.secs(), 7.0);
# Binary left addition operator
time = time_a + secs_b
self.test_value(time.secs(), 11.0);
# Binary right addition operator
time = secs_b + time_a
self.test_value(time.secs(), 11.0);
# Binary subtraction operator
time = time_a - secs_b
self.test_value(time.secs(), 7.0);
# Binary time subtraction operator
time = time_a - time_b
self.test_value(time, 6.0);
# Return
return