From 88dc1eeb782a2b189a1a2bd9f1a7d1d287764749 Mon Sep 17 00:00:00 2001 From: "Oleg E. Peil" Date: Wed, 9 Mar 2016 18:56:59 +0100 Subject: [PATCH] Added a simple C-test of the ATM library The tests are build to be executed by CMake test functionality. --- c++/plovasp/atm/test/CMakeLists.txt | 24 +++++++ c++/plovasp/atm/test/reorder_flag.cpp | 23 +++++++ c++/plovasp/atm/test/reorder_inds.cpp | 27 ++++++++ c++/plovasp/atm/test/testing.hpp | 91 +++++++++++++++++++++++++++ c++/plovasp/atm/test/tet_weights.cpp | 35 +++++++++++ c++/plovasp/atm/test/weights1.cpp | 27 ++++++++ 6 files changed, 227 insertions(+) create mode 100644 c++/plovasp/atm/test/CMakeLists.txt create mode 100644 c++/plovasp/atm/test/reorder_flag.cpp create mode 100644 c++/plovasp/atm/test/reorder_inds.cpp create mode 100644 c++/plovasp/atm/test/testing.hpp create mode 100644 c++/plovasp/atm/test/tet_weights.cpp create mode 100644 c++/plovasp/atm/test/weights1.cpp diff --git a/c++/plovasp/atm/test/CMakeLists.txt b/c++/plovasp/atm/test/CMakeLists.txt new file mode 100644 index 00000000..5407a392 --- /dev/null +++ b/c++/plovasp/atm/test/CMakeLists.txt @@ -0,0 +1,24 @@ +find_package(TriqsTest) +enable_testing() + +# Linking and include info +#add_library(atm_c dos_tetra3d.hpp dos_tetra3d.cpp argsort.h argsort.c) +#set_target_properties(atm_c PROPERTIES LINKER_LANGUAGE CXX) +#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/c++/plovasp/atm ${TRIQS_INCLUDE_ALL}) + +FILE(GLOB TestList RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) +FOREACH( TestName1 ${TestList} ) + STRING(REPLACE ".cpp" "" TestName ${TestName1}) + add_executable( ${TestName} ${CMAKE_CURRENT_SOURCE_DIR}/${TestName}.cpp ) + target_link_libraries( ${TestName} atm_c ${TRIQS_LIBRARY_ALL} ) + triqs_set_rpath_for_target( ${TestName} ) + triqs_add_cpp_test( ${TestName} ) +# if (TESTS_C_WITH_VALGRIND) +# add_test ( ${TestName}_valgrind valgrind --error-exitcode=1 ${CMAKE_CURRENT_BINARY_DIR}/${TestName}) +# endif() +ENDFOREACH( TestName1 ${TestList} ) + +#add_executable(test_atm test2py.cpp) +#target_link_libraries(test_atm atm_c) + +#add_subdirectory(test) diff --git a/c++/plovasp/atm/test/reorder_flag.cpp b/c++/plovasp/atm/test/reorder_flag.cpp new file mode 100644 index 00000000..293b75b8 --- /dev/null +++ b/c++/plovasp/atm/test/reorder_flag.cpp @@ -0,0 +1,23 @@ + +#include "testing.hpp" + +int main() +{ + double e[4], en; + int inds[4]; + int flag, flag_should; + + e[0] = -1.5; + e[1] = -1.309017; + e[2] = -1.0; + e[3] = -0.5; + + en = -0.55; + printf("\n Test case 1\n\n"); + + flag = dos_reorder(en, e, inds); + flag_should = 3; + + if(check_reorder_flag(flag, flag_should)) return 1; +} + diff --git a/c++/plovasp/atm/test/reorder_inds.cpp b/c++/plovasp/atm/test/reorder_inds.cpp new file mode 100644 index 00000000..2e4df33a --- /dev/null +++ b/c++/plovasp/atm/test/reorder_inds.cpp @@ -0,0 +1,27 @@ + +#include "testing.hpp" + +int main() +{ + double e[4], en; + int inds[4], inds_should[4]; + int flag; + + e[0] = -1.5; + e[1] = -1.0; + e[2] = -1.309017; + e[3] = -0.5; + + en = -0.55; + printf("\n Test case 2\n\n"); + + flag = dos_reorder(en, e, inds); + + inds_should[0] = 0; + inds_should[1] = 2; + inds_should[2] = 1; + inds_should[3] = 3; + + if(check_reorder_inds(inds, inds_should)) return 1; +} + diff --git a/c++/plovasp/atm/test/testing.hpp b/c++/plovasp/atm/test/testing.hpp new file mode 100644 index 00000000..79ff99e4 --- /dev/null +++ b/c++/plovasp/atm/test/testing.hpp @@ -0,0 +1,91 @@ + +#include +#include +#include + +#define ASSERT(cond, message) if(!(cond)) { \ + printf("*** Fail: %s\n", message); return 1;} + +// +// Functions defined in 'atm_c' library +// +extern int dos_corner_weights(double en, double *eigs, int *inds, + double *ci); +extern int dos_tet_weights(double en, double *eigs, int *inds, + double *ct); +extern int dos_reorder(double en, double *e, int *inds); + + +int check_reorder_flag(int flag, int flag_should); +int check_reorder_inds(int *inds, int *inds_should); +int check_weights_result(double *res, double *r_should); + +// +// Test templates +// +int check_reorder_flag(int flag, int flag_should) +{ + char mess[128]; + sprintf(mess, "Reorder flag should be %d and not %d", flag_should, flag); + ASSERT(flag == flag_should, mess); + return 0; +} + +int check_reorder_inds(int *inds, int *inds_should) +{ + char mess[128], tmp[128], numb[128]; + int i, flag; + + strcpy(mess, "Inds should be "); + numb[0] = '\0'; + flag = 1; + for(i = 0; i < 4; i++) + { + if(inds_should[i] != inds[i]) flag = 0; + sprintf(tmp, " %d", inds_should[i]); + strcat(numb, tmp); + } + strcat(mess, numb); + strcat(mess, " and not"); + numb[0] = '\0'; + for(i = 0; i < 4; i++) + { + sprintf(tmp, " %d", inds[i]); + strcat(numb, tmp); + } + strcat(mess, numb); + + ASSERT(flag, mess); + + return 0; +} + +int check_weights_result(double *res, double *r_should) +{ + const double tol = 1e-14; + int i, flag; + char mess[128], tmp[128]; + + flag = 1; + for(i = 0; i < 4; i++) + { + if(fabs(r_should[i] - res[i]) > tol) + { + flag = 0; + break; + } + } + strcpy(mess, "Success"); + if(!flag) + { + sprintf(mess, "res[%d] should be %20.15lf", i, r_should[i]); + sprintf(tmp, " and not %20.15lf", res[i]); + strcat(mess, tmp); + } + + ASSERT(flag, mess); + + return 0; +} + + diff --git a/c++/plovasp/atm/test/tet_weights.cpp b/c++/plovasp/atm/test/tet_weights.cpp new file mode 100644 index 00000000..9c1836d7 --- /dev/null +++ b/c++/plovasp/atm/test/tet_weights.cpp @@ -0,0 +1,35 @@ + +#include "testing.hpp" + +int main() +{ + double e[4], en, ci_sum, ct, res[4]; + int inds[4], inds_should[4]; + int i, flag; + char mess[128]; + + e[0] = -1.5; + e[1] = -1.309017; + e[2] = -1.0; + e[3] = -0.5; + + en = -0.55; + printf("\n Test case 2\n\n"); + + flag = dos_reorder(en, e, inds); + + dos_corner_weights(en, e, inds, res); + dos_tet_weights(en, e, inds, &ct); + + for(i = 0, ci_sum = 0.0; i < 4; i++) + { + printf(" res[%d] = %20.15lf\n", i, res[i]); + ci_sum += res[i]; + } + + printf(" Difference: %le\n", fabs(ci_sum - ct)); + + sprintf(mess, "Difference between 'ci_sum' and 'ct' is too large"); + ASSERT(fabs(ci_sum - ct) < 1e-12, mess); +} + diff --git a/c++/plovasp/atm/test/weights1.cpp b/c++/plovasp/atm/test/weights1.cpp new file mode 100644 index 00000000..b04fa297 --- /dev/null +++ b/c++/plovasp/atm/test/weights1.cpp @@ -0,0 +1,27 @@ + +#include "testing.hpp" + +int main() +{ + double e[4], en, res[4], r_should[4]; + int inds[4]; + int flag; + + e[0] = -1.5; + e[1] = -1.309017; + e[2] = -1.0; + e[3] = -0.5; + + en = -0.55; + printf("\n Test case 4\n\n"); + + dos_corner_weights(en, e, inds, res); + + r_should[0] = 0.000309016992226; + r_should[1] = 0.000381966005939; + r_should[2] = 0.000618033984453; + r_should[3] = 0.017232002550965; + + if(check_weights_result(res, r_should)) return 1; +} +