Home | History | Annotate | Download | only in gtl
      1 /* Copyright 2017 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/lib/gtl/compactptrset.h"
     17 
     18 #include "tensorflow/core/lib/hash/hash.h"
     19 #include "tensorflow/core/platform/test.h"
     20 #include "tensorflow/core/platform/types.h"
     21 
     22 namespace tensorflow {
     23 namespace gtl {
     24 namespace {
     25 
     26 typedef CompactPointerSet<const char*> StringSet;
     27 
     28 static std::vector<const char*> SortedContents(const StringSet& set) {
     29   std::vector<const char*> contents(set.begin(), set.end());
     30   std::sort(contents.begin(), contents.end());
     31   return contents;
     32 }
     33 
     34 TEST(CompactPointerSetTest, Simple) {
     35   // Make some aligned and some unaligned pointers.
     36   string data = "ABCDEFG";
     37   const char* a = &data[0];
     38   const char* b = &data[1];
     39   const char* c = &data[2];
     40   const char* d = &data[3];
     41   const char* e = &data[4];
     42   const char* f = &data[5];
     43   const char* g = &data[6];
     44   for (const auto& list : std::vector<std::vector<const char*>>({{
     45            {},                     // Empty
     46            {a},                    // Aligned singleton
     47            {b},                    // Unaligned singleton
     48            {nullptr},              // Test insertion of nullptr
     49            {a, b, c, d, e, f, g},  // Many
     50        }})) {
     51     LOG(INFO) << list.size();
     52 
     53     // Test insert along with accessors.
     54     StringSet set;
     55     ASSERT_TRUE(set.empty());
     56     for (auto p : list) {
     57       ASSERT_EQ(set.count(p), 0);
     58       ASSERT_TRUE(set.insert(p).second);
     59       ASSERT_EQ(set.count(p), 1);
     60       ASSERT_TRUE(set.find(p) != set.end());
     61     }
     62     ASSERT_EQ(set.size(), list.size());
     63 
     64     ASSERT_EQ(SortedContents(set), list);
     65 
     66     // Test copy constructor.
     67     {
     68       StringSet set2(set);
     69       ASSERT_EQ(SortedContents(set2), list);
     70     }
     71 
     72     // Test assignment/copying into a destination with different
     73     // initial elements.
     74     for (const auto& initial : std::vector<std::vector<const char*>>({{
     75              {},            // Empty
     76              {a},           // Aligned singleton
     77              {b},           // Unaligned singleton
     78              {nullptr},     // Test insertion of nullptr
     79              {a, b, c, d},  // Many
     80          }})) {
     81       StringSet dst;
     82       for (auto p : initial) {
     83         dst.insert(p);
     84       }
     85       ASSERT_EQ(dst.size(), initial.size());
     86       dst = set;
     87       ASSERT_EQ(SortedContents(dst), list);
     88       dst.clear();
     89       ASSERT_EQ(dst.size(), 0);
     90     }
     91 
     92     // Test erase along with accessors.
     93     for (auto p : list) {
     94       ASSERT_EQ(set.erase(p), 1);
     95       ASSERT_EQ(set.erase(p), 0);
     96     }
     97     ASSERT_TRUE(set.empty());
     98     ASSERT_EQ(set.size(), 0);
     99   }
    100 }
    101 
    102 }  // namespace
    103 }  // namespace gtl
    104 }  // namespace tensorflow
    105