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 <chrono>
17 #include <thread>
18
19 #include "tensorflow/core/framework/fake_input.h"
20 #include "tensorflow/core/framework/node_def_builder.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/tensor_testutil.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/kernels/ops_testutil.h"
25 #include "tensorflow/core/kernels/ops_util.h"
26 #include "tensorflow/core/lib/core/status_test_util.h"
27 #include "tensorflow/core/lib/strings/str_util.h"
28 #include "tensorflow/core/lib/strings/strcat.h"
29 #include "tensorflow/core/platform/status_matchers.h"
30 #include "tensorflow/core/util/determinism_test_util.h"
31
32 namespace tensorflow {
33 namespace {
34
35 class PrintingV2GraphTest : public OpsTestBase {
36 protected:
Init(const string & output_stream="log(warning)")37 Status Init(const string& output_stream = "log(warning)") {
38 TF_CHECK_OK(NodeDefBuilder("op", "PrintV2")
39 .Input(FakeInput(DT_STRING))
40 .Attr("output_stream", output_stream)
41 .Finalize(node_def()));
42 return InitOp();
43 }
44 };
45
TEST_F(PrintingV2GraphTest,StringSuccess)46 TEST_F(PrintingV2GraphTest, StringSuccess) {
47 TF_ASSERT_OK(Init());
48 AddInputFromArray<tstring>(TensorShape({}), {"bar"});
49 TF_ASSERT_OK(RunOpKernel());
50 }
51
TEST_F(PrintingV2GraphTest,InvalidOutputStream)52 TEST_F(PrintingV2GraphTest, InvalidOutputStream) {
53 ASSERT_NE(OkStatus(), (Init("invalid_output_stream")));
54 }
55
TEST_F(PrintingV2GraphTest,InvalidInputRank)56 TEST_F(PrintingV2GraphTest, InvalidInputRank) {
57 TF_ASSERT_OK(Init());
58 AddInputFromArray<tstring>(TensorShape({2}), {"bar", "foo"});
59 ASSERT_NE(OkStatus(), RunOpKernel());
60 }
61
62 class PrintingGraphTest : public OpsTestBase {
63 protected:
Init(DataType input_type1,DataType input_type2,string msg="",int first_n=-1,int summarize=3)64 Status Init(DataType input_type1, DataType input_type2, string msg = "",
65 int first_n = -1, int summarize = 3) {
66 TF_CHECK_OK(NodeDefBuilder("op", "Print")
67 .Input(FakeInput(input_type1))
68 .Input(FakeInput(2, input_type2))
69 .Attr("message", msg)
70 .Attr("first_n", first_n)
71 .Attr("summarize", summarize)
72 .Finalize(node_def()));
73 return InitOp();
74 }
75 };
76
TEST_F(PrintingGraphTest,Int32Success_6)77 TEST_F(PrintingGraphTest, Int32Success_6) {
78 TF_ASSERT_OK(Init(DT_INT32, DT_INT32));
79 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
80 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
81 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
82 TF_ASSERT_OK(RunOpKernel());
83 Tensor expected(allocator(), DT_INT32, TensorShape({6}));
84 test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
85 test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
86 }
87
TEST_F(PrintingGraphTest,Int32Success_Summarize6)88 TEST_F(PrintingGraphTest, Int32Success_Summarize6) {
89 TF_ASSERT_OK(Init(DT_INT32, DT_INT32, "", -1, 6));
90 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
91 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
92 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
93 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
TEST_F(PrintingGraphTest,StringSuccess)99 TEST_F(PrintingGraphTest, StringSuccess) {
100 TF_ASSERT_OK(Init(DT_INT32, DT_STRING));
101 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
102 AddInputFromArray<tstring>(TensorShape({}), {"foo"});
103 AddInputFromArray<tstring>(TensorShape({}), {"bar"});
104 TF_ASSERT_OK(RunOpKernel());
105 Tensor expected(allocator(), DT_INT32, TensorShape({6}));
106 test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
107 test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
108 }
109
TEST_F(PrintingGraphTest,MsgSuccess)110 TEST_F(PrintingGraphTest, MsgSuccess) {
111 TF_ASSERT_OK(Init(DT_INT32, DT_STRING, "Message: "));
112 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
113 AddInputFromArray<tstring>(TensorShape({}), {"foo"});
114 AddInputFromArray<tstring>(TensorShape({}), {"bar"});
115 TF_ASSERT_OK(RunOpKernel());
116 Tensor expected(allocator(), DT_INT32, TensorShape({6}));
117 test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
118 test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
119 }
120
TEST_F(PrintingGraphTest,FirstNSuccess)121 TEST_F(PrintingGraphTest, FirstNSuccess) {
122 TF_ASSERT_OK(Init(DT_INT32, DT_STRING, "", 3));
123 AddInputFromArray<int32>(TensorShape({6}), {1, 2, 3, 4, 5, 6});
124 AddInputFromArray<tstring>(TensorShape({}), {"foo"});
125 AddInputFromArray<tstring>(TensorShape({}), {"bar"});
126 // run 4 times but we only print 3 as intended
127 for (int i = 0; i < 4; i++) TF_ASSERT_OK(RunOpKernel());
128 Tensor expected(allocator(), DT_INT32, TensorShape({6}));
129 test::FillValues<int32>(&expected, {1, 2, 3, 4, 5, 6});
130 test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
131 }
132
133 class TimestampTest : public OpsTestBase {
134 protected:
Init()135 Status Init() {
136 TF_CHECK_OK(NodeDefBuilder("op", "Timestamp").Finalize(node_def()));
137 return InitOp();
138 }
139 };
140
TEST_F(TimestampTest,WaitAtLeast)141 TEST_F(TimestampTest, WaitAtLeast) {
142 TF_ASSERT_OK(Init());
143 TF_ASSERT_OK(RunOpKernel());
144 double ts1 = *((*GetOutput(0)).flat<double>().data());
145
146 // wait 1 second
147 std::this_thread::sleep_for(std::chrono::seconds(1));
148
149 TF_ASSERT_OK(RunOpKernel());
150 double ts2 = *((*GetOutput(0)).flat<double>().data());
151
152 EXPECT_LE(1.0, ts2 - ts1);
153 }
154
TEST_F(TimestampTest,DeterminismError)155 TEST_F(TimestampTest, DeterminismError) {
156 test::DeterministicOpsScope det_scope;
157 TF_ASSERT_OK(Init());
158 EXPECT_THAT(RunOpKernel(),
159 testing::StatusIs(
160 error::FAILED_PRECONDITION,
161 "Timestamp cannot be called when determinism is enabled"));
162 }
163
164 } // end namespace
165 } // end namespace tensorflow
166