1 //===- llvm/unittest/IR/TypesTest.cpp - Type unit tests -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/DerivedTypes.h"
10 #include "llvm/IR/LLVMContext.h"
11 #include "llvm/IR/TypedPointerType.h"
12 #include "gtest/gtest.h"
17 TEST(TypesTest
, StructType
) {
21 StructType
*Struct
= StructType::create(C
, "FooBar");
22 EXPECT_EQ("FooBar", Struct
->getName());
23 Struct
->setName(Struct
->getName().substr(0, 3));
24 EXPECT_EQ("Foo", Struct
->getName());
26 EXPECT_TRUE(Struct
->getName().empty());
27 EXPECT_FALSE(Struct
->hasName());
30 TEST(TypesTest
, LayoutIdenticalEmptyStructs
) {
33 StructType
*Foo
= StructType::create(C
, "Foo");
34 StructType
*Bar
= StructType::create(C
, "Bar");
35 EXPECT_TRUE(Foo
->isLayoutIdentical(Bar
));
38 TEST(TypesTest
, CopyPointerType
) {
39 LLVMContext COpaquePointers
;
40 COpaquePointers
.setOpaquePointers(true);
42 PointerType
*P1
= PointerType::get(COpaquePointers
, 1);
43 EXPECT_TRUE(P1
->isOpaque());
44 PointerType
*P1C
= PointerType::getWithSamePointeeType(P1
, 1);
46 EXPECT_TRUE(P1C
->isOpaque());
47 PointerType
*P1C0
= PointerType::getWithSamePointeeType(P1
, 0);
49 EXPECT_TRUE(P1C0
->isOpaque());
51 LLVMContext CTypedPointers
;
52 CTypedPointers
.setOpaquePointers(false);
53 Type
*Int8
= Type::getInt8Ty(CTypedPointers
);
54 PointerType
*P2
= PointerType::get(Int8
, 1);
55 EXPECT_FALSE(P2
->isOpaque());
56 PointerType
*P2C
= PointerType::getWithSamePointeeType(P2
, 1);
58 EXPECT_FALSE(P2C
->isOpaque());
59 PointerType
*P2C0
= PointerType::getWithSamePointeeType(P2
, 0);
61 EXPECT_FALSE(P2C0
->isOpaque());
64 TEST(TypedPointerType
, PrintTest
) {
67 raw_string_ostream
OS(Buffer
);
69 Type
*I8Ptr
= TypedPointerType::get(Type::getInt8Ty(Context
), 0);
71 EXPECT_EQ(StringRef(Buffer
), ("typedptr(i8, 0)"));
74 } // end anonymous namespace