Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 1 | #include "bej_dictionary.h" |
| 2 | #include "bej_encoder_core.h" |
| 3 | #include "bej_tree.h" |
| 4 | |
| 5 | #include "nlohmann/json.hpp" |
| 6 | |
| 7 | #include <fstream> |
| 8 | #include <iostream> |
| 9 | #include <optional> |
| 10 | #include <span> |
| 11 | |
| 12 | namespace libbej |
| 13 | { |
| 14 | |
| 15 | // Buffer size for storing a single binary file data. |
| 16 | constexpr uint32_t maxBufferSize = 16 * 1024; |
| 17 | |
| 18 | struct BejTestInputFiles |
| 19 | { |
| 20 | const char* jsonFile; |
| 21 | const char* schemaDictionaryFile; |
| 22 | const char* annotationDictionaryFile; |
| 23 | const char* errorDictionaryFile; |
| 24 | const char* encodedStreamFile; |
| 25 | }; |
| 26 | |
| 27 | struct BejTestInputs |
| 28 | { |
| 29 | nlohmann::json expectedJson; |
| 30 | const uint8_t* schemaDictionary; |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 31 | uint32_t schemaDictionarySize; |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 32 | const uint8_t* annotationDictionary; |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 33 | uint32_t annotationDictionarySize; |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 34 | const uint8_t* errorDictionary; |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 35 | uint32_t errorDictionarySize; |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 36 | std::span<const uint8_t> encodedStream; |
| 37 | }; |
| 38 | |
| 39 | std::streamsize readBinaryFile(const char* fileName, std::span<uint8_t> buffer) |
| 40 | { |
| 41 | std::ifstream inputStream(fileName, std::ios::binary); |
| 42 | if (!inputStream.is_open()) |
| 43 | { |
| 44 | std::cerr << "Cannot open file: " << fileName << "\n"; |
| 45 | return 0; |
| 46 | } |
| 47 | auto readLength = inputStream.readsome( |
| 48 | reinterpret_cast<char*>(buffer.data()), buffer.size_bytes()); |
| 49 | if (inputStream.peek() != EOF) |
| 50 | { |
| 51 | std::cerr << "Failed to read the complete file: " << fileName |
| 52 | << " read length: " << readLength << "\n"; |
| 53 | return 0; |
| 54 | } |
| 55 | return readLength; |
| 56 | } |
| 57 | |
| 58 | std::optional<BejTestInputs> loadInputs(const BejTestInputFiles& files, |
| 59 | bool readErrorDictionary = false) |
| 60 | { |
| 61 | std::ifstream jsonInput(files.jsonFile); |
| 62 | if (!jsonInput.is_open()) |
| 63 | { |
| 64 | std::cerr << "Cannot open file: " << files.jsonFile << "\n"; |
| 65 | return std::nullopt; |
| 66 | } |
| 67 | nlohmann::json expJson; |
| 68 | jsonInput >> expJson; |
| 69 | |
| 70 | static uint8_t schemaDictBuffer[maxBufferSize]; |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 71 | uint32_t schemaDictSize = 0; |
| 72 | if ((schemaDictSize = |
| 73 | readBinaryFile(files.schemaDictionaryFile, |
| 74 | std::span(schemaDictBuffer, maxBufferSize))) == 0) |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 75 | { |
| 76 | return std::nullopt; |
| 77 | } |
| 78 | |
| 79 | static uint8_t annoDictBuffer[maxBufferSize]; |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 80 | uint32_t annoDictSize = 0; |
| 81 | if ((annoDictSize = |
| 82 | readBinaryFile(files.annotationDictionaryFile, |
| 83 | std::span(annoDictBuffer, maxBufferSize))) == 0) |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 84 | { |
| 85 | return std::nullopt; |
| 86 | } |
| 87 | |
| 88 | static uint8_t encBuffer[maxBufferSize]; |
| 89 | auto encLen = readBinaryFile(files.encodedStreamFile, |
| 90 | std::span(encBuffer, maxBufferSize)); |
| 91 | if (encLen == 0) |
| 92 | { |
| 93 | return std::nullopt; |
| 94 | } |
| 95 | |
| 96 | static uint8_t errorDict[maxBufferSize]; |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 97 | uint32_t errorDictSize = 0; |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 98 | if (readErrorDictionary) |
| 99 | { |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 100 | if ((errorDictSize = |
| 101 | readBinaryFile(files.errorDictionaryFile, |
| 102 | std::span(errorDict, maxBufferSize))) == 0) |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 103 | { |
| 104 | return std::nullopt; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | BejTestInputs inputs = { |
| 109 | .expectedJson = expJson, |
| 110 | .schemaDictionary = schemaDictBuffer, |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 111 | .schemaDictionarySize = schemaDictSize, |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 112 | .annotationDictionary = annoDictBuffer, |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 113 | .annotationDictionarySize = annoDictSize, |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 114 | .errorDictionary = errorDict, |
Kasun Athukorala | 7c1be8d | 2025-07-15 05:44:04 +0000 | [diff] [blame] | 115 | .errorDictionarySize = errorDictSize, |
Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 116 | .encodedStream = std::span(encBuffer, encLen), |
| 117 | }; |
| 118 | return inputs; |
| 119 | } |
| 120 | |
| 121 | } // namespace libbej |