libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

at.pass.cpp (4050B)


      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& at(const key_type& k);
     15 // const mapped_type& at(const key_type& k) const;
     16 
     17 #include <cassert>
     18 #include <map>
     19 #include <stdexcept>
     20 
     21 #include "min_allocator.h"
     22 #include "test_macros.h"
     23 
     24 int main()
     25 {
     26     {
     27         typedef std::pair<const int, double> V;
     28         V ar[] =
     29         {
     30             V(1, 1.5),
     31             V(2, 2.5),
     32             V(3, 3.5),
     33             V(4, 4.5),
     34             V(5, 5.5),
     35             V(7, 7.5),
     36             V(8, 8.5),
     37         };
     38         std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
     39         assert(m.size() == 7);
     40         assert(m.at(1) == 1.5);
     41         m.at(1) = -1.5;
     42         assert(m.at(1) == -1.5);
     43         assert(m.at(2) == 2.5);
     44         assert(m.at(3) == 3.5);
     45         assert(m.at(4) == 4.5);
     46         assert(m.at(5) == 5.5);
     47 #ifndef TEST_HAS_NO_EXCEPTIONS
     48         try
     49         {
     50             TEST_IGNORE_NODISCARD m.at(6);
     51             assert(false);
     52         }
     53         catch (std::out_of_range&)
     54         {
     55         }
     56 #endif
     57         assert(m.at(7) == 7.5);
     58         assert(m.at(8) == 8.5);
     59         assert(m.size() == 7);
     60     }
     61     {
     62         typedef std::pair<const int, double> V;
     63         V ar[] =
     64         {
     65             V(1, 1.5),
     66             V(2, 2.5),
     67             V(3, 3.5),
     68             V(4, 4.5),
     69             V(5, 5.5),
     70             V(7, 7.5),
     71             V(8, 8.5),
     72         };
     73         const std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
     74         assert(m.size() == 7);
     75         assert(m.at(1) == 1.5);
     76         assert(m.at(2) == 2.5);
     77         assert(m.at(3) == 3.5);
     78         assert(m.at(4) == 4.5);
     79         assert(m.at(5) == 5.5);
     80 #ifndef TEST_HAS_NO_EXCEPTIONS
     81         try
     82         {
     83             TEST_IGNORE_NODISCARD m.at(6);
     84             assert(false);
     85         }
     86         catch (std::out_of_range&)
     87         {
     88         }
     89 #endif
     90         assert(m.at(7) == 7.5);
     91         assert(m.at(8) == 8.5);
     92         assert(m.size() == 7);
     93     }
     94 #if TEST_STD_VER >= 11
     95     {
     96         typedef std::pair<const int, double> V;
     97         V ar[] =
     98         {
     99             V(1, 1.5),
    100             V(2, 2.5),
    101             V(3, 3.5),
    102             V(4, 4.5),
    103             V(5, 5.5),
    104             V(7, 7.5),
    105             V(8, 8.5),
    106         };
    107         std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
    108         assert(m.size() == 7);
    109         assert(m.at(1) == 1.5);
    110         m.at(1) = -1.5;
    111         assert(m.at(1) == -1.5);
    112         assert(m.at(2) == 2.5);
    113         assert(m.at(3) == 3.5);
    114         assert(m.at(4) == 4.5);
    115         assert(m.at(5) == 5.5);
    116 #ifndef TEST_HAS_NO_EXCEPTIONS
    117         try
    118         {
    119             TEST_IGNORE_NODISCARD m.at(6);
    120             assert(false);
    121         }
    122         catch (std::out_of_range&)
    123         {
    124         }
    125 #endif
    126         assert(m.at(7) == 7.5);
    127         assert(m.at(8) == 8.5);
    128         assert(m.size() == 7);
    129     }
    130     {
    131         typedef std::pair<const int, double> V;
    132         V ar[] =
    133         {
    134             V(1, 1.5),
    135             V(2, 2.5),
    136             V(3, 3.5),
    137             V(4, 4.5),
    138             V(5, 5.5),
    139             V(7, 7.5),
    140             V(8, 8.5),
    141         };
    142         const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
    143         assert(m.size() == 7);
    144         assert(m.at(1) == 1.5);
    145         assert(m.at(2) == 2.5);
    146         assert(m.at(3) == 3.5);
    147         assert(m.at(4) == 4.5);
    148         assert(m.at(5) == 5.5);
    149 #ifndef TEST_HAS_NO_EXCEPTIONS
    150         try
    151         {
    152             TEST_IGNORE_NODISCARD m.at(6);
    153             assert(false);
    154         }
    155         catch (std::out_of_range&)
    156         {
    157         }
    158 #endif
    159         assert(m.at(7) == 7.5);
    160         assert(m.at(8) == 8.5);
    161         assert(m.size() == 7);
    162     }
    163 #endif
    164 }