summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/logging/rtc_event_log/events/rtc_event_field_extraction_unittest.cc
blob: f9fb993af036eb2351dc31c59b13acaec2ced84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*   Copyright (c) 2021 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "logging/rtc_event_log/events/rtc_event_field_extraction.h"

#include "rtc_base/random.h"
#include "test/gtest.h"

namespace webrtc {

TEST(UnsignedBitWidthTest, SmallValues) {
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(0), 1u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(1), 1u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(2), 2u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(3), 2u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(4), 3u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(5), 3u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(6), 3u);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(7), 3u);
}

TEST(UnsignedBitWidthTest, PowersOfTwo) {
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(0), 1u);

  for (unsigned i = 0; i < 64; i++) {
    uint64_t x = 1;
    x = x << i;
    EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), i + 1);
  }
}

TEST(UnsignedBitWidthTest, PowersOfTwoMinusOne) {
  for (unsigned i = 1; i < 64; i++) {
    uint64_t x = 1;
    x = (x << i) - 1;
    EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), i);
  }

  uint64_t x = ~static_cast<uint64_t>(0);
  EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), 64u);
}

TEST(UnsignedBitWidthTest, RandomInputs) {
  Random rand(12345);

  for (unsigned i = 0; i < 64; i++) {
    uint64_t x = 1;
    x = x << i;
    uint64_t high = rand.Rand<uint32_t>();
    uint64_t low = rand.Rand<uint32_t>();
    x += ((high << 32) + low) % x;
    EXPECT_EQ(webrtc_event_logging::UnsignedBitWidth(x), i + 1);
  }
}

TEST(SignedBitWidthTest, SignedBitWidth) {
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(0, 1), 1u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 0), 2u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 2), 2u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 128), 8u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(127, 1), 8u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(127, 128), 8u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(1, 129), 9u);
  EXPECT_EQ(webrtc_event_logging::SignedBitWidth(128, 1), 9u);
}

TEST(MaxUnsignedValueOfBitWidthTest, MaxUnsignedValueOfBitWidth) {
  EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(1), 0x01u);
  EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(6), 0x3Fu);
  EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(8), 0xFFu);
  EXPECT_EQ(webrtc_event_logging::MaxUnsignedValueOfBitWidth(32), 0xFFFFFFFFu);
}

TEST(EncodeAsUnsignedTest, NegativeValues) {
  // Negative values are converted as if cast to unsigned type of
  // the same bitsize using 2-complement representation.
  int16_t x = -1;
  EXPECT_EQ(EncodeAsUnsigned(x), static_cast<uint64_t>(0xFFFF));
  int64_t y = -1;
  EXPECT_EQ(EncodeAsUnsigned(y), static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFull));
}

TEST(EncodeAsUnsignedTest, PositiveValues) {
  // Postive values are unchanged.
  int16_t x = 42;
  EXPECT_EQ(EncodeAsUnsigned(x), static_cast<uint64_t>(42));
  int64_t y = 42;
  EXPECT_EQ(EncodeAsUnsigned(y), static_cast<uint64_t>(42));
}

}  // namespace webrtc