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
21bool g(int x, int y) { return x == y; }
22
23struct PredLWG526 {
24 PredLWG526(int i) : i_(i) {}
25 ~PredLWG526() { i_ = -32767; }
26 bool operator()(const PredLWG526& lhs, const PredLWG526& rhs) const { return lhs.i_ == rhs.i_; }
27
28 bool operator==(int i) const { return i == i_; }
29 int i_;
30};
31
32int main(int, char**) {
33 {
34 int a1[] = {2, 1, 1, 4, 4, 4, 4, 3, 3};
35 int a2[] = {2, 1, 4, 3};
36 typedef std::list<int> L;
37 L c(a1, a1 + sizeof(a1) / sizeof(a1[0]));
38#if TEST_STD_VER > 17
39 ASSERT_SAME_TYPE(L::size_type, decltype(c.unique(g)));
40 assert(c.unique(g) == 5);
41#else
42 ASSERT_SAME_TYPE(void, decltype(c.unique(g)));
43 c.unique(binary_pred: g);
44#endif
45 assert(c == std::list<int>(a2, a2 + 4));
46 }
47
48 { // LWG issue #526
49 int a1[] = {1, 1, 1, 2, 3, 5, 5, 2, 11};
50 int a2[] = {1, 2, 3, 5, 2, 11};
51 std::list<PredLWG526> c(a1, a1 + 9);
52#if TEST_STD_VER > 17
53 assert(c.unique(std::ref(c.front())) == 3);
54#else
55 c.unique(binary_pred: std::ref(t&: c.front()));
56#endif
57 assert(c.size() == 6);
58 for (std::size_t i = 0; i < c.size(); ++i) {
59 assert(c.front() == a2[i]);
60 c.pop_front();
61 }
62 }
63
64#if TEST_STD_VER >= 11
65 {
66 int a1[] = {2, 1, 1, 4, 4, 4, 4, 3, 3};
67 int a2[] = {2, 1, 4, 3};
68 std::list<int, min_allocator<int>> c(a1, a1 + sizeof(a1) / sizeof(a1[0]));
69# if TEST_STD_VER > 17
70 assert(c.unique(g) == 5);
71# else
72 c.unique(g);
73# endif
74 assert((c == std::list<int, min_allocator<int>>(a2, a2 + 4)));
75 }
76#endif
77
78 return 0;
79}
80

source code of libcxx/test/std/containers/sequences/list/list.ops/unique_pred.pass.cpp