xref: /llvm-project/libcxx/test/std/containers/sequences/list/list.ops/unique_pred.pass.cpp (revision f5832bab6f5024cabe32a9f668b7f44e6b7cfef5)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <list>
10 
11 // template <class BinaryPred> void      unique(BinaryPred pred); // before C++20
12 // template <class BinaryPred> size_type unique(BinaryPred pred); // C++20 and later
13 
14 #include <list>
15 #include <cassert>
16 #include <functional>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 
g(int x,int y)21 bool g(int x, int y)
22 {
23     return x == y;
24 }
25 
26 struct PredLWG526 {
PredLWG526PredLWG52627   PredLWG526(int i) : i_(i) {}
~PredLWG526PredLWG52628   ~PredLWG526() { i_ = -32767; }
operator ()PredLWG52629   bool operator()(const PredLWG526& lhs, const PredLWG526& rhs) const { return lhs.i_ == rhs.i_; }
30 
operator ==PredLWG52631   bool operator==(int i) const { return i == i_; }
32   int i_;
33 };
34 
main(int,char **)35 int main(int, char**)
36 {
37     {
38     int a1[] = {2, 1, 1, 4, 4, 4, 4, 3, 3};
39     int a2[] = {2, 1, 4, 3};
40     typedef std::list<int> L;
41     L c(a1, a1+sizeof(a1)/sizeof(a1[0]));
42 #if TEST_STD_VER > 17
43     ASSERT_SAME_TYPE(L::size_type, decltype(c.unique(g)));
44     assert(c.unique(g) == 5);
45 #else
46     ASSERT_SAME_TYPE(void,         decltype(c.unique(g)));
47     c.unique(g);
48 #endif
49     assert(c == std::list<int>(a2, a2+4));
50     }
51 
52     { // LWG issue #526
53     int a1[] = {1, 1, 1, 2, 3, 5, 5, 2, 11};
54     int a2[] = {1,       2, 3, 5,    2, 11};
55     std::list<PredLWG526> c(a1, a1 + 9);
56 #if TEST_STD_VER > 17
57     assert(c.unique(std::ref(c.front())) == 3);
58 #else
59     c.unique(std::ref(c.front()));
60 #endif
61     assert(c.size() == 6);
62     for (std::size_t i = 0; i < c.size(); ++i)
63     {
64         assert(c.front() == a2[i]);
65         c.pop_front();
66     }
67     }
68 
69 #if TEST_STD_VER >= 11
70     {
71     int a1[] = {2, 1, 1, 4, 4, 4, 4, 3, 3};
72     int a2[] = {2, 1, 4, 3};
73     std::list<int, min_allocator<int>> c(a1, a1+sizeof(a1)/sizeof(a1[0]));
74 #if TEST_STD_VER > 17
75     assert(c.unique(g) == 5);
76 #else
77     c.unique(g);
78 #endif
79     assert((c == std::list<int, min_allocator<int>>(a2, a2+4)));
80     }
81 #endif
82 
83   return 0;
84 }
85