1 #include "../../../lib/AST/ByteCode/BitcastBuffer.h"
2 #include "clang/AST/ASTContext.h"
3 #include "gtest/gtest.h"
10 using namespace clang
;
11 using namespace clang::interp
;
13 TEST(BitcastBuffer
, PushData
) {
14 BitcastBuffer
Buff1(Bytes(sizeof(int)).toBits());
16 const unsigned V
= 0xCAFEBABE;
17 Bits VSize
= Bytes(sizeof(V
)).toBits();
18 std::byte Data
[sizeof(V
)];
19 std::memcpy(Data
, &V
, sizeof(V
));
21 Endian HostEndianness
=
22 llvm::sys::IsLittleEndianHost
? Endian::Little
: Endian::Big
;
24 Buff1
.pushData(Data
, Bits::zero(), VSize
, HostEndianness
);
26 // The buffer is in host-endianness.
27 if (llvm::sys::IsLittleEndianHost
) {
28 ASSERT_EQ(Buff1
.Data
[0], std::byte
{0xbe});
29 ASSERT_EQ(Buff1
.Data
[1], std::byte
{0xba});
30 ASSERT_EQ(Buff1
.Data
[2], std::byte
{0xfe});
31 ASSERT_EQ(Buff1
.Data
[3], std::byte
{0xca});
33 ASSERT_EQ(Buff1
.Data
[0], std::byte
{0xca});
34 ASSERT_EQ(Buff1
.Data
[1], std::byte
{0xfe});
35 ASSERT_EQ(Buff1
.Data
[2], std::byte
{0xba});
36 ASSERT_EQ(Buff1
.Data
[3], std::byte
{0xbe});
41 auto D
= Buff1
.copyBits(Bits::zero(), VSize
, VSize
, Endian::Little
);
42 std::memcpy(&V2
, D
.get(), sizeof(V
));
45 D
= Buff1
.copyBits(Bits::zero(), VSize
, VSize
, Endian::Big
);
46 std::memcpy(&V2
, D
.get(), sizeof(V
));
50 BitcastBuffer
Buff2(Bytes(sizeof(int)).toBits());
56 std::memcpy(sdata
, &s1
, sizeof(s1
));
57 Buff2
.pushData(sdata
, Bits::zero(), Bits(sizeof(s1
) * 8), HostEndianness
);
58 std::memcpy(sdata
, &s2
, sizeof(s2
));
59 Buff2
.pushData(sdata
, Bits(sizeof(s1
) * 8), Bits(sizeof(s2
) * 8),
63 if (llvm::sys::IsLittleEndianHost
) {
64 ASSERT_EQ(Buff2
.Data
[0], std::byte
{0xfe});
65 ASSERT_EQ(Buff2
.Data
[1], std::byte
{0xca});
66 ASSERT_EQ(Buff2
.Data
[2], std::byte
{0xbe});
67 ASSERT_EQ(Buff2
.Data
[3], std::byte
{0xba});
69 ASSERT_EQ(Buff2
.Data
[0], std::byte
{0xba});
70 ASSERT_EQ(Buff2
.Data
[1], std::byte
{0xbe});
71 ASSERT_EQ(Buff2
.Data
[2], std::byte
{0xca});
72 ASSERT_EQ(Buff2
.Data
[3], std::byte
{0xfe});
77 auto D
= Buff2
.copyBits(Bits::zero(), Bits(sizeof(V
) * 8),
78 Bits(sizeof(V
) * 8), Endian::Little
);
79 std::memcpy(&V
, D
.get(), sizeof(V
));
80 ASSERT_EQ(V
, 0xBABECAFE);
82 D
= Buff2
.copyBits(Bits::zero(), Bits(sizeof(V
) * 8), Bits(sizeof(V
) * 8),
84 std::memcpy(&V
, D
.get(), sizeof(V
));
85 ASSERT_EQ(V
, 0xBABECAFE);