[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / IR / TypesTest.cpp
blob83fd55919fe7b8dfcdd1769514f3da46589f54df
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 //===----------------------------------------------------------------------===//
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;
15 namespace {
17 TEST(TypesTest, StructType) {
18 LLVMContext C;
20 // PR13522
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());
25 Struct->setName("");
26 EXPECT_TRUE(Struct->getName().empty());
27 EXPECT_FALSE(Struct->hasName());
30 TEST(TypesTest, LayoutIdenticalEmptyStructs) {
31 LLVMContext C;
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);
45 EXPECT_EQ(P1, P1C);
46 EXPECT_TRUE(P1C->isOpaque());
47 PointerType *P1C0 = PointerType::getWithSamePointeeType(P1, 0);
48 EXPECT_NE(P1, P1C0);
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);
57 EXPECT_EQ(P2, P2C);
58 EXPECT_FALSE(P2C->isOpaque());
59 PointerType *P2C0 = PointerType::getWithSamePointeeType(P2, 0);
60 EXPECT_NE(P2, P2C0);
61 EXPECT_FALSE(P2C0->isOpaque());
64 TEST(TypedPointerType, PrintTest) {
65 std::string Buffer;
66 LLVMContext Context;
67 raw_string_ostream OS(Buffer);
69 Type *I8Ptr = TypedPointerType::get(Type::getInt8Ty(Context), 0);
70 I8Ptr->print(OS);
71 EXPECT_EQ(StringRef(Buffer), ("typedptr(i8, 0)"));
74 } // end anonymous namespace