1 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17 
18 // 23.2.2.4 list operations [lib.list.ops]
19 
20 #include <testsuite_hooks.h>
21 
22 // A comparison predicate to order by rightmost digit.  Tracks call
23 // counts for performance checks.
24 struct CompLastLt
25 {
operatorCompLastLt26   bool operator()(const int x, const int y)
27   { ++itsCount; return x % 10 < y % 10; }
countCompLastLt28   static int count() { return itsCount; }
resetCompLastLt29   static void reset() { itsCount = 0; }
30   static int itsCount;
31 };
32 
33 int CompLastLt::itsCount;
34 
35 struct CompLastEq
36 {
operatorCompLastEq37   bool operator()(const int x, const int y)
38   { ++itsCount; return x % 10 == y % 10; }
countCompLastEq39   static int count() { return itsCount; }
resetCompLastEq40   static void reset() { itsCount = 0; }
41   static int itsCount;
42 };
43 
44 int CompLastEq::itsCount;
45 
46 // sort(pred) + merge(pred) + unique(pred)
47 // also checks performance requirements
48 template<typename _Tp>
49 void
operations04()50 operations04()
51 {
52   bool test __attribute__((unused)) = true;
53   typedef _Tp list_type;
54 
55   const int A[] = {1, 2, 3, 4, 5, 6};
56   const int B[] = {12, 15, 13, 14, 11};
57   const int C[] = {11, 12, 13, 14, 15};
58   const int D[] = {1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6};
59   const int N = sizeof(A) / sizeof(int);
60   const int M = sizeof(B) / sizeof(int);
61   const int Q = sizeof(D) / sizeof(int);
62 
63   list_type list0401(A, A + N);
64   list_type list0402(B, B + M);
65   list_type list0403(C, C + M);
66   list_type list0404(D, D + Q);
67   list_type list0405(A, A + N);
68 
69   // sort B
70   CompLastLt lt;
71 
72   CompLastLt::reset();
73   list0402.sort(lt);
74   VERIFY(list0402 == list0403);
75 
76   CompLastLt::reset();
77   list0401.merge(list0402, lt);
78   VERIFY(list0401 == list0404);
79 #ifndef _GLIBCXX_DEBUG
80   VERIFY(lt.count() <= (N + M - 1));
81 #endif
82 
83   CompLastEq eq;
84 
85   CompLastEq::reset();
86   list0401.unique(eq);
87   VERIFY(list0401 == list0405);
88 #ifndef _GLIBCXX_DEBUG
89   VERIFY(eq.count() == (N + M - 1));
90 #endif
91 }
92 
93