4dfcb7ea75da32446aee9ebaf5cab803d22193e8
[oota-llvm.git] / unittests / ADT / PointerUnionTest.cpp
1 //===- 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"
12 using namespace llvm;
13
14 namespace {
15
16 typedef PointerUnion<int *, float *> PU;
17
18 // Test fixture
19 class PointerUnionTest : public testing::Test {};
20
21 float f = 3.14f;
22 int i = 42;
23
24 const PU a(&f);
25 const PU b(&i);
26 const PU n;
27
28 TEST_F(PointerUnionTest, Comparison) {
29   EXPECT_TRUE(a != b);
30   EXPECT_FALSE(a == b);
31   EXPECT_TRUE(b != n);
32   EXPECT_FALSE(b == n);
33 }
34
35 TEST_F(PointerUnionTest, Null) {
36   EXPECT_FALSE(a.isNull());
37   EXPECT_FALSE(b.isNull());
38   EXPECT_TRUE(n.isNull());
39   EXPECT_FALSE(!a);
40   EXPECT_FALSE(!b);
41   EXPECT_TRUE(!n);
42   // workaround an issue with EXPECT macros and explicit bool
43   EXPECT_TRUE((bool)a);
44   EXPECT_TRUE((bool)b);
45   EXPECT_FALSE(n);
46 }
47
48 TEST_F(PointerUnionTest, Is) {
49   EXPECT_FALSE(a.is<int *>());
50   EXPECT_TRUE(a.is<float *>());
51   EXPECT_TRUE(b.is<int *>());
52   EXPECT_FALSE(b.is<float *>());
53   EXPECT_TRUE(n.is<int *>());
54   EXPECT_FALSE(n.is<float *>());
55 }
56
57 TEST_F(PointerUnionTest, Get) {
58   EXPECT_EQ(a.get<float *>(), &f);
59   EXPECT_EQ(b.get<int *>(), &i);
60   EXPECT_EQ(n.get<int *>(), (int *)0);
61 }
62
63 } // end anonymous namespace