LCOV - code coverage report
Current view: top level - /opt/artdaq/srcs/artdaq-database/test/ConfigurationDB/Utils/test001 - ValidateJson_t.cc (source / functions) Coverage Total Hit
Test: artdaq.info.cleaned Lines: 43.1 % 51 22
Test Date: 2026-10-29 00:35:58 Functions: 50.0 % 2 1

            Line data    Source code
       1              : #include "test/common.h"
       2              : 
       3              : #include "artdaq-database/DataFormats/Json/json_common.h"
       4              : 
       5              : #include <fstream>
       6              : #include <sstream>
       7              : 
       8              : namespace jsn = artdaq::database::json;
       9              : 
      10              : // Helper function to write content to a file
      11            0 : bool write_to_file(const std::string& filename, const std::string& content) {
      12            0 :   std::ofstream outfile(filename);
      13              : 
      14            0 :   if (!outfile.is_open()) {
      15            0 :     std::cerr << "ERROR: Failed to open file for writing: " << filename << "\n";
      16            0 :     return false;
      17              :   }
      18              : 
      19            0 :   outfile << content;
      20            0 :   outfile.close();
      21              : 
      22            0 :   std::cout << "Debug file written: " << filename << "\n";
      23            0 :   return true;
      24            0 : }
      25              : 
      26            3 : int main(int argc, char* argv[]) try {
      27            3 :   if (argc != 2) {
      28            0 :     std::cerr << "Usage: " << argv[0] << " <json-file>\n";
      29            0 :     return process_exit_code::INVALID_ARGUMENT;
      30              :   }
      31              : 
      32            6 :   auto file_name = std::string{argv[1]};
      33              : 
      34              :   // Read input file
      35            3 :   std::ifstream is(file_name);
      36            3 :   if (!is.is_open()) {
      37            0 :     std::cerr << "ERROR: Failed to open input file: " << file_name << "\n";
      38            0 :     return process_exit_code::FAILURE;
      39              :   }
      40              : 
      41            3 :   std::stringstream json_buffer;
      42            3 :   json_buffer << is.rdbuf();
      43            3 :   is.close();
      44              : 
      45            3 :   auto original_json = json_buffer.str();
      46              : 
      47              :   // Parse original JSON into first AST
      48              :   auto reader = jsn::JsonReader{};
      49            3 :   jsn::object_t ast1;
      50              : 
      51            3 :   if (!reader.read(original_json, ast1)) {
      52            0 :     std::cerr << "Failed reading JSON file into AST1: << " << original_json << ">>\n";
      53            0 :     return process_exit_code::FAILURE;
      54              :   }
      55              : 
      56              :   // Write AST1 to string buffer
      57              :   auto writer = jsn::JsonWriter{};
      58            3 :   auto serialized_json = std::string{};
      59              : 
      60            3 :   if (!writer.write(ast1, serialized_json)) {
      61            0 :     std::cerr << "Failed writing AST1 to string buffer\n";
      62            0 :     return process_exit_code::FAILURE;
      63              :   }
      64              : 
      65              :   // Parse serialized JSON into second AST
      66            3 :   jsn::object_t ast2;
      67              : 
      68            3 :   if (!reader.read(serialized_json, ast2)) {
      69            0 :     std::cerr << "Failed reading serialized JSON into AST2: << " << serialized_json << ">>\n";
      70            0 :     return process_exit_code::FAILURE;
      71              :   }
      72              : 
      73              :   // Compare both ASTs
      74            3 :   auto compare_result = ast1 == ast2;
      75              : 
      76            3 :   if (compare_result.first) {
      77            3 :     std::cout << "SUCCESS: ASTs are equivalent after roundtrip.\n";
      78            3 :     std::cout << "Original:\n" << original_json << "\n";
      79            3 :     std::cout << "Serialized:\n" << serialized_json << "\n";
      80            3 :     return process_exit_code::SUCCESS;
      81              :   }
      82              : 
      83              :   // Test failed - output diagnostic information
      84            0 :   std::cout << "Test failed (AST1 != AST2); error message: " << compare_result.second << "\n";
      85              : 
      86            0 :   std::cout << "Original JSON (from file):\n" << original_json << "\n";
      87            0 :   std::cout << "Serialized JSON (from AST1):\n" << serialized_json << "\n";
      88              : 
      89              :   // Write debug files in current directory
      90            0 :   write_to_file("ValidateJson.output.json", serialized_json);
      91            0 :   write_to_file("ValidateJson.expected.json", original_json);
      92              : 
      93            0 :   return process_exit_code::FAILURE;
      94            3 : } catch (...) {
      95            0 :   std::cerr << "Process exited with error: " << ::debug::current_exception_diagnostic_information();
      96            0 :   return process_exit_code::UNCAUGHT_EXCEPTION;
      97            0 : }
        

Generated by: LCOV version 2.0-1