"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/test/testinghelpers.cc" (30 Oct 2019, 2888 Bytes) of package /linux/privat/fimex-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   Fimex, test/testinghelpers.cc
    3 
    4   Copyright (C) 2019 met.no
    5 
    6   Contact information:
    7   Norwegian Meteorological Institute
    8   Box 43 Blindern
    9   0313 OSLO
   10   NORWAY
   11   email: diana@met.no
   12 
   13   Project Info:  https://wiki.met.no/fimex/start
   14 
   15   This library is free software; you can redistribute it and/or modify it
   16   under the terms of the GNU Lesser General Public License as published by
   17   the Free Software Foundation; either version 2.1 of the License, or
   18   (at your option) any later version.
   19 
   20   This library is distributed in the hope that it will be useful, but
   21   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   22   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   23   License for more details.
   24 
   25   You should have received a copy of the GNU Lesser General Public
   26   License along with this library; if not, write to the Free Software
   27   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   28   USA.
   29 */
   30 
   31 
   32 #include "test_config.h"
   33 
   34 #if HAVE_BOOST_UNIT_TEST_FRAMEWORK
   35 #define BOOST_TEST_MAIN
   36 #define BOOST_TEST_DYN_LINK
   37 #endif // HAVE_BOOST_UNIT_TEST_FRAMEWORK
   38 #include "testinghelpers.h"
   39 
   40 #include "fimex/CDMFileReaderFactory.h"
   41 
   42 #include <stdexcept>
   43 #include <fstream>
   44 
   45 using std::string;
   46 
   47 namespace {
   48 const string top_src(TOP_SRCDIR);
   49 
   50 const string src_share_etc(TOP_SRCDIR "/share/etc/");
   51 const string src_test(TOP_SRCDIR "/test/");
   52 
   53 const string extra_data_dir(TEST_EXTRADATA_DIR "/");
   54 } // namespace
   55 
   56 namespace MetNoFimex {
   57 
   58 const std::string& topSrcDir()
   59 {
   60     return top_src;
   61 }
   62 
   63 bool exists(const std::string& path)
   64 {
   65     return std::ifstream(path.c_str()).is_open();
   66 }
   67 
   68 void remove(const std::string& path)
   69 {
   70     ::remove(path.c_str());
   71 }
   72 
   73 string require(const std::string& path)
   74 {
   75     if (!exists(path))
   76         throw std::runtime_error("no such file: '" + path + "'");
   77     return path;
   78 }
   79 
   80 size_t file_size(const std::string& path)
   81 {
   82     std::ifstream f(path, std::ios::binary);
   83     const size_t begin = f.tellg();
   84     f.seekg(0, std::ios::end);
   85     const size_t end = f.tellg();
   86     return end - begin;
   87 }
   88 
   89 string pathShareEtc(const std::string& filename)
   90 {
   91     return require(src_share_etc + filename);
   92 }
   93 
   94 string pathTest(const std::string& filename)
   95 {
   96     return require(src_test + filename);
   97 }
   98 
   99 string pathTestExtra(const std::string& filename)
  100 {
  101     return require(extra_data_dir + filename);
  102 }
  103 
  104 bool hasTestExtra()
  105 {
  106     return exists(extra_data_dir + "flth00.dat");
  107 }
  108 
  109 void copyFile(const std::string& from, const std::string& to)
  110 {
  111     std::ifstream  src(from.c_str());
  112     std::ofstream  dst(to.c_str());
  113     dst << src.rdbuf();
  114 }
  115 
  116 bool writeToFile(CDMReader_p input, const std::string& fileName)
  117 {
  118 #ifdef HAVE_NETCDF_H
  119     MetNoFimex::createWriter(input, "netcdf", fileName);
  120     return exists(fileName);
  121 #else
  122     MetNoFimex::createWriter(qe, "null", "");
  123 #endif /* NETCDF */
  124 }
  125 
  126 } // namespace MetNoFimex