Home | History | Annotate | Download | only in map.modifiers
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <map>
     11 
     12 // class map
     13 
     14 // size_type erase(const key_type& k);
     15 
     16 #include <map>
     17 #include <cassert>
     18 
     19 #include "min_allocator.h"
     20 
     21 int main()
     22 {
     23     {
     24         typedef std::map<int, double> M;
     25         typedef std::pair<int, double> P;
     26         typedef M::size_type R;
     27         P ar[] =
     28         {
     29             P(1, 1.5),
     30             P(2, 2.5),
     31             P(3, 3.5),
     32             P(4, 4.5),
     33             P(5, 5.5),
     34             P(6, 6.5),
     35             P(7, 7.5),
     36             P(8, 8.5),
     37         };
     38         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
     39         assert(m.size() == 8);
     40         R s = m.erase(9);
     41         assert(s == 0);
     42         assert(m.size() == 8);
     43         assert(m.begin()->first == 1);
     44         assert(m.begin()->second == 1.5);
     45         assert(next(m.begin())->first == 2);
     46         assert(next(m.begin())->second == 2.5);
     47         assert(next(m.begin(), 2)->first == 3);
     48         assert(next(m.begin(), 2)->second == 3.5);
     49         assert(next(m.begin(), 3)->first == 4);
     50         assert(next(m.begin(), 3)->second == 4.5);
     51         assert(next(m.begin(), 4)->first == 5);
     52         assert(next(m.begin(), 4)->second == 5.5);
     53         assert(next(m.begin(), 5)->first == 6);
     54         assert(next(m.begin(), 5)->second == 6.5);
     55         assert(next(m.begin(), 6)->first == 7);
     56         assert(next(m.begin(), 6)->second == 7.5);
     57         assert(next(m.begin(), 7)->first == 8);
     58         assert(next(m.begin(), 7)->second == 8.5);
     59 
     60         s = m.erase(4);
     61         assert(m.size() == 7);
     62         assert(s == 1);
     63         assert(m.begin()->first == 1);
     64         assert(m.begin()->second == 1.5);
     65         assert(next(m.begin())->first == 2);
     66         assert(next(m.begin())->second == 2.5);
     67         assert(next(m.begin(), 2)->first == 3);
     68         assert(next(m.begin(), 2)->second == 3.5);
     69         assert(next(m.begin(), 3)->first == 5);
     70         assert(next(m.begin(), 3)->second == 5.5);
     71         assert(next(m.begin(), 4)->first == 6);
     72         assert(next(m.begin(), 4)->second == 6.5);
     73         assert(next(m.begin(), 5)->first == 7);
     74         assert(next(m.begin(), 5)->second == 7.5);
     75         assert(next(m.begin(), 6)->first == 8);
     76         assert(next(m.begin(), 6)->second == 8.5);
     77 
     78         s = m.erase(1);
     79         assert(m.size() == 6);
     80         assert(s == 1);
     81         assert(m.begin()->first == 2);
     82         assert(m.begin()->second == 2.5);
     83         assert(next(m.begin())->first == 3);
     84         assert(next(m.begin())->second == 3.5);
     85         assert(next(m.begin(), 2)->first == 5);
     86         assert(next(m.begin(), 2)->second == 5.5);
     87         assert(next(m.begin(), 3)->first == 6);
     88         assert(next(m.begin(), 3)->second == 6.5);
     89         assert(next(m.begin(), 4)->first == 7);
     90         assert(next(m.begin(), 4)->second == 7.5);
     91         assert(next(m.begin(), 5)->first == 8);
     92         assert(next(m.begin(), 5)->second == 8.5);
     93 
     94         s = m.erase(8);
     95         assert(m.size() == 5);
     96         assert(s == 1);
     97         assert(m.begin()->first == 2);
     98         assert(m.begin()->second == 2.5);
     99         assert(next(m.begin())->first == 3);
    100         assert(next(m.begin())->second == 3.5);
    101         assert(next(m.begin(), 2)->first == 5);
    102         assert(next(m.begin(), 2)->second == 5.5);
    103         assert(next(m.begin(), 3)->first == 6);
    104         assert(next(m.begin(), 3)->second == 6.5);
    105         assert(next(m.begin(), 4)->first == 7);
    106         assert(next(m.begin(), 4)->second == 7.5);
    107 
    108         s = m.erase(3);
    109         assert(m.size() == 4);
    110         assert(s == 1);
    111         assert(m.begin()->first == 2);
    112         assert(m.begin()->second == 2.5);
    113         assert(next(m.begin())->first == 5);
    114         assert(next(m.begin())->second == 5.5);
    115         assert(next(m.begin(), 2)->first == 6);
    116         assert(next(m.begin(), 2)->second == 6.5);
    117         assert(next(m.begin(), 3)->first == 7);
    118         assert(next(m.begin(), 3)->second == 7.5);
    119 
    120         s = m.erase(6);
    121         assert(m.size() == 3);
    122         assert(s == 1);
    123         assert(m.begin()->first == 2);
    124         assert(m.begin()->second == 2.5);
    125         assert(next(m.begin())->first == 5);
    126         assert(next(m.begin())->second == 5.5);
    127         assert(next(m.begin(), 2)->first == 7);
    128         assert(next(m.begin(), 2)->second == 7.5);
    129 
    130         s = m.erase(7);
    131         assert(m.size() == 2);
    132         assert(s == 1);
    133         assert(m.begin()->first == 2);
    134         assert(m.begin()->second == 2.5);
    135         assert(next(m.begin())->first == 5);
    136         assert(next(m.begin())->second == 5.5);
    137 
    138         s = m.erase(2);
    139         assert(m.size() == 1);
    140         assert(s == 1);
    141         assert(m.begin()->first == 5);
    142         assert(m.begin()->second == 5.5);
    143 
    144         s = m.erase(5);
    145         assert(m.size() == 0);
    146         assert(s == 1);
    147     }
    148 #if TEST_STD_VER >= 11
    149     {
    150         typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
    151         typedef std::pair<int, double> P;
    152         typedef M::size_type R;
    153         P ar[] =
    154         {
    155             P(1, 1.5),
    156             P(2, 2.5),
    157             P(3, 3.5),
    158             P(4, 4.5),
    159             P(5, 5.5),
    160             P(6, 6.5),
    161             P(7, 7.5),
    162             P(8, 8.5),
    163         };
    164         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
    165         assert(m.size() == 8);
    166         R s = m.erase(9);
    167         assert(s == 0);
    168         assert(m.size() == 8);
    169         assert(m.begin()->first == 1);
    170         assert(m.begin()->second == 1.5);
    171         assert(next(m.begin())->first == 2);
    172         assert(next(m.begin())->second == 2.5);
    173         assert(next(m.begin(), 2)->first == 3);
    174         assert(next(m.begin(), 2)->second == 3.5);
    175         assert(next(m.begin(), 3)->first == 4);
    176         assert(next(m.begin(), 3)->second == 4.5);
    177         assert(next(m.begin(), 4)->first == 5);
    178         assert(next(m.begin(), 4)->second == 5.5);
    179         assert(next(m.begin(), 5)->first == 6);
    180         assert(next(m.begin(), 5)->second == 6.5);
    181         assert(next(m.begin(), 6)->first == 7);
    182         assert(next(m.begin(), 6)->second == 7.5);
    183         assert(next(m.begin(), 7)->first == 8);
    184         assert(next(m.begin(), 7)->second == 8.5);
    185 
    186         s = m.erase(4);
    187         assert(m.size() == 7);
    188         assert(s == 1);
    189         assert(m.begin()->first == 1);
    190         assert(m.begin()->second == 1.5);
    191         assert(next(m.begin())->first == 2);
    192         assert(next(m.begin())->second == 2.5);
    193         assert(next(m.begin(), 2)->first == 3);
    194         assert(next(m.begin(), 2)->second == 3.5);
    195         assert(next(m.begin(), 3)->first == 5);
    196         assert(next(m.begin(), 3)->second == 5.5);
    197         assert(next(m.begin(), 4)->first == 6);
    198         assert(next(m.begin(), 4)->second == 6.5);
    199         assert(next(m.begin(), 5)->first == 7);
    200         assert(next(m.begin(), 5)->second == 7.5);
    201         assert(next(m.begin(), 6)->first == 8);
    202         assert(next(m.begin(), 6)->second == 8.5);
    203 
    204         s = m.erase(1);
    205         assert(m.size() == 6);
    206         assert(s == 1);
    207         assert(m.begin()->first == 2);
    208         assert(m.begin()->second == 2.5);
    209         assert(next(m.begin())->first == 3);
    210         assert(next(m.begin())->second == 3.5);
    211         assert(next(m.begin(), 2)->first == 5);
    212         assert(next(m.begin(), 2)->second == 5.5);
    213         assert(next(m.begin(), 3)->first == 6);
    214         assert(next(m.begin(), 3)->second == 6.5);
    215         assert(next(m.begin(), 4)->first == 7);
    216         assert(next(m.begin(), 4)->second == 7.5);
    217         assert(next(m.begin(), 5)->first == 8);
    218         assert(next(m.begin(), 5)->second == 8.5);
    219 
    220         s = m.erase(8);
    221         assert(m.size() == 5);
    222         assert(s == 1);
    223         assert(m.begin()->first == 2);
    224         assert(m.begin()->second == 2.5);
    225         assert(next(m.begin())->first == 3);
    226         assert(next(m.begin())->second == 3.5);
    227         assert(next(m.begin(), 2)->first == 5);
    228         assert(next(m.begin(), 2)->second == 5.5);
    229         assert(next(m.begin(), 3)->first == 6);
    230         assert(next(m.begin(), 3)->second == 6.5);
    231         assert(next(m.begin(), 4)->first == 7);
    232         assert(next(m.begin(), 4)->second == 7.5);
    233 
    234         s = m.erase(3);
    235         assert(m.size() == 4);
    236         assert(s == 1);
    237         assert(m.begin()->first == 2);
    238         assert(m.begin()->second == 2.5);
    239         assert(next(m.begin())->first == 5);
    240         assert(next(m.begin())->second == 5.5);
    241         assert(next(m.begin(), 2)->first == 6);
    242         assert(next(m.begin(), 2)->second == 6.5);
    243         assert(next(m.begin(), 3)->first == 7);
    244         assert(next(m.begin(), 3)->second == 7.5);
    245 
    246         s = m.erase(6);
    247         assert(m.size() == 3);
    248         assert(s == 1);
    249         assert(m.begin()->first == 2);
    250         assert(m.begin()->second == 2.5);
    251         assert(next(m.begin())->first == 5);
    252         assert(next(m.begin())->second == 5.5);
    253         assert(next(m.begin(), 2)->first == 7);
    254         assert(next(m.begin(), 2)->second == 7.5);
    255 
    256         s = m.erase(7);
    257         assert(m.size() == 2);
    258         assert(s == 1);
    259         assert(m.begin()->first == 2);
    260         assert(m.begin()->second == 2.5);
    261         assert(next(m.begin())->first == 5);
    262         assert(next(m.begin())->second == 5.5);
    263 
    264         s = m.erase(2);
    265         assert(m.size() == 1);
    266         assert(s == 1);
    267         assert(m.begin()->first == 5);
    268         assert(m.begin()->second == 5.5);
    269 
    270         s = m.erase(5);
    271         assert(m.size() == 0);
    272         assert(s == 1);
    273     }
    274 #endif
    275 }
    276