Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2017 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/framework/types.h"
     21 #include "tensorflow/core/kernels/ops_testutil.h"
     22 #include "tensorflow/core/kernels/ops_util.h"
     23 #include "tensorflow/core/kernels/variable_ops.h"
     24 #include "tensorflow/core/lib/strings/strcat.h"
     25 #include "tensorflow/core/platform/test.h"
     26 
     27 namespace tensorflow {
     28 namespace {
     29 
     30 class GuaranteeConstOpTest : public OpsTestBase {
     31  protected:
     32   Status Init(DataType input_type) {
     33     TF_CHECK_OK(NodeDefBuilder("op", "GuaranteeConst")
     34                     .Input(FakeInput(input_type))
     35                     .Finalize(node_def()));
     36     return InitOp();
     37   }
     38 };
     39 
     40 TEST_F(GuaranteeConstOpTest, Int32Success_6) {
     41   TF_ASSERT_OK(Init(DT_INT32));
     42   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     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 }
     48 
     49 TEST_F(GuaranteeConstOpTest, Int32Success_2_3) {
     50   TF_ASSERT_OK(Init(DT_INT32));
     51   AddInputFromArray<int32>(TensorShape({2, 3}), {1, 2, 3, 4, 5, 6});
     52   TF_ASSERT_OK(RunOpKernel());
     53   Tensor expected(allocator(), DT_INT32, TensorShape({2, 3}));
     54   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     55   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     56 }
     57 
     58 TEST_F(GuaranteeConstOpTest, StringSuccess) {
     59   TF_ASSERT_OK(Init(DT_STRING));
     60   AddInputFromArray<string>(TensorShape({6}), {"A", "b", "C", "d", "E", "f"});
     61   TF_ASSERT_OK(RunOpKernel());
     62   Tensor expected(allocator(), DT_STRING, TensorShape({6}));
     63   test::FillValues<string>(&expected, {"A", "b", "C", "d", "E", "f"});
     64   test::ExpectTensorEqual<string>(expected, *GetOutput(0));
     65 }
     66 
     67 TEST_F(GuaranteeConstOpTest, ResourceInputError) {
     68   TF_ASSERT_OK(Init(DT_RESOURCE));
     69   AddResourceInput("", "resource", new Var(DT_INT32));
     70   const auto status = RunOpKernel();
     71   ASSERT_EQ(error::INVALID_ARGUMENT, status.code());
     72 }
     73 
     74 }  // namespace
     75 }  // namespace tensorflow
     76