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 "gtest/gtest.h"
12 using namespace llvm;
13
14 namespace {
15
TEST(TypesTest,StructType)16 TEST(TypesTest, StructType) {
17 LLVMContext C;
18
19 // PR13522
20 StructType *Struct = StructType::create(C, "FooBar");
21 EXPECT_EQ("FooBar", Struct->getName());
22 Struct->setName(Struct->getName().substr(0, 3));
23 EXPECT_EQ("Foo", Struct->getName());
24 Struct->setName("");
25 EXPECT_TRUE(Struct->getName().empty());
26 EXPECT_FALSE(Struct->hasName());
27 }
28
TEST(TypesTest,LayoutIdenticalEmptyStructs)29 TEST(TypesTest, LayoutIdenticalEmptyStructs) {
30 LLVMContext C;
31
32 StructType *Foo = StructType::create(C, "Foo");
33 StructType *Bar = StructType::create(C, "Bar");
34 EXPECT_TRUE(Foo->isLayoutIdentical(Bar));
35 }
36
TEST(TypesTest,CopyPointerType)37 TEST(TypesTest, CopyPointerType) {
38 LLVMContext COpaquePointers;
39 COpaquePointers.setOpaquePointers(true);
40
41 PointerType *P1 = PointerType::get(COpaquePointers, 1);
42 EXPECT_TRUE(P1->isOpaque());
43 PointerType *P1C = PointerType::getWithSamePointeeType(P1, 1);
44 EXPECT_EQ(P1, P1C);
45 EXPECT_TRUE(P1C->isOpaque());
46 PointerType *P1C0 = PointerType::getWithSamePointeeType(P1, 0);
47 EXPECT_NE(P1, P1C0);
48 EXPECT_TRUE(P1C0->isOpaque());
49
50 LLVMContext CTypedPointers;
51 CTypedPointers.setOpaquePointers(false);
52 Type *Int8 = Type::getInt8Ty(CTypedPointers);
53 PointerType *P2 = PointerType::get(Int8, 1);
54 EXPECT_FALSE(P2->isOpaque());
55 PointerType *P2C = PointerType::getWithSamePointeeType(P2, 1);
56 EXPECT_EQ(P2, P2C);
57 EXPECT_FALSE(P2C->isOpaque());
58 PointerType *P2C0 = PointerType::getWithSamePointeeType(P2, 0);
59 EXPECT_NE(P2, P2C0);
60 EXPECT_FALSE(P2C0->isOpaque());
61 }
62
63 } // end anonymous namespace
64