Home | History | Annotate | Download | only in containers
      1 // Copyright 2014 The Android Open Source Project
      2 //
      3 // This software is licensed under the terms of the GNU General Public
      4 // License version 2, as published by the Free Software Foundation, and
      5 // may be copied, distributed, and modified under those terms.
      6 //
      7 // This program is distributed in the hope that it will be useful,
      8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 // GNU General Public License for more details.
     11 
     12 #include "android/base/containers/PodVector.h"
     13 
     14 #include <gtest/gtest.h>
     15 
     16 namespace android {
     17 namespace base {
     18 
     19 static int  hashIndex(size_t n) {
     20     return static_cast<int>(((n >> 14) * 13773) + (n * 51));
     21 }
     22 
     23 TEST(PodVector, Empty) {
     24     PodVector<int> v;
     25     EXPECT_TRUE(v.empty());
     26     EXPECT_EQ(0U, v.size());
     27 }
     28 
     29 TEST(PodVector, AppendOneItem) {
     30     PodVector<int> v;
     31     v.append(10234);
     32     EXPECT_FALSE(v.empty());
     33     EXPECT_EQ(1U, v.size());
     34     EXPECT_EQ(10234, v[0]);
     35 }
     36 
     37 TEST(PodVector, AppendLotsOfItems) {
     38     PodVector<int> v;
     39     const size_t kMaxCount = 10000;
     40     for (size_t n = 0; n < kMaxCount; ++n) {
     41         v.append(hashIndex(n));
     42     }
     43     EXPECT_EQ(kMaxCount, v.size());
     44     for (size_t n = 0; n < kMaxCount; ++n) {
     45         EXPECT_EQ(hashIndex(n), v[n]) << "At index " << n;
     46     }
     47 }
     48 
     49 TEST(PodVector, RemoveFrontItems) {
     50     PodVector<int> v;
     51     const size_t kMaxCount = 100;
     52     for (size_t n = 0; n < kMaxCount; ++n) {
     53         v.append(hashIndex(n));
     54     }
     55     EXPECT_EQ(kMaxCount, v.size());
     56     for (size_t n = 0; n < kMaxCount; ++n) {
     57         EXPECT_EQ(hashIndex(n), v[0]) << "At index " << n;
     58         v.remove(0U);
     59         EXPECT_EQ(kMaxCount - n - 1U, v.size()) << "At index " << n;
     60     }
     61 }
     62 
     63 TEST(PodVector, PrependItems) {
     64     PodVector<int> v;
     65     const size_t kMaxCount = 100;
     66     for (size_t n = 0; n < kMaxCount; ++n) {
     67         v.prepend(hashIndex(n));
     68     }
     69     EXPECT_EQ(kMaxCount, v.size());
     70     for (size_t n = 0; n < kMaxCount; ++n) {
     71         EXPECT_EQ(hashIndex(kMaxCount - n - 1), v[n]) << "At index " << n;
     72     }
     73 }
     74 
     75 TEST(PodVector, ResizeExpands) {
     76     PodVector<int> v;
     77     const size_t kMaxCount = 100;
     78     const size_t kMaxCount2 = 10000;
     79     for (size_t n = 0; n < kMaxCount; ++n) {
     80         v.append(hashIndex(n));
     81     }
     82     EXPECT_EQ(kMaxCount, v.size());
     83     v.resize(kMaxCount2);
     84     EXPECT_EQ(kMaxCount2, v.size());
     85     for (size_t n = 0; n < kMaxCount; ++n) {
     86         EXPECT_EQ(hashIndex(n), v[n]) << "At index " << n;
     87     }
     88 }
     89 
     90 TEST(PodVector, ResizeTruncates) {
     91     PodVector<int> v;
     92     const size_t kMaxCount = 10000;
     93     const size_t kMaxCount2 = 10;
     94     for (size_t n = 0; n < kMaxCount; ++n) {
     95         v.append(hashIndex(n));
     96     }
     97     EXPECT_EQ(kMaxCount, v.size());
     98     v.resize(kMaxCount2);
     99     EXPECT_EQ(kMaxCount2, v.size());
    100     for (size_t n = 0; n < kMaxCount2; ++n) {
    101         EXPECT_EQ(hashIndex(n), v[n]) << "At index " << n;
    102     }
    103 }
    104 
    105 
    106 TEST(PodVector, AssignmentOperator) {
    107     PodVector<int> v1;
    108     const size_t kMaxCount = 10000;
    109     for (size_t n = 0; n < kMaxCount; ++n) {
    110         v1.append(hashIndex(n));
    111     }
    112     EXPECT_EQ(kMaxCount, v1.size());
    113 
    114     PodVector<int> v2;
    115     v2 = v1;
    116 
    117     v1.reserve(0);
    118 
    119     EXPECT_EQ(kMaxCount, v2.size());
    120     for (size_t n = 0; n < kMaxCount; ++n) {
    121         EXPECT_EQ(hashIndex(n), v2[n]) << "At index " << n;
    122     }
    123 }
    124 
    125 }  // namespace base
    126 }  // namespace android
    127