blob: 9d6165d516c39860f60bf9b31ca18fba488f3afa [file] [log] [blame]
Nikhil Namjoshida6e5572023-03-13 10:52:53 -07001#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
12namespace libbej
13{
14
15// Buffer size for storing a single binary file data.
16constexpr uint32_t maxBufferSize = 16 * 1024;
17
18struct BejTestInputFiles
19{
20 const char* jsonFile;
21 const char* schemaDictionaryFile;
22 const char* annotationDictionaryFile;
23 const char* errorDictionaryFile;
24 const char* encodedStreamFile;
25};
26
27struct BejTestInputs
28{
29 nlohmann::json expectedJson;
30 const uint8_t* schemaDictionary;
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000031 uint32_t schemaDictionarySize;
Nikhil Namjoshida6e5572023-03-13 10:52:53 -070032 const uint8_t* annotationDictionary;
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000033 uint32_t annotationDictionarySize;
Nikhil Namjoshida6e5572023-03-13 10:52:53 -070034 const uint8_t* errorDictionary;
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000035 uint32_t errorDictionarySize;
Nikhil Namjoshida6e5572023-03-13 10:52:53 -070036 std::span<const uint8_t> encodedStream;
37};
38
39std::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
58std::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 Athukorala7c1be8d2025-07-15 05:44:04 +000071 uint32_t schemaDictSize = 0;
72 if ((schemaDictSize =
73 readBinaryFile(files.schemaDictionaryFile,
74 std::span(schemaDictBuffer, maxBufferSize))) == 0)
Nikhil Namjoshida6e5572023-03-13 10:52:53 -070075 {
76 return std::nullopt;
77 }
78
79 static uint8_t annoDictBuffer[maxBufferSize];
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +000080 uint32_t annoDictSize = 0;
81 if ((annoDictSize =
82 readBinaryFile(files.annotationDictionaryFile,
83 std::span(annoDictBuffer, maxBufferSize))) == 0)
Nikhil Namjoshida6e5572023-03-13 10:52:53 -070084 {
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 Athukorala7c1be8d2025-07-15 05:44:04 +000097 uint32_t errorDictSize = 0;
Nikhil Namjoshida6e5572023-03-13 10:52:53 -070098 if (readErrorDictionary)
99 {
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000100 if ((errorDictSize =
101 readBinaryFile(files.errorDictionaryFile,
102 std::span(errorDict, maxBufferSize))) == 0)
Nikhil Namjoshida6e5572023-03-13 10:52:53 -0700103 {
104 return std::nullopt;
105 }
106 }
107
108 BejTestInputs inputs = {
109 .expectedJson = expJson,
110 .schemaDictionary = schemaDictBuffer,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000111 .schemaDictionarySize = schemaDictSize,
Nikhil Namjoshida6e5572023-03-13 10:52:53 -0700112 .annotationDictionary = annoDictBuffer,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000113 .annotationDictionarySize = annoDictSize,
Nikhil Namjoshida6e5572023-03-13 10:52:53 -0700114 .errorDictionary = errorDict,
Kasun Athukorala7c1be8d2025-07-15 05:44:04 +0000115 .errorDictionarySize = errorDictSize,
Nikhil Namjoshida6e5572023-03-13 10:52:53 -0700116 .encodedStream = std::span(encBuffer, encLen),
117 };
118 return inputs;
119}
120
121} // namespace libbej