Nikhil Namjoshi | da6e557 | 2023-03-13 10:52:53 -0700 | [diff] [blame] | 1 | #include "bej_common_test.hpp" |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 2 | #include "bej_decoder_json.hpp" |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 3 | |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 4 | #include <memory> |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 5 | #include <string_view> |
| 6 | |
| 7 | #include <gmock/gmock-matchers.h> |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | namespace libbej |
| 12 | { |
| 13 | |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 14 | struct BejDecoderTestParams |
| 15 | { |
| 16 | const std::string testName; |
| 17 | const BejTestInputFiles inputFiles; |
| 18 | }; |
| 19 | |
Brian Ma | a46f985 | 2024-09-27 16:57:20 +0800 | [diff] [blame^] | 20 | void PrintTo(const BejDecoderTestParams& params, std::ostream* os) |
| 21 | { |
| 22 | *os << params.testName; |
| 23 | } |
| 24 | |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 25 | using BejDecoderTest = testing::TestWithParam<BejDecoderTestParams>; |
| 26 | |
| 27 | const 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 | |
| 35 | const 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 | |
| 43 | const 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 | |
| 51 | const 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 | |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 59 | TEST_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 | */ |
| 90 | INSTANTIATE_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 Williams | be27f2e | 2024-08-16 15:22:35 -0400 | [diff] [blame] | 99 | return info.param.testName; |
| 100 | }); |
kasunath | e8946af | 2022-05-23 12:32:09 -0700 | [diff] [blame] | 101 | |
| 102 | } // namespace libbej |