Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test:UnitTest for tool_title #2020

Merged
merged 12 commits into from
Mar 8, 2023
5 changes: 5 additions & 0 deletions source/module_base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ AddTest(
TARGET base_global_file
SOURCES global_file_test.cpp ../global_file.cpp ../global_variable.cpp ../memory.cpp ../timer.cpp
)

AddTest(
TARGET base_tool_title
SOURCES tool_title_test.cpp ../tool_title.cpp ../global_variable.cpp
)
71 changes: 71 additions & 0 deletions source/module_base/test/tool_title_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "../tool_title.h"
#include "../global_variable.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"

/************************************************
* unit test of functions in tool_title.h
***********************************************/

/**
* - Tested Function
* - ModuleBase::TITLE
* - Output title for each function.
*/

class ToolTitleTest : public testing::Test
{
protected:
std::ifstream ifs;
const std::string claname="ComplexMatrix";
const std::string funname="scaled_sum()";
const std::string cfname="ComplexMatrix::scaled_sum()";
void SetUp()
{

}
void TearDown()
{
remove("TITLEtest1.log");
remove("TITLEtest2.log");
remove("TITLEtest3.log");
}
};

TEST_F(ToolTitleTest, TITLE1)
{
GlobalV::ofs_running.open("TITLEtest1.log");
std::string output1;
ModuleBase::TITLE(cfname,true);
GlobalV::ofs_running.close();
ifs.open("TITLEtest1.log");
getline(ifs,output1);
EXPECT_THAT(output1,testing::HasSubstr(" ==> ComplexMatrix::scaled_sum()"));
ifs.close();
}

TEST_F(ToolTitleTest, TITLE2)
{
GlobalV::ofs_running.open("TITLEtest2.log");
std::string output2;
ModuleBase::TITLE(claname,funname,true);
GlobalV::ofs_running.close();
ifs.open("TITLEtest2.log");
getline(ifs,output2);
EXPECT_THAT(output2,testing::HasSubstr(" ==> ComplexMatrix::scaled_sum()"));
ifs.close();
}

TEST_F(ToolTitleTest, TITLE3)
{
std::ofstream oofs;
std::string output3a;
std::string output3b;
oofs.open("TITLEtest3.log");
ModuleBase::TITLE(oofs,claname,funname,true);
oofs.close();
ifs.open("TITLEtest3.log");
getline(ifs,output3a);
EXPECT_THAT(output3a,testing::HasSubstr(" ==> ComplexMatrix::scaled_sum()"));
ifs.close();
}
24 changes: 16 additions & 8 deletions source/module_base/tool_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace ModuleBase
// GLOBAL FUNCTION :
// NAME : TITLE( title for each function )
//==========================================================
void TITLE(const std::string &class_function_name)
void TITLE(const std::string &class_function_name,bool enable)
{
return;//no output

if (!enable)
{
return;//no output
}
#ifdef __NORMAL
std::cout<<" ==> "<<class_function_name<<std::endl;
#else
Expand All @@ -25,9 +27,12 @@ void TITLE(const std::string &class_function_name)
#endif
}

void TITLE(const std::string &class_name,const std::string &function_name)
void TITLE(const std::string &class_name,const std::string &function_name,bool enable)
{
return;//no output
if (!enable)
{
return;//no output
}
#ifdef __NORMAL
std::cout<<" ==> "<<class_name<<"::"<<function_name<<std::endl;
#else
Expand All @@ -39,9 +44,12 @@ void TITLE(const std::string &class_name,const std::string &function_name)
return;
}

void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &function_name)
void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &function_name,bool enable)
{
return;// no output
if (!enable)
{
return;//no output
}
#ifdef __NORMAL
std::cout<<"\n\n ==> "<<class_name<<"::"<<function_name<<std::endl;
#else
Expand All @@ -53,4 +61,4 @@ void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &f
return;
}

}
}
6 changes: 3 additions & 3 deletions source/module_base/tool_title.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
namespace ModuleBase
{

void TITLE(const std::string &class_function_name);
void TITLE(const std::string &class_name,const std::string &function_name);
void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &function_name);
void TITLE(const std::string &class_function_name,bool enable=false);
void TITLE(const std::string &class_name,const std::string &function_name,bool enable=false);
void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &function_name,bool enable=false);
}

#endif
6 changes: 3 additions & 3 deletions source/module_hamilt_general/module_xc/test/xc3_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ namespace ModuleBase
IntArray::IntArray(int,int){};
IntArray::~IntArray(){};

void TITLE(const std::string &class_function_name){};
void TITLE(const std::string &class_name,const std::string &function_name){};
void TITLE(const std::string &class_function_name,bool enable){};
void TITLE(const std::string &class_name,const std::string &function_name,bool enable){};
}

namespace GlobalV
Expand Down Expand Up @@ -118,4 +118,4 @@ void UnitCell::cal_ux()
}

InfoNonlocal::InfoNonlocal(){};
InfoNonlocal::~InfoNonlocal(){};
InfoNonlocal::~InfoNonlocal(){};