blob: 22badb47c7ce4711996712bf039a179e13b79c5e [file] [log] [blame]
Gabor Greifee57dae2010-07-20 16:32:20 +00001//===---------- 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
18using namespace llvm;
19
20namespace {
21
Gabor Greif08993c02010-07-20 16:51:18 +000022const foo *null_foo = NULL;
23
Gabor Greifee57dae2010-07-20 16:32:20 +000024extern bar &B1;
25extern const bar *B2;
Gabor Greif08993c02010-07-20 16:51:18 +000026// test various configurations of const
27const bar &B3 = B1;
28const bar *const B4 = B2;
Gabor Greifee57dae2010-07-20 16:32:20 +000029
Gabor Greifaf8e2ef2010-07-20 16:38:12 +000030TEST(CastingTest, isa) {
Gabor Greifee57dae2010-07-20 16:32:20 +000031 EXPECT_TRUE(isa<foo>(B1));
Gabor Greifaf8e2ef2010-07-20 16:38:12 +000032 EXPECT_TRUE(isa<foo>(B2));
33 EXPECT_TRUE(isa<foo>(B3));
34 EXPECT_TRUE(isa<foo>(B4));
Gabor Greifee57dae2010-07-20 16:32:20 +000035}
36
Gabor Greif08993c02010-07-20 16:51:18 +000037TEST(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
52TEST(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
Gabor Greifee57dae2010-07-20 16:32:20 +000063bar B;
64bar &B1 = B;
65const bar *B2 = &B;
66} // anonymous namespace