|
| 1 | +#include "../tool_check.h" |
| 2 | +#include "gtest/gtest.h" |
| 3 | +#include "gmock/gmock.h" |
| 4 | +#include <cstdio> |
| 5 | +#include <fstream> |
| 6 | + |
| 7 | +/************************************************ |
| 8 | + * unit test of functions in tool_check.h |
| 9 | + ***********************************************/ |
| 10 | + |
| 11 | +/** |
| 12 | + * - Tested Function |
| 13 | + * - ModuleBase::CHECK_NAME |
| 14 | + * - check the next input from ifs is std::string |
| 15 | + * |
| 16 | + * - ModuleBase::CHECK_INT |
| 17 | + * - check the next input from ifs is int |
| 18 | + * |
| 19 | + * - ModuleBase::CHECK_DOUBLE |
| 20 | + * - check the next input from ifs is double |
| 21 | + * |
| 22 | + * - ModuleBase::CHECK_STRING |
| 23 | + * - check the next input from ifs is string |
| 24 | + * |
| 25 | + * - Author: Tianqi Zhao |
| 26 | + */ |
| 27 | + |
| 28 | +class ToolCheckTest : public testing::Test |
| 29 | +{ |
| 30 | + protected: |
| 31 | + std::ofstream ofs; |
| 32 | + std::ifstream ifs; |
| 33 | + // define std::string, int, double variables |
| 34 | + std::string name = "abaqus"; |
| 35 | + int ecut = 100; |
| 36 | + double occupation = 0.23; |
| 37 | + std::string caltype = "nscf"; |
| 38 | + // quit is the swith to control performance of function |
| 39 | + bool quit = false; |
| 40 | + // for capturing stdout |
| 41 | + std::string output = ""; |
| 42 | + void TearDown() |
| 43 | + { |
| 44 | + remove("tmp"); |
| 45 | + } |
| 46 | + |
| 47 | +}; |
| 48 | + |
| 49 | +TEST_F(ToolCheckTest, Name) |
| 50 | +{ |
| 51 | + ofs.open("tmp"); |
| 52 | + // double input to check continus check function |
| 53 | + ofs << name << std::endl; |
| 54 | + ofs << name << std::endl; |
| 55 | + ofs.close(); |
| 56 | + ifs.open("tmp"); |
| 57 | + // non-quit check |
| 58 | + testing::internal::CaptureStdout(); |
| 59 | + ModuleBase::CHECK_NAME(ifs, "abacus", quit); |
| 60 | + output = testing::internal::GetCapturedStdout(); |
| 61 | + EXPECT_THAT(output,testing::HasSubstr("not match")); |
| 62 | + // quit check: quit = false |
| 63 | + testing::internal::CaptureStdout(); |
| 64 | + EXPECT_DEATH(ModuleBase::CHECK_NAME(ifs, "abacus"), ""); |
| 65 | + output = testing::internal::GetCapturedStdout(); |
| 66 | + EXPECT_THAT(output,testing::HasSubstr("NOTICE")); |
| 67 | + ifs.close(); |
| 68 | +} |
| 69 | + |
| 70 | +TEST_F(ToolCheckTest, Int) |
| 71 | +{ |
| 72 | + ofs.open("tmp"); |
| 73 | + // double input to check continus check function |
| 74 | + ofs << ecut << std::endl; |
| 75 | + ofs << ecut << std::endl; |
| 76 | + ofs.close(); |
| 77 | + ifs.open("tmp"); |
| 78 | + // non-quit check |
| 79 | + testing::internal::CaptureStdout(); |
| 80 | + ModuleBase::CHECK_INT(ifs, 80, quit); |
| 81 | + output = testing::internal::GetCapturedStdout(); |
| 82 | + EXPECT_THAT(output,testing::HasSubstr("not match")); |
| 83 | + // quit check: quit = false |
| 84 | + testing::internal::CaptureStdout(); |
| 85 | + EXPECT_DEATH(ModuleBase::CHECK_INT(ifs, 80), ""); |
| 86 | + output = testing::internal::GetCapturedStdout(); |
| 87 | + EXPECT_THAT(output,testing::HasSubstr("NOTICE")); |
| 88 | + ifs.close(); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(ToolCheckTest, Double) |
| 92 | +{ |
| 93 | + ofs.open("tmp"); |
| 94 | + // double input to check continus check function |
| 95 | + ofs << occupation << std::endl; |
| 96 | + ofs << occupation << std::endl; |
| 97 | + ofs.close(); |
| 98 | + ifs.open("tmp"); |
| 99 | + // non-quit check: quit = false |
| 100 | + testing::internal::CaptureStdout(); |
| 101 | + ModuleBase::CHECK_DOUBLE(ifs, 0.23002, quit); |
| 102 | + output = testing::internal::GetCapturedStdout(); |
| 103 | + EXPECT_THAT(output,testing::HasSubstr("not match")); |
| 104 | + // quit check |
| 105 | + testing::internal::CaptureStdout(); |
| 106 | + EXPECT_DEATH(ModuleBase::CHECK_DOUBLE(ifs, 0.22998), ""); |
| 107 | + output = testing::internal::GetCapturedStdout(); |
| 108 | + EXPECT_THAT(output,testing::HasSubstr("NOTICE")); |
| 109 | + ifs.close(); |
| 110 | +} |
| 111 | + |
| 112 | +TEST_F(ToolCheckTest, String) |
| 113 | +{ |
| 114 | + ofs.open("tmp"); |
| 115 | + // double input to check continus check function |
| 116 | + ofs << caltype << std::endl; |
| 117 | + ofs << caltype << std::endl; |
| 118 | + ofs.close(); |
| 119 | + ifs.open("tmp"); |
| 120 | + // non-quit check: quit=false |
| 121 | + testing::internal::CaptureStdout(); |
| 122 | + ModuleBase::CHECK_STRING(ifs, "scf", quit); |
| 123 | + output = testing::internal::GetCapturedStdout(); |
| 124 | + EXPECT_THAT(output,testing::HasSubstr("not match")); |
| 125 | + // quit check |
| 126 | + testing::internal::CaptureStdout(); |
| 127 | + EXPECT_DEATH(ModuleBase::CHECK_STRING(ifs, "scf"), ""); |
| 128 | + output = testing::internal::GetCapturedStdout(); |
| 129 | + EXPECT_THAT(output,testing::HasSubstr("NOTICE")); |
| 130 | + ifs.close(); |
| 131 | +} |
0 commit comments