Home | History | Annotate | Download | only in graph
      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/graph/node_builder.h"
     17 
     18 #include "tensorflow/core/framework/op.h"
     19 #include "tensorflow/core/graph/graph.h"
     20 #include "tensorflow/core/kernels/ops_util.h"
     21 #include "tensorflow/core/lib/core/status_test_util.h"
     22 #include "tensorflow/core/platform/test.h"
     23 
     24 namespace tensorflow {
     25 namespace {
     26 
     27 REGISTER_OP("Source").Output("o: out_types").Attr("out_types: list(type)");
     28 REGISTER_OP("Sink").Input("i: T").Attr("T: type");
     29 
     30 TEST(NodeBuilderTest, Simple) {
     31   Graph graph(OpRegistry::Global());
     32   Node* source_node;
     33   TF_EXPECT_OK(NodeBuilder("source_op", "Source")
     34                    .Attr("out_types", {DT_INT32, DT_STRING})
     35                    .Finalize(&graph, &source_node));
     36   ASSERT_TRUE(source_node != nullptr);
     37 
     38   // Try connecting to each of source_node's outputs.
     39   TF_EXPECT_OK(NodeBuilder("sink1", "Sink")
     40                    .Input(source_node)
     41                    .Finalize(&graph, nullptr));
     42   TF_EXPECT_OK(NodeBuilder("sink2", "Sink")
     43                    .Input(source_node, 1)
     44                    .Finalize(&graph, nullptr));
     45 
     46   // Generate an error if the index is out of range.
     47   EXPECT_FALSE(NodeBuilder("sink3", "Sink")
     48                    .Input(source_node, 2)
     49                    .Finalize(&graph, nullptr)
     50                    .ok());
     51   EXPECT_FALSE(NodeBuilder("sink4", "Sink")
     52                    .Input(source_node, -1)
     53                    .Finalize(&graph, nullptr)
     54                    .ok());
     55   EXPECT_FALSE(NodeBuilder("sink5", "Sink")
     56                    .Input({source_node, -1})
     57                    .Finalize(&graph, nullptr)
     58                    .ok());
     59 
     60   // Generate an error if the node is nullptr.  This can happen when using
     61   // GraphDefBuilder if there was an error creating the input node.
     62   EXPECT_FALSE(NodeBuilder("sink6", "Sink")
     63                    .Input(nullptr)
     64                    .Finalize(&graph, nullptr)
     65                    .ok());
     66   EXPECT_FALSE(NodeBuilder("sink7", "Sink")
     67                    .Input(NodeBuilder::NodeOut(nullptr, 0))
     68                    .Finalize(&graph, nullptr)
     69                    .ok());
     70 }
     71 
     72 }  // namespace
     73 }  // namespace tensorflow
     74