libcxx

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

erase_if.pass.cpp (2303B)


      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 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
     10 
     11 // <unordered_map>
     12 
     13 // template <class Key, class T, class Hash, class Pred, class Allocator, class Predicate>
     14 //   void erase_if(unordered_map<Key, T, Hash, Pred, Allocator>& c, Predicate pred);
     15 
     16 #include <unordered_map>
     17 
     18 #include "test_macros.h"
     19 #include "test_allocator.h"
     20 #include "min_allocator.h"
     21 
     22 using Init = std::initializer_list<int>;
     23 template <typename M>
     24 M make (Init vals)
     25 {
     26     M ret;
     27     for (int v : vals)
     28         ret[v] = v + 10;
     29     return ret;
     30 }
     31 
     32 template <typename M, typename Pred>
     33 void
     34 test0(Init vals, Pred p, Init expected)
     35 {
     36     M s = make<M> (vals);
     37     ASSERT_SAME_TYPE(void, decltype(std::erase_if(s, p)));
     38     std::erase_if(s, p);
     39     M e = make<M>(expected);
     40     assert((std::is_permutation(s.begin(), s.end(), e.begin(), e.end())));
     41 }
     42 
     43 template <typename S>
     44 void test()
     45 {
     46     auto is1 = [](auto v) { return v.first == 1;};
     47     auto is2 = [](auto v) { return v.first == 2;};
     48     auto is3 = [](auto v) { return v.first == 3;};
     49     auto is4 = [](auto v) { return v.first == 4;};
     50     auto True  = [](auto) { return true; };
     51     auto False = [](auto) { return false; };
     52     
     53     test0<S>({}, is1, {});
     54 
     55     test0<S>({1}, is1, {});
     56     test0<S>({1}, is2, {1});
     57 
     58     test0<S>({1,2}, is1, {2});
     59     test0<S>({1,2}, is2, {1});
     60     test0<S>({1,2}, is3, {1,2});
     61 
     62     test0<S>({1,2,3}, is1, {2,3});
     63     test0<S>({1,2,3}, is2, {1,3});
     64     test0<S>({1,2,3}, is3, {1,2});
     65     test0<S>({1,2,3}, is4, {1,2,3});
     66 
     67     test0<S>({1,2,3}, True,  {});
     68     test0<S>({1,2,3}, False, {1,2,3});
     69 }
     70 
     71 int main()
     72 {
     73     test<std::unordered_map<int, int>>();
     74     test<std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, min_allocator<std::pair<const int, int>>>> ();
     75     test<std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int, int>>>> ();
     76 
     77     test<std::unordered_map<long, short>>();
     78     test<std::unordered_map<short, double>>();
     79 }
     80