File indexing completed on 2026-10-20 08:46:00
0001 #include <cassert>
0002 #include <cstdint>
0003 #include <cstdlib>
0004 #include <filesystem>
0005 #include <stdexcept>
0006 #include <string>
0007 #include <sys/wait.h>
0008 #include <unistd.h>
0009
0010 #include "sreport.h"
0011
0012 int main()
0013 {
0014 sreport non_profiled;
0015 non_profiled.run = NP::Make<float>(1);
0016
0017 NPFold* serialized = non_profiled.serialize();
0018 assert(serialized->get("run"));
0019 assert(serialized->get("runprof") == nullptr);
0020 assert(serialized->get("ranges") == nullptr);
0021
0022 sreport restored_non_profiled;
0023 restored_non_profiled.import(serialized);
0024 assert(restored_non_profiled.run);
0025 assert(restored_non_profiled.run != non_profiled.run);
0026 assert(restored_non_profiled.runprof == nullptr);
0027 assert(restored_non_profiled.ranges == nullptr);
0028
0029 const std::filesystem::path directory =
0030 std::filesystem::temp_directory_path() /
0031 ("sreport_test-" + std::to_string(static_cast<long long>(::getpid())));
0032 std::filesystem::remove_all(directory);
0033 non_profiled.save(directory.c_str());
0034
0035 sreport* reloaded_non_profiled = sreport::Load(directory.c_str());
0036 assert(reloaded_non_profiled->run);
0037 assert(reloaded_non_profiled->runprof == nullptr);
0038 assert(reloaded_non_profiled->ranges == nullptr);
0039 std::filesystem::remove_all(directory);
0040
0041 sreport profiled;
0042 profiled.run = NP::Make<float>(1);
0043 profiled.runprof = NP::Make<std::int64_t>(2, 4);
0044 profiled.ranges = NP::Make<std::int64_t>(1, 5);
0045
0046 NPFold* serialized_profiled = profiled.serialize();
0047 sreport restored_profiled;
0048 restored_profiled.import(serialized_profiled);
0049 assert(restored_profiled.run);
0050 assert(restored_profiled.runprof);
0051 assert(restored_profiled.runprof != profiled.runprof);
0052 assert(restored_profiled.ranges);
0053 assert(restored_profiled.ranges != profiled.ranges);
0054
0055 bool rejected_missing_run_on_serialize = false;
0056 try
0057 {
0058 sreport invalid;
0059 (void)invalid.serialize();
0060 }
0061 catch (const std::runtime_error& error)
0062 {
0063 rejected_missing_run_on_serialize =
0064 std::string(error.what()).find("required 'run' array") != std::string::npos;
0065 }
0066 assert(rejected_missing_run_on_serialize);
0067
0068 NPFold missing_run;
0069 bool rejected_missing_run = false;
0070 try
0071 {
0072 sreport invalid;
0073 invalid.import(&missing_run);
0074 }
0075 catch (const std::runtime_error& error)
0076 {
0077 rejected_missing_run =
0078 std::string(error.what()).find("required 'run' array") != std::string::npos;
0079 }
0080 assert(rejected_missing_run);
0081
0082 const std::filesystem::path input = directory / "profiled-input";
0083 const std::filesystem::path output = directory / "profiled-output";
0084 const std::filesystem::path profile = directory / "custom-profile.csv";
0085 NPFold input_fold;
0086 NP* input_run = NP::Make<std::int64_t>(1, 1);
0087 NP::SetMeta<std::string>(input_run->meta,
0088 "EventTimingProfile__Configure",
0089 "1760000000000000,900000000,,,1234,567");
0090 input_fold.add("run", input_run);
0091 input_fold.save(input.c_str());
0092 std::filesystem::copy_file(EVENT_TIMING_PROFILE_FIXTURE, profile);
0093
0094 const std::string command =
0095 "SREPORT_FOLD='" + output.string() + "' '" + SREPORT_EXECUTABLE +
0096 "' '" + input.string() + "' --event-timing-profile '" +
0097 profile.string() + "' >/dev/null 2>&1";
0098 const int status = std::system(command.c_str());
0099 assert(status != -1);
0100 assert(WIFEXITED(status));
0101 assert(WEXITSTATUS(status) == 0);
0102
0103 sreport* generated = sreport::Load(output.c_str());
0104 assert(generated);
0105 assert(generated->runprof);
0106 assert(generated->runprof->shape.size() == 2u);
0107 assert(generated->runprof->shape[0] == 15);
0108 assert(generated->runprof->shape[1] == 4);
0109 assert(generated->ranges);
0110 std::filesystem::remove_all(directory);
0111
0112 return 0;
0113 }