Victor Boivie | 553fd32 | 2021-04-26 15:25:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "rtc_base/hash.h" |
| 11 | |
| 12 | #include <string> |
| 13 | #include <unordered_map> |
| 14 | #include <unordered_set> |
| 15 | |
| 16 | #include "test/gmock.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | |
| 21 | TEST(PairHashTest, CanInsertIntoSet) { |
| 22 | using MyPair = std::pair<int, int>; |
| 23 | |
| 24 | std::unordered_set<MyPair, PairHash> pairs; |
| 25 | |
| 26 | pairs.insert({1, 2}); |
| 27 | pairs.insert({3, 4}); |
| 28 | |
| 29 | EXPECT_NE(pairs.find({1, 2}), pairs.end()); |
| 30 | EXPECT_NE(pairs.find({3, 4}), pairs.end()); |
| 31 | EXPECT_EQ(pairs.find({1, 3}), pairs.end()); |
| 32 | EXPECT_EQ(pairs.find({3, 3}), pairs.end()); |
| 33 | } |
| 34 | |
| 35 | TEST(PairHashTest, CanInsertIntoMap) { |
| 36 | using MyPair = std::pair<std::string, int>; |
| 37 | |
| 38 | std::unordered_map<MyPair, int, PairHash> pairs; |
| 39 | |
| 40 | pairs[{"1", 2}] = 99; |
| 41 | pairs[{"3", 4}] = 100; |
| 42 | |
| 43 | EXPECT_EQ((pairs[{"1", 2}]), 99); |
| 44 | EXPECT_EQ((pairs[{"3", 4}]), 100); |
| 45 | EXPECT_EQ(pairs.find({"1", 3}), pairs.end()); |
| 46 | EXPECT_EQ(pairs.find({"3", 3}), pairs.end()); |
| 47 | } |
| 48 | } // namespace |
| 49 | } // namespace webrtc |