hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "api/stats/rtc_stats.h" |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 12 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 13 | #include <cmath> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <cstdint> |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 15 | #include <cstring> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <iostream> |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Sam Zackrisson | b45bdb5 | 2018-10-02 16:25:59 +0200 | [diff] [blame] | 19 | #include "rtc_base/strings/json.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "stats/test/rtc_test_stats.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "test/gtest.h" |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 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; |
| 31 | |
| 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); |
| 38 | } |
| 39 | |
| 40 | } // namespace |
| 41 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 42 | class RTCChildStats : public RTCStats { |
| 43 | public: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 44 | WEBRTC_RTCSTATS_DECL(); |
| 45 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 46 | RTCChildStats(const std::string& id, int64_t timestamp_us) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | : RTCStats(id, timestamp_us), child_int("childInt") {} |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 48 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 49 | RTCStatsMember<int32_t> child_int; |
| 50 | }; |
| 51 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | WEBRTC_RTCSTATS_IMPL(RTCChildStats, RTCStats, "child-stats", &child_int); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 53 | |
| 54 | class RTCGrandChildStats : public RTCChildStats { |
| 55 | public: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 56 | WEBRTC_RTCSTATS_DECL(); |
| 57 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 58 | RTCGrandChildStats(const std::string& id, int64_t timestamp_us) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 59 | : RTCChildStats(id, timestamp_us), grandchild_int("grandchildInt") {} |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 60 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 61 | RTCStatsMember<int32_t> grandchild_int; |
| 62 | }; |
| 63 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats, |
| 65 | RTCChildStats, |
| 66 | "grandchild-stats", |
| 67 | &grandchild_int); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 68 | |
| 69 | TEST(RTCStatsTest, RTCStatsAndMembers) { |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 70 | RTCTestStats stats("testId", 42); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 71 | EXPECT_EQ(stats.id(), "testId"); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 72 | EXPECT_EQ(stats.timestamp_us(), static_cast<int64_t>(42)); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 73 | std::vector<const RTCStatsMemberInterface*> members = stats.Members(); |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 74 | EXPECT_EQ(members.size(), static_cast<size_t>(14)); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 75 | for (const RTCStatsMemberInterface* member : members) { |
| 76 | EXPECT_FALSE(member->is_defined()); |
| 77 | } |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 78 | stats.m_bool = true; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 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; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 84 | stats.m_string = std::string("123"); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 85 | |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 86 | std::vector<bool> sequence_bool; |
| 87 | sequence_bool.push_back(true); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 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); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 98 | std::vector<std::string> sequence_string; |
hbos | 8faf9e0 | 2016-09-15 06:52:43 -0700 | [diff] [blame] | 99 | sequence_string.push_back(std::string("six")); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 100 | |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 101 | stats.m_sequence_bool = sequence_bool; |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 102 | stats.m_sequence_int32 = sequence_int32; |
| 103 | stats.m_sequence_uint32 = sequence_uint32; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 104 | EXPECT_FALSE(stats.m_sequence_int64.is_defined()); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 105 | stats.m_sequence_int64 = sequence_int64; |
| 106 | stats.m_sequence_uint64 = sequence_uint64; |
| 107 | stats.m_sequence_double = sequence_double; |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 108 | stats.m_sequence_string = sequence_string; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 109 | for (const RTCStatsMemberInterface* member : members) { |
| 110 | EXPECT_TRUE(member->is_defined()); |
| 111 | } |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 112 | EXPECT_EQ(*stats.m_bool, true); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 113 | EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123)); |
| 114 | EXPECT_EQ(*stats.m_uint32, static_cast<uint32_t>(123)); |
| 115 | EXPECT_EQ(*stats.m_int64, static_cast<int64_t>(123)); |
| 116 | EXPECT_EQ(*stats.m_uint64, static_cast<uint64_t>(123)); |
| 117 | EXPECT_EQ(*stats.m_double, 123.0); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 118 | EXPECT_EQ(*stats.m_string, std::string("123")); |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 119 | EXPECT_EQ(*stats.m_sequence_bool, sequence_bool); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 120 | EXPECT_EQ(*stats.m_sequence_int32, sequence_int32); |
| 121 | EXPECT_EQ(*stats.m_sequence_uint32, sequence_uint32); |
| 122 | EXPECT_EQ(*stats.m_sequence_int64, sequence_int64); |
| 123 | EXPECT_EQ(*stats.m_sequence_uint64, sequence_uint64); |
| 124 | EXPECT_EQ(*stats.m_sequence_double, sequence_double); |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 125 | EXPECT_EQ(*stats.m_sequence_string, sequence_string); |
| 126 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 127 | int32_t numbers[] = {4, 8, 15, 16, 23, 42}; |
hbos | fdafab8 | 2016-09-14 06:02:13 -0700 | [diff] [blame] | 128 | std::vector<int32_t> numbers_sequence(&numbers[0], &numbers[6]); |
| 129 | stats.m_sequence_int32->clear(); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 130 | stats.m_sequence_int32->insert(stats.m_sequence_int32->end(), |
| 131 | numbers_sequence.begin(), |
| 132 | numbers_sequence.end()); |
| 133 | EXPECT_EQ(*stats.m_sequence_int32, numbers_sequence); |
| 134 | } |
| 135 | |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 136 | TEST(RTCStatsTest, EqualityOperator) { |
| 137 | RTCTestStats empty_stats("testId", 123); |
| 138 | EXPECT_EQ(empty_stats, empty_stats); |
| 139 | |
| 140 | RTCTestStats stats_with_all_values = empty_stats; |
| 141 | stats_with_all_values.m_bool = true; |
| 142 | stats_with_all_values.m_int32 = 123; |
| 143 | stats_with_all_values.m_uint32 = 123; |
| 144 | stats_with_all_values.m_int64 = 123; |
| 145 | stats_with_all_values.m_uint64 = 123; |
| 146 | stats_with_all_values.m_double = 123.0; |
| 147 | stats_with_all_values.m_string = "123"; |
| 148 | stats_with_all_values.m_sequence_bool = std::vector<bool>(); |
| 149 | stats_with_all_values.m_sequence_int32 = std::vector<int32_t>(); |
| 150 | stats_with_all_values.m_sequence_uint32 = std::vector<uint32_t>(); |
| 151 | stats_with_all_values.m_sequence_int64 = std::vector<int64_t>(); |
| 152 | stats_with_all_values.m_sequence_uint64 = std::vector<uint64_t>(); |
| 153 | stats_with_all_values.m_sequence_double = std::vector<double>(); |
| 154 | stats_with_all_values.m_sequence_string = std::vector<std::string>(); |
| 155 | EXPECT_NE(stats_with_all_values, empty_stats); |
| 156 | EXPECT_EQ(stats_with_all_values, stats_with_all_values); |
| 157 | EXPECT_NE(stats_with_all_values.m_int32, stats_with_all_values.m_uint32); |
| 158 | |
| 159 | RTCTestStats one_member_different[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 160 | stats_with_all_values, stats_with_all_values, stats_with_all_values, |
| 161 | stats_with_all_values, stats_with_all_values, stats_with_all_values, |
| 162 | stats_with_all_values, stats_with_all_values, stats_with_all_values, |
| 163 | stats_with_all_values, stats_with_all_values, stats_with_all_values, |
| 164 | stats_with_all_values, stats_with_all_values, |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 165 | }; |
| 166 | for (size_t i = 0; i < 14; ++i) { |
| 167 | EXPECT_EQ(stats_with_all_values, one_member_different[i]); |
| 168 | } |
| 169 | one_member_different[0].m_bool = false; |
| 170 | one_member_different[1].m_int32 = 321; |
| 171 | one_member_different[2].m_uint32 = 321; |
| 172 | one_member_different[3].m_int64 = 321; |
| 173 | one_member_different[4].m_uint64 = 321; |
| 174 | one_member_different[5].m_double = 321.0; |
| 175 | one_member_different[6].m_string = "321"; |
| 176 | one_member_different[7].m_sequence_bool->push_back(false); |
| 177 | one_member_different[8].m_sequence_int32->push_back(321); |
| 178 | one_member_different[9].m_sequence_uint32->push_back(321); |
| 179 | one_member_different[10].m_sequence_int64->push_back(321); |
| 180 | one_member_different[11].m_sequence_uint64->push_back(321); |
| 181 | one_member_different[12].m_sequence_double->push_back(321.0); |
| 182 | one_member_different[13].m_sequence_string->push_back("321"); |
| 183 | for (size_t i = 0; i < 14; ++i) { |
| 184 | EXPECT_NE(stats_with_all_values, one_member_different[i]); |
| 185 | } |
| 186 | |
| 187 | RTCTestStats empty_stats_different_id("testId2", 123); |
| 188 | EXPECT_NE(empty_stats, empty_stats_different_id); |
| 189 | RTCTestStats empty_stats_different_timestamp("testId", 321); |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 190 | EXPECT_EQ(empty_stats, empty_stats_different_timestamp); |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 191 | |
| 192 | RTCChildStats child("childId", 42); |
| 193 | RTCGrandChildStats grandchild("grandchildId", 42); |
| 194 | EXPECT_NE(child, grandchild); |
hbos | 2874796 | 2016-11-21 09:17:41 -0800 | [diff] [blame] | 195 | |
| 196 | RTCChildStats stats_with_defined_member("leId", 0); |
| 197 | stats_with_defined_member.child_int = 0; |
| 198 | RTCChildStats stats_with_undefined_member("leId", 0); |
| 199 | EXPECT_NE(stats_with_defined_member, stats_with_undefined_member); |
| 200 | EXPECT_NE(stats_with_undefined_member, stats_with_defined_member); |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 201 | } |
| 202 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 203 | TEST(RTCStatsTest, RTCStatsGrandChild) { |
| 204 | RTCGrandChildStats stats("grandchild", 0.0); |
| 205 | stats.child_int = 1; |
| 206 | stats.grandchild_int = 2; |
| 207 | int32_t sum = 0; |
| 208 | for (const RTCStatsMemberInterface* member : stats.Members()) { |
| 209 | sum += *member->cast_to<const RTCStatsMember<int32_t>>(); |
| 210 | } |
| 211 | EXPECT_EQ(sum, static_cast<int32_t>(3)); |
| 212 | |
| 213 | std::unique_ptr<RTCStats> copy_ptr = stats.copy(); |
| 214 | const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>(); |
| 215 | EXPECT_EQ(*copy.child_int, *stats.child_int); |
| 216 | EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int); |
| 217 | } |
| 218 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 219 | TEST(RTCStatsTest, RTCStatsPrintsValidJson) { |
| 220 | std::string id = "statsId"; |
| 221 | int timestamp = 42; |
| 222 | bool m_bool = true; |
| 223 | int m_int32 = 123; |
| 224 | int64_t m_int64 = 1234567890123456499L; |
| 225 | double m_double = 123.4567890123456499; |
| 226 | std::string m_string = "123"; |
| 227 | |
| 228 | std::vector<bool> sequence_bool; |
| 229 | std::vector<int32_t> sequence_int32; |
| 230 | sequence_int32.push_back(static_cast<int32_t>(1)); |
| 231 | std::vector<int64_t> sequence_int64; |
| 232 | sequence_int64.push_back(static_cast<int64_t>(-1234567890123456499L)); |
| 233 | sequence_int64.push_back(static_cast<int64_t>(1)); |
| 234 | sequence_int64.push_back(static_cast<int64_t>(1234567890123456499L)); |
| 235 | std::vector<double> sequence_double; |
| 236 | sequence_double.push_back(123.4567890123456499); |
| 237 | sequence_double.push_back(1234567890123.456499); |
| 238 | std::vector<std::string> sequence_string; |
| 239 | sequence_string.push_back(std::string("four")); |
| 240 | |
| 241 | RTCTestStats stats(id, timestamp); |
| 242 | stats.m_bool = m_bool; |
| 243 | stats.m_int32 = m_int32; |
| 244 | stats.m_int64 = m_int64; |
| 245 | stats.m_double = m_double; |
| 246 | stats.m_string = m_string; |
| 247 | stats.m_sequence_bool = sequence_bool; |
| 248 | stats.m_sequence_int32 = sequence_int32; |
| 249 | stats.m_sequence_int64 = sequence_int64; |
| 250 | stats.m_sequence_double = sequence_double; |
| 251 | stats.m_sequence_string = sequence_string; |
| 252 | |
| 253 | Json::Value json_output; |
| 254 | EXPECT_TRUE(Json::Reader().parse(stats.ToJson(), json_output)); |
| 255 | |
| 256 | EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id)); |
| 257 | EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", ×tamp)); |
| 258 | EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool)); |
| 259 | EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32)); |
| 260 | EXPECT_TRUE(rtc::GetDoubleFromJsonObject(json_output, "mDouble", &m_double)); |
| 261 | EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string)); |
| 262 | |
| 263 | Json::Value json_array; |
| 264 | |
| 265 | EXPECT_TRUE( |
| 266 | rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array)); |
| 267 | EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool)); |
| 268 | |
| 269 | EXPECT_TRUE( |
| 270 | rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array)); |
| 271 | EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32)); |
| 272 | |
| 273 | EXPECT_TRUE( |
| 274 | rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array)); |
| 275 | EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double)); |
| 276 | |
| 277 | EXPECT_TRUE( |
| 278 | rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array)); |
| 279 | EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string)); |
| 280 | |
| 281 | EXPECT_EQ(id, stats.id()); |
| 282 | EXPECT_EQ(timestamp, stats.timestamp_us()); |
| 283 | EXPECT_EQ(m_bool, *stats.m_bool); |
| 284 | EXPECT_EQ(m_int32, *stats.m_int32); |
| 285 | EXPECT_EQ(m_string, *stats.m_string); |
| 286 | EXPECT_EQ(sequence_bool, *stats.m_sequence_bool); |
| 287 | EXPECT_EQ(sequence_int32, *stats.m_sequence_int32); |
| 288 | EXPECT_EQ(sequence_string, *stats.m_sequence_string); |
| 289 | |
| 290 | EXPECT_NEAR(m_double, *stats.m_double, GetExpectedError(*stats.m_double)); |
| 291 | |
| 292 | EXPECT_EQ(sequence_double.size(), stats.m_sequence_double->size()); |
| 293 | for (size_t i = 0; i < stats.m_sequence_double->size(); ++i) { |
| 294 | EXPECT_NEAR(sequence_double[i], stats.m_sequence_double->at(i), |
| 295 | GetExpectedError(stats.m_sequence_double->at(i))); |
| 296 | } |
| 297 | |
| 298 | // We read mInt64 as double since JSON stores all numbers as doubles, so there |
| 299 | // is not enough precision to represent large numbers. |
| 300 | double m_int64_as_double; |
| 301 | std::vector<double> sequence_int64_as_double; |
| 302 | |
| 303 | EXPECT_TRUE( |
| 304 | rtc::GetDoubleFromJsonObject(json_output, "mInt64", &m_int64_as_double)); |
| 305 | |
| 306 | EXPECT_TRUE( |
| 307 | rtc::GetValueFromJsonObject(json_output, "mSequenceInt64", &json_array)); |
| 308 | EXPECT_TRUE( |
| 309 | rtc::JsonArrayToDoubleVector(json_array, &sequence_int64_as_double)); |
| 310 | |
| 311 | double stats_m_int64_as_double = static_cast<double>(*stats.m_int64); |
| 312 | EXPECT_NEAR(m_int64_as_double, stats_m_int64_as_double, |
| 313 | GetExpectedError(stats_m_int64_as_double)); |
| 314 | |
| 315 | EXPECT_EQ(sequence_int64_as_double.size(), stats.m_sequence_int64->size()); |
| 316 | for (size_t i = 0; i < stats.m_sequence_int64->size(); ++i) { |
| 317 | const double stats_value_as_double = |
| 318 | static_cast<double>((*stats.m_sequence_int64)[i]); |
| 319 | EXPECT_NEAR(sequence_int64_as_double[i], stats_value_as_double, |
| 320 | GetExpectedError(stats_value_as_double)); |
| 321 | } |
| 322 | |
| 323 | // Neither stats.m_uint32 nor stats.m_uint64 are defined, so "mUint64" and |
| 324 | // "mUint32" should not be part of the generated JSON object. |
| 325 | int m_uint32; |
| 326 | int m_uint64; |
| 327 | EXPECT_FALSE(stats.m_uint32.is_defined()); |
| 328 | EXPECT_FALSE(stats.m_uint64.is_defined()); |
| 329 | EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint32", &m_uint32)); |
| 330 | EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint64", &m_uint64)); |
| 331 | |
| 332 | std::cout << stats.ToJson() << std::endl; |
| 333 | } |
| 334 | |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 335 | TEST(RTCStatsTest, IsStandardized) { |
| 336 | RTCStatsMember<int32_t> standardized("standardized"); |
| 337 | RTCNonStandardStatsMember<int32_t> unstandardized("unstandardized"); |
| 338 | EXPECT_TRUE(standardized.is_standardized()); |
| 339 | EXPECT_FALSE(unstandardized.is_standardized()); |
| 340 | } |
| 341 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 342 | // Death tests. |
| 343 | // Disabled on Android because death tests misbehave on Android, see |
| 344 | // base/test/gtest_util.h. |
| 345 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 346 | |
| 347 | TEST(RTCStatsDeathTest, ValueOfUndefinedMember) { |
| 348 | RTCTestStats stats("testId", 0.0); |
| 349 | EXPECT_FALSE(stats.m_int32.is_defined()); |
| 350 | EXPECT_DEATH(*stats.m_int32, ""); |
| 351 | } |
| 352 | |
| 353 | TEST(RTCStatsDeathTest, InvalidCasting) { |
| 354 | RTCGrandChildStats stats("grandchild", 0.0); |
| 355 | EXPECT_DEATH(stats.cast_to<RTCChildStats>(), ""); |
| 356 | } |
| 357 | |
| 358 | #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 359 | |
| 360 | } // namespace webrtc |