blob: 19bda1dee058d1b4228e5f6f4913ed7d1a7c103e [file] [log] [blame]
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -08001// 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 Vakulenko9ed0cab2015-10-12 15:21:28 -07005#include <brillo/map_utils.h>
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -08006
7#include <string>
8
9#include <gtest/gtest.h>
10
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070011namespace brillo {
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -080012
13class MapUtilsTest : public ::testing::Test {
14 public:
15 void SetUp() override {
16 map_ = {
Alex Vakulenko05d29042015-01-13 09:39:25 -080017 {"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}, {"key5", 5},
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -080018 };
19 }
20
Alex Vakulenko05d29042015-01-13 09:39:25 -080021 void TearDown() override { map_.clear(); }
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -080022
23 std::map<std::string, int> map_;
24};
25
26TEST_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
32TEST_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
38TEST_F(MapUtilsTest, GetMapValues) {
39 std::vector<int> values = GetMapValues(map_);
40 EXPECT_EQ((std::vector<int>{1, 2, 3, 4, 5}), values);
41}
42
43TEST_F(MapUtilsTest, MapToVector) {
44 std::vector<std::pair<std::string, int>> elements = MapToVector(map_);
45 std::vector<std::pair<std::string, int>> expected{
Alex Vakulenko05d29042015-01-13 09:39:25 -080046 {"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}, {"key5", 5},
Alex Vakulenkof7fd94d2014-12-15 11:02:56 -080047 };
48 EXPECT_EQ(expected, elements);
49}
50
51TEST_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 Vakulenko9ed0cab2015-10-12 15:21:28 -070059} // namespace brillo