From 7c276813de7b4f7635eb024b3806936a4bc9c770 Mon Sep 17 00:00:00 2001 From: rulrich <ralf.m.ulrich@kit.edu> Date: Tue, 14 Jul 2020 18:35:25 +0200 Subject: [PATCH] test for corsika-data --- readLib/CMakeLists.txt | 41 ++++---- readLib/corsika_data/Interface.h | 18 ++-- readLib/source/Interface.boost.cc | 45 ++++---- readLib/source/Interface.dummy.cc | 32 +++--- readLib/source/test.data | 2 + readLib/source/test.data.bz2 | Bin 0 -> 221 bytes readLib/testData.cc | 166 ++++++++++++++++++++++++++++++ 7 files changed, 243 insertions(+), 61 deletions(-) create mode 100644 readLib/source/test.data create mode 100644 readLib/source/test.data.bz2 create mode 100644 readLib/testData.cc diff --git a/readLib/CMakeLists.txt b/readLib/CMakeLists.txt index 5073dd1..1f2c852 100644 --- a/readLib/CMakeLists.txt +++ b/readLib/CMakeLists.txt @@ -13,10 +13,10 @@ else (Boost_iostreams_FOUND) ) endif (Boost_iostreams_FOUND) -add_library(CorsikaData STATIC ${files}) +add_library (CorsikaData STATIC ${files}) if (Boost_iostreams_FOUND) - target_link_libraries(CorsikaData PUBLIC Boost::iostreams) + target_link_libraries (CorsikaData PUBLIC Boost::iostreams) endif (Boost_iostreams_FOUND) set_target_properties ( @@ -42,20 +42,23 @@ install ( #add_executable (readLZMA source/read.cc) #target_link_libraries (readLZMA CorsikaData) -# -------------------- -# code unit testing -# CORSIKA_ADD_TEST(testGeometry) -# target_link_libraries ( -# testGeometry -# CORSIKAgeometry -# CORSIKAunits -# CORSIKAtesting -# ) - -# CORSIKA_ADD_TEST(testFourVector) -# target_link_libraries ( -# testFourVector -# CORSIKAgeometry -# CORSIKAunits -# CORSIKAtesting -# ) +# add unit test, if run inside CORSIKA +if (COMMAND CORSIKA_ADD_TEST) + CORSIKA_ADD_TEST (testData) + set (TEST_WITH_BOOST "") + if (Boost_iostreams_FOUND) + set (TEST_WITH_BOOST "TEST_WITH_BOOST") + endif (Boost_iostreams_FOUND) + + target_compile_definitions ( + testData + PRIVATE + TESTDATA="${CMAKE_CURRENT_SOURCE_DIR}/source/test.data" + ${TEST_WITH_BOOST} + ) + target_link_libraries ( + testData + CorsikaData + CORSIKAtesting + ) +endif (COMMAND CORSIKA_ADD_TEST) diff --git a/readLib/corsika_data/Interface.h b/readLib/corsika_data/Interface.h index d01684c..c8199a3 100644 --- a/readLib/corsika_data/Interface.h +++ b/readLib/corsika_data/Interface.h @@ -15,20 +15,22 @@ namespace corsika_data { // the c++ interface functions - void CorDataOpenFile(char* name); + //void CorDataOpenFile(const char* name); + void CorDataOpenFile(const std::string& name); void CorDataFillArray(double* data, const int& length); void CorDataCloseFile(); double CorDataNextNumber(); - int CorDataNextText(std::string& data); + void CorDataNextText(std::string& data); + void CorDataNextText(char* data, int maxlength); bool CorDataCanDeCompress(); // the fortran interface functions extern "C" { - void cordataopenfile_(char* name); - void cordatafillarray_(double* data, const int& length); - void cordataclosefile_(); - double cordatanextnumber_(); - int cordatanexttext_(char* data, int length); - int cordatacandecompress_(); + void cordataopenfile_(const char* name); + void cordatafillarray_(double* data, const int& length); + void cordataclosefile_(); + double cordatanextnumber_(); + void cordatanexttext_(char* data, int maxlength); + int cordatacandecompress_(); } } // namespace corsika_data diff --git a/readLib/source/Interface.boost.cc b/readLib/source/Interface.boost.cc index 07b854d..14fdd8c 100644 --- a/readLib/source/Interface.boost.cc +++ b/readLib/source/Interface.boost.cc @@ -28,16 +28,17 @@ namespace corsika_data { ~DataFile() {} void Open(const char* name) { if (file_in_.is_open()) { - std::cout << "DataFile is still open! Close it" << std::endl; + std::cout << "DataFile is still open! Closing it now..." << std::endl; file_in_.close(); } std::string s(name); - auto i1 = s.find_first_not_of(" \t\r\n"); - auto i2 = s.find_first_of(" \t\r\n", i1); + auto i1 = s.find_first_not_of(" \t\r\n"); // trimm right + auto i2 = s.find_first_of(" \t\r\n", i1); // trimm left std::string trimmed(s.substr(i1, i2 - i1)); file_in_.open(trimmed, ios_base::in | ios_base::binary); in_.reset(); - in_.push(boost::iostreams::bzip2_decompressor()); + if (trimmed.rfind(".bz2") == trimmed.length()-4) + in_.push(boost::iostreams::bzip2_decompressor()); in_.push(file_in_); } void Close() { file_in_.close(); } @@ -57,30 +58,34 @@ namespace corsika_data { DataFile global_DataFile; - void CorDataOpenFile(char* name) { global_DataFile.Open(name); } + void CorDataOpenFile(const std::string& name) { global_DataFile.Open(name.c_str()); } + //void CorDataOpenFile(const char* name) { global_DataFile.Open(name); } void CorDataFillArray(double* data, const int& length) { global_DataFile.FillArray(data, length); } void CorDataCloseFile() { global_DataFile.Close(); } double CorDataNextNumber() { return global_DataFile.ReadNextNumber(); } - int CorDataNextText(char* data, const int length) { + void CorDataNextText(std::string& data) { + global_DataFile.ReadNextText(data); + } + void CorDataNextText(char* data, const int maxlength) { std::string STR; - global_DataFile.ReadNextText(STR); - for (int i = 0; i < length && i < (int)STR.size(); ++i) data[i] = STR[i]; - return 0; + CorDataNextText(STR); + for (int i = 0; i < maxlength && i < (int)STR.size(); ++i) + data[i] = STR[i]; } bool CorDataCanDeCompress() { return true; } - + extern "C" { - void cordataopenfile_(char* name) { CorDataOpenFile(name); } - void cordatafillarray_(double* data, const int& length) { - global_DataFile.FillArray(data, length); - } - double cordatanextnumber_() { return global_DataFile.ReadNextNumber(); } - int cordatanexttext_(char* data, const int length) { - return CorDataNextText(data, length); - } - void cordataclosefile_() { global_DataFile.Close(); } - int cordatacandecompress_() { return 1; } + void cordataopenfile_(const char* name) { CorDataOpenFile(name); } + void cordatafillarray_(double* data, const int& length) { + global_DataFile.FillArray(data, length); + } + double cordatanextnumber_() { return global_DataFile.ReadNextNumber(); } + void cordatanexttext_(char* data, const int maxlength) { + return CorDataNextText(data, maxlength); + } + void cordataclosefile_() { global_DataFile.Close(); } + int cordatacandecompress_() { return 1; } } } // namespace corsika_data diff --git a/readLib/source/Interface.dummy.cc b/readLib/source/Interface.dummy.cc index 7e0fb8a..8bc4713 100644 --- a/readLib/source/Interface.dummy.cc +++ b/readLib/source/Interface.dummy.cc @@ -14,7 +14,10 @@ namespace corsika_data { // the c++ interface functions - void CorDataOpenFile(char*) { + //void CorDataOpenFile(const char*) { + //std::runtime_error("Cannot read compressed data files with dummy library."); + //} + void CorDataOpenFile(const std::string&) { std::runtime_error("Cannot read compressed data files with dummy library."); } void CorDataFillArray(double*, const int&) { @@ -27,24 +30,25 @@ namespace corsika_data { std::runtime_error("Cannot read compressed data files with dummy library."); return 0; } - int CorDataNextText(std::string&) { + void CorDataNextText(std::string&) { + std::runtime_error("Cannot read compressed data files with dummy library."); + } + void CorDataNextText(char*, int) { std::runtime_error("Cannot read compressed data files with dummy library."); - return 0; } bool CorDataCanDeCompress() { return false; } // the fortran interface functions extern "C" { - void cordataopenfile_(char* name) { CorDataOpenFile(name); } - void cordatafillarray_(double* data, const int& length) { - CorDataFillArray(data, length); - } - void cordataclosefile_() { CorDataCloseFile(); } - double cordatanextnumber_() { return CorDataNextNumber(); } - int cordatanexttext_(char*) { - std::runtime_error("Cannot read compressed data files with dummy library."); - return 0; - } - int cordatacandecompress_() { return 0; } + void cordataopenfile_(const char* name) { CorDataOpenFile(name); } + void cordatafillarray_(double* data, const int& length) { + CorDataFillArray(data, length); + } + void cordataclosefile_() { CorDataCloseFile(); } + double cordatanextnumber_() { return CorDataNextNumber(); } + void cordatanexttext_(char*, int) { + std::runtime_error("Cannot read compressed data files with dummy library."); + } + int cordatacandecompress_() { return 0; } } } // namespace corsika_data diff --git a/readLib/source/test.data b/readLib/source/test.data new file mode 100644 index 0000000..2bef903 --- /dev/null +++ b/readLib/source/test.data @@ -0,0 +1,2 @@ +sibyll20 0.10000E-02 91 261 5 11 20 +0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 7.74900036E-06 1.00000000 1.47967212E-05 3.76063472E-05 3.91302201E-05 5.68050455E-05 5.91166172E-05 6.64338659E-05 8.79753206E-05 9.61698461E-05 1.02615748E-04 1.12829475E-04 1.28208514E-04 1.50734515E-04 1.79381183E-04 2.09064019E-04 2.32736333E-04 2.50067125E-04 2.74370745E-04 diff --git a/readLib/source/test.data.bz2 b/readLib/source/test.data.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..1fd229a02caa7c85f3e8050a7956e684ceadbb38 GIT binary patch literal 221 zcmV<303!cFT4*^jL0KkKSu@s?*8l+RU4TpwKm&i^0ss&s2p}*3`D7ZY{*cqk1pvXR zfD=&zOc5wdlT4YYXtp4cN#+6`E+x3);UH;6-vHJk<wF2gOHzd-nb#KUtLIAUrOtT* zR8tufG+bWsd8dFRfHHRMr*Nk?@&^d9rd3s2E&*APtq!A&?dA=(u1*{swev8AOO0f@ z0mGsiqe6;*rLAl+l-v|qqGnApikZj@c9^J*!h*G(<51=SI!y<6RdYx~NQUvVvxA)& XmJCHu{r}frdhA`v6yZWZ&stAh6ck`g literal 0 HcmV?d00001 diff --git a/readLib/testData.cc b/readLib/testData.cc new file mode 100644 index 0000000..a17495a --- /dev/null +++ b/readLib/testData.cc @@ -0,0 +1,166 @@ +/* + * (c) Copyright 2018 CORSIKA Project, corsika-project@lists.kit.edu + * + * See file AUTHORS for a list of contributors. + * + * This software is distributed under the terms of the GNU General Public + * Licence version 3 (GPL Version 3). See file LICENSE for a full version of + * the license. + */ + +#include <catch2/catch.hpp> + +#include <corsika_data/Interface.h> + +#include <iostream> +#include <array> +using namespace std; + +using namespace corsika_data; + +const array<double, 3*9> testData = {0.00000000, 0.00000000, 0.00000000, + 0.00000000, 0.00000000, 0.00000000, + 0.00000000, 0.00000000, 0.00000000, + 7.74900036E-06, 1.00000000, 1.47967212E-05, + 3.76063472E-05, 3.91302201E-05, 5.68050455E-05, + 5.91166172E-05, 6.64338659E-05, 8.79753206E-05, + 9.61698461E-05, 1.02615748E-04, 1.12829475E-04, + 1.28208514E-04, 1.50734515E-04, 1.79381183E-04, + 2.09064019E-04, 2.32736333E-04, 2.50067125E-04}; + +const std::string fileName = std::string(TESTDATA); +const std::string fileNameBZ2 = std::string(TESTDATA)+std::string(".bz2"); + + +TEST_CASE ("Data", "[data]") { + +#ifdef TEST_WITH_BOOST + SECTION ("c++, uncompressed") { + bool b = CorDataCanDeCompress(); + CHECK( b==true ); + cout << "Reading: " << fileName << endl; + CorDataOpenFile(fileName); + std::string str; + CorDataNextText(str); + CHECK ( str=="sibyll20" ); + CHECK ( CorDataNextNumber()==0.10000E-02 ); + CHECK ( CorDataNextNumber()==91 ); + CHECK ( CorDataNextNumber()==261 ); + CHECK ( CorDataNextNumber()==5 ); + CHECK ( CorDataNextNumber()==11 ); + CHECK ( CorDataNextNumber()==20 ); + + double aData[3*9]; + const int length = 3*9; + CorDataFillArray(aData, length); + + for (int i=0; i<length; ++i) { + CHECK( aData[i] == testData[i] ); + } + CorDataCloseFile(); + } + + SECTION ("c++, compressed") { + bool b = CorDataCanDeCompress(); + CHECK( b==true ); + cout << "Reading: " << fileNameBZ2 << endl; + CorDataOpenFile(fileNameBZ2); + std::string str; + CorDataNextText(str); + CHECK ( str=="sibyll20" ); + CHECK ( CorDataNextNumber()==0.10000E-02 ); + CHECK ( CorDataNextNumber()==91 ); + CHECK ( CorDataNextNumber()==261 ); + CHECK ( CorDataNextNumber()==5 ); + CHECK ( CorDataNextNumber()==11 ); + CHECK ( CorDataNextNumber()==20 ); + + double aData[3*9]; + const int length = 3*9; + CorDataFillArray(aData, length); + + for (int i=0; i<length; ++i) { + CHECK( aData[i] == testData[i] ); + } + CorDataCloseFile(); + } + + SECTION ("fortran, uncompressed") { + bool b = cordatacandecompress_(); + CHECK( b==true ); + cout << "Reading: " << fileName << endl; + cordataopenfile_(fileName.c_str()); + char str[10]; + cordatanexttext_(str, 10); + CHECK ( std::string(str)=="sibyll20" ); + CHECK ( cordatanextnumber_()==0.10000E-02 ); + CHECK ( cordatanextnumber_()==91 ); + CHECK ( cordatanextnumber_()==261 ); + CHECK ( cordatanextnumber_()==5 ); + CHECK ( cordatanextnumber_()==11 ); + CHECK ( cordatanextnumber_()==20 ); + + double aData[3*9]; + const int length = 3*9; + cordatafillarray_(aData, length); + + for (int i=0; i<length; ++i) { + CHECK( aData[i] == testData[i] ); + } + cordataclosefile_(); + } + + SECTION ("fortran, compressed") { + bool b = cordatacandecompress_(); + CHECK( b==true ); + cout << "Reading: " << fileNameBZ2 << endl; + cordataopenfile_(fileNameBZ2.c_str()); + char str[10]; + cordatanexttext_(str, 10); + CHECK ( std::string(str)=="sibyll20" ); + CHECK ( cordatanextnumber_()==0.10000E-02 ); + CHECK ( cordatanextnumber_()==91 ); + CHECK ( cordatanextnumber_()==261 ); + CHECK ( cordatanextnumber_()==5 ); + CHECK ( cordatanextnumber_()==11 ); + CHECK ( cordatanextnumber_()==20 ); + + double aData[3*9]; + const int length = 3*9; + cordatafillarray_(aData, length); + + for (int i=0; i<length; ++i) { + CHECK( aData[i] == testData[i] ); + } + cordataclosefile_(); + } + +#else + + SECTION ("c++") { + CorDataOpenFile(""); + double a[1]; + const int length = 1; + CorDataFillArray(a, length); + CorDataCloseFile(); + double v = CorDataNextNumber(); + string data; + CorDataNextText(data); + bool b = CorDataCanDeCompress(); + CHECK( b==false ); + } + + SECTION ("fortran") { + cordataopenfile_(""); + double a[1]; + const int length = 1; + cordatafillarray_(a, legnth); + cordataclosefile_(); + double d = cordatanextnumber_(); + const char* str = ""; + cordatanexttext_(str, 0); + int b = cordatacandecompress_(); + CHECK( b==0 ); + } +#endif +} -- GitLab