Skip to content

Commit bcf1bde

Browse files
style: Reorder includes and improve code formatting in test-diff-numerics.cpp
1 parent 542b531 commit bcf1bde

1 file changed

Lines changed: 43 additions & 23 deletions

File tree

test/test-diff-numerics.cpp

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
// -------------------------------------------------------------
1010

1111
#include <gtest/gtest.h>
12-
#include "NumericDiff.hpp"
13-
#include <fstream>
14-
#include <filesystem>
15-
#include <cstdio>
12+
1613
#include <array>
14+
#include <cstdio>
15+
#include <filesystem>
16+
#include <fstream>
1717
#include <memory>
1818

19+
#include "NumericDiff.hpp"
20+
1921
namespace fs = std::filesystem;
2022

2123
// Helper to copy test data to a temp file (not used in current tests)
@@ -48,7 +50,9 @@ std::string project_root() {
4850
// Helper to run NumericDiff and capture its output
4951
// This function constructs a NumericDiff object with the given options,
5052
// runs the comparison, and returns the output as a string.
51-
std::string run_diff(const std::string& file1, const std::string& file2, double tol, double threshold, bool side_by_side, bool suppress_common_lines, bool only_equal, bool quiet) {
53+
std::string run_diff(const std::string& file1, const std::string& file2, double tol,
54+
double threshold, bool side_by_side, bool suppress_common_lines,
55+
bool only_equal, bool quiet) {
5256
testing::internal::CaptureStdout();
5357
NumericDiffOption opts;
5458
opts.file1 = file1;
@@ -72,7 +76,7 @@ std::string run_diff_numerics_cli(const std::string& args) {
7276
std::string bin = project_root() + "/build/diff-numerics";
7377
std::string cmd = bin + " " + args + " 2>&1";
7478
std::cout << "[TEST] Running CLI: " << cmd << std::endl;
75-
std::unique_ptr<FILE, int(*)(FILE*)> pipe(popen(cmd.c_str(), "r"), pclose);
79+
std::unique_ptr<FILE, int (*)(FILE*)> pipe(popen(cmd.c_str(), "r"), pclose);
7680
if (!pipe) return "";
7781
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
7882
result += buffer.data();
@@ -85,7 +89,8 @@ TEST(DiffNumerics, DifferentFilesDefaultTolerance) {
8589
std::string file1 = test_data_path("delta_3D2_2.dat");
8690
std::string file2 = test_data_path("delta_3D2.dat");
8791
std::string output = run_diff(file1, file2, 1E-2, 1E-6, false, false, false, false);
88-
std::cout << "[TEST] DefaultTolerance: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n" << output << std::endl;
92+
std::cout << "[TEST] DefaultTolerance: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n"
93+
<< output << std::endl;
8994
EXPECT_FALSE(output.empty());
9095
}
9196

@@ -94,7 +99,8 @@ TEST(DiffNumerics, DifferentFilesTightTolerance) {
9499
std::string file1 = test_data_path("delta_3D2_2.dat");
95100
std::string file2 = test_data_path("delta_3D2.dat");
96101
std::string output = run_diff(file1, file2, 1E-10, 1E-12, false, false, false, false);
97-
std::cout << "[TEST] TightTolerance: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n" << output << std::endl;
102+
std::cout << "[TEST] TightTolerance: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n"
103+
<< output << std::endl;
98104
EXPECT_FALSE(output.empty());
99105
}
100106

@@ -103,7 +109,8 @@ TEST(DiffNumerics, SideBySideOutput) {
103109
std::string file1 = test_data_path("delta_3D2_2.dat");
104110
std::string file2 = test_data_path("delta_3D2.dat");
105111
std::string output = run_diff(file1, file2, 1E-2, 1E-6, true, false, false, false);
106-
std::cout << "[TEST] SideBySide: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n" << output << std::endl;
112+
std::cout << "[TEST] SideBySide: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n"
113+
<< output << std::endl;
107114
EXPECT_FALSE(output.empty());
108115
EXPECT_NE(output.find("|"), std::string::npos);
109116
}
@@ -113,7 +120,9 @@ TEST(DiffNumerics, SuppressCommonLines) {
113120
std::string file1 = test_data_path("delta_3D2_2.dat");
114121
std::string file2 = test_data_path("delta_3D2.dat");
115122
std::string output = run_diff(file1, file2, 1E-2, 1E-6, true, true, false, false);
116-
std::cout << "[TEST] SuppressCommonLines: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n" << output << std::endl;
123+
std::cout << "[TEST] SuppressCommonLines: file1=" << file1 << ", file2=" << file2
124+
<< "\nOutput:\n"
125+
<< output << std::endl;
117126
EXPECT_FALSE(output.empty());
118127
}
119128

@@ -122,39 +131,47 @@ TEST(DiffNumerics, QuietMode) {
122131
std::string file1 = test_data_path("delta_3D2_2.dat");
123132
std::string file2 = test_data_path("delta_3D2.dat");
124133
std::string output = run_diff(file1, file2, 1E-2, 1E-6, false, false, false, true);
125-
std::cout << "[TEST] QuietMode: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n" << output << std::endl;
134+
std::cout << "[TEST] QuietMode: file1=" << file1 << ", file2=" << file2 << "\nOutput:\n"
135+
<< output << std::endl;
126136
EXPECT_FALSE(output.empty());
127137
}
128138

129139
// Test: Invalid column width
130140
TEST(DiffNumericsCLI, InvalidColumnWidth) {
131-
std::string out = run_diff_numerics_cli("-w 5 " + test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2.dat"));
141+
std::string out = run_diff_numerics_cli("-w 5 " + test_data_path("delta_3D2_2.dat") + " " +
142+
test_data_path("delta_3D2.dat"));
132143
std::cout << "[TEST] CLI Output: " << out << std::endl;
133144
EXPECT_NE(out.find("Error: Column width"), std::string::npos);
134-
out = run_diff_numerics_cli("-w 500 " + test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2.dat"));
145+
out = run_diff_numerics_cli("-w 500 " + test_data_path("delta_3D2_2.dat") + " " +
146+
test_data_path("delta_3D2.dat"));
135147
std::cout << "[TEST] CLI Output: " << out << std::endl;
136148
EXPECT_NE(out.find("Error: Column width"), std::string::npos);
137149
}
138150

139151
// Test: Invalid tolerance
140152
TEST(DiffNumericsCLI, InvalidTolerance) {
141-
std::string out = run_diff_numerics_cli("-t 1e-20 " + test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2.dat"));
153+
std::string out = run_diff_numerics_cli("-t 1e-20 " + test_data_path("delta_3D2_2.dat") + " " +
154+
test_data_path("delta_3D2.dat"));
142155
EXPECT_NE(out.find("Error: Tolerance"), std::string::npos);
143-
out = run_diff_numerics_cli("-t 1e5 " + test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2.dat"));
156+
out = run_diff_numerics_cli("-t 1e5 " + test_data_path("delta_3D2_2.dat") + " " +
157+
test_data_path("delta_3D2.dat"));
144158
EXPECT_NE(out.find("Error: Tolerance"), std::string::npos);
145159
}
146160

147161
// Test: Invalid threshold
148162
TEST(DiffNumericsCLI, InvalidThreshold) {
149-
std::string out = run_diff_numerics_cli("-T -1 " + test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2.dat"));
163+
std::string out = run_diff_numerics_cli("-T -1 " + test_data_path("delta_3D2_2.dat") + " " +
164+
test_data_path("delta_3D2.dat"));
150165
EXPECT_NE(out.find("Error: Threshold"), std::string::npos);
151-
out = run_diff_numerics_cli("-T 1e5 " + test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2.dat"));
166+
out = run_diff_numerics_cli("-T 1e5 " + test_data_path("delta_3D2_2.dat") + " " +
167+
test_data_path("delta_3D2.dat"));
152168
EXPECT_NE(out.find("Error: Threshold"), std::string::npos);
153169
}
154170

155171
// Test: Same file error
156172
TEST(DiffNumericsCLI, SameFileError) {
157-
std::string out = run_diff_numerics_cli(test_data_path("delta_3D2_2.dat") + " " + test_data_path("delta_3D2_2.dat"));
173+
std::string out = run_diff_numerics_cli(test_data_path("delta_3D2_2.dat") + " " +
174+
test_data_path("delta_3D2_2.dat"));
158175
EXPECT_NE(out.find("Error: The two input files must be different."), std::string::npos);
159176
}
160177

@@ -216,7 +233,8 @@ TEST(DiffNumerics, P2F2_QuietMode) {
216233

217234
// Test: CLI summary output for these files
218235
TEST(DiffNumericsCLI, P2F2_CLISummary) {
219-
std::string args = std::string("-s ") + test_data_path("delta_3P2-3F2.dat") + " " + test_data_path("delta_3P2-3F2_2.dat");
236+
std::string args = std::string("-s ") + test_data_path("delta_3P2-3F2.dat") + " " +
237+
test_data_path("delta_3P2-3F2_2.dat");
220238
std::string out = run_diff_numerics_cli(args);
221239
EXPECT_NE(out.find("Files DIFFER"), std::string::npos);
222240
EXPECT_NE(out.find("max percentage error"), std::string::npos);
@@ -242,10 +260,12 @@ TEST(DiffNumerics, ColorDifferentDigits) {
242260
// Should not colorize the entire number if only a few digits differ
243261
size_t first_red = output.find("\033[31m");
244262
size_t first_reset = output.find("\033[0m", first_red);
245-
EXPECT_TRUE(first_red != std::string::npos && first_reset != std::string::npos && first_reset > first_red);
263+
EXPECT_TRUE(first_red != std::string::npos && first_reset != std::string::npos &&
264+
first_reset > first_red);
246265
}
247266

248-
// Test: Compare only columns 1, 2, and 4 of delta_3P2-3F2.dat and delta_3P2-3F2_2.dat; expect no output (columns are equal)
267+
// Test: Compare only columns 1, 2, and 4 of delta_3P2-3F2.dat and delta_3P2-3F2_2.dat; expect no
268+
// output (columns are equal)
249269
TEST(DiffNumerics, P2F2_Columns1_2_4_Equal) {
250270
std::string file1 = test_data_path("delta_3P2-3F2.dat");
251271
std::string file2 = test_data_path("delta_3P2-3F2_2.dat");
@@ -254,12 +274,12 @@ TEST(DiffNumerics, P2F2_Columns1_2_4_Equal) {
254274
opts.file2 = file2;
255275
opts.tolerance = 1E-2;
256276
opts.threshold = 1E-6;
257-
opts.side_by_side = false; // normal mode
277+
opts.side_by_side = false; // normal mode
258278
opts.suppress_common_lines = false;
259279
opts.only_equal = false;
260280
opts.quiet = false;
261281
opts.color_diff_digits = false;
262-
opts.columns_to_compare = {1, 2, 4}; // columns are 1-based
282+
opts.columns_to_compare = {1, 2, 4}; // columns are 1-based
263283
testing::internal::CaptureStdout();
264284
NumericDiff diff(opts);
265285
diff.run();

0 commit comments

Comments
 (0)