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/edgeset.h"
     17 
     18 #include <vector>
     19 #include "tensorflow/core/graph/graph.h"
     20 #include "tensorflow/core/platform/test.h"
     21 
     22 namespace tensorflow {
     23 class EdgeSetTest : public ::testing::Test {
     24  public:
     25   EdgeSetTest() : edges_(nullptr), eset_(nullptr) {}
     26 
     27   ~EdgeSetTest() override {
     28     delete eset_;
     29     delete[] edges_;
     30   }
     31 
     32   void MakeEdgeSet(int n) {
     33     delete eset_;
     34     delete[] edges_;
     35     edges_ = new Edge[n];
     36     eset_ = new EdgeSet;
     37     model_.clear();
     38     for (int i = 0; i < n; i++) {
     39       eset_->insert(&edges_[i]);
     40       model_.insert(&edges_[i]);
     41     }
     42   }
     43 
     44   void CheckSame() {
     45     EXPECT_EQ(model_.size(), eset_->size());
     46     EXPECT_EQ(model_.empty(), eset_->empty());
     47     std::vector<const Edge*> modelv(model_.begin(), model_.end());
     48     std::vector<const Edge*> esetv(eset_->begin(), eset_->end());
     49     std::sort(modelv.begin(), modelv.end());
     50     std::sort(esetv.begin(), esetv.end());
     51     EXPECT_EQ(modelv.size(), esetv.size());
     52     for (size_t i = 0; i < modelv.size(); i++) {
     53       EXPECT_EQ(modelv[i], esetv[i]) << i;
     54     }
     55   }
     56 
     57   Edge nonexistent_;
     58   Edge* edges_;
     59   EdgeSet* eset_;
     60   std::set<const Edge*> model_;
     61 };
     62 
     63 namespace {
     64 
     65 TEST_F(EdgeSetTest, Ops) {
     66   for (int n : {0, 1, 2, 3, 4, 10}) {
     67     MakeEdgeSet(n);
     68     CheckSame();
     69     EXPECT_EQ((n == 0), eset_->empty());
     70     EXPECT_EQ(n, eset_->size());
     71 
     72     eset_->clear();
     73     model_.clear();
     74     CheckSame();
     75 
     76     eset_->insert(&edges_[0]);
     77     model_.insert(&edges_[0]);
     78     CheckSame();
     79   }
     80 }
     81 
     82 // Try insert/erase of existing elements at different positions.
     83 TEST_F(EdgeSetTest, Exists) {
     84   for (int n : {0, 1, 2, 3, 4, 10}) {
     85     MakeEdgeSet(n);
     86     for (int pos = 0; pos < n; pos++) {
     87       MakeEdgeSet(n);
     88       auto p = eset_->insert(&edges_[pos]);
     89       EXPECT_FALSE(p.second);
     90       EXPECT_EQ(&edges_[pos], *p.first);
     91 
     92       EXPECT_EQ(1, eset_->erase(&edges_[pos]));
     93       model_.erase(&edges_[pos]);
     94       CheckSame();
     95     }
     96   }
     97 }
     98 
     99 // Try insert/erase of non-existent element.
    100 TEST_F(EdgeSetTest, DoesNotExist) {
    101   for (int n : {0, 1, 2, 3, 4, 10}) {
    102     MakeEdgeSet(n);
    103     EXPECT_EQ(0, eset_->erase(&nonexistent_));
    104     auto p = eset_->insert(&nonexistent_);
    105     EXPECT_TRUE(p.second);
    106     EXPECT_EQ(&nonexistent_, *p.first);
    107   }
    108 }
    109 
    110 }  // namespace
    111 }  // namespace tensorflow
    112