David Blaikie | 296ca41 | 2013-08-21 21:30:23 +0000 | [diff] [blame] | 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 | |
| 22 | float f = 3.14f; |
| 23 | int i = 42; |
| 24 | |
| 25 | const PU a(&f); |
| 26 | const PU b(&i); |
| 27 | const PU n; |
| 28 | |
| 29 | TEST_F(PointerUnionTest, Comparison) { |
| 30 | EXPECT_TRUE(a != b); |
| 31 | EXPECT_FALSE(a == b); |
| 32 | EXPECT_TRUE(b != n); |
| 33 | EXPECT_FALSE(b == n); |
| 34 | } |
| 35 | |
| 36 | TEST_F(PointerUnionTest, Null) { |
| 37 | EXPECT_FALSE(a.isNull()); |
| 38 | EXPECT_FALSE(b.isNull()); |
| 39 | EXPECT_TRUE(n.isNull()); |
| 40 | EXPECT_FALSE(!a); |
| 41 | EXPECT_FALSE(!b); |
| 42 | EXPECT_TRUE(!n); |
| 43 | // workaround an issue with EXPECT macros and explicit bool |
| 44 | EXPECT_TRUE((bool)a); |
| 45 | EXPECT_TRUE((bool)b); |
| 46 | EXPECT_FALSE(n); |
| 47 | } |
| 48 | |
| 49 | TEST_F(PointerUnionTest, Is) { |
| 50 | EXPECT_FALSE(a.is<int*>()); |
| 51 | EXPECT_TRUE(a.is<float*>()); |
| 52 | EXPECT_TRUE(b.is<int*>()); |
| 53 | EXPECT_FALSE(b.is<float*>()); |
| 54 | EXPECT_TRUE(n.is<int*>()); |
| 55 | EXPECT_FALSE(n.is<float*>()); |
| 56 | } |
| 57 | |
| 58 | TEST_F(PointerUnionTest, Get) { |
| 59 | EXPECT_EQ(a.get<float*>(), &f); |
| 60 | EXPECT_EQ(b.get<int*>(), &i); |
| 61 | EXPECT_EQ(n.get<int*>(), (int*)0); |
| 62 | } |
| 63 | |
| 64 | } // end anonymous namespace |