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/framework/types.h"
     21 #include "tensorflow/core/kernels/ops_testutil.h"
     22 #include "tensorflow/core/kernels/ops_util.h"
     23 #include "tensorflow/core/lib/strings/strcat.h"
     24 
     25 namespace tensorflow {
     26 namespace {
     27 
     28 class PrintingGraphTest : public OpsTestBase {
     29  protected:
     30   Status Init(DataType input_type1, DataType input_type2, string msg = "",
     31               int first_n = -1, int summarize = 3) {
     32     TF_CHECK_OK(NodeDefBuilder("op", "Print")
     33                     .Input(FakeInput(input_type1))
     34                     .Input(FakeInput(2, input_type2))
     35                     .Attr("message", msg)
     36                     .Attr("first_n", first_n)
     37                     .Attr("summarize", summarize)
     38                     .Finalize(node_def()));
     39     return InitOp();
     40   }
     41 };
     42 
     43 TEST_F(PrintingGraphTest, Int32Success_6) {
     44   TF_ASSERT_OK(Init(DT_INT32, DT_INT32));
     45   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     46   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     47   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     48   TF_ASSERT_OK(RunOpKernel());
     49   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     50   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     51   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     52 }
     53 
     54 TEST_F(PrintingGraphTest, Int32Success_Summarize6) {
     55   TF_ASSERT_OK(Init(DT_INT32, DT_INT32, "", -1, 6));
     56   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     57   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     58   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     59   TF_ASSERT_OK(RunOpKernel());
     60   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     61   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     62   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     63 }
     64 
     65 TEST_F(PrintingGraphTest, StringSuccess) {
     66   TF_ASSERT_OK(Init(DT_INT32, DT_STRING));
     67   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     68   AddInputFromArray<string>(TensorShape({}), {"foo"});
     69   AddInputFromArray<string>(TensorShape({}), {"bar"});
     70   TF_ASSERT_OK(RunOpKernel());
     71   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     72   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     73   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     74 }
     75 
     76 TEST_F(PrintingGraphTest, MsgSuccess) {
     77   TF_ASSERT_OK(Init(DT_INT32, DT_STRING, "Message: "));
     78   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     79   AddInputFromArray<string>(TensorShape({}), {"foo"});
     80   AddInputFromArray<string>(TensorShape({}), {"bar"});
     81   TF_ASSERT_OK(RunOpKernel());
     82   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     83   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     84   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     85 }
     86 
     87 TEST_F(PrintingGraphTest, FirstNSuccess) {
     88   TF_ASSERT_OK(Init(DT_INT32, DT_STRING, "", 3));
     89   AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
     90   AddInputFromArray<string>(TensorShape({}), {"foo"});
     91   AddInputFromArray<string>(TensorShape({}), {"bar"});
     92   // run 4 times but we only print 3 as intended
     93   for (int i = 0; i < 4; i++) TF_ASSERT_OK(RunOpKernel());
     94   Tensor expected(allocator(), DT_INT32, TensorShape({6}));
     95   test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
     96   test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
     97 }
     98 
     99 }  // end namespace
    100 }  // end namespace tensorflow
    101