blob: a592784ae095f1069dbe518de19242042f45b7bf [file] [log] [blame]
David Blaikiee2760b72013-08-21 21:30:23 +00001//===- llvm/unittest/ADT/PointerUnionTest.cpp - Optional unit tests -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11#include "llvm/ADT/PointerUnion.h"
12using namespace llvm;
13
14namespace {
15
Chandler Carruth1284df12014-04-28 23:37:53 +000016typedef PointerUnion<int *, float *> PU;
David Blaikiee2760b72013-08-21 21:30:23 +000017
Chandler Carruth275c5fc2014-04-28 23:42:22 +000018struct PointerUnionTest : public testing::Test {
19 float f;
20 int i;
David Blaikiee2760b72013-08-21 21:30:23 +000021
Chandler Carrutha468b8e2014-04-28 23:44:14 +000022 PU a, b, c, n;
David Blaikiee2760b72013-08-21 21:30:23 +000023
Chandler Carrutha468b8e2014-04-28 23:44:14 +000024 PointerUnionTest() : f(3.14f), i(42), a(&f), b(&i), c(&i), n() {}
Chandler Carruth275c5fc2014-04-28 23:42:22 +000025};
David Blaikiee2760b72013-08-21 21:30:23 +000026
27TEST_F(PointerUnionTest, Comparison) {
Chandler Carrutha468b8e2014-04-28 23:44:14 +000028 EXPECT_TRUE(a == a);
29 EXPECT_FALSE(a != a);
David Blaikiee2760b72013-08-21 21:30:23 +000030 EXPECT_TRUE(a != b);
31 EXPECT_FALSE(a == b);
Chandler Carrutha468b8e2014-04-28 23:44:14 +000032 EXPECT_TRUE(b == c);
33 EXPECT_FALSE(b != c);
David Blaikiee2760b72013-08-21 21:30:23 +000034 EXPECT_TRUE(b != n);
35 EXPECT_FALSE(b == n);
36}
37
38TEST_F(PointerUnionTest, Null) {
39 EXPECT_FALSE(a.isNull());
40 EXPECT_FALSE(b.isNull());
41 EXPECT_TRUE(n.isNull());
42 EXPECT_FALSE(!a);
43 EXPECT_FALSE(!b);
44 EXPECT_TRUE(!n);
45 // workaround an issue with EXPECT macros and explicit bool
46 EXPECT_TRUE((bool)a);
47 EXPECT_TRUE((bool)b);
48 EXPECT_FALSE(n);
Chandler Carruthd24465f2014-04-29 00:14:27 +000049
50 EXPECT_NE(n, b);
51 EXPECT_EQ(b, c);
52 b = nullptr;
53 EXPECT_EQ(n, b);
54 EXPECT_NE(b, c);
David Blaikiee2760b72013-08-21 21:30:23 +000055}
56
57TEST_F(PointerUnionTest, Is) {
Chandler Carruth1284df12014-04-28 23:37:53 +000058 EXPECT_FALSE(a.is<int *>());
59 EXPECT_TRUE(a.is<float *>());
60 EXPECT_TRUE(b.is<int *>());
61 EXPECT_FALSE(b.is<float *>());
62 EXPECT_TRUE(n.is<int *>());
63 EXPECT_FALSE(n.is<float *>());
David Blaikiee2760b72013-08-21 21:30:23 +000064}
65
66TEST_F(PointerUnionTest, Get) {
Chandler Carruth1284df12014-04-28 23:37:53 +000067 EXPECT_EQ(a.get<float *>(), &f);
68 EXPECT_EQ(b.get<int *>(), &i);
Craig Topper66f09ad2014-06-08 22:29:17 +000069 EXPECT_EQ(n.get<int *>(), (int *)nullptr);
David Blaikiee2760b72013-08-21 21:30:23 +000070}
71
72} // end anonymous namespace