Home | History | Annotate | Download | only in tests
      1 /* -*- c++ -*- */
      2 /*
      3  * Copyright (C) 2010 The Android Open Source Project
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *  * Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  *  * Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in
     13  *    the documentation and/or other materials provided with the
     14  *    distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     20  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     23  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include "../include/list"
     31 #ifndef ANDROID_ASTL_LIST__
     32 #error "Wrong header included!!"
     33 #endif
     34 #include <string>
     35 #include "common.h"
     36 
     37 namespace android {
     38 using std::list;
     39 using std::string;
     40 
     41 bool testConstructor()
     42 {
     43     list<int> list1;
     44     list<int*> list2;
     45     list<string> list3;
     46     list<B> list4;
     47     return true;
     48 }
     49 
     50 bool testClear()
     51 {
     52     {
     53         list<int> l;
     54         for (int i = 0; i < 100; ++i) {
     55             l.push_front(i);
     56             l.push_back(i);
     57         }
     58         l.clear();
     59         EXPECT_TRUE(l.size() == 0);
     60         EXPECT_TRUE(l.empty());
     61     }
     62     {
     63         list<B> l;
     64         for (int i = 0; i < 10; ++i) {
     65             l.push_front(B());
     66             l.push_back(B());
     67         }
     68         l.clear();
     69         EXPECT_TRUE(l.size() == 0);
     70         EXPECT_TRUE(l.empty());
     71     }
     72     return true;
     73 }
     74 bool testSize()
     75 {
     76     list<int> list;
     77 
     78     EXPECT_TRUE(list.size() == 0);
     79     EXPECT_TRUE(list.empty());
     80 
     81     list.push_front(1);
     82     EXPECT_TRUE(list.size() == 1);
     83     EXPECT_FALSE(list.empty());
     84 
     85     for (int i = 0; i < 10; ++i) {
     86         list.push_front(1);
     87         list.push_back(1);
     88     }
     89     EXPECT_TRUE(list.size() == 21);
     90     return true;
     91 }
     92 
     93 bool testIterator()
     94 {
     95     list<int> l;
     96     for (int i = 0; i < 100; ++i) {
     97         l.push_back(i);
     98     }
     99 
    100     list<int>::const_iterator it = l.begin();
    101     for (int i = 0; it != l.end(); ++it, ++i) {
    102         EXPECT_TRUE(*it == i);
    103     }
    104 
    105     l.clear();
    106     for (int i = 0; i < 100; ++i) {
    107         l.push_front(i);
    108     }
    109 
    110     it = l.begin();
    111     for (int i = 99; it != l.end(); ++it, --i) {
    112         EXPECT_TRUE(*it == i);
    113     }
    114 
    115     return true;
    116 }
    117 
    118 bool testErase() {
    119     list<int> l;
    120     for (int i = 0; i < 100; ++i) {
    121         l.push_back(i);
    122     }
    123 
    124     // Deleting the first element.
    125     list<int>::iterator val = l.erase(l.begin());
    126     EXPECT_TRUE(l.size() == 99);
    127     EXPECT_TRUE(*val == 1);
    128 
    129     // Deleting the last should be a no op.
    130     l.erase(l.end());
    131     EXPECT_TRUE(l.size() == 99);
    132 
    133     // Empty bay removing the last element;
    134     while (l.size() > 0) {
    135         val = l.erase(--l.end());
    136     }
    137 
    138     EXPECT_TRUE(l.size() == 0);
    139     EXPECT_TRUE(val == l.end());
    140 
    141     return true;
    142 }
    143 
    144 bool testEraseRange() {
    145     list<int> l;
    146     for (int i = 0; i < 100; ++i) {
    147         l.push_back(i);
    148     }
    149     l.erase(l.begin(), l.end());
    150     EXPECT_TRUE(l.size() == 0);
    151     return true;
    152 }
    153 
    154 bool testPushPop() {
    155     list<int> l;
    156 
    157     l.push_front(10);
    158     EXPECT_TRUE(l.front() == 10);
    159     l.push_back(100);
    160     EXPECT_TRUE(l.back() == 100);
    161 
    162     l.push_front(1);
    163     EXPECT_TRUE(l.front() == 1);
    164     l.push_back(1000);
    165     EXPECT_TRUE(l.back() == 1000);
    166 
    167     l.pop_front();
    168     EXPECT_TRUE(l.front() == 10);
    169     l.pop_back();
    170     EXPECT_TRUE(l.back() == 100);
    171     l.pop_front();
    172     EXPECT_TRUE(l.front() == 100);
    173     EXPECT_TRUE(l.back() == 100);
    174     l.pop_back();
    175     EXPECT_TRUE(l.empty());
    176     // all these are noops
    177     l.pop_back();
    178     l.pop_front();
    179     l.pop_back();
    180     return true;
    181 }
    182 
    183 }  // namespace android
    184 
    185 int main(int argc, char **argv)
    186 {
    187     FAIL_UNLESS(testConstructor);
    188     FAIL_UNLESS(testSize);
    189     FAIL_UNLESS(testClear);
    190     FAIL_UNLESS(testIterator);
    191     FAIL_UNLESS(testErase);
    192     FAIL_UNLESS(testEraseRange);
    193     FAIL_UNLESS(testPushPop);
    194     return kPassed;
    195 }
    196