xref: /llvm-project/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp (revision 129c056d626986eb95f9724283b8f6106cdde2e9)
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 
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 
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:            SFI0
44     Size:            0
45   - Name:            ISG1
46     Size:            0
47   - Name:            OSG1
48     Size:            0
49   - Name:            PSV0
50     Size:            0
51   - Name:            STAT
52     Size:            0
53   - Name:            DXIL
54     Size:            0
55   - Name:            DEAD
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       0x53, 0x46, 0x49, 0x30, 0x00, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x31,
68       0x00, 0x00, 0x00, 0x00, 0x4F, 0x53, 0x47, 0x31, 0x00, 0x00, 0x00, 0x00,
69       0x50, 0x53, 0x56, 0x30, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x41, 0x54,
70       0x00, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x00, 0x00, 0x00, 0x00,
71       0x44, 0x45, 0x41, 0x44, 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:            SFI0
91     Size:            0
92   - Name:            ISG1
93     Size:            0
94   - Name:            OSG1
95     Size:            0
96   - Name:            PSV0
97     Size:            0
98   - Name:            STAT
99     Size:            0
100   - Name:            DXIL
101     Size:            0
102   - Name:            DEAD
103     Size:            0
104 ...
105   )"));
106 
107   EXPECT_EQ(Storage.size(), 116u);
108   EXPECT_TRUE(memcmp(Buffer, Storage.data(), 116) == 0);
109 }
110