libcxx

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

erase_if.pass.cpp (2561B)


      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 // <set>
     12 
     13 // template <class T, class Hash, class Compare, class Allocator, class Predicate>
     14 //   void erase_if(unordered_multiset<T, Hash, Compare, Allocator>& c, Predicate pred);
     15 
     16 #include <unordered_set>
     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 
     24 template <typename M>
     25 M make (Init vals)
     26 {
     27     M ret;
     28     for (int v : vals)
     29         ret.insert(v);
     30     return ret;
     31 }
     32 
     33 template <typename M, typename Pred>
     34 void
     35 test0(Init vals, Pred p, Init expected)
     36 {
     37     M s = make<M> (vals);
     38     ASSERT_SAME_TYPE(void, decltype(std::erase_if(s, p)));
     39     std::erase_if(s, p);
     40     M e = make<M>(expected);
     41     assert((std::is_permutation(s.begin(), s.end(), e.begin(), e.end())));
     42 }
     43 
     44 template <typename S>
     45 void test()
     46 {
     47     auto is1 = [](auto v) { return v == 1;};
     48     auto is2 = [](auto v) { return v == 2;};
     49     auto is3 = [](auto v) { return v == 3;};
     50     auto is4 = [](auto v) { return v == 4;};
     51     auto True  = [](auto) { return true; };
     52     auto False = [](auto) { return false; };
     53     
     54     test0<S>({}, is1, {});
     55 
     56     test0<S>({1}, is1, {});
     57     test0<S>({1}, is2, {1});
     58 
     59     test0<S>({1,2}, is1, {2});
     60     test0<S>({1,2}, is2, {1});
     61     test0<S>({1,2}, is3, {1,2});
     62     test0<S>({1,1}, is1, {});
     63     test0<S>({1,1}, is3, {1,1});
     64 
     65     test0<S>({1,2,3}, is1, {2,3});
     66     test0<S>({1,2,3}, is2, {1,3});
     67     test0<S>({1,2,3}, is3, {1,2});
     68     test0<S>({1,2,3}, is4, {1,2,3});
     69 
     70     test0<S>({1,1,1}, is1, {});
     71     test0<S>({1,1,1}, is2, {1,1,1});
     72     test0<S>({1,1,2}, is1, {2});
     73     test0<S>({1,1,2}, is2, {1,1});
     74     test0<S>({1,1,2}, is3, {1,1,2});
     75     test0<S>({1,2,2}, is1, {2,2});
     76     test0<S>({1,2,2}, is2, {1});
     77     test0<S>({1,2,2}, is3, {1,2,2});
     78     
     79     test0<S>({1,2,3}, True,  {});
     80     test0<S>({1,2,3}, False, {1,2,3});
     81 }
     82 
     83 int main()
     84 {
     85     test<std::unordered_multiset<int>>();
     86     test<std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, min_allocator<int>>> ();
     87     test<std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>> ();
     88 
     89     test<std::unordered_multiset<long>>();
     90     test<std::unordered_multiset<double>>();
     91 }