|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include <vector> |
|
18 #include <algorithm> |
|
19 |
|
20 #include "cppunit/cppunit_proxy.h" |
|
21 |
|
22 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
23 using namespace std; |
|
24 #endif |
|
25 |
|
26 // |
|
27 // TestCase class |
|
28 // |
|
29 class MaxTest : public CPPUNIT_NS::TestCase |
|
30 { |
|
31 CPPUNIT_TEST_SUITE(MaxTest); |
|
32 CPPUNIT_TEST(max1); |
|
33 CPPUNIT_TEST(max2); |
|
34 CPPUNIT_TEST(maxelem1); |
|
35 CPPUNIT_TEST(maxelem2); |
|
36 CPPUNIT_TEST_SUITE_END(); |
|
37 |
|
38 protected: |
|
39 void max1(); |
|
40 void max2(); |
|
41 void maxelem1(); |
|
42 void maxelem2(); |
|
43 |
|
44 static bool str_compare(const char* a_, const char* b_) |
|
45 { return strcmp(a_, b_) < 0 ? 1 : 0; } |
|
46 }; |
|
47 |
|
48 CPPUNIT_TEST_SUITE_REGISTRATION(MaxTest); |
|
49 |
|
50 // |
|
51 // tests implementation |
|
52 // |
|
53 void MaxTest::max1() |
|
54 { |
|
55 int r = max(42, 100); |
|
56 CPPUNIT_ASSERT( r == 100 ); |
|
57 |
|
58 r = max(++r, r); |
|
59 CPPUNIT_ASSERT( r == 101 ); |
|
60 } |
|
61 void MaxTest::max2() |
|
62 { |
|
63 const char* r = max((const char*)"shoe", (const char*)"shine", str_compare); |
|
64 CPPUNIT_ASSERT(!strcmp(r, "shoe")); |
|
65 } |
|
66 void MaxTest::maxelem1() |
|
67 { |
|
68 int numbers[6] = { 4, 10, 56, 11, -42, 19 }; |
|
69 |
|
70 int* r = max_element((int*)numbers, (int*)numbers + 6); |
|
71 CPPUNIT_ASSERT(*r==56); |
|
72 } |
|
73 void MaxTest::maxelem2() |
|
74 { |
|
75 const char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" }; |
|
76 |
|
77 const unsigned namesCt = sizeof(names) / sizeof(names[0]); |
|
78 const char** r = max_element((const char**)names, (const char**)names + namesCt, str_compare); |
|
79 CPPUNIT_ASSERT(!strcmp(*r, "Todd")); |
|
80 } |