|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 //Has to be first for StackAllocator swap overload to be taken |
|
17 //into account (at least using GCC 4.0.1) |
|
18 #include "stack_allocator.h" |
|
19 |
|
20 #include <map> |
|
21 #include <algorithm> |
|
22 #include <functional> |
|
23 #include <e32std.h> |
|
24 |
|
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 MapTest : public CPPUNIT_NS::TestCase |
|
35 { |
|
36 CPPUNIT_TEST_SUITE(MapTest); |
|
37 CPPUNIT_TEST(map1); |
|
38 CPPUNIT_TEST(mmap1); |
|
39 CPPUNIT_TEST(mmap2); |
|
40 CPPUNIT_TEST(iterators); |
|
41 CPPUNIT_TEST(equal_range); |
|
42 CPPUNIT_TEST(allocator_with_state); |
|
43 #if !defined (STLPORT) || !defined (_STLP_USE_CONTAINERS_EXTENSION) |
|
44 CPPUNIT_IGNORE; |
|
45 #endif |
|
46 CPPUNIT_TEST(template_methods); |
|
47 CPPUNIT_TEST(map_cov1); |
|
48 CPPUNIT_TEST(map_cov2); |
|
49 CPPUNIT_TEST(map_cov3); |
|
50 CPPUNIT_TEST(map_cov4); |
|
51 CPPUNIT_TEST(multimap_cov1); |
|
52 CPPUNIT_TEST(multimap_cov2); |
|
53 CPPUNIT_TEST(multimap_cov3); |
|
54 CPPUNIT_TEST(multimap_cov4); |
|
55 CPPUNIT_TEST_SUITE_END(); |
|
56 |
|
57 protected: |
|
58 void map1(); |
|
59 void mmap1(); |
|
60 void mmap2(); |
|
61 void iterators(); |
|
62 void equal_range(); |
|
63 void allocator_with_state(); |
|
64 void template_methods(); |
|
65 void map_cov1(); |
|
66 void map_cov2(); |
|
67 void map_cov3(); |
|
68 void map_cov4(); |
|
69 void multimap_cov1(); |
|
70 void multimap_cov2(); |
|
71 void multimap_cov3(); |
|
72 void multimap_cov4(); |
|
73 }; |
|
74 |
|
75 CPPUNIT_TEST_SUITE_REGISTRATION(MapTest); |
|
76 |
|
77 // |
|
78 // tests implementation |
|
79 // |
|
80 void MapTest::map1() |
|
81 { |
|
82 typedef map<char, int, less<char> > maptype; |
|
83 maptype m; |
|
84 // Store mappings between roman numerals and decimals. |
|
85 m['l'] = 50; |
|
86 m['x'] = 20; // Deliberate mistake. |
|
87 m['v'] = 5; |
|
88 m['i'] = 1; |
|
89 // cout << "m['x'] = " << m['x'] << endl; |
|
90 CPPUNIT_ASSERT( m['x']== 20 ); |
|
91 m['x'] = 10; // Correct mistake. |
|
92 CPPUNIT_ASSERT( m['x']== 10 ); |
|
93 CPPUNIT_ASSERT( m['z']== 0 ); |
|
94 //cout << "m['z'] = " << m['z'] << endl; // Note default value is added. |
|
95 CPPUNIT_ASSERT( m.count('z') == 1 ); |
|
96 //cout << "m.count('z') = " << m.count('z') << endl; |
|
97 pair<maptype::iterator, bool> p = m.insert(pair<const char, int>('c', 100)); |
|
98 CPPUNIT_ASSERT( p.second ); |
|
99 CPPUNIT_ASSERT( p.first != m.end() ); |
|
100 CPPUNIT_ASSERT( (*p.first).first == 'c' ); |
|
101 CPPUNIT_ASSERT( (*p.first).second == 100 ); |
|
102 |
|
103 p = m.insert(pair<const char, int>('c', 100)); |
|
104 CPPUNIT_ASSERT( !p.second ); // already existing pair |
|
105 CPPUNIT_ASSERT( p.first != m.end() ); |
|
106 CPPUNIT_ASSERT( (*p.first).first == 'c' ); |
|
107 CPPUNIT_ASSERT( (*p.first).second == 100 ); |
|
108 } |
|
109 |
|
110 void MapTest::mmap1() |
|
111 { |
|
112 typedef multimap<char, int, less<char> > mmap; |
|
113 mmap m; |
|
114 CPPUNIT_ASSERT(m.count('X')==0); |
|
115 |
|
116 m.insert(pair<const char, int>('X', 10)); // Standard way. |
|
117 CPPUNIT_ASSERT(m.count('X')==1); |
|
118 |
|
119 m.insert(pair<const char, int>('X', 20)); // jbuck: standard way |
|
120 CPPUNIT_ASSERT(m.count('X')==2); |
|
121 |
|
122 m.insert(pair<const char, int>('Y', 32)); // jbuck: standard way |
|
123 mmap::iterator i = m.find('X'); // Find first match. |
|
124 pair<const char, int> p('X', 10); |
|
125 CPPUNIT_ASSERT(*i == p); |
|
126 CPPUNIT_ASSERT((*i).first == 'X'); |
|
127 CPPUNIT_ASSERT((*i).second == 10); |
|
128 i++; |
|
129 CPPUNIT_ASSERT((*i).first == 'X'); |
|
130 CPPUNIT_ASSERT((*i).second == 20); |
|
131 i++; |
|
132 CPPUNIT_ASSERT((*i).first == 'Y'); |
|
133 CPPUNIT_ASSERT((*i).second == 32); |
|
134 i++; |
|
135 CPPUNIT_ASSERT(i == m.end()); |
|
136 |
|
137 size_t count = m.erase('X'); |
|
138 CPPUNIT_ASSERT(count==2); |
|
139 } |
|
140 void MapTest::mmap2() |
|
141 { |
|
142 typedef pair<const int, char> pair_type; |
|
143 |
|
144 pair_type p1(3, 'c'); |
|
145 pair_type p2(6, 'f'); |
|
146 pair_type p3(1, 'a'); |
|
147 pair_type p4(2, 'b'); |
|
148 pair_type p5(3, 'x'); |
|
149 pair_type p6(6, 'f'); |
|
150 |
|
151 typedef multimap<int, char, less<int> > mmap; |
|
152 |
|
153 pair_type array [] = { |
|
154 p1, |
|
155 p2, |
|
156 p3, |
|
157 p4, |
|
158 p5, |
|
159 p6 |
|
160 }; |
|
161 |
|
162 mmap m(array + 0, array + 6); |
|
163 mmap::iterator i; |
|
164 i = m.lower_bound(3); |
|
165 CPPUNIT_ASSERT((*i).first==3); |
|
166 CPPUNIT_ASSERT((*i).second=='c'); |
|
167 |
|
168 i = m.upper_bound(3); |
|
169 CPPUNIT_ASSERT((*i).first==6); |
|
170 CPPUNIT_ASSERT((*i).second=='f'); |
|
171 } |
|
172 |
|
173 |
|
174 void MapTest::iterators() |
|
175 { |
|
176 typedef map<int, char, less<int> > int_map; |
|
177 int_map imap; |
|
178 { |
|
179 int_map::iterator ite(imap.begin()); |
|
180 int_map::const_iterator cite(imap.begin()); |
|
181 CPPUNIT_ASSERT( ite == cite ); |
|
182 CPPUNIT_ASSERT( !(ite != cite) ); |
|
183 CPPUNIT_ASSERT( cite == ite ); |
|
184 CPPUNIT_ASSERT( !(cite != ite) ); |
|
185 } |
|
186 |
|
187 typedef multimap<int, char, less<int> > mmap; |
|
188 typedef mmap::value_type pair_type; |
|
189 |
|
190 pair_type p1(3, 'c'); |
|
191 pair_type p2(6, 'f'); |
|
192 pair_type p3(1, 'a'); |
|
193 pair_type p4(2, 'b'); |
|
194 pair_type p5(3, 'x'); |
|
195 pair_type p6(6, 'f'); |
|
196 |
|
197 pair_type array [] = { |
|
198 p1, |
|
199 p2, |
|
200 p3, |
|
201 p4, |
|
202 p5, |
|
203 p6 |
|
204 }; |
|
205 |
|
206 mmap m(array+0, array + 6); |
|
207 |
|
208 { |
|
209 mmap::iterator ite(m.begin()); |
|
210 mmap::const_iterator cite(m.begin()); |
|
211 //test compare between const_iterator and iterator |
|
212 CPPUNIT_ASSERT( ite == cite ); |
|
213 CPPUNIT_ASSERT( !(ite != cite) ); |
|
214 CPPUNIT_ASSERT( cite == ite ); |
|
215 CPPUNIT_ASSERT( !(cite != ite) ); |
|
216 } |
|
217 |
|
218 #if 0 |
|
219 /* |
|
220 * A check that map and multimap iterators are NOT comparable |
|
221 * the following code should generate a compile time error |
|
222 */ |
|
223 { |
|
224 int_map::iterator mite(imap.begin()); |
|
225 int_map::const_iterator mcite(imap.begin()); |
|
226 mmap::iterator mmite(m.begin()); |
|
227 mmap::const_iterator mmcite(m.begin()); |
|
228 CPPUNIT_ASSERT( !(mite == mmite) ); |
|
229 CPPUNIT_ASSERT( !(mcite == mmcite) ); |
|
230 CPPUNIT_ASSERT( mite != mmite ); |
|
231 CPPUNIT_ASSERT( mcite != mmcite ); |
|
232 CPPUNIT_ASSERT( !(mite == mmcite) ); |
|
233 CPPUNIT_ASSERT( !(mite == mmcite) ); |
|
234 CPPUNIT_ASSERT( mite != mmcite ); |
|
235 CPPUNIT_ASSERT( mite != mmcite ); |
|
236 } |
|
237 |
|
238 #endif |
|
239 |
|
240 mmap::reverse_iterator ri = m.rbegin(); |
|
241 CPPUNIT_ASSERT( ri != m.rend() ); |
|
242 CPPUNIT_ASSERT( ri == m.rbegin() ); |
|
243 CPPUNIT_ASSERT( (*ri).first == 6 ); |
|
244 CPPUNIT_ASSERT( (*ri++).second == 'f' ); |
|
245 CPPUNIT_ASSERT( (*ri).first == 6 ); |
|
246 CPPUNIT_ASSERT( (*ri).second == 'f' ); |
|
247 |
|
248 mmap const& cm = m; |
|
249 mmap::const_reverse_iterator rci = cm.rbegin(); |
|
250 CPPUNIT_ASSERT( rci != cm.rend() ); |
|
251 CPPUNIT_ASSERT( (*rci).first == 6 ); |
|
252 CPPUNIT_ASSERT( (*rci++).second == 'f' ); |
|
253 CPPUNIT_ASSERT( (*rci).first == 6 ); |
|
254 CPPUNIT_ASSERT( (*rci).second == 'f' ); |
|
255 } |
|
256 |
|
257 void MapTest::equal_range() |
|
258 { |
|
259 typedef map<char, int, less<char> > maptype; |
|
260 { |
|
261 maptype m; |
|
262 m['x'] = 10; |
|
263 |
|
264 pair<maptype::iterator, maptype::iterator> ret; |
|
265 ret = m.equal_range('x'); |
|
266 CPPUNIT_ASSERT( ret.first != ret.second ); |
|
267 CPPUNIT_ASSERT( (*(ret.first)).first == 'x' ); |
|
268 CPPUNIT_ASSERT( (*(ret.first)).second == 10 ); |
|
269 CPPUNIT_ASSERT( ++(ret.first) == ret.second ); |
|
270 } |
|
271 { |
|
272 { |
|
273 maptype m; |
|
274 |
|
275 maptype::iterator i = m.lower_bound( 'x' ); |
|
276 CPPUNIT_ASSERT( i == m.end() ); |
|
277 |
|
278 i = m.upper_bound( 'x' ); |
|
279 CPPUNIT_ASSERT( i == m.end() ); |
|
280 |
|
281 pair<maptype::iterator, maptype::iterator> ret; |
|
282 ret = m.equal_range('x'); |
|
283 CPPUNIT_ASSERT( ret.first == ret.second ); |
|
284 CPPUNIT_ASSERT( ret.first == m.end() ); |
|
285 } |
|
286 |
|
287 { |
|
288 const maptype m; |
|
289 pair<maptype::const_iterator, maptype::const_iterator> ret; |
|
290 ret = m.equal_range('x'); |
|
291 CPPUNIT_ASSERT( ret.first == ret.second ); |
|
292 CPPUNIT_ASSERT( ret.first == m.end() ); |
|
293 } |
|
294 } |
|
295 } |
|
296 |
|
297 void MapTest::allocator_with_state() |
|
298 { |
|
299 char buf1[1024]; |
|
300 StackAllocator<pair<const int, int> > stack1(buf1, buf1 + sizeof(buf1)); |
|
301 |
|
302 char buf2[1024]; |
|
303 StackAllocator<pair<const int, int> > stack2(buf2, buf2 + sizeof(buf2)); |
|
304 |
|
305 { |
|
306 typedef map<int, int, less<int>, StackAllocator<pair<const int, int> > > MapInt; |
|
307 less<int> intLess; |
|
308 MapInt mint1(intLess, stack1); |
|
309 int i; |
|
310 for (i = 0; i < 5; ++i) |
|
311 mint1.insert(MapInt::value_type(i, i)); |
|
312 MapInt mint1Cpy(mint1); |
|
313 |
|
314 MapInt mint2(intLess, stack2); |
|
315 for (; i < 10; ++i) |
|
316 mint2.insert(MapInt::value_type(i, i)); |
|
317 MapInt mint2Cpy(mint2); |
|
318 |
|
319 mint1.swap(mint2); |
|
320 |
|
321 CPPUNIT_ASSERT( mint1.get_allocator().swaped() ); |
|
322 CPPUNIT_ASSERT( mint2.get_allocator().swaped() ); |
|
323 |
|
324 CPPUNIT_ASSERT( mint1 == mint2Cpy ); |
|
325 CPPUNIT_ASSERT( mint2 == mint1Cpy ); |
|
326 CPPUNIT_ASSERT( mint1.get_allocator() == stack2 ); |
|
327 CPPUNIT_ASSERT( mint2.get_allocator() == stack1 ); |
|
328 } |
|
329 CPPUNIT_ASSERT( stack1.ok() ); |
|
330 CPPUNIT_ASSERT( stack2.ok() ); |
|
331 } |
|
332 |
|
333 struct Key |
|
334 { |
|
335 Key() : m_data(0) {} |
|
336 explicit Key(int data) : m_data(data) {} |
|
337 |
|
338 int m_data; |
|
339 }; |
|
340 |
|
341 struct KeyCmp |
|
342 { |
|
343 bool operator () (Key lhs, Key rhs) const |
|
344 { return lhs.m_data < rhs.m_data; } |
|
345 |
|
346 bool operator () (Key lhs, int rhs) const |
|
347 { return lhs.m_data < rhs; } |
|
348 |
|
349 bool operator () (int lhs, Key rhs) const |
|
350 { return lhs < rhs.m_data; } |
|
351 }; |
|
352 |
|
353 struct KeyCmpPtr |
|
354 { |
|
355 bool operator () (Key const volatile *lhs, Key const volatile *rhs) const |
|
356 { return (*lhs).m_data < (*rhs).m_data; } |
|
357 |
|
358 bool operator () (Key const volatile *lhs, int rhs) const |
|
359 { return (*lhs).m_data < rhs; } |
|
360 |
|
361 bool operator () (int lhs, Key const volatile *rhs) const |
|
362 { return lhs < (*rhs).m_data; } |
|
363 }; |
|
364 |
|
365 void MapTest::template_methods() |
|
366 { |
|
367 #if defined (STLPORT) && defined (_STLP_USE_CONTAINERS_EXTENSION) |
|
368 { |
|
369 typedef map<Key, int, KeyCmp> Container; |
|
370 typedef Container::value_type value; |
|
371 Container cont; |
|
372 cont.insert(value(Key(1), 1)); |
|
373 cont.insert(value(Key(2), 2)); |
|
374 cont.insert(value(Key(3), 3)); |
|
375 cont.insert(value(Key(4), 4)); |
|
376 |
|
377 CPPUNIT_ASSERT( cont.count(Key(1)) == 1 ); |
|
378 CPPUNIT_ASSERT( cont.count(1) == 1 ); |
|
379 CPPUNIT_ASSERT( cont.count(5) == 0 ); |
|
380 |
|
381 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); |
|
382 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); |
|
383 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); |
|
384 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); |
|
385 |
|
386 Container const& ccont = cont; |
|
387 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); |
|
388 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); |
|
389 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); |
|
390 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.end(), ccont.end()) ); |
|
391 } |
|
392 |
|
393 { |
|
394 typedef map<Key*, int, KeyCmpPtr> Container; |
|
395 typedef Container::value_type value; |
|
396 Container cont; |
|
397 Key key1(1), key2(2), key3(3), key4(4); |
|
398 cont.insert(value(&key1, 1)); |
|
399 cont.insert(value(&key2, 2)); |
|
400 cont.insert(value(&key3, 3)); |
|
401 cont.insert(value(&key4, 4)); |
|
402 |
|
403 CPPUNIT_ASSERT( cont.count(1) == 1 ); |
|
404 CPPUNIT_ASSERT( cont.count(5) == 0 ); |
|
405 |
|
406 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); |
|
407 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); |
|
408 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); |
|
409 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); |
|
410 |
|
411 Container const& ccont = cont; |
|
412 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); |
|
413 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); |
|
414 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); |
|
415 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) ); |
|
416 } |
|
417 { |
|
418 typedef multimap<Key, int, KeyCmp> Container; |
|
419 typedef Container::value_type value; |
|
420 Container cont; |
|
421 cont.insert(value(Key(1), 1)); |
|
422 cont.insert(value(Key(2), 2)); |
|
423 cont.insert(value(Key(3), 3)); |
|
424 cont.insert(value(Key(4), 4)); |
|
425 |
|
426 CPPUNIT_ASSERT( cont.count(Key(1)) == 1 ); |
|
427 CPPUNIT_ASSERT( cont.count(1) == 1 ); |
|
428 CPPUNIT_ASSERT( cont.count(5) == 0 ); |
|
429 |
|
430 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); |
|
431 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); |
|
432 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); |
|
433 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); |
|
434 |
|
435 Container const& ccont = cont; |
|
436 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); |
|
437 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); |
|
438 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); |
|
439 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.end(), ccont.end()) ); |
|
440 } |
|
441 |
|
442 { |
|
443 typedef multimap<Key const volatile*, int, KeyCmpPtr> Container; |
|
444 typedef Container::value_type value; |
|
445 Container cont; |
|
446 Key key1(1), key2(2), key3(3), key4(4); |
|
447 cont.insert(value(&key1, 1)); |
|
448 cont.insert(value(&key2, 2)); |
|
449 cont.insert(value(&key3, 3)); |
|
450 cont.insert(value(&key4, 4)); |
|
451 |
|
452 CPPUNIT_ASSERT( cont.count(1) == 1 ); |
|
453 CPPUNIT_ASSERT( cont.count(5) == 0 ); |
|
454 |
|
455 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); |
|
456 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); |
|
457 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); |
|
458 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); |
|
459 |
|
460 Container const& ccont = cont; |
|
461 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); |
|
462 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); |
|
463 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); |
|
464 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) ); |
|
465 } |
|
466 #endif |
|
467 } |
|
468 |
|
469 void MapTest::map_cov1() |
|
470 { |
|
471 __UHEAP_MARK; |
|
472 { |
|
473 map<int, int> m1; |
|
474 map<int, int>::size_type i; |
|
475 typedef pair<int, int> Int_Pair; |
|
476 m1.insert(Int_Pair(1, 1)); |
|
477 m1.insert(Int_Pair(2, 4)); |
|
478 |
|
479 i = m1.size(); |
|
480 CPPUNIT_ASSERT( i==2 ); |
|
481 m1.clear(); |
|
482 i = m1.size(); |
|
483 CPPUNIT_ASSERT( i==0 ); |
|
484 } |
|
485 { |
|
486 map <int, int> m1, m2; |
|
487 bool flag; |
|
488 typedef pair <int, int> Int_Pair; |
|
489 |
|
490 m1.insert ( Int_Pair ( 1, 1 ) ); |
|
491 flag = m1.empty(); |
|
492 CPPUNIT_ASSERT( flag==false ); |
|
493 flag = m2.empty(); |
|
494 CPPUNIT_ASSERT( flag==true ); |
|
495 } |
|
496 { |
|
497 map <int, int> m1; |
|
498 map <int, int> :: iterator m1_Iter; |
|
499 map <int, int> :: reverse_iterator m1_rIter; |
|
500 typedef pair <int, int> Int_Pair; |
|
501 |
|
502 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
503 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
504 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
505 m1_rIter = m1.rbegin( ); |
|
506 CPPUNIT_ASSERT( m1_rIter->first == 3 ); |
|
507 m1_rIter = m1.rend( ); |
|
508 m1_rIter--; |
|
509 CPPUNIT_ASSERT( m1_rIter->first == 1 ); |
|
510 } |
|
511 __UHEAP_MARKEND; |
|
512 } |
|
513 void MapTest::map_cov2() |
|
514 { |
|
515 __UHEAP_MARK; |
|
516 { |
|
517 map<int, int> m1, m2, m3; |
|
518 map<int, int>::iterator pIter, Iter1, Iter2; |
|
519 int i; |
|
520 typedef pair<int, int> Int_Pair; |
|
521 |
|
522 for (i = 1; i < 5; i++) |
|
523 { |
|
524 m1.insert(Int_Pair(i, i)); |
|
525 m2.insert(Int_Pair(i, i*i)); |
|
526 m3.insert(Int_Pair(i, i-1)); |
|
527 } |
|
528 Iter1 = ++m1.begin(); |
|
529 m1.erase(Iter1); |
|
530 |
|
531 pIter = m1.begin(); |
|
532 CPPUNIT_ASSERT( pIter->second == 1 ); |
|
533 pIter++; |
|
534 CPPUNIT_ASSERT( pIter->second == 3 ); |
|
535 pIter++; |
|
536 CPPUNIT_ASSERT( pIter->second == 4 ); |
|
537 |
|
538 Iter1 = ++m2.begin(); |
|
539 Iter2 = --m2.end(); |
|
540 m2.erase(Iter1, Iter2); |
|
541 pIter = m2.begin(); |
|
542 CPPUNIT_ASSERT( pIter->second == 1 ); |
|
543 pIter++; |
|
544 CPPUNIT_ASSERT( pIter->second == 16 ); |
|
545 |
|
546 map<int, int>::size_type n = m3.erase(2); |
|
547 pIter = m3.begin(); |
|
548 CPPUNIT_ASSERT( pIter->second == 0 ); |
|
549 pIter++; |
|
550 CPPUNIT_ASSERT( pIter->second == 2 ); |
|
551 pIter++; |
|
552 CPPUNIT_ASSERT( pIter->second == 3 ); |
|
553 } |
|
554 { |
|
555 map <int, int> m1; |
|
556 m1.max_size( ); |
|
557 } |
|
558 __UHEAP_MARKEND; |
|
559 } |
|
560 void MapTest::map_cov3() |
|
561 { |
|
562 __UHEAP_MARK; |
|
563 { |
|
564 map <int, int> m1; |
|
565 |
|
566 map <int, int> :: iterator m1_Iter; |
|
567 map <int, int> :: reverse_iterator m1_rIter; |
|
568 map <int, int> :: const_reverse_iterator m1_crIter; |
|
569 typedef pair <int, int> Int_Pair; |
|
570 |
|
571 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
572 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
573 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
574 m1_crIter = m1.rbegin( ); |
|
575 CPPUNIT_ASSERT( m1_crIter->first == 3 ); |
|
576 m1_crIter = m1.rend( ); |
|
577 m1_crIter--; |
|
578 CPPUNIT_ASSERT( m1_crIter->first == 1 ); |
|
579 } |
|
580 { |
|
581 map <int, int, less<int> > m1; |
|
582 map <int, int, less<int> >::value_compare vc1 = m1.value_comp( ); |
|
583 pair< map<int,int>::iterator, bool > pr1, pr2; |
|
584 |
|
585 pr1= m1.insert ( map <int, int> :: value_type ( 1, 10 ) ); |
|
586 pr2= m1.insert ( map <int, int> :: value_type ( 2, 5 ) ); |
|
587 |
|
588 CPPUNIT_ASSERT( vc1( *pr1.first, *pr2.first ) == true ); |
|
589 } |
|
590 { |
|
591 map <int, int> m1, m2; |
|
592 typedef pair <int, int> Int_Pair; |
|
593 map <int, int>::iterator m2_Iter; |
|
594 map<int, int>::size_type i; |
|
595 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
596 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
597 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
598 m2=m1; |
|
599 m2_Iter = m2.begin( ); |
|
600 CPPUNIT_ASSERT(m2_Iter -> second == 10); |
|
601 i = m2.size(); |
|
602 CPPUNIT_ASSERT(i == 3); |
|
603 } |
|
604 __UHEAP_MARKEND; |
|
605 } |
|
606 void MapTest::map_cov4() |
|
607 { |
|
608 __UHEAP_MARK; |
|
609 { |
|
610 map <int, int> m1, m3; |
|
611 map <int, int>::iterator m1_Iter; |
|
612 typedef pair <int, int> Int_Pair; |
|
613 |
|
614 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
615 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
616 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
617 m3.insert ( Int_Pair ( 30, 300 ) ); |
|
618 |
|
619 swap(m1,m3); |
|
620 m1_Iter = m1.begin(); |
|
621 |
|
622 CPPUNIT_ASSERT(m1_Iter -> second == 300); |
|
623 } |
|
624 { |
|
625 map <int, int> m1, m3; |
|
626 typedef pair <int, int> Int_Pair; |
|
627 |
|
628 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
629 m3.insert ( Int_Pair ( 30, 300 ) ); |
|
630 |
|
631 bool x = m1 < m3; |
|
632 CPPUNIT_ASSERT(x == true); |
|
633 } |
|
634 __UHEAP_MARKEND; |
|
635 } |
|
636 |
|
637 void MapTest::multimap_cov1() |
|
638 { |
|
639 __UHEAP_MARK; |
|
640 { |
|
641 multimap<int, int> m1; |
|
642 multimap<int, int>::size_type i; |
|
643 typedef pair<int, int> Int_Pair; |
|
644 |
|
645 m1.insert(Int_Pair(1, 1)); |
|
646 m1.insert(Int_Pair(2, 4)); |
|
647 |
|
648 i = m1.size(); |
|
649 CPPUNIT_ASSERT( i==2 ); |
|
650 m1.clear(); |
|
651 i = m1.size(); |
|
652 CPPUNIT_ASSERT( i==0 ); |
|
653 } |
|
654 { |
|
655 multimap <int, int> m1, m2; |
|
656 bool flag; |
|
657 typedef pair <int, int> Int_Pair; |
|
658 |
|
659 m1.insert ( Int_Pair ( 1, 1 ) ); |
|
660 flag = m1.empty(); |
|
661 CPPUNIT_ASSERT( flag==false ); |
|
662 flag = m2.empty(); |
|
663 CPPUNIT_ASSERT( flag==true ); |
|
664 } |
|
665 { |
|
666 multimap <int, int>::allocator_type m1_Alloc; |
|
667 multimap <int, int>::allocator_type m2_Alloc; |
|
668 multimap <int, int>::allocator_type m4_Alloc; |
|
669 |
|
670 multimap <int, int> m1; |
|
671 multimap <int, int, allocator<int> > m2; |
|
672 m1_Alloc = m1.get_allocator( ); |
|
673 m2_Alloc = m2.get_allocator( ); |
|
674 map <int, int> m4( less<int>( ), m1_Alloc ); |
|
675 m4_Alloc = m4.get_allocator( ); |
|
676 |
|
677 CPPUNIT_ASSERT( m1_Alloc == m4_Alloc ); |
|
678 } |
|
679 __UHEAP_MARKEND; |
|
680 } |
|
681 void MapTest::multimap_cov2() |
|
682 { |
|
683 __UHEAP_MARK; |
|
684 { |
|
685 multimap<int, int> m1, m2; |
|
686 multimap<int, int>::iterator pIter, Iter1, Iter2; |
|
687 int i; |
|
688 typedef pair<int, int> Int_Pair; |
|
689 |
|
690 for (i = 1; i < 5; i++) |
|
691 { |
|
692 m1.insert(Int_Pair(i, i)); |
|
693 m2.insert(Int_Pair(i, i*i)); |
|
694 } |
|
695 Iter1 = ++m1.begin(); |
|
696 m1.erase(Iter1); |
|
697 |
|
698 pIter = m1.begin(); |
|
699 CPPUNIT_ASSERT( pIter->second == 1 ); |
|
700 pIter++; |
|
701 CPPUNIT_ASSERT( pIter->second == 3 ); |
|
702 pIter++; |
|
703 CPPUNIT_ASSERT( pIter->second == 4 ); |
|
704 |
|
705 Iter1 = ++m2.begin(); |
|
706 Iter2 = --m2.end(); |
|
707 m2.erase(Iter1, Iter2); |
|
708 pIter = m2.begin(); |
|
709 CPPUNIT_ASSERT( pIter->second == 1 ); |
|
710 pIter++; |
|
711 CPPUNIT_ASSERT( pIter->second == 16 ); |
|
712 } |
|
713 { |
|
714 multimap <int, int>::iterator m1_pIter, m2_pIter; |
|
715 multimap <int, int> :: reverse_iterator m1_rIter; |
|
716 multimap <int, int> m1, m2; |
|
717 typedef pair <int, int> Int_Pair; |
|
718 |
|
719 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
720 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
721 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
722 m1.insert( --m1.end( ), Int_Pair ( 4, 40 ) ); |
|
723 |
|
724 m1_rIter = m1.rbegin( ); |
|
725 CPPUNIT_ASSERT( m1_rIter->first == 4 ); |
|
726 |
|
727 m2.insert ( Int_Pair ( 10, 100 ) ); |
|
728 m2.insert( ++m1.begin( ), --m1.end( ) ); |
|
729 m2_pIter = m2.begin( ); |
|
730 CPPUNIT_ASSERT(m2_pIter -> first == 2); |
|
731 } |
|
732 { |
|
733 multimap <int, int> m1; |
|
734 m1.max_size( ); |
|
735 } |
|
736 __UHEAP_MARKEND; |
|
737 } |
|
738 void MapTest::multimap_cov3() |
|
739 { |
|
740 __UHEAP_MARK; |
|
741 { |
|
742 multimap <int, int, less<int> > m1; |
|
743 multimap <int, int, less<int> >::key_compare kc1 = m1.key_comp( ) ; |
|
744 bool result1 = kc1( 2, 3 ) ; |
|
745 CPPUNIT_ASSERT(result1 == true); |
|
746 |
|
747 multimap <int, int, greater_equal<int> > m2; |
|
748 multimap <int, int, greater_equal<int> >::key_compare kc2 = m2.key_comp( ); |
|
749 bool result2 = kc2( 3, 3 ) ; |
|
750 CPPUNIT_ASSERT(result1 == true); |
|
751 } |
|
752 { |
|
753 multimap<int, int> m1, m2; |
|
754 multimap<int, int>::size_type i; |
|
755 typedef pair<int, int> Int_Pair; |
|
756 |
|
757 m1.insert(Int_Pair(1, 1)); |
|
758 i = m1.size(); |
|
759 CPPUNIT_ASSERT(i == 1); |
|
760 m1.insert(Int_Pair(2, 4)); |
|
761 i = m1.size(); |
|
762 CPPUNIT_ASSERT(i == 2); |
|
763 } |
|
764 { |
|
765 multimap <int, int> m1, m2; |
|
766 multimap <int, int>::iterator m1_Iter; |
|
767 typedef pair <int, int> Int_Pair; |
|
768 multimap<int, int>::size_type i; |
|
769 |
|
770 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
771 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
772 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
773 m2.insert ( Int_Pair ( 30, 300 ) ); |
|
774 m1.swap( m2 ); |
|
775 m1_Iter = m1.begin( ); |
|
776 CPPUNIT_ASSERT(m1_Iter -> second == 300); |
|
777 i = m1.size(); |
|
778 CPPUNIT_ASSERT(i == 1); |
|
779 } |
|
780 __UHEAP_MARKEND; |
|
781 } |
|
782 void MapTest::multimap_cov4() |
|
783 { |
|
784 __UHEAP_MARK; |
|
785 { |
|
786 multimap <int, int, less<int> > m1; |
|
787 multimap <int, int, less<int> >::value_compare vc1 = m1.value_comp( ); |
|
788 multimap<int,int>::iterator Iter1, Iter2; |
|
789 |
|
790 Iter1= m1.insert ( multimap <int, int> :: value_type ( 1, 10 ) ); |
|
791 Iter2= m1.insert ( multimap <int, int> :: value_type ( 2, 5 ) ); |
|
792 CPPUNIT_ASSERT( vc1( *Iter1, *Iter2 ) == true ); |
|
793 } |
|
794 { |
|
795 multimap <int, int> m1, m2; |
|
796 typedef pair <int, int> Int_Pair; |
|
797 multimap <int, int>::iterator m2_Iter; |
|
798 multimap<int, int>::size_type i; |
|
799 |
|
800 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
801 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
802 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
803 |
|
804 m2=m1; |
|
805 m2_Iter = m2.begin( ); |
|
806 CPPUNIT_ASSERT(m2_Iter -> second == 10); |
|
807 i = m2.size(); |
|
808 CPPUNIT_ASSERT(i == 3); |
|
809 |
|
810 } |
|
811 { |
|
812 multimap <int, int> m1, m2; |
|
813 multimap <int, int>::iterator m1_Iter; |
|
814 typedef pair <int, int> Int_Pair; |
|
815 multimap<int, int>::size_type i; |
|
816 |
|
817 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
818 m1.insert ( Int_Pair ( 2, 20 ) ); |
|
819 m1.insert ( Int_Pair ( 3, 30 ) ); |
|
820 m2.insert ( Int_Pair ( 30, 300 ) ); |
|
821 swap( m1,m2 ); |
|
822 m1_Iter = m1.begin( ); |
|
823 CPPUNIT_ASSERT(m1_Iter -> second == 300); |
|
824 i = m1.size(); |
|
825 CPPUNIT_ASSERT(i == 1); |
|
826 } |
|
827 { |
|
828 multimap <int, int> m1, m2; |
|
829 typedef pair <int, int> Int_Pair; |
|
830 |
|
831 m1.insert ( Int_Pair ( 1, 10 ) ); |
|
832 m2.insert ( Int_Pair ( 30, 300 ) ); |
|
833 |
|
834 bool val = m1 < m2; |
|
835 CPPUNIT_ASSERT(val == true); |
|
836 val = (m1 == m2); |
|
837 CPPUNIT_ASSERT(val == false); |
|
838 } |
|
839 __UHEAP_MARKEND; |
|
840 } |