Home | History | Annotate | Download | only in ADT
      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 struct PointerUnionTest : public testing::Test {
     19   float f;
     20   int i;
     21 
     22   PU a, b, c, n;
     23 
     24   PointerUnionTest() : f(3.14f), i(42), a(&f), b(&i), c(&i), n() {}
     25 };
     26 
     27 TEST_F(PointerUnionTest, Comparison) {
     28   EXPECT_TRUE(a == a);
     29   EXPECT_FALSE(a != a);
     30   EXPECT_TRUE(a != b);
     31   EXPECT_FALSE(a == b);
     32   EXPECT_TRUE(b == c);
     33   EXPECT_FALSE(b != c);
     34   EXPECT_TRUE(b != n);
     35   EXPECT_FALSE(b == n);
     36 }
     37 
     38 TEST_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);
     49 
     50   EXPECT_NE(n, b);
     51   EXPECT_EQ(b, c);
     52   b = nullptr;
     53   EXPECT_EQ(n, b);
     54   EXPECT_NE(b, c);
     55 }
     56 
     57 TEST_F(PointerUnionTest, Is) {
     58   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 *>());
     64 }
     65 
     66 TEST_F(PointerUnionTest, Get) {
     67   EXPECT_EQ(a.get<float *>(), &f);
     68   EXPECT_EQ(b.get<int *>(), &i);
     69   EXPECT_EQ(n.get<int *>(), (int *)nullptr);
     70 }
     71 
     72 } // end anonymous namespace
     73