blob: 55b4358f9a040a57829dea72dd3ce4cbb1d77a9d [file] [log] [blame]
Nikhil Namjoshida6e5572023-03-13 10:52:53 -07001#include "bej_common_test.hpp"
kasunathe8946af2022-05-23 12:32:09 -07002#include "bej_decoder_json.hpp"
kasunathe8946af2022-05-23 12:32:09 -07003
kasunathe8946af2022-05-23 12:32:09 -07004#include <memory>
kasunathe8946af2022-05-23 12:32:09 -07005#include <string_view>
6
7#include <gmock/gmock-matchers.h>
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11namespace libbej
12{
13
kasunathe8946af2022-05-23 12:32:09 -070014struct BejDecoderTestParams
15{
16 const std::string testName;
17 const BejTestInputFiles inputFiles;
18};
19
Brian Maa46f9852024-09-27 16:57:20 +080020void PrintTo(const BejDecoderTestParams& params, std::ostream* os)
21{
22 *os << params.testName;
23}
24
kasunathe8946af2022-05-23 12:32:09 -070025using BejDecoderTest = testing::TestWithParam<BejDecoderTestParams>;
26
27const BejTestInputFiles driveOemTestFiles = {
28 .jsonFile = "../test/json/drive_oem.json",
29 .schemaDictionaryFile = "../test/dictionaries/drive_oem_dict.bin",
30 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
31 .errorDictionaryFile = "",
32 .encodedStreamFile = "../test/encoded/drive_oem_enc.bin",
33};
34
35const BejTestInputFiles circuitTestFiles = {
36 .jsonFile = "../test/json/circuit.json",
37 .schemaDictionaryFile = "../test/dictionaries/circuit_dict.bin",
38 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
39 .errorDictionaryFile = "",
40 .encodedStreamFile = "../test/encoded/circuit_enc.bin",
41};
42
43const BejTestInputFiles storageTestFiles = {
44 .jsonFile = "../test/json/storage.json",
45 .schemaDictionaryFile = "../test/dictionaries/storage_dict.bin",
46 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
47 .errorDictionaryFile = "",
48 .encodedStreamFile = "../test/encoded/storage_enc.bin",
49};
50
51const BejTestInputFiles dummySimpleTestFiles = {
52 .jsonFile = "../test/json/dummysimple.json",
53 .schemaDictionaryFile = "../test/dictionaries/dummy_simple_dict.bin",
54 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
55 .errorDictionaryFile = "",
56 .encodedStreamFile = "../test/encoded/dummy_simple_enc.bin",
57};
58
kasunathe8946af2022-05-23 12:32:09 -070059TEST_P(BejDecoderTest, Decode)
60{
61 const BejDecoderTestParams& test_case = GetParam();
62 auto inputsOrErr = loadInputs(test_case.inputFiles);
63 EXPECT_TRUE(inputsOrErr);
64
65 BejDictionaries dictionaries = {
66 .schemaDictionary = inputsOrErr->schemaDictionary,
67 .annotationDictionary = inputsOrErr->annotationDictionary,
68 .errorDictionary = inputsOrErr->errorDictionary,
69 };
70
71 BejDecoderJson decoder;
72 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream), 0);
73 std::string decoded = decoder.getOutput();
74 nlohmann::json jsonDecoded = nlohmann::json::parse(decoded);
75
76 // Just comparing nlohmann::json types could lead to errors. It compares the
77 // byte values. So int64 and unit64 comparisons might be incorrect. Eg:
78 // bytes values for -5 and 18446744073709551611 are the same. So compare the
79 // string values.
80 EXPECT_TRUE(jsonDecoded.dump() == inputsOrErr->expectedJson.dump());
81}
82
83/**
84 * TODO: Add more test cases.
85 * - Test Enums inside array elemets
86 * - Array inside an array: is this a valid case?
87 * - Real numbers with exponent part
88 * - Every type inside an array.
89 */
90INSTANTIATE_TEST_SUITE_P(
91 , BejDecoderTest,
92 testing::ValuesIn<BejDecoderTestParams>({
93 {"DriveOEM", driveOemTestFiles},
94 {"Circuit", circuitTestFiles},
95 {"Storage", storageTestFiles},
96 {"DummySimple", dummySimpleTestFiles},
97 }),
98 [](const testing::TestParamInfo<BejDecoderTest::ParamType>& info) {
Patrick Williamsbe27f2e2024-08-16 15:22:35 -040099 return info.param.testName;
100 });
kasunathe8946af2022-05-23 12:32:09 -0700101
102} // namespace libbej