Home | History | Annotate | Download | only in base
      1 // Copyright 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/stl_util.h"
      6 
      7 #include <set>
      8 
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace {
     12 
     13 // Used as test case to ensure the various base::STLXxx functions don't require
     14 // more than operators "<" and "==" on values stored in containers.
     15 class ComparableValue {
     16  public:
     17   explicit ComparableValue(int value) : value_(value) {}
     18 
     19   bool operator==(const ComparableValue& rhs) const {
     20     return value_ == rhs.value_;
     21   }
     22 
     23   bool operator<(const ComparableValue& rhs) const {
     24     return value_ < rhs.value_;
     25   }
     26 
     27  private:
     28   int value_;
     29 };
     30 
     31 }  // namespace
     32 
     33 namespace base {
     34 namespace {
     35 
     36 TEST(STLUtilTest, STLIsSorted) {
     37   {
     38     std::set<int> set;
     39     set.insert(24);
     40     set.insert(1);
     41     set.insert(12);
     42     EXPECT_TRUE(STLIsSorted(set));
     43   }
     44 
     45   {
     46     std::set<ComparableValue> set;
     47     set.insert(ComparableValue(24));
     48     set.insert(ComparableValue(1));
     49     set.insert(ComparableValue(12));
     50     EXPECT_TRUE(STLIsSorted(set));
     51   }
     52 
     53   {
     54     std::vector<int> vector;
     55     vector.push_back(1);
     56     vector.push_back(1);
     57     vector.push_back(4);
     58     vector.push_back(64);
     59     vector.push_back(12432);
     60     EXPECT_TRUE(STLIsSorted(vector));
     61     vector.back() = 1;
     62     EXPECT_FALSE(STLIsSorted(vector));
     63   }
     64 }
     65 
     66 TEST(STLUtilTest, STLSetDifference) {
     67   std::set<int> a1;
     68   a1.insert(1);
     69   a1.insert(2);
     70   a1.insert(3);
     71   a1.insert(4);
     72 
     73   std::set<int> a2;
     74   a2.insert(3);
     75   a2.insert(4);
     76   a2.insert(5);
     77   a2.insert(6);
     78   a2.insert(7);
     79 
     80   {
     81     std::set<int> difference;
     82     difference.insert(1);
     83     difference.insert(2);
     84     EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2));
     85   }
     86 
     87   {
     88     std::set<int> difference;
     89     difference.insert(5);
     90     difference.insert(6);
     91     difference.insert(7);
     92     EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1));
     93   }
     94 
     95   {
     96     std::vector<int> difference;
     97     difference.push_back(1);
     98     difference.push_back(2);
     99     EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2));
    100   }
    101 
    102   {
    103     std::vector<int> difference;
    104     difference.push_back(5);
    105     difference.push_back(6);
    106     difference.push_back(7);
    107     EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1));
    108   }
    109 }
    110 
    111 TEST(STLUtilTest, STLSetUnion) {
    112   std::set<int> a1;
    113   a1.insert(1);
    114   a1.insert(2);
    115   a1.insert(3);
    116   a1.insert(4);
    117 
    118   std::set<int> a2;
    119   a2.insert(3);
    120   a2.insert(4);
    121   a2.insert(5);
    122   a2.insert(6);
    123   a2.insert(7);
    124 
    125   {
    126     std::set<int> result;
    127     result.insert(1);
    128     result.insert(2);
    129     result.insert(3);
    130     result.insert(4);
    131     result.insert(5);
    132     result.insert(6);
    133     result.insert(7);
    134     EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2));
    135   }
    136 
    137   {
    138     std::set<int> result;
    139     result.insert(1);
    140     result.insert(2);
    141     result.insert(3);
    142     result.insert(4);
    143     result.insert(5);
    144     result.insert(6);
    145     result.insert(7);
    146     EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1));
    147   }
    148 
    149   {
    150     std::vector<int> result;
    151     result.push_back(1);
    152     result.push_back(2);
    153     result.push_back(3);
    154     result.push_back(4);
    155     result.push_back(5);
    156     result.push_back(6);
    157     result.push_back(7);
    158     EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2));
    159   }
    160 
    161   {
    162     std::vector<int> result;
    163     result.push_back(1);
    164     result.push_back(2);
    165     result.push_back(3);
    166     result.push_back(4);
    167     result.push_back(5);
    168     result.push_back(6);
    169     result.push_back(7);
    170     EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1));
    171   }
    172 }
    173 
    174 TEST(STLUtilTest, STLSetIntersection) {
    175   std::set<int> a1;
    176   a1.insert(1);
    177   a1.insert(2);
    178   a1.insert(3);
    179   a1.insert(4);
    180 
    181   std::set<int> a2;
    182   a2.insert(3);
    183   a2.insert(4);
    184   a2.insert(5);
    185   a2.insert(6);
    186   a2.insert(7);
    187 
    188   {
    189     std::set<int> result;
    190     result.insert(3);
    191     result.insert(4);
    192     EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2));
    193   }
    194 
    195   {
    196     std::set<int> result;
    197     result.insert(3);
    198     result.insert(4);
    199     EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1));
    200   }
    201 
    202   {
    203     std::vector<int> result;
    204     result.push_back(3);
    205     result.push_back(4);
    206     EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2));
    207   }
    208 
    209   {
    210     std::vector<int> result;
    211     result.push_back(3);
    212     result.push_back(4);
    213     EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1));
    214   }
    215 }
    216 
    217 TEST(STLUtilTest, STLIncludes) {
    218   std::set<int> a1;
    219   a1.insert(1);
    220   a1.insert(2);
    221   a1.insert(3);
    222   a1.insert(4);
    223 
    224   std::set<int> a2;
    225   a2.insert(3);
    226   a2.insert(4);
    227 
    228   std::set<int> a3;
    229   a3.insert(3);
    230   a3.insert(4);
    231   a3.insert(5);
    232 
    233   EXPECT_TRUE(STLIncludes<std::set<int> >(a1, a2));
    234   EXPECT_FALSE(STLIncludes<std::set<int> >(a1, a3));
    235   EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a1));
    236   EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a3));
    237   EXPECT_FALSE(STLIncludes<std::set<int> >(a3, a1));
    238   EXPECT_TRUE(STLIncludes<std::set<int> >(a3, a2));
    239 }
    240 
    241 TEST(StringAsArrayTest, Empty) {
    242   std::string empty;
    243   EXPECT_EQ(nullptr, string_as_array(&empty));
    244 }
    245 
    246 TEST(StringAsArrayTest, NullTerminated) {
    247   // If any std::string implementation is not null-terminated, this should
    248   // fail. All compilers we use return a null-terminated buffer, but please do
    249   // not rely on this fact in your code.
    250   std::string str("abcde");
    251   str.resize(3);
    252   EXPECT_STREQ("abc", string_as_array(&str));
    253 }
    254 
    255 TEST(StringAsArrayTest, WriteCopy) {
    256   // With a COW implementation, this test will fail if
    257   // string_as_array(&str) is implemented as
    258   // const_cast<char*>(str->data()).
    259   std::string s1("abc");
    260   const std::string s2(s1);
    261   string_as_array(&s1)[1] = 'x';
    262   EXPECT_EQ("axc", s1);
    263   EXPECT_EQ("abc", s2);
    264 }
    265 
    266 }  // namespace
    267 }  // namespace base
    268