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/core/status_test_util.h"
24 #include "tensorflow/core/lib/strings/str_util.h"
25 #include "tensorflow/core/lib/strings/strcat.h"
26
27 namespace tensorflow {
28 namespace {
29
30 class StringFormatGraphTest : public OpsTestBase {
31 protected:
Init(int num_inputs,DataType input_type,const string & template_="%s",const string & placeholder="%s",int summarize=3)32 Status Init(int num_inputs, DataType input_type,
33 const string& template_ = "%s", const string& placeholder = "%s",
34 int summarize = 3) {
35 TF_CHECK_OK(NodeDefBuilder("op", "StringFormat")
36 .Input(FakeInput(num_inputs, input_type))
37 .Attr("template", template_)
38 .Attr("placeholder", placeholder)
39 .Attr("summarize", summarize)
40 .Finalize(node_def()));
41 return InitOp();
42 }
43 };
44
TEST_F(StringFormatGraphTest,Int32Success_7)45 TEST_F(StringFormatGraphTest, Int32Success_7) {
46 TF_ASSERT_OK(Init(1, DT_INT32, "First tensor: %s"));
47
48 AddInputFromArray<int32>(TensorShape({7}), {1, 2, 3, 4, 5, 6, 7});
49 TF_ASSERT_OK(RunOpKernel());
50 Tensor expected(allocator(), DT_STRING, TensorShape({}));
51 test::FillValues<tstring>(&expected, {"First tensor: [1 2 3 ... 5 6 7]"});
52 test::ExpectTensorEqual<tstring>(expected, *GetOutput(0));
53 }
54
TEST_F(StringFormatGraphTest,Int32Success_3_3)55 TEST_F(StringFormatGraphTest, Int32Success_3_3) {
56 TF_ASSERT_OK(Init(1, DT_INT32, "First tensor: %s", "%s", 1));
57
58 AddInputFromArray<int32>(TensorShape({3, 3}), {1, 2, 3, 4, 5, 6, 7, 8, 9});
59 TF_ASSERT_OK(RunOpKernel());
60 Tensor expected(allocator(), DT_STRING, TensorShape({}));
61 test::FillValues<tstring>(&expected, {"First tensor: [[1 ... 3]\n ..."
62 "\n [7 ... 9]]"});
63 test::ExpectTensorEqual<tstring>(expected, *GetOutput(0));
64 }
65
66 } // end namespace
67 } // end namespace tensorflow
68