1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/compiler/xla/service/hlo_casting_utils.h"
17 
18 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
19 #include "tensorflow/core/platform/test.h"
20 
21 namespace xla {
22 namespace {
23 
24 class DummyInstruction : public HloInstruction {
25  public:
DummyInstruction()26   DummyInstruction()
27       : HloInstruction(HloOpcode::kConstant, ShapeUtil::MakeShape(F32, {})) {}
28 };
29 
30 class AnotherDummyInstruction : public HloInstruction {
31  public:
AnotherDummyInstruction()32   AnotherDummyInstruction()
33       : HloInstruction(HloOpcode::kParameter, ShapeUtil::MakeShape(F32, {})) {}
34 };
35 
TEST(HloCastingUtilsTest,CastSucceeds)36 TEST(HloCastingUtilsTest, CastSucceeds) {
37   DummyInstruction instruction;
38   DummyInstruction* casted =
39       Cast<DummyInstruction>(static_cast<HloInstruction*>(&instruction));
40   ASSERT_EQ(casted, &instruction);
41 }
42 
TEST(HloCastingUtilsTest,CastDiesForWrongType)43 TEST(HloCastingUtilsTest, CastDiesForWrongType) {
44   AnotherDummyInstruction instruction;
45   ASSERT_DEATH(
46       Cast<DummyInstruction>(static_cast<HloInstruction*>(&instruction)), "");
47 }
48 
TEST(HloCastingUtilsTest,CastDiesForNullptr)49 TEST(HloCastingUtilsTest, CastDiesForNullptr) {
50   HloInstruction* null = nullptr;
51   ASSERT_DEATH(Cast<DummyInstruction>(null), "");
52 }
53 
TEST(HloCastingUtilsTest,CastOrNullSucceeds)54 TEST(HloCastingUtilsTest, CastOrNullSucceeds) {
55   DummyInstruction instruction;
56   DummyInstruction* casted =
57       Cast<DummyInstruction>(static_cast<HloInstruction*>(&instruction));
58   ASSERT_EQ(casted, &instruction);
59 }
60 
TEST(HloCastingUtilsTest,CastOrNullDiesForWrongType)61 TEST(HloCastingUtilsTest, CastOrNullDiesForWrongType) {
62   AnotherDummyInstruction instruction;
63   ASSERT_DEATH(
64       Cast<DummyInstruction>(static_cast<HloInstruction*>(&instruction)), "");
65 }
66 
TEST(HloCastingUtilsTest,CastOrNullReturnsNullptrForNullptr)67 TEST(HloCastingUtilsTest, CastOrNullReturnsNullptrForNullptr) {
68   HloInstruction* null = nullptr;
69   DummyInstruction* casted = CastOrNull<DummyInstruction>(null);
70   ASSERT_EQ(casted, nullptr);
71 }
72 
TEST(HloCastingUtilsTest,DynCastSucceeds)73 TEST(HloCastingUtilsTest, DynCastSucceeds) {
74   DummyInstruction instruction;
75   DummyInstruction* casted =
76       DynCast<DummyInstruction>(static_cast<HloInstruction*>(&instruction));
77   ASSERT_EQ(casted, &instruction);
78 }
79 
TEST(HloCastingUtilsTest,DynCastReturnsNullptrForWrongType)80 TEST(HloCastingUtilsTest, DynCastReturnsNullptrForWrongType) {
81   AnotherDummyInstruction instruction;
82   DummyInstruction* casted =
83       DynCast<DummyInstruction>(static_cast<HloInstruction*>(&instruction));
84   ASSERT_EQ(casted, nullptr);
85 }
86 
TEST(HloCastingUtilsTest,DynCastDiesForNullptr)87 TEST(HloCastingUtilsTest, DynCastDiesForNullptr) {
88   HloInstruction* null = nullptr;
89   ASSERT_DEATH(DynCast<DummyInstruction>(null), "");
90 }
91 
TEST(HloCastingUtilsTest,DynCastOrNullSucceeds)92 TEST(HloCastingUtilsTest, DynCastOrNullSucceeds) {
93   DummyInstruction instruction;
94   DummyInstruction* casted = DynCastOrNull<DummyInstruction>(
95       static_cast<HloInstruction*>(&instruction));
96   ASSERT_EQ(casted, &instruction);
97 }
98 
TEST(HloCastingUtilsTest,DynCastOrNullReturnsNullptrForWrongType)99 TEST(HloCastingUtilsTest, DynCastOrNullReturnsNullptrForWrongType) {
100   AnotherDummyInstruction instruction;
101   DummyInstruction* casted = DynCastOrNull<DummyInstruction>(
102       static_cast<HloInstruction*>(&instruction));
103   ASSERT_EQ(casted, nullptr);
104 }
105 
TEST(HloCastingUtilsTest,DynCastOrNullReturnsNullptrForNullptr)106 TEST(HloCastingUtilsTest, DynCastOrNullReturnsNullptrForNullptr) {
107   HloInstruction* null = nullptr;
108   DummyInstruction* casted = DynCastOrNull<DummyInstruction>(null);
109   ASSERT_EQ(casted, nullptr);
110 }
111 
112 }  // namespace
113 }  // namespace xla
114