22badb47c7ce4711996712bf039a179e13b79c5e
[oota-llvm.git] / unittests / Support / Casting.cpp
1 //===---------- llvm/unittest/Support/Casting.cpp - Casting 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 "llvm/Support/raw_ostream.h"
11 #include "llvm/Support/Debug.h"
12 #define DEBUG_CAST_OPERATORS
13 #include "llvm/Support/Casting.h"
14
15 #include "gtest/gtest.h"
16 #include <cstdlib>
17
18 using namespace llvm;
19
20 namespace {
21
22 const foo *null_foo = NULL;
23
24 extern bar &B1;
25 extern const bar *B2;
26 // test various configurations of const
27 const bar &B3 = B1;
28 const bar *const B4 = B2;
29
30 TEST(CastingTest, isa) {
31   EXPECT_TRUE(isa<foo>(B1));
32   EXPECT_TRUE(isa<foo>(B2));
33   EXPECT_TRUE(isa<foo>(B3));
34   EXPECT_TRUE(isa<foo>(B4));
35 }
36
37 TEST(CastingTest, cast) {
38   foo &F1 = cast<foo>(B1);
39   EXPECT_NE(&F1, null_foo);
40   const foo *F3 = cast<foo>(B2);
41   EXPECT_NE(F3, null_foo);
42   const foo *F4 = cast<foo>(B2);
43   EXPECT_NE(F4, null_foo);
44   const foo &F8 = cast<foo>(B3);
45   EXPECT_NE(&F8, null_foo);
46   const foo *F9 = cast<foo>(B4);
47   EXPECT_NE(F9, null_foo);
48   foo *F10 = cast<foo>(fub());
49   EXPECT_EQ(F10, null_foo);
50 }
51
52 TEST(CastingTest, cast_or_null) {
53   const foo *F11 = cast_or_null<foo>(B2);
54   EXPECT_NE(F11, null_foo);
55   const foo *F12 = cast_or_null<foo>(B2);
56   EXPECT_NE(F12, null_foo);
57   const foo *F13 = cast_or_null<foo>(B4);
58   EXPECT_NE(F13, null_foo);
59   const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
60   EXPECT_EQ(F14, null_foo);
61 }
62
63 bar B;
64 bar &B1 = B;
65 const bar *B2 = &B;
66 }  // anonymous namespace