1#include "llvm/Object/OffloadBinary.h"
2
3#include "llvm/Testing/Support/Error.h"
4#include "gtest/gtest.h"
5#include <random>
6
7using namespace llvm;
8using namespace llvm::object;
9
10TEST(OffloadingTest, checkOffloadingBinary) {
11 // Create random data to fill the image.
12 std::mt19937 Rng(std::random_device{}());
13 std::uniform_int_distribution<uint64_t> SizeDist(0, 256);
14 std::uniform_int_distribution<uint16_t> KindDist(0);
15 std::uniform_int_distribution<uint16_t> BinaryDist(
16 std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
17 std::uniform_int_distribution<int16_t> StringDist('!', '~');
18 std::vector<uint8_t> Image(SizeDist(Rng));
19 std::generate(first: Image.begin(), last: Image.end(), gen: [&]() { return BinaryDist(Rng); });
20 std::vector<std::pair<std::string, std::string>> Strings(SizeDist(Rng));
21 for (auto &KeyAndValue : Strings) {
22 std::string Key(SizeDist(Rng), '\0');
23 std::string Value(SizeDist(Rng), '\0');
24
25 std::generate(first: Key.begin(), last: Key.end(), gen: [&]() { return StringDist(Rng); });
26 std::generate(first: Value.begin(), last: Value.end(),
27 gen: [&]() { return StringDist(Rng); });
28
29 KeyAndValue = std::make_pair(x&: Key, y&: Value);
30 }
31
32 // Create the image.
33 MapVector<StringRef, StringRef> StringData;
34 for (auto &KeyAndValue : Strings)
35 StringData[KeyAndValue.first] = KeyAndValue.second;
36 std::unique_ptr<MemoryBuffer> ImageData = MemoryBuffer::getMemBuffer(
37 InputData: {reinterpret_cast<char *>(Image.data()), Image.size()}, BufferName: "", RequiresNullTerminator: false);
38
39 OffloadBinary::OffloadingImage Data;
40 Data.TheImageKind = static_cast<ImageKind>(KindDist(Rng));
41 Data.TheOffloadKind = static_cast<OffloadKind>(KindDist(Rng));
42 Data.Flags = KindDist(Rng);
43 Data.StringData = StringData;
44 Data.Image = std::move(ImageData);
45
46 auto BinaryBuffer =
47 MemoryBuffer::getMemBufferCopy(InputData: OffloadBinary::write(Data));
48 auto BinaryOrErr = OffloadBinary::create(*BinaryBuffer);
49 if (!BinaryOrErr)
50 FAIL();
51
52 // Make sure we get the same data out.
53 auto &Binary = **BinaryOrErr;
54 ASSERT_EQ(Data.TheImageKind, Binary.getImageKind());
55 ASSERT_EQ(Data.TheOffloadKind, Binary.getOffloadKind());
56 ASSERT_EQ(Data.Flags, Binary.getFlags());
57
58 for (auto &KeyAndValue : Strings)
59 ASSERT_TRUE(StringData[KeyAndValue.first] ==
60 Binary.getString(KeyAndValue.first));
61
62 EXPECT_TRUE(Data.Image->getBuffer() == Binary.getImage());
63
64 // Ensure the size and alignment of the data is correct.
65 EXPECT_TRUE(Binary.getSize() % OffloadBinary::getAlignment() == 0);
66 EXPECT_TRUE(Binary.getSize() == BinaryBuffer->getBuffer().size());
67}
68

source code of llvm/unittests/Object/OffloadingTest.cpp