Commit 6b0f4241a30fd7505252ac0a72cdaddde5eb4843
1 parent
1006ac2b
Add unit tests for GModelSpectralGauss class
Showing
3 changed files
with
98 additions
and
0 deletions
test/data/model_point_gauss.xml
0 → 100644
| 1 | +<?xml version="1.0" standalone="no"?> | ||
| 2 | +<source_library title="source library"> | ||
| 3 | + <source name="Crab" type="PointSource"> | ||
| 4 | + <spectrum type="Gaussian"> | ||
| 5 | + <parameter name="Prefactor" scale="1e-16" value="5.7" min="1e-07" max="1000.0" free="1"/> | ||
| 6 | + <parameter name="Mean" scale="1e6" value="2.48" min="0.0" max="+5.0" free="1"/> | ||
| 7 | + <parameter name="Sigma" scale="1e6" value="1.0" min="0.0001" max="1000.0" free="1"/> | ||
| 8 | + </spectrum> | ||
| 9 | + <spatialModel type="SkyDirFunction"> | ||
| 10 | + <parameter free="0" max="360" min="-360" name="RA" scale="1" value="83.6331" /> | ||
| 11 | + <parameter free="0" max="90" min="-90" name="DEC" scale="1" value="22.0145" /> | ||
| 12 | + </spatialModel> | ||
| 13 | + </source> | ||
| 14 | +</source_library> |
test/test_GModel.cpp
| @@ -49,6 +49,7 @@ void TestGModel::set(void) | @@ -49,6 +49,7 @@ void TestGModel::set(void) | ||
| 49 | m_cube_file = "data/test_cube.fits"; | 49 | m_cube_file = "data/test_cube.fits"; |
| 50 | m_xml_file = "data/crab.xml"; | 50 | m_xml_file = "data/crab.xml"; |
| 51 | m_xml_model_point_const = "data/model_point_const.xml"; | 51 | m_xml_model_point_const = "data/model_point_const.xml"; |
| 52 | + m_xml_model_point_gauss = "data/model_point_gauss.xml"; | ||
| 52 | m_xml_model_point_plaw = "data/model_point_plaw.xml"; | 53 | m_xml_model_point_plaw = "data/model_point_plaw.xml"; |
| 53 | m_xml_model_point_plaw2 = "data/model_point_plaw2.xml"; | 54 | m_xml_model_point_plaw2 = "data/model_point_plaw2.xml"; |
| 54 | m_xml_model_point_eplaw = "data/model_point_eplaw.xml"; | 55 | m_xml_model_point_eplaw = "data/model_point_eplaw.xml"; |
| @@ -81,6 +82,7 @@ void TestGModel::set(void) | @@ -81,6 +82,7 @@ void TestGModel::set(void) | ||
| 81 | 82 | ||
| 82 | // Append spectral model tests | 83 | // Append spectral model tests |
| 83 | append(static_cast<pfunction>(&TestGModel::test_const), "Test GModelSpectralConst"); | 84 | append(static_cast<pfunction>(&TestGModel::test_const), "Test GModelSpectralConst"); |
| 85 | + append(static_cast<pfunction>(&TestGModel::test_gauss), "Test GModelSpectralGauss"); | ||
| 84 | append(static_cast<pfunction>(&TestGModel::test_plaw), "Test GModelSpectralPlaw"); | 86 | append(static_cast<pfunction>(&TestGModel::test_plaw), "Test GModelSpectralPlaw"); |
| 85 | append(static_cast<pfunction>(&TestGModel::test_plaw2), "Test GModelSpectralPlaw2"); | 87 | append(static_cast<pfunction>(&TestGModel::test_plaw2), "Test GModelSpectralPlaw2"); |
| 86 | append(static_cast<pfunction>(&TestGModel::test_eplaw), "Test GModelSpectralExpPlaw"); | 88 | append(static_cast<pfunction>(&TestGModel::test_eplaw), "Test GModelSpectralExpPlaw"); |
| @@ -1154,6 +1156,86 @@ void TestGModel::test_const(void) | @@ -1154,6 +1156,86 @@ void TestGModel::test_const(void) | ||
| 1154 | 1156 | ||
| 1155 | 1157 | ||
| 1156 | /***********************************************************************//** | 1158 | /***********************************************************************//** |
| 1159 | + * @brief Test GModelSpectralGauss class | ||
| 1160 | + ***************************************************************************/ | ||
| 1161 | +void TestGModel::test_gauss(void) | ||
| 1162 | +{ | ||
| 1163 | + // Test void constructor | ||
| 1164 | + test_try("Test void constructor"); | ||
| 1165 | + try { | ||
| 1166 | + GModelSpectralGauss model; | ||
| 1167 | + test_assert(model.type() == "Gaussian", | ||
| 1168 | + "Model type \"Gaussian\" expected."); | ||
| 1169 | + test_try_success(); | ||
| 1170 | + } | ||
| 1171 | + catch (std::exception &e) { | ||
| 1172 | + test_try_failure(e); | ||
| 1173 | + } | ||
| 1174 | + | ||
| 1175 | + // Test value constructor | ||
| 1176 | + test_try("Test value constructor"); | ||
| 1177 | + try { | ||
| 1178 | + GModelSpectralGauss model(1.0, GEnergy(42.0, "MeV"), GEnergy(0.5, "MeV")); | ||
| 1179 | + test_value(model.prefactor(), 1.0); | ||
| 1180 | + test_value(model.mean().MeV(), 42.0); | ||
| 1181 | + test_value(model.sigma().MeV(), 0.5); | ||
| 1182 | + test_try_success(); | ||
| 1183 | + } | ||
| 1184 | + catch (std::exception &e) { | ||
| 1185 | + test_try_failure(e); | ||
| 1186 | + } | ||
| 1187 | + | ||
| 1188 | + // Test XML constructor and value | ||
| 1189 | + test_try("Test XML constructor, value and gradients"); | ||
| 1190 | + try { | ||
| 1191 | + // Test XML constructor | ||
| 1192 | + GXml xml(m_xml_model_point_gauss); | ||
| 1193 | + GXmlElement* element = xml.element(0)->element(0)->element("spectrum", 0); | ||
| 1194 | + GModelSpectralGauss model(*element); | ||
| 1195 | + test_value(model.size(), 3); | ||
| 1196 | + test_assert(model.type() == "Gaussian", "Expected \"Gaussian\""); | ||
| 1197 | + test_value(model.prefactor(), 5.7e-16); | ||
| 1198 | + test_value(model.mean().MeV(), 2.48e6); | ||
| 1199 | + test_value(model.sigma().MeV(), 1e6); | ||
| 1200 | + | ||
| 1201 | + // Test prefactor method | ||
| 1202 | + model.prefactor(2.3e-16); | ||
| 1203 | + test_value(model.prefactor(), 2.3e-16); | ||
| 1204 | + | ||
| 1205 | + // Test mean method | ||
| 1206 | + model.mean(GEnergy(96.0, "MeV")); | ||
| 1207 | + test_value(model.mean().MeV(), 96.0); | ||
| 1208 | + | ||
| 1209 | + // Test sigma method | ||
| 1210 | + model.sigma(GEnergy(0.5e7, "keV")); | ||
| 1211 | + test_value(model.sigma().keV(), 0.5e7); | ||
| 1212 | + | ||
| 1213 | + // Test operator access | ||
| 1214 | + const char* strarray[] = {"Prefactor", "Mean", "Sigma"}; | ||
| 1215 | + for (int i = 0; i < 3; ++i) { | ||
| 1216 | + std::string keyname(strarray[i]); | ||
| 1217 | + model[keyname].remove_range(); // To allow setting of any value | ||
| 1218 | + model[keyname].value(2.1); | ||
| 1219 | + model[keyname].error(1.9); | ||
| 1220 | + model[keyname].gradient(0.8); | ||
| 1221 | + test_value(model[keyname].value(), 2.1); | ||
| 1222 | + test_value(model[keyname].error(), 1.9); | ||
| 1223 | + test_value(model[keyname].gradient(), 0.8); | ||
| 1224 | + } | ||
| 1225 | + | ||
| 1226 | + // Success if we reached this point | ||
| 1227 | + test_try_success(); | ||
| 1228 | + } | ||
| 1229 | + catch (std::exception &e) { | ||
| 1230 | + test_try_failure(e); | ||
| 1231 | + } | ||
| 1232 | + | ||
| 1233 | + // Exit test | ||
| 1234 | + return; | ||
| 1235 | +} | ||
| 1236 | + | ||
| 1237 | + | ||
| 1238 | +/***********************************************************************//** | ||
| 1157 | * @brief Test GModelSpectralPlaw class | 1239 | * @brief Test GModelSpectralPlaw class |
| 1158 | ***************************************************************************/ | 1240 | ***************************************************************************/ |
| 1159 | void TestGModel::test_plaw(void) | 1241 | void TestGModel::test_plaw(void) |
test/test_GModel.hpp
| @@ -58,6 +58,7 @@ public: | @@ -58,6 +58,7 @@ public: | ||
| 58 | void test_elliptical_disk(void); | 58 | void test_elliptical_disk(void); |
| 59 | void test_spatial_model(void); | 59 | void test_spatial_model(void); |
| 60 | void test_const(void); | 60 | void test_const(void); |
| 61 | + void test_gauss(void); | ||
| 61 | void test_plaw(void); | 62 | void test_plaw(void); |
| 62 | void test_plaw2(void); | 63 | void test_plaw2(void); |
| 63 | void test_eplaw(void); | 64 | void test_eplaw(void); |
| @@ -79,6 +80,7 @@ private: | @@ -79,6 +80,7 @@ private: | ||
| 79 | std::string m_cube_file; | 80 | std::string m_cube_file; |
| 80 | std::string m_xml_file; | 81 | std::string m_xml_file; |
| 81 | std::string m_xml_model_point_const; | 82 | std::string m_xml_model_point_const; |
| 83 | + std::string m_xml_model_point_gauss; | ||
| 82 | std::string m_xml_model_point_plaw; | 84 | std::string m_xml_model_point_plaw; |
| 83 | std::string m_xml_model_point_plaw2; | 85 | std::string m_xml_model_point_plaw2; |
| 84 | std::string m_xml_model_point_eplaw; | 86 | std::string m_xml_model_point_eplaw; |