1129c056dSChris Bieneman //===- DXContainerTest.cpp - Tests for DXContainerFile --------------------===//
2129c056dSChris Bieneman //
3129c056dSChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4129c056dSChris Bieneman // See https://llvm.org/LICENSE.txt for license information.
5129c056dSChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6129c056dSChris Bieneman //
7129c056dSChris Bieneman //===----------------------------------------------------------------------===//
8129c056dSChris Bieneman
9129c056dSChris Bieneman #include "llvm/ADT/StringRef.h"
10129c056dSChris Bieneman #include "llvm/ADT/Twine.h"
11129c056dSChris Bieneman #include "llvm/ObjectYAML/ObjectYAML.h"
12129c056dSChris Bieneman #include "llvm/ObjectYAML/yaml2obj.h"
13129c056dSChris Bieneman #include "llvm/Support/MemoryBufferRef.h"
14129c056dSChris Bieneman #include "llvm/Support/YAMLTraits.h"
15129c056dSChris Bieneman #include "llvm/Support/raw_ostream.h"
16129c056dSChris Bieneman #include "llvm/Testing/Support/Error.h"
17129c056dSChris Bieneman #include "gtest/gtest.h"
18129c056dSChris Bieneman
19129c056dSChris Bieneman using namespace llvm;
20129c056dSChris Bieneman using namespace llvm::object;
21129c056dSChris Bieneman
convert(SmallVectorImpl<char> & Output,const char * YAML)22129c056dSChris Bieneman static bool convert(SmallVectorImpl<char> &Output, const char *YAML) {
23129c056dSChris Bieneman raw_svector_ostream OS(Output);
24129c056dSChris Bieneman yaml::Input YIn(YAML);
25129c056dSChris Bieneman return convertYAML(YIn, OS, [](const Twine &Err) { errs() << Err; });
26129c056dSChris Bieneman }
27129c056dSChris Bieneman
TEST(DXCFile,ParseEmptyParts)28129c056dSChris Bieneman TEST(DXCFile, ParseEmptyParts) {
29129c056dSChris Bieneman SmallString<128> Storage;
30129c056dSChris Bieneman
31129c056dSChris Bieneman // First read a fully explicit yaml with all sizes and offsets provided
32129c056dSChris Bieneman ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
33129c056dSChris Bieneman Header:
34129c056dSChris Bieneman Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
35129c056dSChris Bieneman 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
36129c056dSChris Bieneman Version:
37129c056dSChris Bieneman Major: 1
38129c056dSChris Bieneman Minor: 0
39129c056dSChris Bieneman FileSize: 116
40129c056dSChris Bieneman PartCount: 7
41129c056dSChris Bieneman PartOffsets: [ 60, 68, 76, 84, 92, 100, 108 ]
42129c056dSChris Bieneman Parts:
43*76fca147SChris Bieneman - Name: FKE0
44129c056dSChris Bieneman Size: 0
45*76fca147SChris Bieneman - Name: FKE1
46129c056dSChris Bieneman Size: 0
47*76fca147SChris Bieneman - Name: FKE2
48129c056dSChris Bieneman Size: 0
49*76fca147SChris Bieneman - Name: FKE3
50129c056dSChris Bieneman Size: 0
51*76fca147SChris Bieneman - Name: FKE4
52129c056dSChris Bieneman Size: 0
53*76fca147SChris Bieneman - Name: FKE5
54129c056dSChris Bieneman Size: 0
55*76fca147SChris Bieneman - Name: FKE6
56129c056dSChris Bieneman Size: 0
57129c056dSChris Bieneman ...
58129c056dSChris Bieneman )"));
59129c056dSChris Bieneman
60129c056dSChris Bieneman // Result
61129c056dSChris Bieneman char Buffer[] = {
62129c056dSChris Bieneman 0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63129c056dSChris Bieneman 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
64129c056dSChris Bieneman 0x74, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00,
65129c056dSChris Bieneman 0x44, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
66129c056dSChris Bieneman 0x5C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00,
67*76fca147SChris Bieneman 0x46, 0x4B, 0x45, 0x30, 0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x31,
68*76fca147SChris Bieneman 0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x32, 0x00, 0x00, 0x00, 0x00,
69*76fca147SChris Bieneman 0x46, 0x4B, 0x45, 0x33, 0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x34,
70*76fca147SChris Bieneman 0x00, 0x00, 0x00, 0x00, 0x46, 0x4B, 0x45, 0x35, 0x00, 0x00, 0x00, 0x00,
71*76fca147SChris Bieneman 0x46, 0x4B, 0x45, 0x36, 0x00, 0x00, 0x00, 0x00,
72129c056dSChris Bieneman };
73129c056dSChris Bieneman
74129c056dSChris Bieneman EXPECT_EQ(Storage.size(), 116u);
75129c056dSChris Bieneman EXPECT_TRUE(memcmp(Buffer, Storage.data(), 116) == 0);
76129c056dSChris Bieneman
77129c056dSChris Bieneman Storage.clear();
78129c056dSChris Bieneman
79129c056dSChris Bieneman // Next, read the same file without the part offsets or file size. Both cases
80129c056dSChris Bieneman // should result in the same final output.
81129c056dSChris Bieneman ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
82129c056dSChris Bieneman Header:
83129c056dSChris Bieneman Hash: [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
84129c056dSChris Bieneman 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
85129c056dSChris Bieneman Version:
86129c056dSChris Bieneman Major: 1
87129c056dSChris Bieneman Minor: 0
88129c056dSChris Bieneman PartCount: 7
89129c056dSChris Bieneman Parts:
90*76fca147SChris Bieneman - Name: FKE0
91129c056dSChris Bieneman Size: 0
92*76fca147SChris Bieneman - Name: FKE1
93129c056dSChris Bieneman Size: 0
94*76fca147SChris Bieneman - Name: FKE2
95129c056dSChris Bieneman Size: 0
96*76fca147SChris Bieneman - Name: FKE3
97129c056dSChris Bieneman Size: 0
98*76fca147SChris Bieneman - Name: FKE4
99129c056dSChris Bieneman Size: 0
100*76fca147SChris Bieneman - Name: FKE5
101129c056dSChris Bieneman Size: 0
102*76fca147SChris Bieneman - Name: FKE6
103129c056dSChris Bieneman Size: 0
104129c056dSChris Bieneman ...
105129c056dSChris Bieneman )"));
106129c056dSChris Bieneman
107129c056dSChris Bieneman EXPECT_EQ(Storage.size(), 116u);
108129c056dSChris Bieneman EXPECT_TRUE(memcmp(Buffer, Storage.data(), 116) == 0);
109129c056dSChris Bieneman }
110