]>
git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/util/checked_cast_test.cc
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <type_traits>
21 #include <gtest/gtest.h>
23 #include "arrow/util/checked_cast.h"
30 virtual ~Foo() = default;
34 class FooSub
: public Foo
{};
36 class Baz
: public Foo
{};
38 TEST(CheckedCast
, TestInvalidSubclassCast
) {
39 static_assert(std::is_polymorphic
<Foo
>::value
, "Foo is not polymorphic");
43 const Foo
& foosubref
= foosub
;
45 const Foo
& bazref
= baz
;
47 #ifndef NDEBUG // debug mode
48 // illegal pointer cast
49 ASSERT_EQ(nullptr, checked_cast
<Bar
*>(&foo
));
51 // illegal reference cast
52 ASSERT_THROW(checked_cast
<const Bar
&>(foosubref
), std::bad_cast
);
54 // legal reference casts
55 ASSERT_NO_THROW(checked_cast
<const FooSub
&>(foosubref
));
56 ASSERT_NO_THROW(checked_cast
<const Baz
<double>&>(bazref
));
58 // failure modes for the invalid casts occur at compile time
61 ASSERT_NE(nullptr, checked_cast
<const FooSub
*>(&foosubref
));
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
);
68 auto& y
= checked_cast
<const Baz
<double>&>(bazref
);
69 ASSERT_EQ(&bazref
, &y
);
73 } // namespace internal