xref: /aosp_15_r20/external/webrtc/logging/rtc_event_log/events/rtc_event_field_extraction_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*   Copyright (c) 2021 The WebRTC Project Authors. All rights reserved.
2  *
3  *  Use of this source code is governed by a BSD-style license
4  *  that can be found in the LICENSE file in the root of the source
5  *  tree. An additional intellectual property rights grant can be found
6  *  in the file PATENTS.  All contributing project authors may
7  *  be found in the AUTHORS file in the root of the source tree.
8  */
9 
10 #include "logging/rtc_event_log/events/rtc_event_field_extraction.h"
11 
12 #include "rtc_base/random.h"
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 
TEST(UnsignedBitWidthTest,SmallValues)17 TEST(UnsignedBitWidthTest, SmallValues) {
18   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(0), 1u);
19   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(1), 1u);
20   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(2), 2u);
21   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(3), 2u);
22   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(4), 3u);
23   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(5), 3u);
24   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(6), 3u);
25   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(7), 3u);
26 }
27 
TEST(UnsignedBitWidthTest,PowersOfTwo)28 TEST(UnsignedBitWidthTest, PowersOfTwo) {
29   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(0), 1u);
30 
31   for (unsigned i = 0; i < 64; i++) {
32     uint64_t x = 1;
33     x = x << i;
34     EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), i + 1);
35   }
36 }
37 
TEST(UnsignedBitWidthTest,PowersOfTwoMinusOne)38 TEST(UnsignedBitWidthTest, PowersOfTwoMinusOne) {
39   for (unsigned i = 1; i < 64; i++) {
40     uint64_t x = 1;
41     x = (x << i) - 1;
42     EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), i);
43   }
44 
45   uint64_t x = ~static_cast<uint64_t>(0);
46   EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), 64u);
47 }
48 
TEST(UnsignedBitWidthTest,RandomInputs)49 TEST(UnsignedBitWidthTest, RandomInputs) {
50   Random rand(12345);
51 
52   for (unsigned i = 0; i < 64; i++) {
53     uint64_t x = 1;
54     x = x << i;
55     uint64_t high = rand.Rand<uint32_t>();
56     uint64_t low = rand.Rand<uint32_t>();
57     x += ((high << 32) + low) % x;
58     EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), i + 1);
59   }
60 }
61 
TEST(SignedBitWidthTest,SignedBitWidth)62 TEST(SignedBitWidthTest, SignedBitWidth) {
63   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(0, 1), 1u);
64   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 0), 2u);
65   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 2), 2u);
66   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 128), 8u);
67   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(127, 1), 8u);
68   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(127, 128), 8u);
69   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 129), 9u);
70   EXPECT_EQ(webrtc_event_logging::SignedBitWidth(128, 1), 9u);
71 }
72 
TEST(MaxUnsignedValueOfBitWidthTest,MaxUnsignedValueOfBitWidth)73 TEST(MaxUnsignedValueOfBitWidthTest, MaxUnsignedValueOfBitWidth) {
74   EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(1), 0x01u);
75   EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(6), 0x3Fu);
76   EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(8), 0xFFu);
77   EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(32), 0xFFFFFFFFu);
78 }
79 
TEST(EncodeAsUnsignedTest,NegativeValues)80 TEST(EncodeAsUnsignedTest, NegativeValues) {
81   // Negative values are converted as if cast to unsigned type of
82   // the same bitsize using 2-complement representation.
83   int16_t x = -1;
84   EXPECT_EQ(EncodeAsUnsigned(x), static_cast<uint64_t>(0xFFFF));
85   int64_t y = -1;
86   EXPECT_EQ(EncodeAsUnsigned(y), static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFull));
87 }
88 
TEST(EncodeAsUnsignedTest,PositiveValues)89 TEST(EncodeAsUnsignedTest, PositiveValues) {
90   // Postive values are unchanged.
91   int16_t x = 42;
92   EXPECT_EQ(EncodeAsUnsigned(x), static_cast<uint64_t>(42));
93   int64_t y = 42;
94   EXPECT_EQ(EncodeAsUnsigned(y), static_cast<uint64_t>(42));
95 }
96 
97 }  // namespace webrtc
98