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;
31 uint32_t schemaDictionarySize;
32 const uint8_t* annotationDictionary;
33 uint32_t annotationDictionarySize;
34 const uint8_t* errorDictionary;
35 uint32_t errorDictionarySize;
36 std::span<const uint8_t> encodedStream;
37 };
38
readBinaryFile(const char * fileName,std::span<uint8_t> buffer)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
loadInputs(const BejTestInputFiles & files,bool readErrorDictionary=false)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];
71 uint32_t schemaDictSize = 0;
72 if ((schemaDictSize =
73 readBinaryFile(files.schemaDictionaryFile,
74 std::span(schemaDictBuffer, maxBufferSize))) == 0)
75 {
76 return std::nullopt;
77 }
78
79 static uint8_t annoDictBuffer[maxBufferSize];
80 uint32_t annoDictSize = 0;
81 if ((annoDictSize =
82 readBinaryFile(files.annotationDictionaryFile,
83 std::span(annoDictBuffer, maxBufferSize))) == 0)
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];
97 uint32_t errorDictSize = 0;
98 if (readErrorDictionary)
99 {
100 if ((errorDictSize =
101 readBinaryFile(files.errorDictionaryFile,
102 std::span(errorDict, maxBufferSize))) == 0)
103 {
104 return std::nullopt;
105 }
106 }
107
108 BejTestInputs inputs = {
109 .expectedJson = expJson,
110 .schemaDictionary = schemaDictBuffer,
111 .schemaDictionarySize = schemaDictSize,
112 .annotationDictionary = annoDictBuffer,
113 .annotationDictionarySize = annoDictSize,
114 .errorDictionary = errorDict,
115 .errorDictionarySize = errorDictSize,
116 .encodedStream = std::span(encBuffer, encLen),
117 };
118 return inputs;
119 }
120
121 } // namespace libbej
122