Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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/core/framework/fake_input.h"
     17 #include "tensorflow/core/framework/node_def_builder.h"
     18 #include "tensorflow/core/framework/tensor.h"
     19 #include "tensorflow/core/framework/tensor_testutil.h"
     20 #include "tensorflow/core/kernels/ops_testutil.h"
     21 #include "tensorflow/core/kernels/ops_util.h"
     22 #include "tensorflow/core/platform/test.h"
     23 
     24 namespace tensorflow {
     25 namespace {
     26 
     27 // Tests for the switch op
     28 class SwitchOpTest : public OpsTestBase {
     29  protected:
     30   void Initialize(DataType dt) {
     31     TF_ASSERT_OK(NodeDefBuilder("op", "Switch")
     32                      .Input(FakeInput(dt))
     33                      .Input(FakeInput())
     34                      .Finalize(node_def()));
     35     TF_ASSERT_OK(InitOp());
     36   }
     37 };
     38 
     39 TEST_F(SwitchOpTest, Int32Success_6_s0) {
     40   Initialize(DT_INT32);
     41   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     42   AddInputFromArray<bool>(TensorShape({}), {false});
     43   TF_ASSERT_OK(RunOpKernel());
     44   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     45   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     46   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     47   EXPECT_EQ(nullptr, GetOutput(1));
     48 }
     49 
     50 TEST_F(SwitchOpTest, Int32Success_6_s1) {
     51   Initialize(DT_INT32);
     52   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     53   AddInputFromArray<bool>(TensorShape({}), {true});
     54   TF_ASSERT_OK(RunOpKernel());
     55   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     56   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     57   test::ExpectTensorEqual<int32>(expected, *GetOutput(1));
     58   EXPECT_EQ(nullptr, GetOutput(0));
     59 }
     60 
     61 TEST_F(SwitchOpTest, Int32Success_2_3_s0) {
     62   Initialize(DT_INT32);
     63   AddInputFromArray<int32>(TensorShape({2, 3}), {1, 2, 3, 4, 5, 6});
     64   AddInputFromArray<bool>(TensorShape({}), {false});
     65   TF_ASSERT_OK(RunOpKernel());
     66   Tensor expected(allocator(), DT_INT32, TensorShape({2, 3}));
     67   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     68   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     69   EXPECT_EQ(nullptr, GetOutput(1));
     70 }
     71 
     72 TEST_F(SwitchOpTest, StringSuccess_s1) {
     73   Initialize(DT_STRING);
     74   AddInputFromArray<string>(TensorShape({6}), {"A", "b", "C", "d", "E", "f"});
     75   AddInputFromArray<bool>(TensorShape({}), {true});
     76   TF_ASSERT_OK(RunOpKernel());
     77   Tensor expected(allocator(), DT_STRING, TensorShape({6}));
     78   test::FillValues<string>(&expected, {"A", "b", "C", "d", "E", "f"});
     79   test::ExpectTensorEqual<string>(expected, *GetOutput(1));
     80   EXPECT_EQ(nullptr, GetOutput(0));
     81 }
     82 
     83 class AbortOpTest : public OpsTestBase {
     84  protected:
     85 };
     86 
     87 #ifdef PLATFORM_WINDOWS
     88 #define SIGABRT 3
     89 
     90 class KilledBySignal {
     91  public:
     92   explicit KilledBySignal(int signum) : signum_(signum) {}
     93   bool operator()(int exit_status) const { return exit_status == signum_; }
     94 
     95  private:
     96   const int signum_;
     97 };
     98 #else
     99 #define KilledBySignal ::testing::KilledBySignal
    100 #endif
    101 
    102 // Pass an error message to the op.
    103 TEST_F(AbortOpTest, pass_error_msg) {
    104   TF_ASSERT_OK(NodeDefBuilder("abort_op", "Abort")
    105                    .Attr("error_msg", "abort_op_test")
    106                    .Finalize(node_def()));
    107   TF_ASSERT_OK(InitOp());
    108   EXPECT_EXIT(RunOpKernel().IgnoreError(), KilledBySignal(SIGABRT),
    109               "Abort_op intentional failure; abort_op_test");
    110 }
    111 
    112 // Use the default error message.
    113 TEST_F(AbortOpTest, default_msg) {
    114   TF_ASSERT_OK(NodeDefBuilder("abort_op", "Abort").Finalize(node_def()));
    115   TF_ASSERT_OK(InitOp());
    116   EXPECT_EXIT(RunOpKernel().IgnoreError(), KilledBySignal(SIGABRT),
    117               "Abort_op intentional failure; ");
    118 }
    119 
    120 // Exit normally.
    121 TEST_F(AbortOpTest, exit_normally) {
    122   TF_ASSERT_OK(NodeDefBuilder("abort_op", "Abort")
    123                    .Attr("exit_without_error", true)
    124                    .Finalize(node_def()));
    125   TF_ASSERT_OK(InitOp());
    126   EXPECT_EXIT(RunOpKernel().IgnoreError(), ::testing::ExitedWithCode(0), "");
    127 }
    128 
    129 }  // namespace
    130 }  // namespace tensorflow
    131