Casting.cpp revision 08993c03abed359171d12b50ab38da0ac331cfb7
1ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//===---------- llvm/unittest/Support/Casting.cpp - Casting tests --------===//
2ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//
3ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//                     The LLVM Compiler Infrastructure
4ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//
5ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif// This file is distributed under the University of Illinois Open Source
6ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif// License. See LICENSE.TXT for details.
7ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//
8ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//===----------------------------------------------------------------------===//
9ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
10ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include "llvm/Support/raw_ostream.h"
11ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include "llvm/Support/Debug.h"
12ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#define DEBUG_CAST_OPERATORS
13ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include "llvm/Support/Casting.h"
14ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
15ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include "gtest/gtest.h"
16ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include <cstdlib>
17ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
18ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifusing namespace llvm;
19ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
20ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifnamespace {
21ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
2208993c03abed359171d12b50ab38da0ac331cfb7Gabor Greifconst foo *null_foo = NULL;
2308993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
24ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifextern bar &B1;
25ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifextern const bar *B2;
2608993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif// test various configurations of const
2708993c03abed359171d12b50ab38da0ac331cfb7Gabor Greifconst bar &B3 = B1;
2808993c03abed359171d12b50ab38da0ac331cfb7Gabor Greifconst bar *const B4 = B2;
29ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
30af8e2ef649b90e88f9d595a638279e3bc4892845Gabor GreifTEST(CastingTest, isa) {
31ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif  EXPECT_TRUE(isa<foo>(B1));
32af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greif  EXPECT_TRUE(isa<foo>(B2));
33af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greif  EXPECT_TRUE(isa<foo>(B3));
34af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greif  EXPECT_TRUE(isa<foo>(B4));
35ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif}
36ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
3708993c03abed359171d12b50ab38da0ac331cfb7Gabor GreifTEST(CastingTest, cast) {
3808993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  foo &F1 = cast<foo>(B1);
3908993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(&F1, null_foo);
4008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F3 = cast<foo>(B2);
4108993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(F3, null_foo);
4208993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F4 = cast<foo>(B2);
4308993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(F4, null_foo);
4408993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo &F8 = cast<foo>(B3);
4508993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(&F8, null_foo);
4608993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F9 = cast<foo>(B4);
4708993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(F9, null_foo);
4808993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  foo *F10 = cast<foo>(fub());
4908993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_EQ(F10, null_foo);
5008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif}
5108993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
5208993c03abed359171d12b50ab38da0ac331cfb7Gabor GreifTEST(CastingTest, cast_or_null) {
5308993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F11 = cast_or_null<foo>(B2);
5408993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(F11, null_foo);
5508993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F12 = cast_or_null<foo>(B2);
5608993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(F12, null_foo);
5708993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F13 = cast_or_null<foo>(B4);
5808993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_NE(F13, null_foo);
5908993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
6008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  EXPECT_EQ(F14, null_foo);
6108993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif}
6208993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
63ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifbar B;
64ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifbar &B1 = B;
65ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifconst bar *B2 = &B;
66ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif}  // anonymous namespace
67