blob: 87c60887ad3796c5bf8a2ec80a933320937c8533 [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 Carruth275c5fc2014-04-28 23:42:22 +000022 PU a, b, n;
David Blaikiee2760b72013-08-21 21:30:23 +000023
Chandler Carruth275c5fc2014-04-28 23:42:22 +000024 PointerUnionTest() : f(3.14f), i(42), a(&f), b(&i), n() {}
25};
David Blaikiee2760b72013-08-21 21:30:23 +000026
27TEST_F(PointerUnionTest, Comparison) {
28 EXPECT_TRUE(a != b);
29 EXPECT_FALSE(a == b);
30 EXPECT_TRUE(b != n);
31 EXPECT_FALSE(b == n);
32}
33
34TEST_F(PointerUnionTest, Null) {
35 EXPECT_FALSE(a.isNull());
36 EXPECT_FALSE(b.isNull());
37 EXPECT_TRUE(n.isNull());
38 EXPECT_FALSE(!a);
39 EXPECT_FALSE(!b);
40 EXPECT_TRUE(!n);
41 // workaround an issue with EXPECT macros and explicit bool
42 EXPECT_TRUE((bool)a);
43 EXPECT_TRUE((bool)b);
44 EXPECT_FALSE(n);
45}
46
47TEST_F(PointerUnionTest, Is) {
Chandler Carruth1284df12014-04-28 23:37:53 +000048 EXPECT_FALSE(a.is<int *>());
49 EXPECT_TRUE(a.is<float *>());
50 EXPECT_TRUE(b.is<int *>());
51 EXPECT_FALSE(b.is<float *>());
52 EXPECT_TRUE(n.is<int *>());
53 EXPECT_FALSE(n.is<float *>());
David Blaikiee2760b72013-08-21 21:30:23 +000054}
55
56TEST_F(PointerUnionTest, Get) {
Chandler Carruth1284df12014-04-28 23:37:53 +000057 EXPECT_EQ(a.get<float *>(), &f);
58 EXPECT_EQ(b.get<int *>(), &i);
59 EXPECT_EQ(n.get<int *>(), (int *)0);
David Blaikiee2760b72013-08-21 21:30:23 +000060}
61
62} // end anonymous namespace