|
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 <numeric> |
|
18 #include <string> |
|
19 #include <iterator> |
|
20 #include <vector> |
|
21 #include <algorithm> |
|
22 #include <functional> |
|
23 |
|
24 #include "iota.h" |
|
25 #include "cppunit/cppunit_proxy.h" |
|
26 |
|
27 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) |
|
28 using namespace std; |
|
29 #endif |
|
30 |
|
31 // |
|
32 // TestCase class |
|
33 // |
|
34 class SetUnionTest : public CPPUNIT_NS::TestCase |
|
35 { |
|
36 CPPUNIT_TEST_SUITE(SetUnionTest); |
|
37 CPPUNIT_TEST(setunon0); |
|
38 CPPUNIT_TEST(setunon1); |
|
39 CPPUNIT_TEST(setunon2); |
|
40 CPPUNIT_TEST_SUITE_END(); |
|
41 |
|
42 protected: |
|
43 void setunon0(); |
|
44 void setunon1(); |
|
45 void setunon2(); |
|
46 }; |
|
47 |
|
48 CPPUNIT_TEST_SUITE_REGISTRATION(SetUnionTest); |
|
49 |
|
50 // |
|
51 // tests implementation |
|
52 // |
|
53 void SetUnionTest::setunon0() |
|
54 { |
|
55 int v1[3] = { 13, 18, 23 }; |
|
56 int v2[4] = { 10, 13, 17, 23 }; |
|
57 int result[7] = { 0, 0, 0, 0, 0, 0, 0 }; |
|
58 |
|
59 set_union((int*)v1, (int*)v1 + 3, (int*)v2, (int*)v2 + 4, (int*)result); |
|
60 |
|
61 CPPUNIT_ASSERT(result[0]==10); |
|
62 CPPUNIT_ASSERT(result[1]==13); |
|
63 CPPUNIT_ASSERT(result[2]==17); |
|
64 CPPUNIT_ASSERT(result[3]==18); |
|
65 CPPUNIT_ASSERT(result[4]==23); |
|
66 CPPUNIT_ASSERT(result[5]==0); |
|
67 CPPUNIT_ASSERT(result[6]==0); |
|
68 } |
|
69 |
|
70 void SetUnionTest::setunon1() |
|
71 { |
|
72 vector <int> v1(10); |
|
73 __iota(v1.begin(), v1.end(), 0); |
|
74 vector <int> v2(10); |
|
75 __iota(v2.begin(), v2.end(), 7); |
|
76 |
|
77 vector<int> diff; |
|
78 set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(diff)); |
|
79 CPPUNIT_ASSERT( diff.size() == 17 ); |
|
80 for (int i = 0; i < 17; ++i) { |
|
81 CPPUNIT_ASSERT( diff[i] == i ); |
|
82 } |
|
83 } |
|
84 |
|
85 void SetUnionTest::setunon2() |
|
86 { |
|
87 char* word1 = "ABCDEFGHIJKLMNO"; |
|
88 char* word2 = "LMNOPQRSTUVWXYZ"; |
|
89 |
|
90 string diff; |
|
91 set_union(word1, word1 + ::strlen(word1), word2, word2 + ::strlen(word2), |
|
92 back_inserter(diff), less<char>()); |
|
93 CPPUNIT_ASSERT( diff.size() == 26 ); |
|
94 for (int i = 0; i < 26; ++i) { |
|
95 CPPUNIT_ASSERT( diff[i] == ('A' + i) ); |
|
96 } |
|
97 } |