1 | //===- llvm/unittest/IR/TypesTest.cpp - Type unit tests -------------------===// |
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/IR/DerivedTypes.h" |
10 | #include "llvm/IR/LLVMContext.h" |
11 | #include "llvm/IR/TypedPointerType.h" |
12 | #include "gtest/gtest.h" |
13 | using namespace llvm; |
14 | |
15 | namespace { |
16 | |
17 | TEST(TypesTest, StructType) { |
18 | LLVMContext C; |
19 | |
20 | // PR13522 |
21 | StructType *Struct = StructType::create(Context&: C, Name: "FooBar" ); |
22 | EXPECT_EQ("FooBar" , Struct->getName()); |
23 | Struct->setName(Struct->getName().substr(Start: 0, N: 3)); |
24 | EXPECT_EQ("Foo" , Struct->getName()); |
25 | Struct->setName("" ); |
26 | EXPECT_TRUE(Struct->getName().empty()); |
27 | EXPECT_FALSE(Struct->hasName()); |
28 | } |
29 | |
30 | TEST(TypesTest, LayoutIdenticalEmptyStructs) { |
31 | LLVMContext C; |
32 | |
33 | StructType *Foo = StructType::create(Context&: C, Name: "Foo" ); |
34 | StructType *Bar = StructType::create(Context&: C, Name: "Bar" ); |
35 | EXPECT_TRUE(Foo->isLayoutIdentical(Bar)); |
36 | } |
37 | |
38 | TEST(TypesTest, TargetExtType) { |
39 | LLVMContext Context; |
40 | Type *A = TargetExtType::get(Context, Name: "typea" ); |
41 | Type *Aparam = TargetExtType::get(Context, Name: "typea" , Types: {}, Ints: {0, 1}); |
42 | Type *Aparam2 = TargetExtType::get(Context, Name: "typea" , Types: {}, Ints: {0, 1}); |
43 | // Opaque types with same parameters are identical... |
44 | EXPECT_EQ(Aparam, Aparam2); |
45 | // ... but just having the same name is not enough. |
46 | EXPECT_NE(A, Aparam); |
47 | } |
48 | |
49 | TEST(TypedPointerType, PrintTest) { |
50 | std::string Buffer; |
51 | LLVMContext Context; |
52 | raw_string_ostream OS(Buffer); |
53 | |
54 | Type *I8Ptr = TypedPointerType::get(ElementType: Type::getInt8Ty(C&: Context), AddressSpace: 0); |
55 | I8Ptr->print(O&: OS); |
56 | EXPECT_EQ(StringRef(Buffer), ("typedptr(i8, 0)" )); |
57 | } |
58 | |
59 | } // end anonymous namespace |
60 | |