diff --git a/include/GFilename.hpp b/include/GFilename.hpp new file mode 100644 index 0000000..d54badd --- /dev/null +++ b/include/GFilename.hpp @@ -0,0 +1,220 @@ +/*************************************************************************** + * GFilename.hpp - Filename class * + * ----------------------------------------------------------------------- * + * copyright (C) 2015 by 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 . * + * * + ***************************************************************************/ +/** + * @file GFilename.hpp + * @brief Filename class interface definition + * @author Juergen Knoedlseder + */ + +#ifndef GFILENAME_HPP +#define GFILENAME_HPP + +/* __ Includes ___________________________________________________________ */ +#include +#include "GBase.hpp" + + +/***********************************************************************//** + * @class GFilename + * + * @brief Filename class + * + * This class handles filenames. + ***************************************************************************/ +class GFilename : public GBase { + +public: + // Constructors and destructors + GFilename(void); + GFilename(const std::string& filename); + GFilename(const char* filename); + GFilename(const GFilename& filename); + virtual ~GFilename(void); + + // Operators + GFilename& operator=(const GFilename& filename); + + // Methods + void clear(void); + GFilename* clone(void) const; + std::string classname(void) const; + bool empty(void) const; + int size(void) const; + int length(void) const; + const std::string& filename(void) const; + const std::string& extname(void) const; + const int& extno(void) const; + const int& extver(void) const; + bool has_extname(void) const; + bool has_extno(void) const; + bool has_extver(void) const; + void default_extname(const std::string& extname); + std::string print(const GChatter& chatter = NORMAL) const; + +protected: + // Protected methods + void init_members(void); + void copy_members(const GFilename& filename); + void free_members(void); + void set_filename(const std::string& filename); + + // Protected members + std::string m_filename; //!< Filename (with stripped extension info) + std::string m_extname; //!< Extension name + int m_extno; //!< Extension number (-1: not set) + int m_extver; //!< Extension version (0: not set) +}; + + +/***********************************************************************//** + * @brief Return class name + * + * @return String containing the class name ("GFilename"). + ***************************************************************************/ +inline +std::string GFilename::classname(void) const +{ + return ("GFilename"); +} + + +/***********************************************************************//** + * @brief Signal if filename is empty + * + * @return True if filename is empty, false otherwise. + ***************************************************************************/ +inline +bool GFilename::empty(void) const +{ + return (m_filename.empty()); +} + + +/***********************************************************************//** + * @brief Return size of filename + * + * @return Size of filename. + * + * The size of the file name is equal to its length. + ***************************************************************************/ +inline +int GFilename::size(void) const +{ + return (m_filename.size()); +} + + +/***********************************************************************//** + * @brief Return length of filename + * + * @return Length of filename. + ***************************************************************************/ +inline +int GFilename::length(void) const +{ + return (m_filename.length()); +} + + +/***********************************************************************//** + * @brief Return file name + * + * @return String containing file name. + ***************************************************************************/ +inline +const std::string& GFilename::filename(void) const +{ + return (m_filename); +} + + +/***********************************************************************//** + * @brief Return extension name + * + * @return String containing extension name. + ***************************************************************************/ +inline +const std::string& GFilename::extname(void) const +{ + return (m_extname); +} + + +/***********************************************************************//** + * @brief Return extension number + * + * @return Extension number (-1 if not set). + ***************************************************************************/ +inline +const int& GFilename::extno(void) const +{ + return (m_extno); +} + + +/***********************************************************************//** + * @brief Return extension version + * + * @return Extension version (0 if not set). + ***************************************************************************/ +inline +const int& GFilename::extver(void) const +{ + return (m_extver); +} + + +/***********************************************************************//** + * @brief Signal if filename has an extension name + * + * @return True if filename has an extension name, false otherwise. + ***************************************************************************/ +inline +bool GFilename::has_extname(void) const +{ + return (!m_extname.empty()); +} + + +/***********************************************************************//** + * @brief Signal if filename has an extension number + * + * @return True if filename has an extension number, false otherwise. + ***************************************************************************/ +inline +bool GFilename::has_extno(void) const +{ + return (m_extno >= 0); +} + + +/***********************************************************************//** + * @brief Signal if filename has an extension version + * + * @return True if filename has an extension version, false otherwise. + ***************************************************************************/ +inline +bool GFilename::has_extver(void) const +{ + return (m_extver > 0); +} + +#endif /* GFILENAME_HPP */ diff --git a/include/GammaLib.hpp b/include/GammaLib.hpp index 9b92eb5..a9756fe 100644 --- a/include/GammaLib.hpp +++ b/include/GammaLib.hpp @@ -49,6 +49,7 @@ #include "GUrl.hpp" #include "GUrlFile.hpp" #include "GUrlString.hpp" +#include "GFilename.hpp" /* __ Linear algebra module ______________________________________________ */ #include "GVector.hpp" diff --git a/include/Makefile.am b/include/Makefile.am index a227a79..bd6fade 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -13,6 +13,7 @@ pkginclude_HEADERS = GammaLib.hpp \ GUrl.hpp \ GUrlFile.hpp \ GUrlString.hpp \ + GFilename.hpp \ GVector.hpp \ GMatrixBase.hpp \ GMatrix.hpp \ diff --git a/pyext/GFilename.i b/pyext/GFilename.i new file mode 100644 index 0000000..6d2a478 --- /dev/null +++ b/pyext/GFilename.i @@ -0,0 +1,72 @@ +/*************************************************************************** + * GFilename.i - Filename class * + * ----------------------------------------------------------------------- * + * copyright (C) 2015 by 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 . * + * * + ***************************************************************************/ +/** + * @file GFilename.i + * @brief Filename class interface definition + * @author Juergen Knoedlseder + */ +%{ +/* Put headers and other declarations here that are needed for compilation */ +#include "GFilename.hpp" +%} + + +/***********************************************************************//** + * @class GFilename + * + * @brief Filename class + ***************************************************************************/ +class GFilename : public GBase { + +public: + // Constructors and destructors + GFilename(void); + GFilename(const std::string& filename); + GFilename(const GFilename& filename); + GFilename(const char* filename); + virtual ~GFilename(void); + + // Methods + void clear(void); + GFilename* clone(void) const; + std::string classname(void) const; + bool empty(void) const; + int size(void) const; + int length(void) const; + const std::string& filename(void) const; + const std::string& extname(void) const; + const int& extno(void) const; + const int& extver(void) const; + bool has_extname(void) const; + bool has_extno(void) const; + bool has_extver(void) const; + void default_extname(const std::string& extname); +}; + + +/***********************************************************************//** + * @brief GFilename class extension + ***************************************************************************/ +%extend GFilename { + GFilename copy() { + return (*self); + } +}; diff --git a/pyext/gammalib/support.i b/pyext/gammalib/support.i index 8ce4520..1dfdf09 100644 --- a/pyext/gammalib/support.i +++ b/pyext/gammalib/support.i @@ -71,3 +71,4 @@ %include "GUrl.i" %include "GUrlFile.i" %include "GUrlString.i" +%include "GFilename.i" diff --git a/src/support/GFilename.cpp b/src/support/GFilename.cpp new file mode 100644 index 0000000..69fc3bd --- /dev/null +++ b/src/support/GFilename.cpp @@ -0,0 +1,464 @@ +/*************************************************************************** + * GFilename.cpp - Filename class * + * ----------------------------------------------------------------------- * + * copyright (C) 2015 by 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 . * + * * + ***************************************************************************/ +/** + * @file GFilename.cpp + * @brief Filename class interface implementation + * @author Juergen Knoedlseder + */ + +/* __ Includes ___________________________________________________________ */ +#ifdef HAVE_CONFIG_H +#include +#endif +#include "GFilename.hpp" +#include "GTools.hpp" +#include "GException.hpp" + +/* __ Method name definitions ____________________________________________ */ +#define G_SET_FILENAME "GFilename::set_filename(std::string&)" + +/* __ Macros _____________________________________________________________ */ + +/* __ Coding definitions _________________________________________________ */ + +/* __ Debug definitions __________________________________________________ */ + + +/*========================================================================== + = = + = Constructors/destructors = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Void constructor + ***************************************************************************/ +GFilename::GFilename(void) +{ + // Initialise members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Filename constructor + * + * @param[in] filename File name. + * + * Constructs GFilename object by assigning a @p filename. + ***************************************************************************/ +GFilename::GFilename(const std::string& filename) +{ + // Initialise members + init_members(); + + // Set filename + set_filename(filename); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Filename constructor + * + * @param[in] filename File name. + * + * Constructs GFilename object by assigning a @p filename. + ***************************************************************************/ +GFilename::GFilename(const char* filename) +{ + // Initialise members + init_members(); + + // Set filename + set_filename(std::string(filename)); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy constructor + * + * @param[in] filename File name. + ***************************************************************************/ +GFilename::GFilename(const GFilename& filename) +{ + // Initialise members + init_members(); + + // Copy members + copy_members(filename); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Destructor + ***************************************************************************/ +GFilename::~GFilename(void) +{ + // Free members + free_members(); + + // Return + return; +} + + +/*========================================================================== + = = + = Operators = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Assignment operator + * + * @param[in] filename File name. + * @return File name. + ***************************************************************************/ +GFilename& GFilename::operator=(const GFilename& filename) +{ + // Execute only if object is not identical + if (this != &filename) { + + // Free members + free_members(); + + // Initialise members + init_members(); + + // Copy members + copy_members(filename); + + } // endif: object was not identical + + // Return + return *this; +} + + +/*========================================================================== + = = + = Public methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Clear file name + ***************************************************************************/ +void GFilename::clear(void) +{ + // Free class members + free_members(); + + // Initialise members + init_members(); + + // Return + return; +} + + +/***********************************************************************//** + * @brief Clone file name + * + * @return Pointer to deep copy of file name. + ***************************************************************************/ +GFilename* GFilename::clone(void) const +{ + // Clone object + return new GFilename(*this); +} + + +/***********************************************************************//** + * @brief Set default extension name + * + * @param[in] extname Extension name. + * + * If the object has no extension name then set the extension name. + ***************************************************************************/ +void GFilename::default_extname(const std::string& extname) +{ + // Set extension name if it has not yet been set + if (!has_extname()) { + m_extname = extname; + } + + // Return + return; +} + + +/***********************************************************************//** + * @brief Print file name information + * + * @param[in] chatter Chattiness (default: NORMAL). + * @return String containing file name information. + ***************************************************************************/ +std::string GFilename::print(const GChatter& chatter) const +{ + // Initialise result string + std::string result; + + // Continue only if chatter is not silent + if (chatter != SILENT) { + + // Append header + result.append("=== GFilename ==="); + + // Append file name + result.append("\n"+gammalib::parformat("File name")); + result.append(m_filename); + + // Append extension name + result.append("\n"+gammalib::parformat("Extension name")); + if (has_extname()) { + result.append(m_extname); + } + else { + result.append("Not provided"); + } + + // Append extension number + result.append("\n"+gammalib::parformat("Extension number")); + if (has_extno()) { + result.append(gammalib::str(m_extno)); + } + else { + result.append("Not provided"); + } + + // Append extension version + result.append("\n"+gammalib::parformat("Extension version")); + if (has_extver()) { + result.append(gammalib::str(m_extver)); + } + else { + result.append("Not provided"); + } + + } // endif: chatter was not silent + + // Return result + return result; +} + + +/*========================================================================== + = = + = Private methods = + = = + ==========================================================================*/ + +/***********************************************************************//** + * @brief Initialise class members + ***************************************************************************/ +void GFilename::init_members(void) +{ + // Initialise members + m_filename.clear(); + m_extname.clear(); + m_extno = -1; + m_extver = 0; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Copy class members + * + * @param[in] filename File name. + ***************************************************************************/ +void GFilename::copy_members(const GFilename& filename) +{ + // Copy members + m_filename = filename.m_filename; + m_extname = filename.m_extname; + m_extno = filename.m_extno; + m_extver = filename.m_extver; + + // Return + return; +} + + +/***********************************************************************//** + * @brief Delete class members + ***************************************************************************/ +void GFilename::free_members(void) +{ + // Return + return; +} + + +/***********************************************************************//** + * @brief Set file name + * + * @param[in] filename File name. + * + * @exception GException::invalid_argument + * Invalid file name specified. + * + * Sets all attributes of the object by parsing the file name. The file name + * can have one of the following formats: + * + * file.fits No extension + * file.fits[1] Extension number 1 (starting from 0) + * file.fits[EVENTS] EVENTS extension + * file.fits[EVENTS,2] Version 2 of the EVENTS extension + ***************************************************************************/ +void GFilename::set_filename(const std::string& filename) +{ + // Clear file name + clear(); + + // Strip any whitespace + std::string fname = gammalib::strip_whitespace(filename); + + // Continue only if filename is not empty + if (!fname.empty()) { + + // Check for [ symbol in filename + size_t start = fname.find_first_of("["); + + // If we have an extension then separate filename into filename + // part and extension part and fill the information of th object + if (start != std::string::npos) { + + // Check for ] symbol in filename + size_t stop = fname.find_first_of("]", start); + + // If there is no ] symbol then throw an exception + if (stop == std::string::npos) { + std::string msg = "Missing ] symbol in filename \""+ + fname+"\" extension. Please correct the " + "filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + + // If ] is not the last character then throw an exception + if (stop < fname.length()-1) { + std::string msg = "Characters beyond ] symbol in filename \""+ + fname+"\". Please correct the filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + + // Extract extension name + std::string extname = gammalib::strip_whitespace(fname.substr(start+1, stop-start-1)); + + // Check if there is a , symbol that separates extension name + // and extension version + size_t sep = extname.find_first_of(","); + + // If we have a separator, then extract the extension version + if (sep != std::string::npos) { + + // Get extension version string + std::string extver = gammalib::strip_whitespace(extname.substr(sep+1, std::string::npos)); + + // If string is empty then throw an exception + if (extver.empty()) { + std::string msg = "No extension version found after , " + "symbol in filename \""+fname+ + "\". Please correct the filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + + // Extract extension version + m_extver = gammalib::toint(extver); + + // Throw an exception if the extension version is not + // positive + if (m_extver <= 0) { + std::string msg = "Non-positive extension version " + "encountered in filename \""+fname+ + "\". Please correct the filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + + // Update the extension name + extname = gammalib::strip_whitespace(extname.substr(0,sep)); + + } // endif: extension version provided + + // If we have a purely numerical extension then convert the + // extension name into an extension number + if (extname.find_first_of("0123456789") != std::string::npos) { + + // Extract extension number + m_extno = gammalib::toint(extname); + + // Throw an exception if the extension version is not + // positive + if (m_extno < 0) { + std::string msg = "Negative extension number encountered " + "in filename \""+fname+"\". Please " + "correct the filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + + } // endif: extension number provided + + // ... otherwise store the extension name + else { + + // Store extension name + m_extname = gammalib::toupper(extname); + + // Throw an exception if extension name is empty + if (m_extname.empty()) { + std::string msg = "Empty extension name encountered " + "in filename \""+fname+"\". Please " + "correct the filename."; + throw GException::invalid_argument(G_SET_FILENAME, msg); + } + + } // endelse: extension name provided + + // Store the file name + m_filename = gammalib::strip_whitespace(fname.substr(0,start)); + + } // endif: had extension + + // ... otherwise simply set the filename + else { + m_filename = fname; + } + + } // endif: filename was not empty + + // Return + return; +} diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 3bb8fc4..eb6e105 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -14,7 +14,8 @@ sources = GException.cpp \ GRan.cpp \ GUrl.cpp \ GUrlFile.cpp \ - GUrlString.cpp + GUrlString.cpp \ + GFilename.cpp # Build libtool library noinst_LTLIBRARIES = libsupport.la diff --git a/test/test_GSupport.cpp b/test/test_GSupport.cpp index 8016dcc..8030e51 100644 --- a/test/test_GSupport.cpp +++ b/test/test_GSupport.cpp @@ -55,6 +55,7 @@ void TestGSupport::set(void){ append(static_cast(&TestGSupport::test_bilinear), "Test GBilinear"); append(static_cast(&TestGSupport::test_url_file), "Test GUrlFile"); append(static_cast(&TestGSupport::test_url_string), "Test GUrlString"); + append(static_cast(&TestGSupport::test_filename), "Test GFilename"); // Return return; @@ -516,7 +517,7 @@ void TestGSupport::test_url_file(void) std::string result = std::string(buffer, 21); test_assert(result.compare("abcdefghijklm419.9xyz") == 0, "Expected \"abcdefghijklm419.9xyz\" in file, found \""+ - result+"\n"); + result+"\""); url.close(); // Test file reading using get_char() method @@ -532,7 +533,7 @@ void TestGSupport::test_url_file(void) } while (character != EOF); test_assert(result.compare("abcdefghijklm419.9xyz") == 0, "Expected \"abcdefghijklm419.9xyz\" in file, found \""+ - result+"\n"); + result+"\""); url.close(); // Test file reading using scanf() method @@ -542,7 +543,7 @@ void TestGSupport::test_url_file(void) result = std::string(buffer, 21); test_assert(result.compare("abcdefghijklm419.9xyz") == 0, "Expected \"abcdefghijklm419.9xyz\" in file, found \""+ - result+"\n"); + result+"\""); url.close(); // Return @@ -590,7 +591,7 @@ void TestGSupport::test_url_string(void) std::string result = std::string(buffer, 21); test_assert(result.compare("abcdefghijklm419.9xyz") == 0, "Expected \"abcdefghijklm419.9xyz\" in file, found \""+ - result+"\n"); + result+"\""); // Test string reading using get_char() method result.clear(); @@ -605,7 +606,7 @@ void TestGSupport::test_url_string(void) } while (character != EOF); test_assert(result.compare("abcdefghijklm419.9xyz") == 0, "Expected \"abcdefghijklm419.9xyz\" in file, found \""+ - result+"\n"); + result+"\""); // Test string reading using scanf() method char buffer2[100]; @@ -615,7 +616,7 @@ void TestGSupport::test_url_string(void) result = std::string(buffer2, 21); test_assert(result.compare("abcdefghijklm419.9xyz") == 0, "Expected \"abcdefghijklm419.9xyz\" in file, found \""+ - result+"\n"); + result+"\""); url.close(); // Return @@ -624,6 +625,157 @@ void TestGSupport::test_url_string(void) /***********************************************************************//** + * @brief Test GFilename class + * + * Test the GFilename class. + ***************************************************************************/ +void TestGSupport::test_filename(void) +{ + // Test void constructor + test_try("Void constructor"); + try { + GFilename filename; + test_try_success(); + } + catch (std::exception &e) { + test_try_failure(e); + } + + // Set filename without extension + GFilename filename = "myfile.fits"; + test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\" filename, " + "found \""+filename.filename()+"\""); + test_assert(!filename.has_extname(), + "Expected that extension name is not set."); + + // Set filename with extension name + filename = "myfile.fits[EVENTS]"; + test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\" filename, " + "found \""+filename.filename()+"\""); + test_assert(filename.extname() == "EVENTS", + "Expected \"EVENTS\" extension name, " + "found \""+filename.extname()+"\""); + test_assert(filename.has_extname(), + "Expected that extension name is set."); + + // Set filename with extension name and version + filename = "myfile.fits[EVENTS,2]"; + test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\" filename, " + "found \""+filename.filename()+"\""); + test_assert(filename.extname() == "EVENTS", + "Expected \"EVENTS\" extension name, " + "found \""+filename.extname()+"\""); + test_assert(filename.has_extname(), + "Expected that extension name is set."); + test_value(filename.extver(), 2); + test_assert(filename.has_extver(), + "Expected that extension version is set."); + + // Set filename with lower case extension name + filename = "myfile.fits[events]"; + test_assert(filename.extname() == "EVENTS", + "Expected \"EVENTS\" extension name, " + "found \""+filename.extname()+"\""); + + // Set filename with extension number and version + filename = "myfile.fits[1,2]"; + test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\" filename, " + "found \""+filename.filename()+"\""); + test_value(filename.extno(), 1); + test_assert(filename.has_extno(), + "Expected that extension number is set."); + test_value(filename.extver(), 2); + test_assert(filename.has_extver(), + "Expected that extension version is set."); + + // Test missing closing symbol + test_try("Missing ] symbol"); + try { + GFilename filename("myfile.fits["); + test_try_failure(); + } + catch (GException::invalid_argument &e) { + test_try_success(); + } + + // Test character after closing symbol + test_try("Character after ] symbol"); + try { + GFilename filename("myfile.fits[EVENTS]a"); + test_try_failure(); + } + catch (GException::invalid_argument &e) { + test_try_success(); + } + + // Test empty extension name + test_try("Empty extension name"); + try { + GFilename filename("myfile.fits[]"); + test_try_failure(); + } + catch (GException::invalid_argument &e) { + test_try_success(); + } + + // Test invalid extension number + test_try("Invalid extension number"); + try { + GFilename filename("myfile.fits[-1]"); + test_try_failure(); + } + catch (GException::invalid_argument &e) { + test_try_success(); + } + + // Test missing extension version + test_try("Missing extension version"); + try { + GFilename filename("myfile.fits[EVENTS,]"); + test_try_failure(); + } + catch (GException::invalid_argument &e) { + test_try_success(); + } + + // Test invalid extension version + test_try("Invalid extension version"); + try { + GFilename filename("myfile.fits[EVENTS,-1]"); + test_try_failure(); + } + catch (GException::invalid_argument &e) { + test_try_success(); + } + + // Test size operators + filename = "myfile.fits"; + test_assert(!filename.empty(), "Non empty file name expected."); + test_value(filename.size(), 11); + test_value(filename.length(), 11); + + // Test default extension name + filename = "myfile.fits"; + filename.default_extname("EVENTS"); + test_assert(filename.extname() == "EVENTS", + "Expected \"EVENTS\" extension name, " + "found \""+filename.extname()+"\""); + filename = "myfile.fits[LUCKY]"; + filename.default_extname("EVENTS"); + test_assert(filename.extname() == "LUCKY", + "Expected \"LUCKY\" extension name, " + "found \""+filename.extname()+"\""); + + // Return + return; +} + + +/***********************************************************************//** * @brief Main test entry point ***************************************************************************/ int main(void) diff --git a/test/test_GSupport.hpp b/test/test_GSupport.hpp index e684dae..f78967c 100644 --- a/test/test_GSupport.hpp +++ b/test/test_GSupport.hpp @@ -47,12 +47,13 @@ public: virtual void set(void); virtual TestGSupport* clone(void) const; virtual std::string classname(void) const { return "TestGSupport"; } + void test_tools(void); void test_expand_env(void); void test_node_array(void); void test_bilinear(void); void test_url_file(void); void test_url_string(void); - void test_tools(void); + void test_filename(void); private: // Private methods diff --git a/test/test_GSupport.py b/test/test_GSupport.py index 2b28a9b..540da93 100755 --- a/test/test_GSupport.py +++ b/test/test_GSupport.py @@ -51,6 +51,7 @@ class Test(gammalib.GPythonTestSuite): self.append(self.test_node_array, "Test GNodeArray") self.append(self.test_url_file, "Test GUrlFile") self.append(self.test_url_string, "Test GUrlString") + self.append(self.test_filename, "Test GFilename") # Return return @@ -106,7 +107,7 @@ class Test(gammalib.GPythonTestSuite): buffer = url.read(99) self.test_assert(buffer == "abcde", "Expected \"abcde\" in file, found \""+ - buffer+"\n"); + buffer+"\"") url.close() # Return @@ -128,9 +129,41 @@ class Test(gammalib.GPythonTestSuite): buffer = url.read(99) self.test_assert(buffer == "abcde", "Expected \"abcde\" in file, found \""+ - buffer+"\n"); + buffer+"\"") url.close() # Return return + # Test GFilename class + def test_filename(self): + """ + Test GFilename class. + """ + # Test assigning file names + filename = gammalib.GFilename("myfile.fits") + self.test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\", found \""+ + filename.filename()+"\"") + filename = gammalib.GFilename("myfile.fits[EVENTS]") + self.test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\", found \""+ + filename.filename()+"\"") + self.test_assert(filename.extname() == "EVENTS", + "Expected extension \"EVENTS\", found \""+ + filename.extname()+"\"") + filename = gammalib.GFilename("myfile.fits[0]") + self.test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\", found \""+ + filename.filename()+"\"") + self.test_value(filename.extno(), 0) + filename = gammalib.GFilename("myfile.fits[0,2]") + self.test_assert(filename.filename() == "myfile.fits", + "Expected \"myfile.fits\", found \""+ + filename.filename()+"\"") + self.test_value(filename.extno(), 0) + self.test_value(filename.extver(), 2) + + # Return + return + \ No newline at end of file