xref: /aosp_15_r20/external/emboss/compiler/back_end/cpp/testcode/next_keyword_test.cc (revision 99e0aae7469b87d12f0ad23e61142c2d74c1ef70)
1 // Copyright 2022 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 of generated code for structures using the `$next` keyword.
16 // Note that `$next` is removed from the IR before it reaches the back end, so
17 // this is really testing that the front end desugared correctly.
18 #include <stdint.h>
19 
20 #include <type_traits>
21 #include <utility>
22 #include <vector>
23 
24 #include "gtest/gtest.h"
25 #include "testdata/next_keyword.emb.h"
26 
27 namespace emboss {
28 namespace test {
29 namespace {
30 
31 // For reference in the code below, the NextKeyword structure is defined as:
32 //
33 // [$default byte_order: "LittleEndian"]
34 // struct NextKeyword:
35 //   0       [+4]  UInt  value32
36 //   $next   [+2]  UInt  value16
37 //   $next   [+1]  UInt  value8
38 //   $next+3 [+1]  UInt  value8_offset
TEST(NextKeyword,FieldsAreCorrectlyLocated)39 TEST(NextKeyword, FieldsAreCorrectlyLocated) {
40   ::std::array<char, NextKeyword::IntrinsicSizeInBytes()> values = {
41     1, 0, 0, 0,
42     2, 0,
43     3,
44     5, 6, 7, 4,
45   };
46   const auto view = MakeNextKeywordView(&values);
47   ASSERT_TRUE(view.Ok());
48   EXPECT_EQ(1, view.value32().Read());
49   EXPECT_EQ(2, view.value16().Read());
50   EXPECT_EQ(3, view.value8().Read());
51   EXPECT_EQ(4, view.value8_offset().Read());
52 }
53 
54 }  // namespace
55 }  // namespace test
56 }  // namespace emboss
57