Backed out changeset 2fc34d798e24 (bug 1917771) for causing failures at baseline...
[gecko.git] / third_party / libwebrtc / stats / rtc_stats_unittest.cc
blob555360f07fbcef90aca98d723ecac02e313fd2bc
1 /*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
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 */
11 #include "api/stats/rtc_stats.h"
13 #include <cmath>
14 #include <cstdint>
15 #include <cstring>
16 #include <iostream>
18 #include "absl/types/optional.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/strings/json.h"
21 #include "stats/test/rtc_test_stats.h"
22 #include "test/gtest.h"
24 namespace webrtc {
26 namespace {
28 // JSON stores numbers as floating point numbers with 53 significant bits, which
29 // amounts to about 15.95 decimal digits. Thus, when comparing large numbers
30 // processed by JSON, that's all the precision we should expect.
31 const double JSON_EPSILON = 1e-15;
33 // We do this since Google Test doesn't support relative error.
34 // This is computed as follows:
35 // If |a - b| / |a| < EPS, then |a - b| < |a| * EPS, so |a| * EPS is the
36 // maximum expected error.
37 double GetExpectedError(const double expected_value) {
38 return JSON_EPSILON * fabs(expected_value);
41 } // namespace
43 class RTCChildStats : public RTCStats {
44 public:
45 WEBRTC_RTCSTATS_DECL();
47 RTCChildStats(const std::string& id, Timestamp timestamp)
48 : RTCStats(id, timestamp) {}
50 absl::optional<int32_t> child_int;
53 WEBRTC_RTCSTATS_IMPL(RTCChildStats,
54 RTCStats,
55 "child-stats",
56 AttributeInit("childInt", &child_int))
58 class RTCGrandChildStats : public RTCChildStats {
59 public:
60 WEBRTC_RTCSTATS_DECL();
62 RTCGrandChildStats(const std::string& id, Timestamp timestamp)
63 : RTCChildStats(id, timestamp) {}
65 absl::optional<int32_t> grandchild_int;
68 WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats,
69 RTCChildStats,
70 "grandchild-stats",
71 AttributeInit("grandchildInt", &grandchild_int))
73 TEST(RTCStatsTest, RTCStatsAndAttributes) {
74 RTCTestStats stats("testId", Timestamp::Micros(42));
75 EXPECT_EQ(stats.id(), "testId");
76 EXPECT_EQ(stats.timestamp().us(), static_cast<int64_t>(42));
77 std::vector<Attribute> attributes = stats.Attributes();
78 EXPECT_EQ(attributes.size(), static_cast<size_t>(16));
79 for (const auto& attribute : attributes) {
80 EXPECT_FALSE(attribute.has_value());
82 stats.m_bool = true;
83 stats.m_int32 = 123;
84 stats.m_uint32 = 123;
85 stats.m_int64 = 123;
86 stats.m_uint64 = 123;
87 stats.m_double = 123.0;
88 stats.m_string = std::string("123");
90 std::vector<bool> sequence_bool;
91 sequence_bool.push_back(true);
92 std::vector<int32_t> sequence_int32;
93 sequence_int32.push_back(static_cast<int32_t>(1));
94 std::vector<uint32_t> sequence_uint32;
95 sequence_uint32.push_back(static_cast<uint32_t>(2));
96 std::vector<int64_t> sequence_int64;
97 sequence_int64.push_back(static_cast<int64_t>(3));
98 std::vector<uint64_t> sequence_uint64;
99 sequence_uint64.push_back(static_cast<uint64_t>(4));
100 std::vector<double> sequence_double;
101 sequence_double.push_back(5.0);
102 std::vector<std::string> sequence_string;
103 sequence_string.push_back(std::string("six"));
105 std::map<std::string, uint64_t> map_string_uint64{{"seven", 8}};
106 std::map<std::string, double> map_string_double{{"nine", 10.0}};
108 stats.m_sequence_bool = sequence_bool;
109 stats.m_sequence_int32 = sequence_int32;
110 stats.m_sequence_uint32 = sequence_uint32;
111 EXPECT_FALSE(stats.m_sequence_int64.has_value());
112 stats.m_sequence_int64 = sequence_int64;
113 stats.m_sequence_uint64 = sequence_uint64;
114 stats.m_sequence_double = sequence_double;
115 stats.m_sequence_string = sequence_string;
116 stats.m_map_string_uint64 = map_string_uint64;
117 stats.m_map_string_double = map_string_double;
118 for (const auto& attribute : attributes) {
119 EXPECT_TRUE(attribute.has_value());
121 EXPECT_EQ(*stats.m_bool, true);
122 EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123));
123 EXPECT_EQ(*stats.m_uint32, static_cast<uint32_t>(123));
124 EXPECT_EQ(*stats.m_int64, static_cast<int64_t>(123));
125 EXPECT_EQ(*stats.m_uint64, static_cast<uint64_t>(123));
126 EXPECT_EQ(*stats.m_double, 123.0);
127 EXPECT_EQ(*stats.m_string, std::string("123"));
128 EXPECT_EQ(*stats.m_sequence_bool, sequence_bool);
129 EXPECT_EQ(*stats.m_sequence_int32, sequence_int32);
130 EXPECT_EQ(*stats.m_sequence_uint32, sequence_uint32);
131 EXPECT_EQ(*stats.m_sequence_int64, sequence_int64);
132 EXPECT_EQ(*stats.m_sequence_uint64, sequence_uint64);
133 EXPECT_EQ(*stats.m_sequence_double, sequence_double);
134 EXPECT_EQ(*stats.m_sequence_string, sequence_string);
135 EXPECT_EQ(*stats.m_map_string_uint64, map_string_uint64);
136 EXPECT_EQ(*stats.m_map_string_double, map_string_double);
138 int32_t numbers[] = {4, 8, 15, 16, 23, 42};
139 std::vector<int32_t> numbers_sequence(&numbers[0], &numbers[6]);
140 stats.m_sequence_int32->clear();
141 stats.m_sequence_int32->insert(stats.m_sequence_int32->end(),
142 numbers_sequence.begin(),
143 numbers_sequence.end());
144 EXPECT_EQ(*stats.m_sequence_int32, numbers_sequence);
147 TEST(RTCStatsTest, EqualityOperator) {
148 RTCTestStats empty_stats("testId", Timestamp::Micros(123));
149 EXPECT_EQ(empty_stats, empty_stats);
151 RTCTestStats stats_with_all_values = empty_stats;
152 stats_with_all_values.m_bool = true;
153 stats_with_all_values.m_int32 = 123;
154 stats_with_all_values.m_uint32 = 123;
155 stats_with_all_values.m_int64 = 123;
156 stats_with_all_values.m_uint64 = 123;
157 stats_with_all_values.m_double = 123.0;
158 stats_with_all_values.m_string = "123";
159 stats_with_all_values.m_sequence_bool = std::vector<bool>();
160 stats_with_all_values.m_sequence_int32 = std::vector<int32_t>();
161 stats_with_all_values.m_sequence_uint32 = std::vector<uint32_t>();
162 stats_with_all_values.m_sequence_int64 = std::vector<int64_t>();
163 stats_with_all_values.m_sequence_uint64 = std::vector<uint64_t>();
164 stats_with_all_values.m_sequence_double = std::vector<double>();
165 stats_with_all_values.m_sequence_string = std::vector<std::string>();
166 stats_with_all_values.m_map_string_uint64 = std::map<std::string, uint64_t>();
167 stats_with_all_values.m_map_string_double = std::map<std::string, double>();
168 EXPECT_NE(stats_with_all_values, empty_stats);
169 EXPECT_EQ(stats_with_all_values, stats_with_all_values);
170 EXPECT_NE(stats_with_all_values.GetAttribute(stats_with_all_values.m_int32),
171 stats_with_all_values.GetAttribute(stats_with_all_values.m_uint32));
173 RTCTestStats one_member_different[] = {
174 stats_with_all_values, stats_with_all_values, stats_with_all_values,
175 stats_with_all_values, stats_with_all_values, stats_with_all_values,
176 stats_with_all_values, stats_with_all_values, stats_with_all_values,
177 stats_with_all_values, stats_with_all_values, stats_with_all_values,
178 stats_with_all_values, stats_with_all_values,
180 for (size_t i = 0; i < 14; ++i) {
181 EXPECT_EQ(stats_with_all_values, one_member_different[i]);
183 one_member_different[0].m_bool = false;
184 one_member_different[1].m_int32 = 321;
185 one_member_different[2].m_uint32 = 321;
186 one_member_different[3].m_int64 = 321;
187 one_member_different[4].m_uint64 = 321;
188 one_member_different[5].m_double = 321.0;
189 one_member_different[6].m_string = "321";
190 one_member_different[7].m_sequence_bool->push_back(false);
191 one_member_different[8].m_sequence_int32->push_back(321);
192 one_member_different[9].m_sequence_uint32->push_back(321);
193 one_member_different[10].m_sequence_int64->push_back(321);
194 one_member_different[11].m_sequence_uint64->push_back(321);
195 one_member_different[12].m_sequence_double->push_back(321.0);
196 one_member_different[13].m_sequence_string->push_back("321");
197 (*one_member_different[13].m_map_string_uint64)["321"] = 321;
198 (*one_member_different[13].m_map_string_double)["321"] = 321.0;
199 for (size_t i = 0; i < 14; ++i) {
200 EXPECT_NE(stats_with_all_values, one_member_different[i]);
203 RTCTestStats empty_stats_different_id("testId2", Timestamp::Micros(123));
204 EXPECT_NE(empty_stats, empty_stats_different_id);
205 RTCTestStats empty_stats_different_timestamp("testId",
206 Timestamp::Micros(321));
207 EXPECT_EQ(empty_stats, empty_stats_different_timestamp);
209 RTCChildStats child("childId", Timestamp::Micros(42));
210 RTCGrandChildStats grandchild("grandchildId", Timestamp::Micros(42));
211 EXPECT_NE(child, grandchild);
213 RTCChildStats stats_with_defined_member("leId", Timestamp::Micros(0));
214 stats_with_defined_member.child_int = 0;
215 RTCChildStats stats_with_undefined_member("leId", Timestamp::Micros(0));
216 EXPECT_NE(stats_with_defined_member, stats_with_undefined_member);
217 EXPECT_NE(stats_with_undefined_member, stats_with_defined_member);
220 TEST(RTCStatsTest, RTCStatsGrandChild) {
221 RTCGrandChildStats stats("grandchild", Timestamp::Micros(0.0));
222 stats.child_int = 1;
223 stats.grandchild_int = 2;
224 int32_t sum = 0;
225 for (const auto& attribute : stats.Attributes()) {
226 sum += attribute.get<int32_t>();
228 EXPECT_EQ(sum, static_cast<int32_t>(3));
230 std::unique_ptr<RTCStats> copy_ptr = stats.copy();
231 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>();
232 EXPECT_EQ(*copy.child_int, *stats.child_int);
233 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
236 TEST(RTCStatsTest, RTCStatsPrintsValidJson) {
237 std::string id = "statsId";
238 int timestamp = 42;
239 bool m_bool = true;
240 int m_int32 = 123;
241 int64_t m_int64 = 1234567890123456499L;
242 double m_double = 123.4567890123456499;
243 std::string m_string = "123";
245 std::vector<bool> sequence_bool;
246 std::vector<int32_t> sequence_int32;
247 sequence_int32.push_back(static_cast<int32_t>(1));
248 std::vector<int64_t> sequence_int64;
249 sequence_int64.push_back(static_cast<int64_t>(-1234567890123456499L));
250 sequence_int64.push_back(static_cast<int64_t>(1));
251 sequence_int64.push_back(static_cast<int64_t>(1234567890123456499L));
252 std::vector<double> sequence_double;
253 sequence_double.push_back(123.4567890123456499);
254 sequence_double.push_back(1234567890123.456499);
255 std::vector<std::string> sequence_string;
256 sequence_string.push_back(std::string("four"));
258 std::map<std::string, uint64_t> map_string_uint64{
259 {"long", static_cast<uint64_t>(1234567890123456499L)}};
260 std::map<std::string, double> map_string_double{
261 {"three", 123.4567890123456499}, {"thirteen", 123.4567890123456499}};
263 RTCTestStats stats(id, Timestamp::Micros(timestamp));
264 stats.m_bool = m_bool;
265 stats.m_int32 = m_int32;
266 stats.m_int64 = m_int64;
267 stats.m_double = m_double;
268 stats.m_string = m_string;
269 stats.m_sequence_bool = sequence_bool;
270 stats.m_sequence_int32 = sequence_int32;
271 stats.m_sequence_int64 = sequence_int64;
272 stats.m_sequence_double = sequence_double;
273 stats.m_sequence_string = sequence_string;
274 stats.m_map_string_uint64 = map_string_uint64;
275 stats.m_map_string_double = map_string_double;
276 std::string json_stats = stats.ToJson();
278 Json::CharReaderBuilder builder;
279 Json::Value json_output;
280 std::unique_ptr<Json::CharReader> json_reader(builder.newCharReader());
281 EXPECT_TRUE(json_reader->parse(json_stats.c_str(),
282 json_stats.c_str() + json_stats.size(),
283 &json_output, nullptr));
285 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id));
286 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", &timestamp));
287 EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool));
288 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32));
289 EXPECT_TRUE(rtc::GetDoubleFromJsonObject(json_output, "mDouble", &m_double));
290 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string));
292 Json::Value json_array;
294 EXPECT_TRUE(
295 rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array));
296 EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool));
298 EXPECT_TRUE(
299 rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array));
300 EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32));
302 EXPECT_TRUE(
303 rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array));
304 EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double));
306 EXPECT_TRUE(
307 rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array));
308 EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string));
310 Json::Value json_map;
311 EXPECT_TRUE(
312 rtc::GetValueFromJsonObject(json_output, "mMapStringDouble", &json_map));
313 for (const auto& entry : map_string_double) {
314 double double_output = 0.0;
315 EXPECT_TRUE(
316 rtc::GetDoubleFromJsonObject(json_map, entry.first, &double_output));
317 EXPECT_NEAR(double_output, entry.second, GetExpectedError(entry.second));
320 EXPECT_EQ(id, stats.id());
321 EXPECT_EQ(timestamp, stats.timestamp().us());
322 EXPECT_EQ(m_bool, *stats.m_bool);
323 EXPECT_EQ(m_int32, *stats.m_int32);
324 EXPECT_EQ(m_string, *stats.m_string);
325 EXPECT_EQ(sequence_bool, *stats.m_sequence_bool);
326 EXPECT_EQ(sequence_int32, *stats.m_sequence_int32);
327 EXPECT_EQ(sequence_string, *stats.m_sequence_string);
328 EXPECT_EQ(map_string_double, *stats.m_map_string_double);
330 EXPECT_NEAR(m_double, *stats.m_double, GetExpectedError(*stats.m_double));
332 EXPECT_EQ(sequence_double.size(), stats.m_sequence_double->size());
333 for (size_t i = 0; i < stats.m_sequence_double->size(); ++i) {
334 EXPECT_NEAR(sequence_double[i], stats.m_sequence_double->at(i),
335 GetExpectedError(stats.m_sequence_double->at(i)));
338 EXPECT_EQ(map_string_double.size(), stats.m_map_string_double->size());
339 for (const auto& entry : map_string_double) {
340 auto it = stats.m_map_string_double->find(entry.first);
341 EXPECT_NE(it, stats.m_map_string_double->end());
342 EXPECT_NEAR(entry.second, it->second, GetExpectedError(it->second));
345 // We read mInt64 as double since JSON stores all numbers as doubles, so there
346 // is not enough precision to represent large numbers.
347 double m_int64_as_double;
348 std::vector<double> sequence_int64_as_double;
350 EXPECT_TRUE(
351 rtc::GetDoubleFromJsonObject(json_output, "mInt64", &m_int64_as_double));
353 EXPECT_TRUE(
354 rtc::GetValueFromJsonObject(json_output, "mSequenceInt64", &json_array));
355 EXPECT_TRUE(
356 rtc::JsonArrayToDoubleVector(json_array, &sequence_int64_as_double));
358 double stats_m_int64_as_double = static_cast<double>(*stats.m_int64);
359 EXPECT_NEAR(m_int64_as_double, stats_m_int64_as_double,
360 GetExpectedError(stats_m_int64_as_double));
362 EXPECT_EQ(sequence_int64_as_double.size(), stats.m_sequence_int64->size());
363 for (size_t i = 0; i < stats.m_sequence_int64->size(); ++i) {
364 const double stats_value_as_double =
365 static_cast<double>((*stats.m_sequence_int64)[i]);
366 EXPECT_NEAR(sequence_int64_as_double[i], stats_value_as_double,
367 GetExpectedError(stats_value_as_double));
370 // Similarly, read Uint64 as double
371 EXPECT_TRUE(
372 rtc::GetValueFromJsonObject(json_output, "mMapStringUint64", &json_map));
373 for (const auto& entry : map_string_uint64) {
374 const double stats_value_as_double =
375 static_cast<double>((*stats.m_map_string_uint64)[entry.first]);
376 double double_output = 0.0;
377 EXPECT_TRUE(
378 rtc::GetDoubleFromJsonObject(json_map, entry.first, &double_output));
379 EXPECT_NEAR(double_output, stats_value_as_double,
380 GetExpectedError(stats_value_as_double));
383 // Neither stats.m_uint32 nor stats.m_uint64 are defined, so "mUint64" and
384 // "mUint32" should not be part of the generated JSON object.
385 int m_uint32;
386 int m_uint64;
387 EXPECT_FALSE(stats.m_uint32.has_value());
388 EXPECT_FALSE(stats.m_uint64.has_value());
389 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint32", &m_uint32));
390 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint64", &m_uint64));
392 std::cout << stats.ToJson() << std::endl;
395 TEST(RTCStatsTest, IsSequence) {
396 RTCTestStats stats("statsId", Timestamp::Micros(42));
397 EXPECT_FALSE(stats.GetAttribute(stats.m_bool).is_sequence());
398 EXPECT_FALSE(stats.GetAttribute(stats.m_int32).is_sequence());
399 EXPECT_FALSE(stats.GetAttribute(stats.m_uint32).is_sequence());
400 EXPECT_FALSE(stats.GetAttribute(stats.m_int64).is_sequence());
401 EXPECT_FALSE(stats.GetAttribute(stats.m_uint64).is_sequence());
402 EXPECT_FALSE(stats.GetAttribute(stats.m_double).is_sequence());
403 EXPECT_FALSE(stats.GetAttribute(stats.m_string).is_sequence());
404 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_bool).is_sequence());
405 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_int32).is_sequence());
406 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_uint32).is_sequence());
407 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_int64).is_sequence());
408 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_uint64).is_sequence());
409 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_double).is_sequence());
410 EXPECT_TRUE(stats.GetAttribute(stats.m_sequence_string).is_sequence());
411 EXPECT_FALSE(stats.GetAttribute(stats.m_map_string_uint64).is_sequence());
412 EXPECT_FALSE(stats.GetAttribute(stats.m_map_string_double).is_sequence());
415 TEST(RTCStatsTest, IsString) {
416 RTCTestStats stats("statsId", Timestamp::Micros(42));
417 EXPECT_TRUE(stats.GetAttribute(stats.m_string).is_string());
418 EXPECT_FALSE(stats.GetAttribute(stats.m_bool).is_string());
419 EXPECT_FALSE(stats.GetAttribute(stats.m_int32).is_string());
420 EXPECT_FALSE(stats.GetAttribute(stats.m_uint32).is_string());
421 EXPECT_FALSE(stats.GetAttribute(stats.m_int64).is_string());
422 EXPECT_FALSE(stats.GetAttribute(stats.m_uint64).is_string());
423 EXPECT_FALSE(stats.GetAttribute(stats.m_double).is_string());
424 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_bool).is_string());
425 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_int32).is_string());
426 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_uint32).is_string());
427 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_int64).is_string());
428 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_uint64).is_string());
429 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_double).is_string());
430 EXPECT_FALSE(stats.GetAttribute(stats.m_sequence_string).is_string());
431 EXPECT_FALSE(stats.GetAttribute(stats.m_map_string_uint64).is_string());
432 EXPECT_FALSE(stats.GetAttribute(stats.m_map_string_double).is_string());
435 TEST(RTCStatsTest, AttributeToString) {
436 RTCTestStats stats("statsId", Timestamp::Micros(42));
437 stats.m_bool = true;
438 EXPECT_EQ("true", stats.GetAttribute(stats.m_bool).ToString());
440 stats.m_string = "foo";
441 EXPECT_EQ("foo", stats.GetAttribute(stats.m_string).ToString());
442 stats.m_int32 = -32;
443 EXPECT_EQ("-32", stats.GetAttribute(stats.m_int32).ToString());
444 stats.m_uint32 = 32;
445 EXPECT_EQ("32", stats.GetAttribute(stats.m_uint32).ToString());
446 stats.m_int64 = -64;
447 EXPECT_EQ("-64", stats.GetAttribute(stats.m_int64).ToString());
448 stats.m_uint64 = 64;
449 EXPECT_EQ("64", stats.GetAttribute(stats.m_uint64).ToString());
450 stats.m_double = 0.5;
451 EXPECT_EQ("0.5", stats.GetAttribute(stats.m_double).ToString());
452 stats.m_sequence_bool = {true, false};
453 EXPECT_EQ("[true,false]",
454 stats.GetAttribute(stats.m_sequence_bool).ToString());
455 stats.m_sequence_int32 = {-32, 32};
456 EXPECT_EQ("[-32,32]", stats.GetAttribute(stats.m_sequence_int32).ToString());
457 stats.m_sequence_uint32 = {64, 32};
458 EXPECT_EQ("[64,32]", stats.GetAttribute(stats.m_sequence_uint32).ToString());
459 stats.m_sequence_int64 = {-64, 32};
460 EXPECT_EQ("[-64,32]", stats.GetAttribute(stats.m_sequence_int64).ToString());
461 stats.m_sequence_uint64 = {16, 32};
462 EXPECT_EQ("[16,32]", stats.GetAttribute(stats.m_sequence_uint64).ToString());
463 stats.m_sequence_double = {0.5, 0.25};
464 EXPECT_EQ("[0.5,0.25]",
465 stats.GetAttribute(stats.m_sequence_double).ToString());
466 stats.m_sequence_string = {"foo", "bar"};
467 EXPECT_EQ("[\"foo\",\"bar\"]",
468 stats.GetAttribute(stats.m_sequence_string).ToString());
469 stats.m_map_string_uint64 = std::map<std::string, uint64_t>();
470 stats.m_map_string_uint64->emplace("foo", 32);
471 stats.m_map_string_uint64->emplace("bar", 64);
472 EXPECT_EQ("{\"bar\":64,\"foo\":32}",
473 stats.GetAttribute(stats.m_map_string_uint64).ToString());
474 stats.m_map_string_double = std::map<std::string, double>();
475 stats.m_map_string_double->emplace("foo", 0.5);
476 stats.m_map_string_double->emplace("bar", 0.25);
477 EXPECT_EQ("{\"bar\":0.25,\"foo\":0.5}",
478 stats.GetAttribute(stats.m_map_string_double).ToString());
481 // Death tests.
482 // Disabled on Android because death tests misbehave on Android, see
483 // base/test/gtest_util.h.
484 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
486 TEST(RTCStatsDeathTest, ValueOfUndefinedMember) {
487 RTCTestStats stats("testId", Timestamp::Micros(0));
488 EXPECT_FALSE(stats.m_int32.has_value());
489 EXPECT_DEATH(*stats.m_int32, "");
492 TEST(RTCStatsDeathTest, InvalidCasting) {
493 RTCGrandChildStats stats("grandchild", Timestamp::Micros(0.0));
494 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), "");
497 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
499 } // namespace webrtc