|
1 | 1 | #include "deepks_test.h" |
2 | 2 |
|
| 3 | +#include <cerrno> |
3 | 4 | #include <complex> |
| 5 | +#include <cstdlib> |
4 | 6 | #include <gtest/gtest.h> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace |
| 10 | +{ |
| 11 | +bool parse_double_token(const std::string& token, double* value) |
| 12 | +{ |
| 13 | + char* end = nullptr; |
| 14 | + errno = 0; |
| 15 | + const double parsed = std::strtod(token.c_str(), &end); |
| 16 | + if (end == token.c_str() || *end != '\0' || errno == ERANGE) |
| 17 | + { |
| 18 | + return false; |
| 19 | + } |
| 20 | + *value = parsed; |
| 21 | + return true; |
| 22 | +} |
| 23 | + |
| 24 | +bool parse_complex_token(const std::string& token, double* real, double* imag) |
| 25 | +{ |
| 26 | + if (token.size() < 5 || token[0] != '(' || token[token.size() - 1] != ')') |
| 27 | + { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + const std::string value = token.substr(1, token.size() - 2); |
| 32 | + const std::string::size_type comma = value.find(','); |
| 33 | + if (comma == std::string::npos || comma == 0 || comma == value.size() - 1) |
| 34 | + { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + return parse_double_token(value.substr(0, comma), real) && parse_double_token(value.substr(comma + 1), imag); |
| 39 | +} |
| 40 | +} // namespace |
5 | 41 |
|
6 | 42 | namespace Test_Deepks |
7 | 43 | { |
@@ -44,21 +80,30 @@ void test_deepks<T>::assert_file_matches_reference(const std::string& actual_fil |
44 | 80 | { |
45 | 81 | ASSERT_TRUE(reference >> reference_word) |
46 | 82 | << reference_file << " has fewer entries than " << actual_file << " at entry " << entry; |
47 | | - if ((actual_word[0] - '0' >= 0 && actual_word[0] - '0' < 10) || actual_word[0] == '-') |
| 83 | + |
| 84 | + double actual_num = 0.0; |
| 85 | + double reference_num = 0.0; |
| 86 | + const bool actual_is_number = parse_double_token(actual_word, &actual_num); |
| 87 | + const bool reference_is_number = parse_double_token(reference_word, &reference_num); |
| 88 | + double actual_real = 0.0; |
| 89 | + double actual_imag = 0.0; |
| 90 | + double reference_real = 0.0; |
| 91 | + double reference_imag = 0.0; |
| 92 | + const bool actual_is_complex = parse_complex_token(actual_word, &actual_real, &actual_imag); |
| 93 | + const bool reference_is_complex = parse_complex_token(reference_word, &reference_real, &reference_imag); |
| 94 | + |
| 95 | + if (actual_is_number || reference_is_number) |
48 | 96 | { |
49 | | - const double num1 = std::stod(actual_word); |
50 | | - const double num2 = std::stod(reference_word); |
51 | | - EXPECT_NEAR(num1, num2, test_thr) << "numeric mismatch at entry " << entry; |
| 97 | + ASSERT_TRUE(actual_is_number) << "Cannot parse actual numeric entry " << entry << ": " << actual_word; |
| 98 | + ASSERT_TRUE(reference_is_number) |
| 99 | + << "Cannot parse reference numeric entry " << entry << ": " << reference_word; |
| 100 | + EXPECT_NEAR(actual_num, reference_num, test_thr) << "numeric mismatch at entry " << entry; |
52 | 101 | } |
53 | | - else if (actual_word[0] == '(' && actual_word[actual_word.size() - 1] == ')' && reference_word[0] == '(' |
54 | | - && reference_word[reference_word.size() - 1] == ')') |
| 102 | + else if (actual_is_complex || reference_is_complex) |
55 | 103 | { |
56 | | - const std::string actual_str = actual_word.substr(1, actual_word.size() - 2); |
57 | | - const std::string reference_str = reference_word.substr(1, reference_word.size() - 2); |
58 | | - const double actual_real = std::stod(actual_str.substr(0, actual_str.find(','))); |
59 | | - const double actual_imag = std::stod(actual_str.substr(actual_str.find(',') + 1)); |
60 | | - const double reference_real = std::stod(reference_str.substr(0, reference_str.find(','))); |
61 | | - const double reference_imag = std::stod(reference_str.substr(reference_str.find(',') + 1)); |
| 104 | + ASSERT_TRUE(actual_is_complex) << "Cannot parse actual complex entry " << entry << ": " << actual_word; |
| 105 | + ASSERT_TRUE(reference_is_complex) |
| 106 | + << "Cannot parse reference complex entry " << entry << ": " << reference_word; |
62 | 107 | EXPECT_NEAR(actual_real, reference_real, test_thr) << "complex real mismatch at entry " << entry; |
63 | 108 | EXPECT_NEAR(actual_imag, reference_imag, test_thr) << "complex imag mismatch at entry " << entry; |
64 | 109 | } |
|
0 commit comments