|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "opencensus/stats/tag_set.h" |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <unordered_map> |
| 19 | +#include <utility> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "gmock/gmock.h" |
| 23 | +#include "gtest/gtest.h" |
| 24 | + |
| 25 | +namespace opencensus { |
| 26 | +namespace stats { |
| 27 | +namespace { |
| 28 | + |
| 29 | +TEST(TagSetTest, ConstructorsEquivalent) { |
| 30 | + const std::vector<std::pair<std::string, std::string>> tags({{"k", "v"}}); |
| 31 | + EXPECT_EQ(TagSet(tags), TagSet({{"k", "v"}})); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(TagSetTest, TagsSorted) { |
| 35 | + const std::vector<std::pair<std::string, std::string>> expected = { |
| 36 | + {"B", "v"}, {"a", "v"}, {"b", "v"}}; |
| 37 | + const std::vector<std::pair<std::string, std::string>> tags( |
| 38 | + {{"b", "v"}, {"a", "v"}, {"B", "v"}}); |
| 39 | + EXPECT_THAT(TagSet(tags).tags(), ::testing::ElementsAreArray(expected)); |
| 40 | + EXPECT_THAT(TagSet({{"B", "v"}, {"b", "v"}, {"a", "v"}}).tags(), |
| 41 | + ::testing::ElementsAreArray(expected)); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(TagSetTest, EqualityDisregardsOrder) { |
| 45 | + EXPECT_EQ(TagSet({{"k1", "v1"}, {"k2", "v2"}}), |
| 46 | + TagSet({{"k2", "v2"}, {"k1", "v1"}})); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(TagSetTest, EqualityRespectsMissingKeys) { |
| 50 | + EXPECT_NE(TagSet({{"k1", "v1"}, {"k2", "v2"}}), TagSet({{"k1", "v1"}})); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(TagSetTest, EqualityRespectsKeyValuePairings) { |
| 54 | + EXPECT_NE(TagSet({{"k1", "v1"}, {"k2", "v2"}}), |
| 55 | + TagSet({{"k1", "v2"}, {"k2", "v1"}})); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(TagSetTest, HashDisregardsOrder) { |
| 59 | + TagSet ts1({{"k1", "v1"}, {"k2", "v2"}}); |
| 60 | + TagSet ts2({{"k2", "v2"}, {"k1", "v1"}}); |
| 61 | + EXPECT_EQ(TagSet::Hash()(ts1), TagSet::Hash()(ts2)); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(TagSetTest, HashRespectsKeyValuePairings) { |
| 65 | + TagSet ts1({{"k1", "v1"}, {"k2", "v2"}}); |
| 66 | + TagSet ts2({{"k1", "v2"}, {"k2", "v1"}}); |
| 67 | + EXPECT_NE(TagSet::Hash()(ts1), TagSet::Hash()(ts2)); |
| 68 | +} |
| 69 | + |
| 70 | +TEST(TagSetTest, UnorderedMap) { |
| 71 | + // Test that the operators and hash are compatible with std::unordered_map. |
| 72 | + std::unordered_map<TagSet, int, TagSet::Hash> map; |
| 73 | + TagSet ts = {{"key", "value"}}; |
| 74 | + map.emplace(ts, 1); |
| 75 | + EXPECT_NE(map.end(), map.find(ts)); |
| 76 | + EXPECT_EQ(1, map.erase(ts)); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | +} // namespace stats |
| 81 | +} // namespace opencensus |
0 commit comments