2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / min_max.cc
blobbd07009c015b3697c4dc13e4ff14b2a53bb5c9f9
1 // 2000-03-29 sss/bkoz
3 // Copyright (C) 2000, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 #include <algorithm>
22 #include <functional>
23 #include <testsuite_hooks.h>
25 void test01()
27 bool test __attribute__((unused)) = true;
29 const int& x = std::max(1, 2);
30 const int& y = std::max(4, 3);
31 VERIFY( x == 2 );
32 VERIFY( y == 4 );
34 const int& xc = std::max(1, 2, std::greater<int>());
35 const int& yc = std::max(4, 3, std::greater<int>());
36 VERIFY( xc == 1 );
37 VERIFY( yc == 3 );
39 const int& z = std::min(1, 2);
40 const int& w = std::min(4, 3);
41 VERIFY( z == 1 );
42 VERIFY( w == 3 );
44 const int& zc = std::min(1, 2, std::greater<int>());
45 const int& wc = std::min(4, 3, std::greater<int>());
46 VERIFY( zc == 2 );
47 VERIFY( wc == 4 );
50 template<typename T>
51 struct A { static const T a; };
53 template<typename T>
54 const T A<T>::a = T(3);
56 #if !__GXX_WEAK__
57 // Explicitly instantiate for systems with no COMDAT or weak support.
58 template int A<int>::a;
59 template int A<unsigned int>::a;
60 template int A<short>::a;
61 template int A<unsigned short>::a;
62 template int A<long>::a;
63 template int A<unsigned long>::a;
64 template int A<long long>::a;
65 template int A<unsigned long long>::a;
66 template int A<char>::a;
67 template int A<signed char>::a;
68 template int A<unsigned char>::a;
69 template int A<wchar_t>::a;
70 template int A<float>::a;
71 template int A<double>::a;
72 template int A<long double>::a;
73 #endif
75 void test02()
77 bool test __attribute__((unused)) = true;
79 VERIFY( 2 == std::min(A<int>::a, 2) );
80 VERIFY( 3 == std::min(A<int>::a, 4) );
82 VERIFY( 2u == std::min(A<unsigned int>::a, 2u) );
83 VERIFY( 3u == std::min(A<unsigned int>::a, 4u) );
85 VERIFY( 2l == std::min(A<long>::a, 2l) );
86 VERIFY( 3l == std::min(A<long>::a, 4l) );
88 VERIFY( 2ul == std::min(A<unsigned long>::a, 2ul) );
89 VERIFY( 3ul == std::min(A<unsigned long>::a, 4ul) );
91 #ifdef _GLIBCXX_USE_LONG_LONG
92 VERIFY( 2ll == std::min(A<long long>::a, 2ll) );
93 VERIFY( 3ll == std::min(A<long long>::a, 4ll) );
95 VERIFY( 2ull == std::min(A<unsigned long long>::a, 2ull) );
96 VERIFY( 3ull == std::min(A<unsigned long long>::a, 4ull) );
97 #endif
99 VERIFY( short(2) == std::min(A<short>::a, short(2)) );
100 VERIFY( short(3) == std::min(A<short>::a, short(4)) );
102 VERIFY( (unsigned short)2 == std::min(A<unsigned short>::a, (unsigned short)2) );
103 VERIFY( (unsigned short)3 == std::min(A<unsigned short>::a, (unsigned short)4) );
105 VERIFY( (char)2 == std::min(A<char>::a, (char)2) );
106 VERIFY( (char)3 == std::min(A<char>::a, (char)4) );
108 VERIFY( (signed char)2 == std::min(A<signed char>::a, (signed char)2) );
109 VERIFY( (signed char)3 == std::min(A<signed char>::a, (signed char)4) );
111 VERIFY( (unsigned char)2 == std::min(A<unsigned char>::a, (unsigned char)2) );
112 VERIFY( (unsigned char)3 == std::min(A<unsigned char>::a, (unsigned char)4) );
114 VERIFY( (wchar_t)2 == std::min(A<wchar_t>::a, (wchar_t)2) );
115 VERIFY( (wchar_t)3 == std::min(A<wchar_t>::a, (wchar_t)4) );
117 VERIFY( 2.0 == std::min(A<double>::a, 2.0) );
118 VERIFY( 3.0 == std::min(A<double>::a, 4.0) );
120 VERIFY( float(2) == std::min(A<float>::a, float(2)) );
121 VERIFY( float(3) == std::min(A<float>::a, float(4)) );
123 VERIFY( (long double)2 == std::min(A<long double>::a, (long double)2) );
124 VERIFY( (long double)3 == std::min(A<long double>::a, (long double)4) );
127 VERIFY( 3 == std::max(A<int>::a, 2) );
128 VERIFY( 4 == std::max(A<int>::a, 4) );
130 VERIFY( 3u == std::max(A<unsigned int>::a, 2u) );
131 VERIFY( 4u == std::max(A<unsigned int>::a, 4u) );
133 VERIFY( 3l == std::max(A<long>::a, 2l) );
134 VERIFY( 4l == std::max(A<long>::a, 4l) );
136 VERIFY( 3ul == std::max(A<unsigned long>::a, 2ul) );
137 VERIFY( 4ul == std::max(A<unsigned long>::a, 4ul) );
139 #ifdef _GLIBCXX_USE_LONG_LONG
140 VERIFY( 3ll == std::max(A<long long>::a, 2ll) );
141 VERIFY( 4ll == std::max(A<long long>::a, 4ll) );
143 VERIFY( 3ull == std::max(A<unsigned long long>::a, 2ull) );
144 VERIFY( 4ull == std::max(A<unsigned long long>::a, 4ull) );
145 #endif
147 VERIFY( short(3) == std::max(A<short>::a, short(2)) );
148 VERIFY( short(4) == std::max(A<short>::a, short(4)) );
150 VERIFY( (unsigned short)3 == std::max(A<unsigned short>::a, (unsigned short)2) );
151 VERIFY( (unsigned short)4 == std::max(A<unsigned short>::a, (unsigned short)4) );
153 VERIFY( (char)3 == std::max(A<char>::a, (char)2) );
154 VERIFY( (char)4 == std::max(A<char>::a, (char)4) );
156 VERIFY( (signed char)3 == std::max(A<signed char>::a, (signed char)2) );
157 VERIFY( (signed char)4 == std::max(A<signed char>::a, (signed char)4) );
159 VERIFY( (unsigned char)3 == std::max(A<unsigned char>::a, (unsigned char)2) );
160 VERIFY( (unsigned char)4 == std::max(A<unsigned char>::a, (unsigned char)4) );
162 VERIFY( (wchar_t)3 == std::max(A<wchar_t>::a, (wchar_t)2) );
163 VERIFY( (wchar_t)4 == std::max(A<wchar_t>::a, (wchar_t)4) );
166 int main()
168 test01();
169 test02();
170 return 0;