1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#include <type_traits>
19#include <typeinfo>
20
21#include <gtest/gtest.h>
22
23#include "arrow/util/checked_cast.h"
24
25namespace arrow {
26namespace internal {
27
28class Foo {
29 public:
30 virtual ~Foo() = default;
31};
32
33class Bar {};
34class FooSub : public Foo {};
35template <typename T>
36class Baz : public Foo {};
37
38TEST(CheckedCast, TestInvalidSubclassCast) {
39 static_assert(std::is_polymorphic<Foo>::value, "Foo is not polymorphic");
40
41 Foo foo;
42 FooSub foosub;
43 const Foo& foosubref = foosub;
44 Baz<double> baz;
45 const Foo& bazref = baz;
46
47#ifndef NDEBUG // debug mode
48 // illegal pointer cast
49 ASSERT_EQ(nullptr, checked_cast<Bar*>(&foo));
50
51 // illegal reference cast
52 ASSERT_THROW(checked_cast<const Bar&>(foosubref), std::bad_cast);
53
54 // legal reference casts
55 ASSERT_NO_THROW(checked_cast<const FooSub&>(foosubref));
56 ASSERT_NO_THROW(checked_cast<const Baz<double>&>(bazref));
57#else // release mode
58 // failure modes for the invalid casts occur at compile time
59
60 // legal pointer cast
61 ASSERT_NE(nullptr, checked_cast<const FooSub*>(&foosubref));
62
63 // legal reference casts: this is static_cast in a release build, so ASSERT_NO_THROW
64 // doesn't make a whole lot of sense here.
65 auto& x = checked_cast<const FooSub&>(foosubref);
66 ASSERT_EQ(&foosubref, &x);
67
68 auto& y = checked_cast<const Baz<double>&>(bazref);
69 ASSERT_EQ(&bazref, &y);
70#endif
71}
72
73} // namespace internal
74} // namespace arrow
75