xref: /aosp_15_r20/external/emboss/compiler/back_end/cpp/testcode/anonymous_bits_test.cc (revision 99e0aae7469b87d12f0ad23e61142c2d74c1ef70)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Tests for generated code for anonymous "bits" types, using
16 // anonymous_bits.emb.
17 #include <stdint.h>
18 
19 #include <vector>
20 
21 #include "gtest/gtest.h"
22 #include "runtime/cpp/emboss_cpp_util.h"
23 #include "testdata/anonymous_bits.emb.h"
24 
25 namespace emboss {
26 namespace test {
27 namespace bits {
28 namespace {
29 
TEST(AnonymousBits,InnerEnumIsVisibleAtOuterScope)30 TEST(AnonymousBits, InnerEnumIsVisibleAtOuterScope) {
31   EXPECT_EQ(static_cast<Foo::Bar>(0), Foo::Bar::BAR);
32 }
33 
TEST(AnonymousBits,BitsAreReadable)34 TEST(AnonymousBits, BitsAreReadable) {
35   alignas(8)::std::uint8_t data[] = {0x01, 0x00, 0x00, 0x80,
36                                      0x01, 0x00, 0x80, 0x00};
37   EXPECT_FALSE((FooWriter{data, sizeof data - 1}.Ok()));
38 
39   auto foo = MakeAlignedFooView</**/ ::std::uint8_t, 8>(data, sizeof data);
40   ASSERT_TRUE(foo.Ok());
41   EXPECT_TRUE(foo.high_bit().Read());
42   EXPECT_TRUE(foo.first_bit().Read());
43   EXPECT_TRUE(foo.bit_23().Read());
44   EXPECT_TRUE(foo.low_bit().Read());
45   foo.first_bit().Write(false);
46   EXPECT_EQ(0, data[0]);
47   foo.bit_23().Write(false);
48   EXPECT_EQ(0, data[6]);
49 }
50 
TEST(AnonymousBits,Equals)51 TEST(AnonymousBits, Equals) {
52   alignas(8)::std::uint8_t buf_x[] = {0x01, 0x00, 0x00, 0x80,
53                                       0x01, 0x00, 0x80, 0x00};
54   alignas(8)::std::uint8_t buf_y[] = {0x01, 0x00, 0x00, 0x80,
55                                       0x01, 0x00, 0x80, 0x00};
56 
57   auto x = MakeAlignedFooView</**/ ::std::uint8_t, 8>(buf_x, sizeof buf_x);
58   auto x_const =
59       MakeFooView(static_cast<const ::std::uint8_t *>(buf_x), sizeof buf_x);
60   auto y = MakeAlignedFooView</**/ ::std::uint8_t, 8>(buf_y, sizeof buf_y);
61 
62   EXPECT_TRUE(x.Equals(x));
63   EXPECT_TRUE(x.UncheckedEquals(x));
64   EXPECT_TRUE(y.Equals(y));
65   EXPECT_TRUE(y.UncheckedEquals(y));
66 
67   EXPECT_TRUE(x.Equals(y));
68   EXPECT_TRUE(x.UncheckedEquals(y));
69   EXPECT_TRUE(y.Equals(x));
70   EXPECT_TRUE(y.UncheckedEquals(x));
71 
72   EXPECT_TRUE(x_const.Equals(y));
73   EXPECT_TRUE(x_const.UncheckedEquals(y));
74   EXPECT_TRUE(y.Equals(x_const));
75   EXPECT_TRUE(y.UncheckedEquals(x_const));
76 
77   // Changing the second byte of buf_y should have no effect on equality.
78   ++buf_y[1];
79   EXPECT_NE(buf_x, buf_y);
80   EXPECT_TRUE(x.Equals(y));
81   EXPECT_TRUE(x.UncheckedEquals(y));
82   EXPECT_TRUE(y.Equals(x));
83   EXPECT_TRUE(y.UncheckedEquals(x));
84 
85   ++buf_y[0];
86   EXPECT_FALSE(x.Equals(y));
87   EXPECT_FALSE(x.UncheckedEquals(y));
88   EXPECT_FALSE(y.Equals(x));
89   EXPECT_FALSE(y.UncheckedEquals(x));
90 }
91 
TEST(AnonymousBits,WriteToString)92 TEST(AnonymousBits, WriteToString) {
93   const ::std::uint8_t data[] = {0x01, 0x00, 0x00, 0x80,
94                                  0x01, 0x00, 0x80, 0x00};
95   auto foo = MakeFooView(data, sizeof data);
96   ASSERT_TRUE(foo.Ok());
97   EXPECT_EQ(
98       "{ high_bit: true, bar: BAR, first_bit: true, bit_23: true, low_bit: "
99       "true }",
100       ::emboss::WriteToString(foo));
101 }
102 
TEST(AnonymousBits,ReadFromString)103 TEST(AnonymousBits, ReadFromString) {
104   const ::std::uint8_t data[] = {0x01, 0x00, 0x00, 0x80,
105                                  0x01, 0x00, 0x80, 0x00};
106   ::std::uint8_t data2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
107   auto foo = MakeFooView(data, sizeof data);
108   auto foo_writer = MakeFooView(data2, sizeof data2);
109   ASSERT_TRUE(foo.Ok());
110   ASSERT_TRUE(foo_writer.Ok());
111   ::emboss::UpdateFromText(foo_writer, ::emboss::WriteToString(foo));
112   EXPECT_EQ(::std::vector</**/ ::std::uint8_t>(data, data + sizeof data),
113             ::std::vector</**/ ::std::uint8_t>(data2, data2 + sizeof data2));
114 }
115 
116 }  // namespace
117 }  // namespace bits
118 }  // namespace test
119 }  // namespace emboss
120