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 5 #include <brillo/map_utils.h> 6 7 #include <string> 8 9 #include <gtest/gtest.h> 10 11 namespace brillo { 12 13 class MapUtilsTest : public ::testing::Test { 14 public: 15 void SetUp() override { 16 map_ = { 17 {"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}, {"key5", 5}, 18 }; 19 } 20 21 void TearDown() override { map_.clear(); } 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{ 46 {"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}, {"key5", 5}, 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 59 } // namespace brillo 60