diff options
author | yzshen <yzshen@chromium.org> | 2016-03-08 02:53:34 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-08 10:54:51 +0000 |
commit | 387d3cbc24dd51d48220372b9a4a70b0086357f3 (patch) | |
tree | 81b69700f8e163addc1decc53b52dc1bf592e8e8 | |
parent | 65339401972be806df6af91a3c77208e346b34ba (diff) | |
download | chromium_src-387d3cbc24dd51d48220372b9a4a70b0086357f3.zip chromium_src-387d3cbc24dd51d48220372b9a4a70b0086357f3.tar.gz chromium_src-387d3cbc24dd51d48220372b9a4a70b0086357f3.tar.bz2 |
Mojo C++ bindings: add empty() for Array and Map.
BUG=None
Review URL: https://codereview.chromium.org/1776583002
Cr-Commit-Position: refs/heads/master@{#379804}
-rw-r--r-- | mojo/public/cpp/bindings/array.h | 3 | ||||
-rw-r--r-- | mojo/public/cpp/bindings/map.h | 7 | ||||
-rw-r--r-- | mojo/public/cpp/bindings/tests/array_unittest.cc | 17 | ||||
-rw-r--r-- | mojo/public/cpp/bindings/tests/map_unittest.cc | 17 |
4 files changed, 43 insertions, 1 deletions
diff --git a/mojo/public/cpp/bindings/array.h b/mojo/public/cpp/bindings/array.h index cb611c6..575ba77 100644 --- a/mojo/public/cpp/bindings/array.h +++ b/mojo/public/cpp/bindings/array.h @@ -88,6 +88,9 @@ class Array { // Indicates whether the array is null (which is distinct from empty). bool is_null() const { return is_null_; } + // Indicates whether the array is empty (which is distinct from null). + bool empty() const { return vec_.empty() && !is_null_; } + // Returns a reference to the first element of the array. Calling this on a // null or empty array causes undefined behavior. ConstRefType front() const { return vec_.front(); } diff --git a/mojo/public/cpp/bindings/map.h b/mojo/public/cpp/bindings/map.h index 4c64d48..6d0d57d 100644 --- a/mojo/public/cpp/bindings/map.h +++ b/mojo/public/cpp/bindings/map.h @@ -87,9 +87,14 @@ class Map { return TypeConverter<U, Map>::Convert(*this); } + // Indicates whether the map is null (which is distinct from empty). bool is_null() const { return is_null_; } - // Indicates the number of keys in the map. + // Indicates whether the map is empty (which is distinct from null). + bool empty() const { return map_.empty() && !is_null_; } + + // Indicates the number of keys in the map, which will be zero if the map is + // null. size_t size() const { return map_.size(); } // Inserts a key-value pair into the map. Like std::map, this does not insert diff --git a/mojo/public/cpp/bindings/tests/array_unittest.cc b/mojo/public/cpp/bindings/tests/array_unittest.cc index c5a227e..f4dad7c 100644 --- a/mojo/public/cpp/bindings/tests/array_unittest.cc +++ b/mojo/public/cpp/bindings/tests/array_unittest.cc @@ -26,6 +26,23 @@ using mojo::internal::String_Data; using ArrayTest = testing::Test; +// Tests null and empty arrays. +TEST_F(ArrayTest, NullAndEmpty) { + Array<char> array0; + EXPECT_TRUE(array0.empty()); + EXPECT_FALSE(array0.is_null()); + array0 = nullptr; + EXPECT_TRUE(array0.is_null()); + EXPECT_FALSE(array0.empty()); + + Array<char> array1(nullptr); + EXPECT_TRUE(array1.is_null()); + EXPECT_FALSE(array1.empty()); + array1.SetToEmpty(); + EXPECT_TRUE(array1.empty()); + EXPECT_FALSE(array1.is_null()); +} + // Tests that basic Array operations work. TEST_F(ArrayTest, Basic) { Array<char> array(8); diff --git a/mojo/public/cpp/bindings/tests/map_unittest.cc b/mojo/public/cpp/bindings/tests/map_unittest.cc index 4ccd8c0..e4d2bee 100644 --- a/mojo/public/cpp/bindings/tests/map_unittest.cc +++ b/mojo/public/cpp/bindings/tests/map_unittest.cc @@ -42,6 +42,23 @@ const size_t kStringIntDataSize = 4; using MapTest = testing::Test; +// Tests null and empty maps. +TEST_F(MapTest, NullAndEmpty) { + Map<char, char> map0; + EXPECT_TRUE(map0.empty()); + EXPECT_FALSE(map0.is_null()); + map0 = nullptr; + EXPECT_TRUE(map0.is_null()); + EXPECT_FALSE(map0.empty()); + + Map<char, char> map1(nullptr); + EXPECT_TRUE(map1.is_null()); + EXPECT_FALSE(map1.empty()); + map1.SetToEmpty(); + EXPECT_TRUE(map1.empty()); + EXPECT_FALSE(map1.is_null()); +} + // Tests that basic Map operations work. TEST_F(MapTest, InsertWorks) { Map<String, int> map; |