1+ #include < iostream>
2+ #include < fstream>
3+ #include < sstream>
4+ #include < string>
5+ #include < vector>
6+ #include < map>
7+ #include < set>
8+ #include < algorithm>
9+
10+ // Helper to trim whitespace
11+ std::string trim (const std::string& s) {
12+ size_t start = s.find_first_not_of (" \t\r\n " );
13+ size_t end = s.find_last_not_of (" \t\r\n " );
14+ return (start == std::string::npos) ? " " : s.substr (start, end - start + 1 );
15+ }
16+
17+ // Parse matrices from XML file
18+ std::map<std::string, std::vector<std::string>> parse_matrices (const std::string& filename) {
19+ std::ifstream file (filename);
20+ std::map<std::string, std::vector<std::string>> matrices;
21+ std::string line, current_name;
22+ bool in_matrix = false ;
23+ std::vector<std::string> values;
24+
25+ while (std::getline (file, line)) {
26+ line = trim (line);
27+ if (line.find (" <Matrix" ) == 0 ) {
28+ size_t name_pos = line.find (" name=\" " );
29+ if (name_pos != std::string::npos) {
30+ size_t start = name_pos + 6 ;
31+ size_t end = line.find (" \" " , start);
32+ current_name = line.substr (start, end - start);
33+ in_matrix = true ;
34+ values.clear ();
35+ }
36+ } else if (in_matrix && line.find (" </Matrix>" ) == 0 ) {
37+ matrices[current_name] = values;
38+ in_matrix = false ;
39+ } else if (in_matrix) {
40+ std::istringstream iss (line);
41+ std::string val;
42+ while (iss >> val) {
43+ values.push_back (val);
44+ }
45+ }
46+ }
47+ return matrices;
48+ }
49+
50+ int main (int argc, char * argv[]) {
51+ if (argc != 3 ) {
52+ std::cerr << " Usage: compare_matrices good_output.xml test_output.xml\n " ;
53+ return 1 ;
54+ }
55+
56+ auto good = parse_matrices (argv[1 ]);
57+ auto test = parse_matrices (argv[2 ]);
58+
59+ std::set<std::string> all_vars;
60+ for (const auto & kv : good) all_vars.insert (kv.first );
61+ for (const auto & kv : test) all_vars.insert (kv.first );
62+
63+ int compared = 0 ;
64+ std::vector<std::string> mismatched_vars;
65+
66+ for (const auto & var : all_vars) {
67+
68+ int in_good = good.count (var);
69+ int in_test = test.count (var);
70+
71+ if (in_good==0 ) {
72+ std::cout << " Variable '" << var << " ' present in test output but missing from good output.\n " ;
73+ continue ;
74+ }
75+ if (in_test==0 ) {
76+ std::cout << " Variable '" << var << " ' present in good output but missing from test output.\n " ;
77+ continue ;
78+ }
79+
80+ compared++;
81+ const auto & good_vals = good[var];
82+ const auto & test_vals = test[var];
83+ if (good_vals.size () != test_vals.size ()) {
84+ std::cout << " Variable '" << var << " ' has different number of values (good: " << good_vals.size ()
85+ << " , test: " << test_vals.size () << " ).\n " ;
86+ mismatched_vars.push_back (var);
87+ continue ;
88+ }
89+ bool mismatch = false ;
90+ for (size_t i = 0 ; i < good_vals.size (); ++i) {
91+ if (good_vals[i] != test_vals[i]) {
92+ mismatch = true ;
93+ break ;
94+ }
95+ }
96+ if (mismatch) {
97+ std::cout << " Variable '" << var << " ' has mismatched values.\n " ;
98+ mismatched_vars.push_back (var);
99+ }
100+ }
101+
102+ std::cout << compared << " variables compared.\n " ;
103+ if (!mismatched_vars.empty ()) {
104+ std::cout << " Mismatched values found for variables:\n " ;
105+ for (const auto & var : mismatched_vars) {
106+ std::cout << " " << var << " \n " ;
107+ }
108+ } else {
109+ std::cout << " All compared variables matched.\n " ;
110+ }
111+ return 0 ;
112+ }
0 commit comments