Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 5 | #include <brillo/map_utils.h> |
Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 11 | namespace brillo { |
Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 12 | |
| 13 | class MapUtilsTest : public ::testing::Test { |
| 14 | public: |
| 15 | void SetUp() override { |
| 16 | map_ = { |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame] | 17 | {"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}, {"key5", 5}, |
Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 18 | }; |
| 19 | } |
| 20 | |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame] | 21 | void TearDown() override { map_.clear(); } |
Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 22 | |
| 23 | std::map<std::string, int> map_; |
| 24 | }; |
| 25 | |
| 26 | TEST_F(MapUtilsTest, GetMapKeys) { |
| 27 | std::set<std::string> keys = GetMapKeys(map_); |
| 28 | EXPECT_EQ((std::set<std::string>{"key1", "key2", "key3", "key4", "key5"}), |
| 29 | keys); |
| 30 | } |
| 31 | |
| 32 | TEST_F(MapUtilsTest, GetMapKeysAsVector) { |
| 33 | std::vector<std::string> keys = GetMapKeysAsVector(map_); |
| 34 | EXPECT_EQ((std::vector<std::string>{"key1", "key2", "key3", "key4", "key5"}), |
| 35 | keys); |
| 36 | } |
| 37 | |
| 38 | TEST_F(MapUtilsTest, GetMapValues) { |
| 39 | std::vector<int> values = GetMapValues(map_); |
| 40 | EXPECT_EQ((std::vector<int>{1, 2, 3, 4, 5}), values); |
| 41 | } |
| 42 | |
| 43 | TEST_F(MapUtilsTest, MapToVector) { |
| 44 | std::vector<std::pair<std::string, int>> elements = MapToVector(map_); |
| 45 | std::vector<std::pair<std::string, int>> expected{ |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame] | 46 | {"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}, {"key5", 5}, |
Alex Vakulenko | f7fd94d | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 47 | }; |
| 48 | EXPECT_EQ(expected, elements); |
| 49 | } |
| 50 | |
| 51 | TEST_F(MapUtilsTest, Empty) { |
| 52 | std::map<int, double> empty_map; |
| 53 | EXPECT_TRUE(GetMapKeys(empty_map).empty()); |
| 54 | EXPECT_TRUE(GetMapKeysAsVector(empty_map).empty()); |
| 55 | EXPECT_TRUE(GetMapValues(empty_map).empty()); |
| 56 | EXPECT_TRUE(MapToVector(empty_map).empty()); |
| 57 | } |
| 58 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 59 | } // namespace brillo |