@@ -13,7 +13,6 @@ namespace nostd = opentelemetry::nostd;
13
13
14
14
TEST (AttributesHashMap, BasicTests)
15
15
{
16
-
17
16
// Empty map
18
17
AttributesHashMap hash_map;
19
18
EXPECT_EQ (hash_map.Size (), 0 );
@@ -73,3 +72,78 @@ TEST(AttributesHashMap, BasicTests)
73
72
});
74
73
EXPECT_EQ (count, hash_map.Size ());
75
74
}
75
+
76
+ std::string make_unique_string (const char *str)
77
+ {
78
+ return std::string (str);
79
+ }
80
+
81
+ TEST (AttributesHashMap, HashWithKeyValueIterable)
82
+ {
83
+ std::string key1 = make_unique_string (" k1" );
84
+ std::string value1 = make_unique_string (" v1" );
85
+ std::string key2 = make_unique_string (" k2" );
86
+ std::string value2 = make_unique_string (" v2" );
87
+ std::string key3 = make_unique_string (" k3" );
88
+ std::string value3 = make_unique_string (" v3" );
89
+
90
+ // Create mock KeyValueIterable instances with the same content but different variables
91
+ std::map<std::string, std::string> attributes1 ({{key1, value1}, {key2, value2}});
92
+ std::map<std::string, std::string> attributes2 ({{key1, value1}, {key2, value2}});
93
+ std::map<std::string, std::string> attributes3 ({{key1, value1}, {key2, value2}, {key3, value3}});
94
+
95
+ // Create a callback that filters "k3" key
96
+ auto is_key_filter_k3_callback = [](nostd::string_view key) {
97
+ if (key == " k3" )
98
+ {
99
+ return false ;
100
+ }
101
+ return true ;
102
+ };
103
+ // Calculate hash
104
+ size_t hash1 = opentelemetry::sdk::common::GetHashForAttributeMap (
105
+ opentelemetry::common::KeyValueIterableView<std::map<std::string, std::string>>(attributes1),
106
+ is_key_filter_k3_callback);
107
+ size_t hash2 = opentelemetry::sdk::common::GetHashForAttributeMap (
108
+ opentelemetry::common::KeyValueIterableView<std::map<std::string, std::string>>(attributes2),
109
+ is_key_filter_k3_callback);
110
+
111
+ size_t hash3 = opentelemetry::sdk::common::GetHashForAttributeMap (
112
+ opentelemetry::common::KeyValueIterableView<std::map<std::string, std::string>>(attributes3),
113
+ is_key_filter_k3_callback);
114
+
115
+ // Expect the hashes to be the same because the content is the same
116
+ EXPECT_EQ (hash1, hash2);
117
+ // Expect the hashes to be the same because the content is the same
118
+ EXPECT_EQ (hash1, hash3);
119
+ }
120
+
121
+ TEST (AttributesHashMap, HashConsistencyAcrossStringTypes)
122
+ {
123
+ const char *c_str = " teststring" ;
124
+ std::string std_str = " teststring" ;
125
+ nostd::string_view nostd_str_view = " teststring" ;
126
+ #if __cplusplus >= 201703L
127
+ std::string_view std_str_view = " teststring" ;
128
+ #endif
129
+
130
+ size_t hash_c_str = 0 ;
131
+ size_t hash_std_str = 0 ;
132
+ size_t hash_nostd_str_view = 0 ;
133
+ #if __cplusplus >= 201703L
134
+ size_t hash_std_str_view = 0 ;
135
+ #endif
136
+
137
+ opentelemetry::sdk::common::GetHash (hash_c_str, c_str);
138
+ opentelemetry::sdk::common::GetHash (hash_std_str, std_str);
139
+ opentelemetry::sdk::common::GetHash (hash_nostd_str_view, nostd_str_view);
140
+ #if __cplusplus >= 201703L
141
+ opentelemetry::sdk::common::GetHash (hash_std_str_view, std_str_view);
142
+ #endif
143
+
144
+ EXPECT_EQ (hash_c_str, hash_std_str);
145
+ EXPECT_EQ (hash_c_str, hash_nostd_str_view);
146
+ #if __cplusplus >= 201703L
147
+ EXPECT_EQ (hash_c_str, hash_std_str_view);
148
+ #endif
149
+ }
0 commit comments