1 #include <vector>
2 #include <algorithm>
3
4 #include "cppunit/cppunit_proxy.h"
5
6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
7 using namespace std;
8 #endif
9
10 //
11 // TestCase class
12 //
13 class MinTest : public CPPUNIT_NS::TestCase
14 {
15 CPPUNIT_TEST_SUITE(MinTest);
16 CPPUNIT_TEST(min1);
17 CPPUNIT_TEST(min2);
18 CPPUNIT_TEST(minelem1);
19 CPPUNIT_TEST(minelem2);
20 CPPUNIT_TEST_SUITE_END();
21
22 protected:
23 void min1();
24 void min2();
25 void minelem1();
26 void minelem2();
str_compare(const char * a_,const char * b_)27 static bool str_compare(const char* a_, const char* b_)
28 { return strcmp(a_, b_) < 0 ? 1 : 0; }
29 };
30
31 CPPUNIT_TEST_SUITE_REGISTRATION(MinTest);
32
33 //
34 // tests implementation
35 //
min1()36 void MinTest::min1()
37 {
38 int r = min(42, 100);
39 CPPUNIT_ASSERT( r == 42 );
40
41 r = min(--r, r);
42 CPPUNIT_ASSERT( r == 41 );
43 }
min2()44 void MinTest::min2()
45 {
46 const char* r = min((const char*)"shoe", (const char*)"shine", str_compare);
47 CPPUNIT_ASSERT(!strcmp(r, "shine"));
48 }
minelem1()49 void MinTest::minelem1()
50 {
51 int numbers[6] = { -10, 15, -100, 36, -242, 42 };
52 int* r = min_element((int*)numbers, (int*)numbers + 6);
53 CPPUNIT_ASSERT(*r==-242);
54 }
minelem2()55 void MinTest::minelem2()
56 {
57 const char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" };
58
59 const unsigned namesCt = sizeof(names) / sizeof(names[0]);
60 const char** r = min_element((const char**)names, (const char**)names + namesCt, str_compare);
61 CPPUNIT_ASSERT(!strcmp(*r, "Brett"));
62 }
63