Add libbej encoder wrapper for C++
Add unit tests and the test data files needed for unit tests.
Move APIs from bej_decoder_test.cpp to a common file, to share those
with bej_encoder_test.cpp
Tested:
Verified that encoding using the wrapper works well at runtime.
Verified that the unit tests pass.
Change-Id: I61b7c17690eb7e7fefd1973a22d4316c6214267e
Signed-off-by: Nikhil Namjoshi <nikhilnamjoshi@google.com>
diff --git a/test/bej_decoder_test.cpp b/test/bej_decoder_test.cpp
index b390017..16cd19a 100644
--- a/test/bej_decoder_test.cpp
+++ b/test/bej_decoder_test.cpp
@@ -1,11 +1,7 @@
+#include "bej_common_test.hpp"
#include "bej_decoder_json.hpp"
-#include "nlohmann/json.hpp"
-#include <fstream>
-#include <iostream>
#include <memory>
-#include <optional>
-#include <span>
#include <string_view>
#include <gmock/gmock-matchers.h>
@@ -15,24 +11,6 @@
namespace libbej
{
-struct BejTestInputFiles
-{
- const char* jsonFile;
- const char* schemaDictionaryFile;
- const char* annotationDictionaryFile;
- const char* errorDictionaryFile;
- const char* encodedStreamFile;
-};
-
-struct BejTestInputs
-{
- const nlohmann::json expectedJson;
- const uint8_t* schemaDictionary;
- const uint8_t* annotationDictionary;
- const uint8_t* errorDictionary;
- std::span<const uint8_t> encodedStream;
-};
-
struct BejDecoderTestParams
{
const std::string testName;
@@ -73,82 +51,6 @@
.encodedStreamFile = "../test/encoded/dummy_simple_enc.bin",
};
-// Buffer size for storing a single binary file data.
-constexpr uint32_t maxBufferSize = 16 * 1024;
-
-std::streamsize readBinaryFile(const char* fileName, std::span<uint8_t> buffer)
-{
- std::ifstream inputStream(fileName, std::ios::binary);
- if (!inputStream.is_open())
- {
- std::cerr << "Cannot open file: " << fileName << "\n";
- return 0;
- }
- auto readLength = inputStream.readsome(
- reinterpret_cast<char*>(buffer.data()), buffer.size_bytes());
- if (inputStream.peek() != EOF)
- {
- std::cerr << "Failed to read the complete file: " << fileName
- << " read length: " << readLength << "\n";
- return 0;
- }
- return readLength;
-}
-
-std::optional<BejTestInputs> loadInputs(const BejTestInputFiles& files,
- bool readErrorDictionary = false)
-{
- std::ifstream jsonInput(files.jsonFile);
- if (!jsonInput.is_open())
- {
- std::cerr << "Cannot open file: " << files.jsonFile << "\n";
- return std::nullopt;
- }
- nlohmann::json expJson;
- jsonInput >> expJson;
-
- static uint8_t schemaDictBuffer[maxBufferSize];
- if (readBinaryFile(files.schemaDictionaryFile,
- std::span(schemaDictBuffer, maxBufferSize)) == 0)
- {
- return std::nullopt;
- }
-
- static uint8_t annoDictBuffer[maxBufferSize];
- if (readBinaryFile(files.annotationDictionaryFile,
- std::span(annoDictBuffer, maxBufferSize)) == 0)
- {
- return std::nullopt;
- }
-
- static uint8_t encBuffer[maxBufferSize];
- auto encLen = readBinaryFile(files.encodedStreamFile,
- std::span(encBuffer, maxBufferSize));
- if (encLen == 0)
- {
- return std::nullopt;
- }
-
- static uint8_t errorDict[maxBufferSize];
- if (readErrorDictionary)
- {
- if (readBinaryFile(files.errorDictionaryFile,
- std::span(errorDict, maxBufferSize)) == 0)
- {
- return std::nullopt;
- }
- }
-
- BejTestInputs inputs = {
- .expectedJson = expJson,
- .schemaDictionary = schemaDictBuffer,
- .annotationDictionary = annoDictBuffer,
- .errorDictionary = errorDict,
- .encodedStream = std::span(encBuffer, encLen),
- };
- return inputs;
-}
-
TEST_P(BejDecoderTest, Decode)
{
const BejDecoderTestParams& test_case = GetParam();