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