xref: /llvm-project/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp (revision 76fca14750e3ca536e9b65a99ed55730cd704403)
1 //===- DXContainerTest.cpp - Tests for DXContainerFile --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ObjectYAML/ObjectYAML.h"
12 #include "llvm/ObjectYAML/yaml2obj.h"
13 #include "llvm/Support/MemoryBufferRef.h"
14 #include "llvm/Support/YAMLTraits.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Testing/Support/Error.h"
17 #include "gtest/gtest.h"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 
convert(SmallVectorImpl<char> & Output,const char * YAML)22 static bool convert(SmallVectorImpl<char> &Output, const char *YAML) {
23   raw_svector_ostream OS(Output);
24   yaml::Input YIn(YAML);
25   return convertYAML(YIn, OS, [](const Twine &Err) { errs() << Err; });
26 }
27 
TEST(DXCFile,ParseEmptyParts)28 TEST(DXCFile, ParseEmptyParts) {
29   SmallString<128> Storage;
30 
31   // First read a fully explicit yaml with all sizes and offsets provided
32   ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
33 Header:
34   Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
35                      0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
36   Version:
37     Major:           1
38     Minor:           0
39   FileSize:        116
40   PartCount:       7
41   PartOffsets:     [ 60, 68, 76, 84, 92, 100, 108 ]
42 Parts:
43   - Name:            FKE0
44     Size:            0
45   - Name:            FKE1
46     Size:            0
47   - Name:            FKE2
48     Size:            0
49   - Name:            FKE3
50     Size:            0
51   - Name:            FKE4
52     Size:            0
53   - Name:            FKE5
54     Size:            0
55   - Name:            FKE6
56     Size:            0
57 ...
58   )"));
59 
60   // Result
61   char Buffer[] = {
62       0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
64       0x74, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00,
65       0x44, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
66       0x5C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00,
67       0x46, 0x4B, 0x45, 0x30, 0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x31,
68       0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x32, 0x00, 0x00, 0x00, 0x00,
69       0x46, 0x4B, 0x45, 0x33, 0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x34,
70       0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x35, 0x00, 0x00, 0x00, 0x00,
71       0x46, 0x4B, 0x45, 0x36, 0x00, 0x00, 0x00, 0x00,
72   };
73 
74   EXPECT_EQ(Storage.size(), 116u);
75   EXPECT_TRUE(memcmp(Buffer, Storage.data(), 116) == 0);
76 
77   Storage.clear();
78 
79   // Next, read the same file without the part offsets or file size. Both cases
80   // should result in the same final output.
81   ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
82 Header:
83   Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
84                      0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
85   Version:
86     Major:           1
87     Minor:           0
88   PartCount:       7
89 Parts:
90   - Name:            FKE0
91     Size:            0
92   - Name:            FKE1
93     Size:            0
94   - Name:            FKE2
95     Size:            0
96   - Name:            FKE3
97     Size:            0
98   - Name:            FKE4
99     Size:            0
100   - Name:            FKE5
101     Size:            0
102   - Name:            FKE6
103     Size:            0
104 ...
105   )"));
106 
107   EXPECT_EQ(Storage.size(), 116u);
108   EXPECT_TRUE(memcmp(Buffer, Storage.data(), 116) == 0);
109 }
110