Bug 1833854 - Part 6: Round requested nursery before checking range when changing...
[gecko.git] / third_party / libwebrtc / stats / rtc_stats_unittest.cc
blob84b9a63ed4a94b27687e9954155896da9332b801
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 "rtc_base/checks.h"
19 #include "rtc_base/strings/json.h"
20 #include "stats/test/rtc_test_stats.h"
21 #include "test/gtest.h"
23 namespace webrtc {
25 namespace {
27 // JSON stores numbers as floating point numbers with 53 significant bits, which
28 // amounts to about 15.95 decimal digits. Thus, when comparing large numbers
29 // processed by JSON, that's all the precision we should expect.
30 const double JSON_EPSILON = 1e-15;
32 // We do this since Google Test doesn't support relative error.
33 // This is computed as follows:
34 // If |a - b| / |a| < EPS, then |a - b| < |a| * EPS, so |a| * EPS is the
35 // maximum expected error.
36 double GetExpectedError(const double expected_value) {
37 return JSON_EPSILON * fabs(expected_value);
40 } // namespace
42 class RTCChildStats : public RTCStats {
43 public:
44 WEBRTC_RTCSTATS_DECL();
46 RTCChildStats(const std::string& id, Timestamp timestamp)
47 : RTCStats(id, timestamp), child_int("childInt") {}
49 RTCStatsMember<int32_t> child_int;
52 WEBRTC_RTCSTATS_IMPL(RTCChildStats, RTCStats, "child-stats", &child_int)
54 class RTCGrandChildStats : public RTCChildStats {
55 public:
56 WEBRTC_RTCSTATS_DECL();
58 RTCGrandChildStats(const std::string& id, Timestamp timestamp)
59 : RTCChildStats(id, timestamp), grandchild_int("grandchildInt") {}
61 RTCStatsMember<int32_t> grandchild_int;
64 WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats,
65 RTCChildStats,
66 "grandchild-stats",
67 &grandchild_int)
69 TEST(RTCStatsTest, RTCStatsAndMembers) {
70 RTCTestStats stats("testId", Timestamp::Micros(42));
71 EXPECT_EQ(stats.id(), "testId");
72 EXPECT_EQ(stats.timestamp().us(), static_cast<int64_t>(42));
73 std::vector<const RTCStatsMemberInterface*> members = stats.Members();
74 EXPECT_EQ(members.size(), static_cast<size_t>(16));
75 for (const RTCStatsMemberInterface* member : members) {
76 EXPECT_FALSE(member->is_defined());
78 stats.m_bool = true;
79 stats.m_int32 = 123;
80 stats.m_uint32 = 123;
81 stats.m_int64 = 123;
82 stats.m_uint64 = 123;
83 stats.m_double = 123.0;
84 stats.m_string = std::string("123");
86 std::vector<bool> sequence_bool;
87 sequence_bool.push_back(true);
88 std::vector<int32_t> sequence_int32;
89 sequence_int32.push_back(static_cast<int32_t>(1));
90 std::vector<uint32_t> sequence_uint32;
91 sequence_uint32.push_back(static_cast<uint32_t>(2));
92 std::vector<int64_t> sequence_int64;
93 sequence_int64.push_back(static_cast<int64_t>(3));
94 std::vector<uint64_t> sequence_uint64;
95 sequence_uint64.push_back(static_cast<uint64_t>(4));
96 std::vector<double> sequence_double;
97 sequence_double.push_back(5.0);
98 std::vector<std::string> sequence_string;
99 sequence_string.push_back(std::string("six"));
101 std::map<std::string, uint64_t> map_string_uint64{{"seven", 8}};
102 std::map<std::string, double> map_string_double{{"nine", 10.0}};
104 stats.m_sequence_bool = sequence_bool;
105 stats.m_sequence_int32 = sequence_int32;
106 stats.m_sequence_uint32 = sequence_uint32;
107 EXPECT_FALSE(stats.m_sequence_int64.is_defined());
108 stats.m_sequence_int64 = sequence_int64;
109 stats.m_sequence_uint64 = sequence_uint64;
110 stats.m_sequence_double = sequence_double;
111 stats.m_sequence_string = sequence_string;
112 stats.m_map_string_uint64 = map_string_uint64;
113 stats.m_map_string_double = map_string_double;
114 for (const RTCStatsMemberInterface* member : members) {
115 EXPECT_TRUE(member->is_defined());
117 EXPECT_EQ(*stats.m_bool, true);
118 EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123));
119 EXPECT_EQ(*stats.m_uint32, static_cast<uint32_t>(123));
120 EXPECT_EQ(*stats.m_int64, static_cast<int64_t>(123));
121 EXPECT_EQ(*stats.m_uint64, static_cast<uint64_t>(123));
122 EXPECT_EQ(*stats.m_double, 123.0);
123 EXPECT_EQ(*stats.m_string, std::string("123"));
124 EXPECT_EQ(*stats.m_sequence_bool, sequence_bool);
125 EXPECT_EQ(*stats.m_sequence_int32, sequence_int32);
126 EXPECT_EQ(*stats.m_sequence_uint32, sequence_uint32);
127 EXPECT_EQ(*stats.m_sequence_int64, sequence_int64);
128 EXPECT_EQ(*stats.m_sequence_uint64, sequence_uint64);
129 EXPECT_EQ(*stats.m_sequence_double, sequence_double);
130 EXPECT_EQ(*stats.m_sequence_string, sequence_string);
131 EXPECT_EQ(*stats.m_map_string_uint64, map_string_uint64);
132 EXPECT_EQ(*stats.m_map_string_double, map_string_double);
134 int32_t numbers[] = {4, 8, 15, 16, 23, 42};
135 std::vector<int32_t> numbers_sequence(&numbers[0], &numbers[6]);
136 stats.m_sequence_int32->clear();
137 stats.m_sequence_int32->insert(stats.m_sequence_int32->end(),
138 numbers_sequence.begin(),
139 numbers_sequence.end());
140 EXPECT_EQ(*stats.m_sequence_int32, numbers_sequence);
143 TEST(RTCStatsTest, EqualityOperator) {
144 RTCTestStats empty_stats("testId", Timestamp::Micros(123));
145 EXPECT_EQ(empty_stats, empty_stats);
147 RTCTestStats stats_with_all_values = empty_stats;
148 stats_with_all_values.m_bool = true;
149 stats_with_all_values.m_int32 = 123;
150 stats_with_all_values.m_uint32 = 123;
151 stats_with_all_values.m_int64 = 123;
152 stats_with_all_values.m_uint64 = 123;
153 stats_with_all_values.m_double = 123.0;
154 stats_with_all_values.m_string = "123";
155 stats_with_all_values.m_sequence_bool = std::vector<bool>();
156 stats_with_all_values.m_sequence_int32 = std::vector<int32_t>();
157 stats_with_all_values.m_sequence_uint32 = std::vector<uint32_t>();
158 stats_with_all_values.m_sequence_int64 = std::vector<int64_t>();
159 stats_with_all_values.m_sequence_uint64 = std::vector<uint64_t>();
160 stats_with_all_values.m_sequence_double = std::vector<double>();
161 stats_with_all_values.m_sequence_string = std::vector<std::string>();
162 stats_with_all_values.m_map_string_uint64 = std::map<std::string, uint64_t>();
163 stats_with_all_values.m_map_string_double = std::map<std::string, double>();
164 EXPECT_NE(stats_with_all_values, empty_stats);
165 EXPECT_EQ(stats_with_all_values, stats_with_all_values);
166 EXPECT_NE(stats_with_all_values.m_int32, stats_with_all_values.m_uint32);
168 RTCTestStats one_member_different[] = {
169 stats_with_all_values, stats_with_all_values, stats_with_all_values,
170 stats_with_all_values, stats_with_all_values, stats_with_all_values,
171 stats_with_all_values, stats_with_all_values, stats_with_all_values,
172 stats_with_all_values, stats_with_all_values, stats_with_all_values,
173 stats_with_all_values, stats_with_all_values,
175 for (size_t i = 0; i < 14; ++i) {
176 EXPECT_EQ(stats_with_all_values, one_member_different[i]);
178 one_member_different[0].m_bool = false;
179 one_member_different[1].m_int32 = 321;
180 one_member_different[2].m_uint32 = 321;
181 one_member_different[3].m_int64 = 321;
182 one_member_different[4].m_uint64 = 321;
183 one_member_different[5].m_double = 321.0;
184 one_member_different[6].m_string = "321";
185 one_member_different[7].m_sequence_bool->push_back(false);
186 one_member_different[8].m_sequence_int32->push_back(321);
187 one_member_different[9].m_sequence_uint32->push_back(321);
188 one_member_different[10].m_sequence_int64->push_back(321);
189 one_member_different[11].m_sequence_uint64->push_back(321);
190 one_member_different[12].m_sequence_double->push_back(321.0);
191 one_member_different[13].m_sequence_string->push_back("321");
192 (*one_member_different[13].m_map_string_uint64)["321"] = 321;
193 (*one_member_different[13].m_map_string_double)["321"] = 321.0;
194 for (size_t i = 0; i < 14; ++i) {
195 EXPECT_NE(stats_with_all_values, one_member_different[i]);
198 RTCTestStats empty_stats_different_id("testId2", Timestamp::Micros(123));
199 EXPECT_NE(empty_stats, empty_stats_different_id);
200 RTCTestStats empty_stats_different_timestamp("testId",
201 Timestamp::Micros(321));
202 EXPECT_EQ(empty_stats, empty_stats_different_timestamp);
204 RTCChildStats child("childId", Timestamp::Micros(42));
205 RTCGrandChildStats grandchild("grandchildId", Timestamp::Micros(42));
206 EXPECT_NE(child, grandchild);
208 RTCChildStats stats_with_defined_member("leId", Timestamp::Micros(0));
209 stats_with_defined_member.child_int = 0;
210 RTCChildStats stats_with_undefined_member("leId", Timestamp::Micros(0));
211 EXPECT_NE(stats_with_defined_member, stats_with_undefined_member);
212 EXPECT_NE(stats_with_undefined_member, stats_with_defined_member);
215 TEST(RTCStatsTest, RTCStatsGrandChild) {
216 RTCGrandChildStats stats("grandchild", Timestamp::Micros(0.0));
217 stats.child_int = 1;
218 stats.grandchild_int = 2;
219 int32_t sum = 0;
220 for (const RTCStatsMemberInterface* member : stats.Members()) {
221 sum += *member->cast_to<const RTCStatsMember<int32_t>>();
223 EXPECT_EQ(sum, static_cast<int32_t>(3));
225 std::unique_ptr<RTCStats> copy_ptr = stats.copy();
226 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>();
227 EXPECT_EQ(*copy.child_int, *stats.child_int);
228 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
231 TEST(RTCStatsTest, RTCStatsPrintsValidJson) {
232 std::string id = "statsId";
233 int timestamp = 42;
234 bool m_bool = true;
235 int m_int32 = 123;
236 int64_t m_int64 = 1234567890123456499L;
237 double m_double = 123.4567890123456499;
238 std::string m_string = "123";
240 std::vector<bool> sequence_bool;
241 std::vector<int32_t> sequence_int32;
242 sequence_int32.push_back(static_cast<int32_t>(1));
243 std::vector<int64_t> sequence_int64;
244 sequence_int64.push_back(static_cast<int64_t>(-1234567890123456499L));
245 sequence_int64.push_back(static_cast<int64_t>(1));
246 sequence_int64.push_back(static_cast<int64_t>(1234567890123456499L));
247 std::vector<double> sequence_double;
248 sequence_double.push_back(123.4567890123456499);
249 sequence_double.push_back(1234567890123.456499);
250 std::vector<std::string> sequence_string;
251 sequence_string.push_back(std::string("four"));
253 std::map<std::string, uint64_t> map_string_uint64{
254 {"long", static_cast<uint64_t>(1234567890123456499L)}};
255 std::map<std::string, double> map_string_double{
256 {"three", 123.4567890123456499}, {"thirteen", 123.4567890123456499}};
258 RTCTestStats stats(id, Timestamp::Micros(timestamp));
259 stats.m_bool = m_bool;
260 stats.m_int32 = m_int32;
261 stats.m_int64 = m_int64;
262 stats.m_double = m_double;
263 stats.m_string = m_string;
264 stats.m_sequence_bool = sequence_bool;
265 stats.m_sequence_int32 = sequence_int32;
266 stats.m_sequence_int64 = sequence_int64;
267 stats.m_sequence_double = sequence_double;
268 stats.m_sequence_string = sequence_string;
269 stats.m_map_string_uint64 = map_string_uint64;
270 stats.m_map_string_double = map_string_double;
271 std::string json_stats = stats.ToJson();
273 Json::CharReaderBuilder builder;
274 Json::Value json_output;
275 std::unique_ptr<Json::CharReader> json_reader(builder.newCharReader());
276 EXPECT_TRUE(json_reader->parse(json_stats.c_str(),
277 json_stats.c_str() + json_stats.size(),
278 &json_output, nullptr));
280 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id));
281 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", &timestamp));
282 EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool));
283 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32));
284 EXPECT_TRUE(rtc::GetDoubleFromJsonObject(json_output, "mDouble", &m_double));
285 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string));
287 Json::Value json_array;
289 EXPECT_TRUE(
290 rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array));
291 EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool));
293 EXPECT_TRUE(
294 rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array));
295 EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32));
297 EXPECT_TRUE(
298 rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array));
299 EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double));
301 EXPECT_TRUE(
302 rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array));
303 EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string));
305 Json::Value json_map;
306 EXPECT_TRUE(
307 rtc::GetValueFromJsonObject(json_output, "mMapStringDouble", &json_map));
308 for (const auto& entry : map_string_double) {
309 double double_output = 0.0;
310 EXPECT_TRUE(
311 rtc::GetDoubleFromJsonObject(json_map, entry.first, &double_output));
312 EXPECT_NEAR(double_output, entry.second, GetExpectedError(entry.second));
315 EXPECT_EQ(id, stats.id());
316 EXPECT_EQ(timestamp, stats.timestamp().us());
317 EXPECT_EQ(m_bool, *stats.m_bool);
318 EXPECT_EQ(m_int32, *stats.m_int32);
319 EXPECT_EQ(m_string, *stats.m_string);
320 EXPECT_EQ(sequence_bool, *stats.m_sequence_bool);
321 EXPECT_EQ(sequence_int32, *stats.m_sequence_int32);
322 EXPECT_EQ(sequence_string, *stats.m_sequence_string);
323 EXPECT_EQ(map_string_double, *stats.m_map_string_double);
325 EXPECT_NEAR(m_double, *stats.m_double, GetExpectedError(*stats.m_double));
327 EXPECT_EQ(sequence_double.size(), stats.m_sequence_double->size());
328 for (size_t i = 0; i < stats.m_sequence_double->size(); ++i) {
329 EXPECT_NEAR(sequence_double[i], stats.m_sequence_double->at(i),
330 GetExpectedError(stats.m_sequence_double->at(i)));
333 EXPECT_EQ(map_string_double.size(), stats.m_map_string_double->size());
334 for (const auto& entry : map_string_double) {
335 auto it = stats.m_map_string_double->find(entry.first);
336 EXPECT_NE(it, stats.m_map_string_double->end());
337 EXPECT_NEAR(entry.second, it->second, GetExpectedError(it->second));
340 // We read mInt64 as double since JSON stores all numbers as doubles, so there
341 // is not enough precision to represent large numbers.
342 double m_int64_as_double;
343 std::vector<double> sequence_int64_as_double;
345 EXPECT_TRUE(
346 rtc::GetDoubleFromJsonObject(json_output, "mInt64", &m_int64_as_double));
348 EXPECT_TRUE(
349 rtc::GetValueFromJsonObject(json_output, "mSequenceInt64", &json_array));
350 EXPECT_TRUE(
351 rtc::JsonArrayToDoubleVector(json_array, &sequence_int64_as_double));
353 double stats_m_int64_as_double = static_cast<double>(*stats.m_int64);
354 EXPECT_NEAR(m_int64_as_double, stats_m_int64_as_double,
355 GetExpectedError(stats_m_int64_as_double));
357 EXPECT_EQ(sequence_int64_as_double.size(), stats.m_sequence_int64->size());
358 for (size_t i = 0; i < stats.m_sequence_int64->size(); ++i) {
359 const double stats_value_as_double =
360 static_cast<double>((*stats.m_sequence_int64)[i]);
361 EXPECT_NEAR(sequence_int64_as_double[i], stats_value_as_double,
362 GetExpectedError(stats_value_as_double));
365 // Similarly, read Uint64 as double
366 EXPECT_TRUE(
367 rtc::GetValueFromJsonObject(json_output, "mMapStringUint64", &json_map));
368 for (const auto& entry : map_string_uint64) {
369 const double stats_value_as_double =
370 static_cast<double>((*stats.m_map_string_uint64)[entry.first]);
371 double double_output = 0.0;
372 EXPECT_TRUE(
373 rtc::GetDoubleFromJsonObject(json_map, entry.first, &double_output));
374 EXPECT_NEAR(double_output, stats_value_as_double,
375 GetExpectedError(stats_value_as_double));
378 // Neither stats.m_uint32 nor stats.m_uint64 are defined, so "mUint64" and
379 // "mUint32" should not be part of the generated JSON object.
380 int m_uint32;
381 int m_uint64;
382 EXPECT_FALSE(stats.m_uint32.is_defined());
383 EXPECT_FALSE(stats.m_uint64.is_defined());
384 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint32", &m_uint32));
385 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint64", &m_uint64));
387 std::cout << stats.ToJson() << std::endl;
390 TEST(RTCStatsTest, IsStandardized) {
391 RTCStatsMember<int32_t> standardized("standardized");
392 RTCNonStandardStatsMember<int32_t> unstandardized("unstandardized");
393 EXPECT_TRUE(standardized.is_standardized());
394 EXPECT_FALSE(unstandardized.is_standardized());
397 TEST(RTCStatsTest, IsSequence) {
398 RTCTestStats stats("statsId", Timestamp::Micros(42));
399 EXPECT_FALSE(stats.m_bool.is_sequence());
400 EXPECT_FALSE(stats.m_int32.is_sequence());
401 EXPECT_FALSE(stats.m_uint32.is_sequence());
402 EXPECT_FALSE(stats.m_int64.is_sequence());
403 EXPECT_FALSE(stats.m_uint64.is_sequence());
404 EXPECT_FALSE(stats.m_double.is_sequence());
405 EXPECT_FALSE(stats.m_string.is_sequence());
406 EXPECT_TRUE(stats.m_sequence_bool.is_sequence());
407 EXPECT_TRUE(stats.m_sequence_int32.is_sequence());
408 EXPECT_TRUE(stats.m_sequence_uint32.is_sequence());
409 EXPECT_TRUE(stats.m_sequence_int64.is_sequence());
410 EXPECT_TRUE(stats.m_sequence_uint64.is_sequence());
411 EXPECT_TRUE(stats.m_sequence_double.is_sequence());
412 EXPECT_TRUE(stats.m_sequence_string.is_sequence());
413 EXPECT_FALSE(stats.m_map_string_uint64.is_sequence());
414 EXPECT_FALSE(stats.m_map_string_double.is_sequence());
417 TEST(RTCStatsTest, Type) {
418 RTCTestStats stats("statsId", Timestamp::Micros(42));
419 EXPECT_EQ(RTCStatsMemberInterface::kBool, stats.m_bool.type());
420 EXPECT_EQ(RTCStatsMemberInterface::kInt32, stats.m_int32.type());
421 EXPECT_EQ(RTCStatsMemberInterface::kUint32, stats.m_uint32.type());
422 EXPECT_EQ(RTCStatsMemberInterface::kInt64, stats.m_int64.type());
423 EXPECT_EQ(RTCStatsMemberInterface::kUint64, stats.m_uint64.type());
424 EXPECT_EQ(RTCStatsMemberInterface::kDouble, stats.m_double.type());
425 EXPECT_EQ(RTCStatsMemberInterface::kString, stats.m_string.type());
426 EXPECT_EQ(RTCStatsMemberInterface::kSequenceBool,
427 stats.m_sequence_bool.type());
428 EXPECT_EQ(RTCStatsMemberInterface::kSequenceInt32,
429 stats.m_sequence_int32.type());
430 EXPECT_EQ(RTCStatsMemberInterface::kSequenceUint32,
431 stats.m_sequence_uint32.type());
432 EXPECT_EQ(RTCStatsMemberInterface::kSequenceInt64,
433 stats.m_sequence_int64.type());
434 EXPECT_EQ(RTCStatsMemberInterface::kSequenceUint64,
435 stats.m_sequence_uint64.type());
436 EXPECT_EQ(RTCStatsMemberInterface::kSequenceDouble,
437 stats.m_sequence_double.type());
438 EXPECT_EQ(RTCStatsMemberInterface::kSequenceString,
439 stats.m_sequence_string.type());
440 EXPECT_EQ(RTCStatsMemberInterface::kMapStringUint64,
441 stats.m_map_string_uint64.type());
442 EXPECT_EQ(RTCStatsMemberInterface::kMapStringDouble,
443 stats.m_map_string_double.type());
446 TEST(RTCStatsTest, IsString) {
447 RTCTestStats stats("statsId", Timestamp::Micros(42));
448 EXPECT_TRUE(stats.m_string.is_string());
449 EXPECT_FALSE(stats.m_bool.is_string());
450 EXPECT_FALSE(stats.m_int32.is_string());
451 EXPECT_FALSE(stats.m_uint32.is_string());
452 EXPECT_FALSE(stats.m_int64.is_string());
453 EXPECT_FALSE(stats.m_uint64.is_string());
454 EXPECT_FALSE(stats.m_double.is_string());
455 EXPECT_FALSE(stats.m_sequence_bool.is_string());
456 EXPECT_FALSE(stats.m_sequence_int32.is_string());
457 EXPECT_FALSE(stats.m_sequence_uint32.is_string());
458 EXPECT_FALSE(stats.m_sequence_int64.is_string());
459 EXPECT_FALSE(stats.m_sequence_uint64.is_string());
460 EXPECT_FALSE(stats.m_sequence_double.is_string());
461 EXPECT_FALSE(stats.m_sequence_string.is_string());
462 EXPECT_FALSE(stats.m_map_string_uint64.is_string());
463 EXPECT_FALSE(stats.m_map_string_double.is_string());
466 TEST(RTCStatsTest, ValueToString) {
467 RTCTestStats stats("statsId", Timestamp::Micros(42));
468 stats.m_bool = true;
469 EXPECT_EQ("true", stats.m_bool.ValueToString());
471 stats.m_string = "foo";
472 EXPECT_EQ("foo", stats.m_string.ValueToString());
473 stats.m_int32 = -32;
474 EXPECT_EQ("-32", stats.m_int32.ValueToString());
475 stats.m_uint32 = 32;
476 EXPECT_EQ("32", stats.m_uint32.ValueToString());
477 stats.m_int64 = -64;
478 EXPECT_EQ("-64", stats.m_int64.ValueToString());
479 stats.m_uint64 = 64;
480 EXPECT_EQ("64", stats.m_uint64.ValueToString());
481 stats.m_double = 0.5;
482 EXPECT_EQ("0.5", stats.m_double.ValueToString());
483 stats.m_sequence_bool = {true, false};
484 EXPECT_EQ("[true,false]", stats.m_sequence_bool.ValueToString());
485 stats.m_sequence_int32 = {-32, 32};
486 EXPECT_EQ("[-32,32]", stats.m_sequence_int32.ValueToString());
487 stats.m_sequence_uint32 = {64, 32};
488 EXPECT_EQ("[64,32]", stats.m_sequence_uint32.ValueToString());
489 stats.m_sequence_int64 = {-64, 32};
490 EXPECT_EQ("[-64,32]", stats.m_sequence_int64.ValueToString());
491 stats.m_sequence_uint64 = {16, 32};
492 EXPECT_EQ("[16,32]", stats.m_sequence_uint64.ValueToString());
493 stats.m_sequence_double = {0.5, 0.25};
494 EXPECT_EQ("[0.5,0.25]", stats.m_sequence_double.ValueToString());
495 stats.m_sequence_string = {"foo", "bar"};
496 EXPECT_EQ("[\"foo\",\"bar\"]", stats.m_sequence_string.ValueToString());
497 stats.m_map_string_uint64 = std::map<std::string, uint64_t>();
498 stats.m_map_string_uint64->emplace("foo", 32);
499 stats.m_map_string_uint64->emplace("bar", 64);
500 EXPECT_EQ("{bar:64,foo:32}", stats.m_map_string_uint64.ValueToString());
501 stats.m_map_string_double = std::map<std::string, double>();
502 stats.m_map_string_double->emplace("foo", 0.5);
503 stats.m_map_string_double->emplace("bar", 0.25);
504 EXPECT_EQ("{bar:0.25,foo:0.5}", stats.m_map_string_double.ValueToString());
507 TEST(RTCStatsTest, RestrictedStatsTest) {
508 RTCStatsMember<bool> unrestricted("unrestricted");
509 EXPECT_EQ(unrestricted.exposure_criteria(), StatExposureCriteria::kAlways);
510 RTCRestrictedStatsMember<bool, StatExposureCriteria::kHardwareCapability>
511 restricted("restricted");
512 EXPECT_EQ(restricted.exposure_criteria(),
513 StatExposureCriteria::kHardwareCapability);
515 unrestricted = true;
516 restricted = true;
517 EXPECT_NE(unrestricted, restricted)
518 << "These can not be equal as they have different exposure criteria.";
521 TEST(RTCStatsTest, NonStandardGroupId) {
522 auto group_id = NonStandardGroupId::kGroupIdForTesting;
523 RTCNonStandardStatsMember<int32_t> with_group_id("stat", {group_id});
524 std::vector<NonStandardGroupId> expected_ids({group_id});
525 EXPECT_EQ(expected_ids, with_group_id.group_ids());
527 RTCNonStandardStatsMember<int32_t> without_group_id("stat");
528 EXPECT_TRUE(without_group_id.group_ids().empty());
531 // Death tests.
532 // Disabled on Android because death tests misbehave on Android, see
533 // base/test/gtest_util.h.
534 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
536 TEST(RTCStatsDeathTest, ValueOfUndefinedMember) {
537 RTCTestStats stats("testId", Timestamp::Micros(0));
538 EXPECT_FALSE(stats.m_int32.is_defined());
539 EXPECT_DEATH(*stats.m_int32, "");
542 TEST(RTCStatsDeathTest, InvalidCasting) {
543 RTCGrandChildStats stats("grandchild", Timestamp::Micros(0.0));
544 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), "");
547 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
549 } // namespace webrtc