we prefer the binary_path over csv_path
| 60 | |
| 61 | // we prefer the binary_path over csv_path |
| 62 | void read_data(std::vector<double>& data, const std::string& csv_file_path, const std::string& bin_file_path) { |
| 63 | if (!bin_file_path.empty()) { |
| 64 | |
| 65 | // Open the binary file in input mode |
| 66 | std::ifstream file(bin_file_path, std::ios::binary | std::ios::in); |
| 67 | |
| 68 | if (!file) { throw std::runtime_error("Failed to open file: " + bin_file_path + " - " + strerror(errno)); } |
| 69 | |
| 70 | // Get the size of the file |
| 71 | file.seekg(0, std::ios::end); |
| 72 | std::streamsize fileSize = file.tellg(); |
| 73 | file.seekg(0, std::ios::beg); |
| 74 | |
| 75 | // Ensure the file size is a multiple of the size of a double |
| 76 | if (fileSize % sizeof(double) != 0) { throw std::runtime_error("File size is not a multiple of double size!"); } |
| 77 | // Calculate the number of doubles |
| 78 | std::size_t numDoubles = fileSize / sizeof(double); |
| 79 | |
| 80 | // Resize the vector to hold all the doubles |
| 81 | data.resize(numDoubles); |
| 82 | |
| 83 | // Read the data into the vector |
| 84 | file.read(reinterpret_cast<char*>(data.data()), fileSize); |
| 85 | |
| 86 | // Close the file |
| 87 | file.close(); |
| 88 | return; |
| 89 | } |
| 90 | if (!csv_file_path.empty()) { |
| 91 | const auto& path = csv_file_path; |
| 92 | std::ifstream file(path); |
| 93 | |
| 94 | if (!file) { throw std::runtime_error("Failed to open file: " + path); } |
| 95 | |
| 96 | std::string line; |
| 97 | // Read each line, convert it to double, and store it in the vector |
| 98 | while (std::getline(file, line)) { |
| 99 | try { |
| 100 | // Convert the string to double and add to the vector |
| 101 | data.push_back(std::stod(line)); |
| 102 | } catch (const std::invalid_argument& e) { |
| 103 | throw std::runtime_error("Invalid data in file: " + line); |
| 104 | } catch (const std::out_of_range& e) { |
| 105 | // |
| 106 | throw std::runtime_error("Number out of range in file: " + line); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | file.close(); |
| 111 | return; |
| 112 | } |
| 113 | throw std::runtime_error("No bin or csv file specified"); |
| 114 | } |
| 115 | |
| 116 | class alp_test : public ::testing::Test { |
| 117 | public: |
no test coverage detected