blob: 16cd19a622d23d21bf9b1d8b4155208ae378253d [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
20using BejDecoderTest = testing::TestWithParam<BejDecoderTestParams>;
21
22const BejTestInputFiles driveOemTestFiles = {
23 .jsonFile = "../test/json/drive_oem.json",
24 .schemaDictionaryFile = "../test/dictionaries/drive_oem_dict.bin",
25 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
26 .errorDictionaryFile = "",
27 .encodedStreamFile = "../test/encoded/drive_oem_enc.bin",
28};
29
30const BejTestInputFiles circuitTestFiles = {
31 .jsonFile = "../test/json/circuit.json",
32 .schemaDictionaryFile = "../test/dictionaries/circuit_dict.bin",
33 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
34 .errorDictionaryFile = "",
35 .encodedStreamFile = "../test/encoded/circuit_enc.bin",
36};
37
38const BejTestInputFiles storageTestFiles = {
39 .jsonFile = "../test/json/storage.json",
40 .schemaDictionaryFile = "../test/dictionaries/storage_dict.bin",
41 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
42 .errorDictionaryFile = "",
43 .encodedStreamFile = "../test/encoded/storage_enc.bin",
44};
45
46const BejTestInputFiles dummySimpleTestFiles = {
47 .jsonFile = "../test/json/dummysimple.json",
48 .schemaDictionaryFile = "../test/dictionaries/dummy_simple_dict.bin",
49 .annotationDictionaryFile = "../test/dictionaries/annotation_dict.bin",
50 .errorDictionaryFile = "",
51 .encodedStreamFile = "../test/encoded/dummy_simple_enc.bin",
52};
53
kasunathe8946af2022-05-23 12:32:09 -070054TEST_P(BejDecoderTest, Decode)
55{
56 const BejDecoderTestParams& test_case = GetParam();
57 auto inputsOrErr = loadInputs(test_case.inputFiles);
58 EXPECT_TRUE(inputsOrErr);
59
60 BejDictionaries dictionaries = {
61 .schemaDictionary = inputsOrErr->schemaDictionary,
62 .annotationDictionary = inputsOrErr->annotationDictionary,
63 .errorDictionary = inputsOrErr->errorDictionary,
64 };
65
66 BejDecoderJson decoder;
67 EXPECT_THAT(decoder.decode(dictionaries, inputsOrErr->encodedStream), 0);
68 std::string decoded = decoder.getOutput();
69 nlohmann::json jsonDecoded = nlohmann::json::parse(decoded);
70
71 // Just comparing nlohmann::json types could lead to errors. It compares the
72 // byte values. So int64 and unit64 comparisons might be incorrect. Eg:
73 // bytes values for -5 and 18446744073709551611 are the same. So compare the
74 // string values.
75 EXPECT_TRUE(jsonDecoded.dump() == inputsOrErr->expectedJson.dump());
76}
77
78/**
79 * TODO: Add more test cases.
80 * - Test Enums inside array elemets
81 * - Array inside an array: is this a valid case?
82 * - Real numbers with exponent part
83 * - Every type inside an array.
84 */
85INSTANTIATE_TEST_SUITE_P(
86 , BejDecoderTest,
87 testing::ValuesIn<BejDecoderTestParams>({
88 {"DriveOEM", driveOemTestFiles},
89 {"Circuit", circuitTestFiles},
90 {"Storage", storageTestFiles},
91 {"DummySimple", dummySimpleTestFiles},
92 }),
93 [](const testing::TestParamInfo<BejDecoderTest::ParamType>& info) {
Patrick Williams435526a2023-05-10 14:44:12 -050094 return info.param.testName;
Patrick Williams6f3082e2023-10-20 11:20:27 -050095});
kasunathe8946af2022-05-23 12:32:09 -070096
97} // namespace libbej