diff options
author | Shawn Willden <swillden@google.com> | 2020-12-14 23:24:49 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-12-14 23:24:49 +0000 |
commit | b6b24c2ab8f40a99f60edbce1c233c601c5166c9 (patch) | |
tree | 8d84cfe31231ffaf35432df1b7313674b7ef5aa0 | |
parent | cb173a1f06c9c37bda2e58183b6513cb405b4410 (diff) | |
parent | 7aa436b8cae4c99277aab05ac2380e8228f71623 (diff) | |
download | libcppbor-b6b24c2ab8f40a99f60edbce1c233c601c5166c9.tar.gz |
Remove downcastItem<> am: f79067125c am: 7aa436b8ca
Original change: https://android-review.googlesource.com/c/platform/external/libcppbor/+/1515384
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: Icb8f251953eac9e5d58d9231e2c00fe76522abfa
-rw-r--r-- | include/cppbor/cppbor.h | 16 | ||||
-rw-r--r-- | tests/cppbor_test.cpp | 162 |
2 files changed, 0 insertions, 178 deletions
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h index c33960b..f9d371e 100644 --- a/include/cppbor/cppbor.h +++ b/include/cppbor/cppbor.h @@ -779,22 +779,6 @@ std::string prettyPrint(const Item* item, size_t maxBStrSize = 32, std::string prettyPrint(const std::vector<uint8_t>& encodedCbor, size_t maxBStrSize = 32, const std::vector<std::string>& mapKeysNotToPrint = {}); -template <typename T> -std::unique_ptr<T> downcastItem(std::unique_ptr<Item>&& v) { - static_assert(std::is_base_of_v<Item, T> && !std::is_abstract_v<T>, - "returned type is not an Item or is an abstract class"); - if (v && T::kMajorType == v->type()) { - if constexpr (std::is_base_of_v<Simple, T>) { - if (T::kSimpleType != v->asSimple()->simpleType()) { - return nullptr; - } - } - return std::unique_ptr<T>(static_cast<T*>(v.release())); - } else { - return nullptr; - } -} - /** * Details. Mostly you shouldn't have to look below, except perhaps at the docstring for makeItem. */ diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp index ed4b94e..b45d804 100644 --- a/tests/cppbor_test.cpp +++ b/tests/cppbor_test.cpp @@ -1424,168 +1424,6 @@ TEST(FullParserTest, MapWithTruncatedEntry) { EXPECT_EQ("Need 4 byte(s) for length field, have 3.", message); } -TEST(ItemDowncastingTest, Uint) { - auto item = std::unique_ptr<Item>(new Uint(1)); - EXPECT_NE(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Nint) { - auto item = std::unique_ptr<Item>(new Nint(-211)); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Tstr) { - auto item = std::unique_ptr<Item>(new Tstr("string")); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Bstr) { - auto item = std::unique_ptr<Item>(new Bstr(std::vector<uint8_t>{1, 2, 3, 4, 5, 6})); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Array) { - auto item = std::unique_ptr<Item>(new Array(1, 2, "3", "4", Array(5, "6"))); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Map) { - auto item = std::unique_ptr<Item>(new Map(1, 2, "key", "value")); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Bool) { - auto item = std::unique_ptr<Item>(new Bool(false)); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Null) { - auto item = std::unique_ptr<Item>(new Null()); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - -TEST(ItemDowncastingTest, Semantic) { - auto item = std::unique_ptr<Item>(new Semantic(11, Map("key", Array(1, 2, 3)))); - EXPECT_EQ(downcastItem<Uint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Nint>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Tstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bstr>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Array>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Map>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Bool>(std::move(item)).get(), nullptr); - EXPECT_EQ(downcastItem<Null>(std::move(item)).get(), nullptr); - EXPECT_NE(downcastItem<Semantic>(std::move(item)).get(), nullptr); - // Uncommenting following lines should not compile - // EXPECT_EQ(downcastItem<Int>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<CompoundItem>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Simple>(std::move(item)).get(), nullptr); - // EXPECT_EQ(downcastItem<Item>(std::move(item)).get(), nullptr); -} - TEST(MapGetValueByKeyTest, Map) { Array compoundItem(1, 2, 3, 4, 5, Map(4, 5, "a", "b")); auto clone = compoundItem.clone(); |