Home | History | Annotate | Download | only in map.access
      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 // mapped_type& operator[](const key_type& k);
     15 
     16 #include <map>
     17 #include <cassert>
     18 
     19 #include "test_macros.h"
     20 #include "count_new.hpp"
     21 #include "min_allocator.h"
     22 #include "private_constructor.hpp"
     23 #if TEST_STD_VER >= 11
     24 #include "container_test_types.h"
     25 #endif
     26 
     27 int main()
     28 {
     29     {
     30     typedef std::pair<const int, double> V;
     31     V ar[] =
     32     {
     33         V(1, 1.5),
     34         V(2, 2.5),
     35         V(3, 3.5),
     36         V(4, 4.5),
     37         V(5, 5.5),
     38         V(7, 7.5),
     39         V(8, 8.5),
     40     };
     41     std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
     42     assert(m.size() == 7);
     43     assert(m[1] == 1.5);
     44     assert(m.size() == 7);
     45     m[1] = -1.5;
     46     assert(m[1] == -1.5);
     47     assert(m.size() == 7);
     48     assert(m[6] == 0);
     49     assert(m.size() == 8);
     50     m[6] = 6.5;
     51     assert(m[6] == 6.5);
     52     assert(m.size() == 8);
     53     }
     54 #if TEST_STD_VER >= 11
     55     {
     56     typedef std::pair<const int, double> V;
     57     V ar[] =
     58     {
     59         V(1, 1.5),
     60         V(2, 2.5),
     61         V(3, 3.5),
     62         V(4, 4.5),
     63         V(5, 5.5),
     64         V(7, 7.5),
     65         V(8, 8.5),
     66     };
     67     std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
     68     assert(m.size() == 7);
     69     assert(m[1] == 1.5);
     70     assert(m.size() == 7);
     71     const int i = 1;
     72     m[i] = -1.5;
     73     assert(m[1] == -1.5);
     74     assert(m.size() == 7);
     75     assert(m[6] == 0);
     76     assert(m.size() == 8);
     77     m[6] = 6.5;
     78     assert(m[6] == 6.5);
     79     assert(m.size() == 8);
     80     }
     81     {
     82         // Use "container_test_types.h" to check what arguments get passed
     83         // to the allocator for operator[]
     84         using Container = TCT::map<>;
     85         using Key = Container::key_type;
     86         using MappedType = Container::mapped_type;
     87         ConstructController* cc = getConstructController();
     88         cc->reset();
     89         {
     90             Container c;
     91             const Key k(1);
     92             cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>();
     93             MappedType& mref = c[k];
     94             assert(!cc->unchecked());
     95             {
     96                 DisableAllocationGuard g;
     97                 MappedType& mref2 = c[k];
     98                 assert(&mref == &mref2);
     99             }
    100         }
    101         {
    102             Container c;
    103             Key k(1);
    104             cc->expect<std::piecewise_construct_t const&, std::tuple<Key const&>&&, std::tuple<>&&>();
    105             MappedType& mref = c[k];
    106             assert(!cc->unchecked());
    107             {
    108                 DisableAllocationGuard g;
    109                 MappedType& mref2 = c[k];
    110                 assert(&mref == &mref2);
    111             }
    112         }
    113     }
    114 #endif
    115 #if TEST_STD_VER > 11
    116     {
    117     typedef std::pair<const int, double> V;
    118     V ar[] =
    119     {
    120         V(1, 1.5),
    121         V(2, 2.5),
    122         V(3, 3.5),
    123         V(4, 4.5),
    124         V(5, 5.5),
    125         V(7, 7.5),
    126         V(8, 8.5),
    127     };
    128     std::map<int, double, std::less<>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
    129 
    130     assert(m.size() == 7);
    131     assert(m[1] == 1.5);
    132     assert(m.size() == 7);
    133     m[1] = -1.5;
    134     assert(m[1] == -1.5);
    135     assert(m.size() == 7);
    136     assert(m[6] == 0);
    137     assert(m.size() == 8);
    138     m[6] = 6.5;
    139     assert(m[6] == 6.5);
    140     assert(m.size() == 8);
    141     }
    142 #endif
    143 }
    144