DR 538, [Ready]
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / min_max.cc
1 // 2000-03-29 sss/bkoz
2
3 // Copyright (C) 2000, 2003, 2004, 2005 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.
10
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.
15
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 #include <algorithm>
22 #include <functional>
23 #include <testsuite_hooks.h>
24
25 void test01()
26 {
27 bool test __attribute__((unused)) = true;
28
29 const int& x = std::max(1, 2);
30 const int& y = std::max(4, 3);
31 VERIFY( x == 2 );
32 VERIFY( y == 4 );
33
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 );
38
39 const int& z = std::min(1, 2);
40 const int& w = std::min(4, 3);
41 VERIFY( z == 1 );
42 VERIFY( w == 3 );
43
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 );
48 }
49
50 template<typename T>
51 struct A { static const T a; };
52
53 template<typename T>
54 const T A<T>::a = T(3);
55
56 void test02()
57 {
58 bool test __attribute__((unused)) = true;
59
60 VERIFY( 2 == std::min(A<int>::a, 2) );
61 VERIFY( 3 == std::min(A<int>::a, 4) );
62
63 VERIFY( 2u == std::min(A<unsigned int>::a, 2u) );
64 VERIFY( 3u == std::min(A<unsigned int>::a, 4u) );
65
66 VERIFY( 2l == std::min(A<long>::a, 2l) );
67 VERIFY( 3l == std::min(A<long>::a, 4l) );
68
69 VERIFY( 2ul == std::min(A<unsigned long>::a, 2ul) );
70 VERIFY( 3ul == std::min(A<unsigned long>::a, 4ul) );
71
72 #ifdef _GLIBCXX_USE_LONG_LONG
73 VERIFY( 2ll == std::min(A<long long>::a, 2ll) );
74 VERIFY( 3ll == std::min(A<long long>::a, 4ll) );
75
76 VERIFY( 2ull == std::min(A<unsigned long long>::a, 2ull) );
77 VERIFY( 3ull == std::min(A<unsigned long long>::a, 4ull) );
78 #endif
79
80 VERIFY( short(2) == std::min(A<short>::a, short(2)) );
81 VERIFY( short(3) == std::min(A<short>::a, short(4)) );
82
83 VERIFY( (unsigned short)2 == std::min(A<unsigned short>::a, (unsigned short)2) );
84 VERIFY( (unsigned short)3 == std::min(A<unsigned short>::a, (unsigned short)4) );
85
86 VERIFY( (char)2 == std::min(A<char>::a, (char)2) );
87 VERIFY( (char)3 == std::min(A<char>::a, (char)4) );
88
89 VERIFY( (signed char)2 == std::min(A<signed char>::a, (signed char)2) );
90 VERIFY( (signed char)3 == std::min(A<signed char>::a, (signed char)4) );
91
92 VERIFY( (unsigned char)2 == std::min(A<unsigned char>::a, (unsigned char)2) );
93 VERIFY( (unsigned char)3 == std::min(A<unsigned char>::a, (unsigned char)4) );
94
95 VERIFY( (wchar_t)2 == std::min(A<wchar_t>::a, (wchar_t)2) );
96 VERIFY( (wchar_t)3 == std::min(A<wchar_t>::a, (wchar_t)4) );
97
98 VERIFY( 2.0 == std::min(A<double>::a, 2.0) );
99 VERIFY( 3.0 == std::min(A<double>::a, 4.0) );
100
101 VERIFY( float(2) == std::min(A<float>::a, float(2)) );
102 VERIFY( float(3) == std::min(A<float>::a, float(4)) );
103
104 VERIFY( (long double)2 == std::min(A<long double>::a, (long double)2) );
105 VERIFY( (long double)3 == std::min(A<long double>::a, (long double)4) );
106
107
108 VERIFY( 3 == std::max(A<int>::a, 2) );
109 VERIFY( 4 == std::max(A<int>::a, 4) );
110
111 VERIFY( 3u == std::max(A<unsigned int>::a, 2u) );
112 VERIFY( 4u == std::max(A<unsigned int>::a, 4u) );
113
114 VERIFY( 3l == std::max(A<long>::a, 2l) );
115 VERIFY( 4l == std::max(A<long>::a, 4l) );
116
117 VERIFY( 3ul == std::max(A<unsigned long>::a, 2ul) );
118 VERIFY( 4ul == std::max(A<unsigned long>::a, 4ul) );
119
120 #ifdef _GLIBCXX_USE_LONG_LONG
121 VERIFY( 3ll == std::max(A<long long>::a, 2ll) );
122 VERIFY( 4ll == std::max(A<long long>::a, 4ll) );
123
124 VERIFY( 3ull == std::max(A<unsigned long long>::a, 2ull) );
125 VERIFY( 4ull == std::max(A<unsigned long long>::a, 4ull) );
126 #endif
127
128 VERIFY( short(3) == std::max(A<short>::a, short(2)) );
129 VERIFY( short(4) == std::max(A<short>::a, short(4)) );
130
131 VERIFY( (unsigned short)3 == std::max(A<unsigned short>::a, (unsigned short)2) );
132 VERIFY( (unsigned short)4 == std::max(A<unsigned short>::a, (unsigned short)4) );
133
134 VERIFY( (char)3 == std::max(A<char>::a, (char)2) );
135 VERIFY( (char)4 == std::max(A<char>::a, (char)4) );
136
137 VERIFY( (signed char)3 == std::max(A<signed char>::a, (signed char)2) );
138 VERIFY( (signed char)4 == std::max(A<signed char>::a, (signed char)4) );
139
140 VERIFY( (unsigned char)3 == std::max(A<unsigned char>::a, (unsigned char)2) );
141 VERIFY( (unsigned char)4 == std::max(A<unsigned char>::a, (unsigned char)4) );
142
143 VERIFY( (wchar_t)3 == std::max(A<wchar_t>::a, (wchar_t)2) );
144 VERIFY( (wchar_t)4 == std::max(A<wchar_t>::a, (wchar_t)4) );
145 }
146
147 int main()
148 {
149 test01();
150 test02();
151 return 0;
152 }