2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / list / operators / 4.cc
blobbbebfb59a23b415a0d235ea54b6e4d222816f2d4
1 // Copyright (C) 2001 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 2, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without Pred the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
19 // 23.2.2.4 list operations [lib.list.ops]
21 #include <list>
22 #include <testsuite_hooks.h>
24 bool test __attribute__((unused)) = true;
26 // A comparison predicate to order by rightmost digit. Tracks call counts for
27 // performance checks.
28 struct CompLastLt
30 bool operator()(const int x, const int y)
31 { ++itsCount; return x % 10 < y % 10; }
32 static int count() { return itsCount; }
33 static void reset() { itsCount = 0; }
34 static int itsCount;
37 int CompLastLt::itsCount;
39 struct CompLastEq
41 bool operator()(const int x, const int y)
42 { ++itsCount; return x % 10 == y % 10; }
43 static int count() { return itsCount; }
44 static void reset() { itsCount = 0; }
45 static int itsCount;
48 int CompLastEq::itsCount;
50 // sort(pred) + merge(pred) + unique(pred)
51 // also checks performance requirements
52 void
53 test04()
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);
63 std::list<int> list0401(A, A + N);
64 std::list<int> list0402(B, B + M);
65 std::list<int> list0403(C, C + M);
66 std::list<int> list0404(D, D + Q);
67 std::list<int> list0405(A, A + N);
69 // sort B
70 CompLastLt lt;
72 CompLastLt::reset();
73 list0402.sort(lt);
74 VERIFY(list0402 == list0403);
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
83 CompLastEq eq;
85 CompLastEq::reset();
86 list0401.unique(eq);
87 VERIFY(list0401 == list0405);
88 #ifndef _GLIBCXX_DEBUG
89 VERIFY(eq.count() == (N + M - 1));
90 #endif
93 int main()
95 test04();
96 return 0;
98 // vi:set sw=2 ts=2: