1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/has_bits.h"
9
10 #include <gmock/gmock.h>
11 #include <gtest/gtest.h>
12
13 namespace google {
14 namespace protobuf {
15 namespace internal {
16 namespace {
17
18 using ::testing::Eq;
19
20 template <int n>
TestDefaultInit()21 void TestDefaultInit() {
22 HasBits<n> bits;
23 EXPECT_TRUE(bits.empty());
24 for (int i = 0; i < n; ++i) {
25 EXPECT_THAT(bits[i], Eq(0));
26 }
27 }
28
TEST(HasBits,DefaultInit)29 TEST(HasBits, DefaultInit) {
30 TestDefaultInit<1>();
31 TestDefaultInit<2>();
32 TestDefaultInit<3>();
33 TestDefaultInit<4>();
34 }
35
TEST(HasBits,ValueInit)36 TEST(HasBits, ValueInit) {
37 {
38 HasBits<4> bits;
39 EXPECT_TRUE(bits.empty());
40 }
41 {
42 HasBits<4> bits({});
43 EXPECT_TRUE(bits.empty());
44 }
45 {
46 HasBits<4> bits({1});
47 EXPECT_FALSE(bits.empty());
48 EXPECT_THAT(bits[0], Eq(1));
49 }
50 {
51 HasBits<4> bits({1, 2, 3, 4});
52 EXPECT_FALSE(bits.empty());
53 EXPECT_THAT(bits[0], Eq(1));
54 EXPECT_THAT(bits[1], Eq(2));
55 EXPECT_THAT(bits[2], Eq(3));
56 EXPECT_THAT(bits[3], Eq(4));
57 }
58 }
59
TEST(HasBits,ConstexprValueInit)60 TEST(HasBits, ConstexprValueInit) {
61 {
62 constexpr HasBits<4> bits;
63 EXPECT_TRUE(bits.empty());
64 }
65 {
66 constexpr HasBits<4> bits({});
67 EXPECT_TRUE(bits.empty());
68 }
69 {
70 constexpr HasBits<4> bits({1});
71 EXPECT_FALSE(bits.empty());
72 EXPECT_THAT(bits[0], Eq(1));
73 }
74 {
75 constexpr HasBits<4> bits({1, 2, 3, 4});
76 EXPECT_FALSE(bits.empty());
77 EXPECT_THAT(bits[0], Eq(1));
78 EXPECT_THAT(bits[1], Eq(2));
79 EXPECT_THAT(bits[2], Eq(3));
80 EXPECT_THAT(bits[3], Eq(4));
81 }
82 }
83
TEST(HasBits,operator_equal)84 TEST(HasBits, operator_equal) {
85 EXPECT_FALSE(HasBits<4>({1, 2, 3, 4}) == HasBits<4>({0, 2, 3, 4}));
86 EXPECT_FALSE(HasBits<4>({1, 2, 3, 4}) == HasBits<4>({1, 0, 3, 4}));
87 EXPECT_FALSE(HasBits<4>({1, 2, 3, 4}) == HasBits<4>({1, 2, 0, 4}));
88 EXPECT_FALSE(HasBits<4>({1, 2, 3, 4}) == HasBits<4>({1, 2, 3, 0}));
89 EXPECT_TRUE(HasBits<4>({1, 2, 3, 4}) == HasBits<4>({1, 2, 3, 4}));
90 }
91
TEST(HasBits,Or)92 TEST(HasBits, Or) {
93 HasBits<4> bits1({1, 2, 4, 8});
94 HasBits<4> bits2({16, 32, 64, 128});
95 bits1.Or(bits2);
96 EXPECT_TRUE(bits1 == HasBits<4>({17, 34, 68, 136}));
97 }
98
TEST(HasBits,Copy)99 TEST(HasBits, Copy) {
100 HasBits<4> bits1({1, 2, 4, 8});
101 HasBits<4> bits2(bits1);
102 EXPECT_TRUE(bits1 == bits2);
103 }
104
105 } // namespace
106 } // namespace internal
107 } // namespace protobuf
108 } // namespace google
109