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