Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
20.smartptr.shared.cpp
Go to the documentation of this file.
1// -*- C++ -*-
2
30#include <rw_driver.h>
31
32// compile out all test code if extensions disabled
33#ifndef _RWSTD_NO_EXT_CXX_0X
34
35#include <rw/_smartptr.h>
36
37
40
41template <class T>
42struct Base
43{
44 static int n_dtors;
45
46 int id;
47
48 Base () { }
49
50 void init () {
51 id = ++next_id;
52 }
53
54 // Base dtor is not virtual in order to detect
55 // deletions through a pointer to the wrong base
56 /* NOT virtual */ ~Base () {
57 ++n_dtors;
58 }
59};
60
61template <class T>
63
64
65template <class T>
66struct Base_0: virtual Base<T>
67{
68 static int n_dtors;
69
70 T dummy; // make body non-empty
71
72 Base_0 (): Base<T>() { this->init (); }
73
74 virtual ~Base_0 () {
75 ++n_dtors;
76 }
77};
78
79template <class T>
81
82
83template <class T>
84struct Base_1: virtual Base<T>
85{
86 static int n_dtors;
87
88 T dummy; // make body non-empty
89
90 Base_1 (): Base<T>() { this->init (); }
91
92 virtual ~Base_1 () {
93 ++n_dtors;
94 }
95};
96
97template <class T>
99
100
101template <class T>
102struct Derived: Base_0<T>, Base_1<T>
103{
104 static int n_dtors;
105
106 T dummy; // make body non-empty
107
108 Derived (): Base_0<T>(), Base_1<T>() { }
109
110 virtual ~Derived () {
111 ++n_dtors;
112 }
113};
114
115template <class T>
117
118
119template <class T>
121{
122 static int n_def_ctors;
123 static int n_cpy_ctors;
124 static int n_dtors;
125 static int n_funcalls;
126
128 ++n_def_ctors;
129 }
130
131 Deleter (const Deleter&) {
132 ++n_cpy_ctors;
133 }
134
135 virtual ~Deleter () {
136 ++n_dtors;
137 }
138
139 void operator()(T *p) {
140 ++n_funcalls;
141 delete p;
142 }
143
144private:
145 void operator=(Deleter&); // not defined
146};
147
148template <class T>
150
151template <class T>
153
154template <class T>
156
157template <class T>
159
160
162static void
163test_ctor ()
164{
165 rw_info (0, "util.smartptr.shared.cons", 0,
166 "shared_ptr constructors");
167
168 { // default ctor
169 std::shared_ptr<char> ptr;
170 rw_assert (0 == ptr.get (), 0, __LINE__, "");
171 rw_assert (0 == ptr.use_count (), 0, __LINE__, "");
172 }
173
174 { // default ctor
175 std::shared_ptr<const short> ptr;
176 rw_assert (0 == ptr.get (), 0, __LINE__, "");
177 rw_assert (0 == ptr.use_count (), 0, __LINE__, "");
178 }
179
180 { // default ctor
181 std::shared_ptr<volatile int> ptr;
182 rw_assert (0 == ptr.get (), 0, __LINE__, "");
183 rw_assert (0 == ptr.use_count (), 0, __LINE__, "");
184 }
185
186 { // default ctor
187 std::shared_ptr<const volatile long> ptr;
188 rw_assert (0 == ptr.get (), 0, __LINE__, "");
189 rw_assert (0 == ptr.use_count (), 0, __LINE__, "");
190 }
191
192 { // template <class U> shared_ptr(U*)
193 const char* const p = new char ('A');
194 std::shared_ptr<const char> ptr (p);
195 rw_assert (p == ptr.get (), 0, __LINE__, "");
196 rw_assert (1 == ptr.use_count (), 0, __LINE__, "");
197 }
198
199 { // template <class U> shared_ptr(U*)
200 const char* const p = new char ('A');
201 std::shared_ptr<const void> ptr (p);
202 rw_assert (p == ptr.get (), 0, __LINE__, "");
203 rw_assert (1 == ptr.use_count (), 0, __LINE__, "");
204 }
205
206 { // template <class U> shared_ptr(U*)
207 short* const p = new short (__LINE__);
208 std::shared_ptr<volatile void> ptr (p);
209 rw_assert (p == ptr.get (), 0, __LINE__, "");
210 rw_assert (1 == ptr.use_count (), 0, __LINE__, "");
211 }
212
213 { // template <class U> shared_ptr(U*)
214 double* const p = new double ();
215 std::shared_ptr<const volatile void> ptr (p);
216 rw_assert (p == ptr.get (), 0, __LINE__, "");
217 rw_assert (1 == ptr.use_count (), 0, __LINE__, "");
218 }
219
220#if ILL_FORMED == -1 || ILL_FORMED == __LINE__
221
222 { // template <class U> shared_ptr(U*)
223 char* const p = new char ('A');
224 std::shared_ptr<int> ptr (p);
225 }
226
227#endif // ILL_FORMED
228
229 { // template <class U> shared_ptr(U*)
230 Derived<int>* const pd0 = new Derived<int>;
231 Derived<int>* const pd1 = new Derived<int>;
232 Derived<int>* const pd2 = new Derived<int>;
233 Derived<int>* const pd3 = new Derived<int>;
234
235 std::shared_ptr<const Derived<int> > ptr_d (pd0);
236 std::shared_ptr<const Base_0<int> > ptr_0 (pd1);
237 std::shared_ptr<const Base_1<int> > ptr_1 (pd2);
238 std::shared_ptr<const Base<int> > ptr_b (pd3);
239
240 rw_assert (pd0 == ptr_d.get (), 0, __LINE__,
241 "shared_ptr<Derived>(Derived* = %#p).get() == %#p, got %#p",
242 pd0, pd0, ptr_d.get ());
243
244 rw_assert (pd1 == ptr_0.get (), 0, __LINE__,
245 "shared_ptr<Base_0>(Derived* = %#p).get() == %#p, got %#p",
246 pd1, (Base_0<int>*)pd1, ptr_0.get ());
247
248 rw_assert (pd2 == ptr_1.get (), 0, __LINE__,
249 "shared_ptr<Base_1>(Derived* = %#p).get() == %#p, got %#p",
250 pd2, (Base_1<int>*)pd2, ptr_1.get ());
251
252 rw_assert (pd3 == ptr_b.get (), 0, __LINE__,
253 "shared_ptr<Baee>(Derived* = %#p).get() == %#p, got %#p",
254 pd3, (Base<int>*)pd3, ptr_b.get ());
255
256 rw_assert (1 == ptr_d.use_count (), 0, __LINE__, "");
257 rw_assert (1 == ptr_0.use_count (), 0, __LINE__, "");
258 rw_assert (1 == ptr_1.use_count (), 0, __LINE__, "");
259 rw_assert (1 == ptr_b.use_count (), 0, __LINE__, "");
260 }
261
262 { // template <class U, class D> shared_ptr(U*, D)
263 int* const p = new int (__LINE__);
265 std::shared_ptr<int> ptr (p, d);
266 rw_assert (p == ptr.get (), 0, __LINE__, "");
267 rw_assert (1 == ptr.use_count (), 0, __LINE__, "");
268 }
269}
270
271
273static void
275{
276 rw_info (0, "util.smartptr.shared.copy", 0,
277 "shared_ptr copy constructors");
278
279 {
280 // shared_ptr (const shared_ptr&)
281 std::shared_ptr<void> ptr;
282 std::shared_ptr<void> cpy (ptr);
283 rw_assert (0 == ptr.get (), 0, __LINE__, "");
284 rw_assert (0 == cpy.get (), 0, __LINE__, "");
285 rw_assert (0 == ptr.use_count (), 0, __LINE__, "");
286 rw_assert (0 == cpy.use_count (), 0, __LINE__, "");
287 }
288
289 {
290 // shared_ptr (const shared_ptr&)
291 std::shared_ptr<double> ptr;
292 std::shared_ptr<double> cpy (ptr);
293 rw_assert (ptr.get () == cpy.get (), 0, __LINE__, "");
294 rw_assert (ptr.use_count () == cpy.use_count (), 0, __LINE__, "");
295 }
296
297 {
298 // shared_ptr (const shared_ptr&)
299 int* const p = new int (__LINE__);
300 std::shared_ptr<int> ptr (p);
301 std::shared_ptr<int> cpy (ptr);
302 rw_assert (ptr.get () == cpy.get (), 0, __LINE__, "");
303 rw_assert (ptr.use_count () == cpy.use_count (), 0, __LINE__, "");
304 }
305
306 {
307 // shared_ptr (const shared_ptr&)
308 int* const p = new int (__LINE__);
309 std::shared_ptr<int> ptr (p);
310 std::shared_ptr<void> cpy (ptr);
311 rw_assert (ptr.get () == cpy.get (), 0, __LINE__, "");
312 rw_assert (ptr.use_count () == cpy.use_count (), 0, __LINE__, "");
313 }
314
315#if ILL_FORMED == -1 || ILL_FORMED == __LINE__
316
317 {
318 // shared_ptr (const shared_ptr&)
319 int* const p = new int (__LINE__);
320 std::shared_ptr<void> ptr (p);
321 std::shared_ptr<int> cpy (ptr);
322 rw_assert (ptr.get () == cpy.get (), 0, __LINE__, "");
323 rw_assert (ptr.use_count () == cpy.use_count (), 0, __LINE__, "");
324 }
325
326#endif // ILL_FORMED
327
328
329 { // template <class U> shared_ptr(const shared_ptr<U>&)
330 Derived<int>* const pd = new Derived<int>;
331
332#if !defined (_RWSTD_HP_aCC_MAJOR) || 6 <= _RWSTD_HP_aCC_MAJOR \
333 || 3 == _RWSTD_HP_aCC_MAJOR && __hpxstd98
334
335 typedef volatile Derived<int> v_Derived_i;
336 typedef volatile Base_0<int> v_Base_0_i;
337 typedef volatile Base_1<int> v_Base_1_i;
338 typedef volatile Base<int> v_Base_i;
339
340#else // HP aCC
341
342 // volatile disabled for HP aCC 3 without the +hpxstd98
343 // option available starting with aCC 3.74 to work around
344 // a compiler bug (see STDCXX-615)
345
346 typedef /* volatile */ Derived<int> v_Derived_i;
347 typedef /* volatile */ Base_0<int> v_Base_0_i;
348 typedef /* volatile */ Base_1<int> v_Base_1_i;
349 typedef /* volatile */ Base<int> v_Base_i;
350
351#endif // HP aCC
352
353 std::shared_ptr<v_Derived_i> ptr_d (pd);
354 std::shared_ptr<v_Base_0_i> ptr_0 (ptr_d);
355 std::shared_ptr<v_Base_1_i> ptr_1 (ptr_d);
356 std::shared_ptr<v_Base_i> ptr_b (ptr_d);
357
358 rw_assert (pd == ptr_d.get (), 0, __LINE__,
359 "shared_ptr<Derived>(Derived* = %#p).get() == %#p, got %#p",
360 pd, pd, ptr_d.get ());
361
362 rw_assert (ptr_d.get () == ptr_0.get (), 0, __LINE__,
363 "shared_ptr<Base_0>(const shared_ptr<Derived>(%#p))"
364 ".get() == %#p, got %#p",
365 ptr_d.get (),
366 (v_Base_0_i*)ptr_d.get (), ptr_0.get ());
367
368 rw_assert (ptr_d.get () == ptr_1.get (), 0, __LINE__,
369 "shared_ptr<Base_1>(const shared_ptr<Derived>(%#p))"
370 ".get() == %#p, got %#p",
371 ptr_d.get (),
372 (v_Base_1_i*)ptr_d.get (), ptr_1.get ());
373
374 rw_assert (ptr_d.get () == ptr_b.get (), 0, __LINE__,
375 "shared_ptr<Base>(const shared_ptr<Derived>(%#p))"
376 ".get() == %#p, got %#p",
377 ptr_d.get (),
378 (v_Base_i*)ptr_d.get (), ptr_b.get ());
379
380 rw_assert (4 == ptr_d.use_count (), 0, __LINE__, "");
381 rw_assert (ptr_d.use_count () == ptr_0.use_count (), 0, __LINE__, "");
382 rw_assert (ptr_d.use_count () == ptr_1.use_count (), 0, __LINE__, "");
383 rw_assert (ptr_d.use_count () == ptr_b.use_count (), 0, __LINE__, "");
384 }
385}
386
387
389static void
390test_dtor ()
391{
392 rw_info (0, "util.smartptr.shared.dest", 0,
393 "shared_ptr destructor");
394
395 {
396 // ~shared_ptr()
397 const int base_dtors = Base<int>::n_dtors;
398 {
399 std::shared_ptr<Base<int> > ptr;
400 }
401 rw_assert (base_dtors == Base<int>::n_dtors, 0, __LINE__, "");
402 }
403
404 {
405 // ~shared_ptr()
406 const int base_dtors = Base<int>::n_dtors;
407 {
408 std::shared_ptr<Base<int> > ptr (new Base<int>);
409 }
410 rw_assert (base_dtors + 1 == Base<int>::n_dtors, 0, __LINE__, "");
411 }
412
413 {
414 // ~shared_ptr()
415 const int base_dtors = Base<int>::n_dtors;
416 const int derived_dtors = Derived<int>::n_dtors;
417 {
418 std::shared_ptr<Derived<int> > ptr (new Derived<int>);
419 }
420 rw_assert (base_dtors + 1 == Base<int>::n_dtors, 0, __LINE__, "");
421 rw_assert (derived_dtors + 1 == Derived<int>::n_dtors, 0, __LINE__, "");
422 }
423
424 {
425 // ~shared_ptr()
426 const int base_dtors = Base<int>::n_dtors;
427 const int derived_dtors = Derived<int>::n_dtors;
428 {
429 std::shared_ptr<Base<int> > ptr (new Derived<int>);
430 }
431 rw_assert (base_dtors + 1 == Base<int>::n_dtors, 0, __LINE__, "");
432 rw_assert (derived_dtors + 1 == Derived<int>::n_dtors, 0, __LINE__, "");
433 }
434
435 {
436 // ~shared_ptr()
437 const int base_dtors = Base<int>::n_dtors;
438 const int derived_dtors = Derived<int>::n_dtors;
439 {
440 std::shared_ptr<void> ptr (new Derived<int>);
441 }
442 rw_assert (base_dtors + 1 == Base<int>::n_dtors, 0, __LINE__, "");
443 rw_assert (derived_dtors + 1 == Derived<int>::n_dtors, 0, __LINE__, "");
444 }
445
446 {
447 // ~shared_ptr()
449 const int funcalls = Deleter<int>::n_funcalls;
450 {
451 std::shared_ptr<int> ptr ((int*)0, d);
452 {
453 std::shared_ptr<int> cpy (ptr);
454 _RWSTD_UNUSED (cpy);
455 }
456 rw_assert (funcalls == Deleter<int>::n_funcalls, 0, __LINE__, "");
457 }
458 rw_assert (funcalls + 1 == Deleter<int>::n_funcalls, 0, __LINE__, "");
459 }
460
461 {
462 // ~shared_ptr()
464 const int funcalls = Deleter<int>::n_funcalls;
465 {
466 std::shared_ptr<int> ptr ((int*)0, d);
468 }
469 rw_assert (funcalls + 1 == Deleter<int>::n_funcalls, 0, __LINE__, "");
470 }
471
472 {
473 // ~shared_ptr()
474 int* const p = new int (__LINE__);
476 const int funcalls = Deleter<int>::n_funcalls;
477 {
478 std::shared_ptr<int> ptr (p, d);
479 }
480 rw_assert (funcalls + 1 == Deleter<int>::n_funcalls, 0, __LINE__, "");
481 }
482
483 { // ~shared_ptr
484
485 // verify that the destructor of the referenced object
486 // including all its subobjects get invoked
487 int d_dtors = Derived<int>::n_dtors;
488 int b1_dtors = Base_1<int>::n_dtors;
489 int b0_dtors = Base_0<int>::n_dtors;
490 int b_dtors = Base<int>::n_dtors;
491
492 {
493 std::shared_ptr<Derived<int> >(new Derived<int>);
494 }
495
496 rw_assert (1 == Derived<int>::n_dtors - d_dtors, 0, __LINE__,
497 "shared_ptr<Derived>(Derived*) called ~Derived()");
498 rw_assert (1 == Base_1<int>::n_dtors - b1_dtors, 0, __LINE__,
499 "shared_ptr<Derived>(Derived*) called ~Base_1()");
500 rw_assert (1 == Base_0<int>::n_dtors - b0_dtors, 0, __LINE__,
501 "shared_ptr<Derived>(Derived*) called ~Base_0()");
502 rw_assert (1 == Base<int>::n_dtors - b_dtors, 0, __LINE__,
503 "shared_ptr<Derived>(Derived*) called ~Base()");
504
505 d_dtors = Derived<int>::n_dtors;
506 b1_dtors = Base_1<int>::n_dtors;
507 b0_dtors = Base_0<int>::n_dtors;
508 b_dtors = Base<int>::n_dtors;
509
510 {
511 std::shared_ptr<Base_1<int> >(new Derived<int>);
512 }
513
514 rw_assert (1 == Derived<int>::n_dtors - d_dtors, 0, __LINE__,
515 "shared_ptr<Base_1>(Derived*) called ~Derived()");
516 rw_assert (1 == Base_1<int>::n_dtors - b1_dtors, 0, __LINE__,
517 "shared_ptr<Base_1>(Derived*) called ~Base_1()");
518 rw_assert (1 == Base_0<int>::n_dtors - b0_dtors, 0, __LINE__,
519 "shared_ptr<Base_1>(Derived*) called ~Base_0()");
520 rw_assert (1 == Base<int>::n_dtors - b_dtors, 0, __LINE__,
521 "shared_ptr<Base_1>(Derived*) called ~Base()");
522
523 d_dtors = Derived<int>::n_dtors;
524 b1_dtors = Base_1<int>::n_dtors;
525 b0_dtors = Base_0<int>::n_dtors;
526 b_dtors = Base<int>::n_dtors;
527
528 {
529 std::shared_ptr<Base_0<int> >(new Derived<int>);
530 }
531
532 rw_assert (1 == Derived<int>::n_dtors - d_dtors, 0, __LINE__,
533 "shared_ptr<Base_0>(Derived*) called ~Derived()");
534 rw_assert (1 == Base_1<int>::n_dtors - b1_dtors, 0, __LINE__,
535 "shared_ptr<Base_0>(Derived*) called ~Base_1()");
536 rw_assert (1 == Base_0<int>::n_dtors - b0_dtors, 0, __LINE__,
537 "shared_ptr<Base_0>(Derived*) called ~Base_0()");
538 rw_assert (1 == Base<int>::n_dtors - b_dtors, 0, __LINE__,
539 "shared_ptr<Base_0>(Derived*) called ~Base()");
540
541 d_dtors = Derived<int>::n_dtors;
542 b1_dtors = Base_1<int>::n_dtors;
543 b0_dtors = Base_0<int>::n_dtors;
544 b_dtors = Base<int>::n_dtors;
545
546 {
547 std::shared_ptr<Base<int> >(new Derived<int>);
548 }
549
550 rw_assert (1 == Derived<int>::n_dtors - d_dtors, 0, __LINE__,
551 "shared_ptr<Base>(Derived*) called ~Derived()");
552 rw_assert (1 == Base_1<int>::n_dtors - b1_dtors, 0, __LINE__,
553 "shared_ptr<Base>(Derived*) called ~Base_1()");
554 rw_assert (1 == Base_0<int>::n_dtors - b0_dtors, 0, __LINE__,
555 "shared_ptr<Base>(Derived*) called ~Base_0()");
556 rw_assert (1 == Base<int>::n_dtors - b_dtors, 0, __LINE__,
557 "shared_ptr<Base>(Derived*) called ~Base()");
558
559 d_dtors = Derived<int>::n_dtors;
560 b1_dtors = Base_1<int>::n_dtors;
561 b0_dtors = Base_0<int>::n_dtors;
562 b_dtors = Base<int>::n_dtors;
563
564 {
565 std::shared_ptr<void>(new Derived<int>);
566 }
567
568 rw_assert (1 == Derived<int>::n_dtors - d_dtors, 0, __LINE__,
569 "shared_ptr<void>(Derived*) called ~Derived()");
570 rw_assert (1 == Base_1<int>::n_dtors - b1_dtors, 0, __LINE__,
571 "shared_ptr<void>(Derived*) called ~Base_1()");
572 rw_assert (1 == Base_0<int>::n_dtors - b0_dtors, 0, __LINE__,
573 "shared_ptr<void>(Derived*) called ~Base_0()");
574 rw_assert (1 == Base<int>::n_dtors - b_dtors, 0, __LINE__,
575 "shared_ptr<void>(Derived*) called ~Base()");
576 }
577}
578
579
581static void
583{
584 rw_info (0, "util.smartptr.shared.assign", 0,
585 "shared_ptr assignment operators");
586
587 { // operator=(const shared_ptr&)
588 std::shared_ptr<void> ptr0;
589 std::shared_ptr<void> ptr1;
590
591 ptr1 = ptr0;
592
593 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
594 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
595 }
596
597 { // operator=(const shared_ptr&)
598 int* const p = new int (__LINE__);
599 std::shared_ptr<int> ptr0 (p);
600 std::shared_ptr<int> ptr1;
601
602 ptr1 = ptr0;
603
604 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
605 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
606 }
607
608 { // template <class U> operator=(const shared_ptr<U>&)
609 int* const p = new int (__LINE__);
610 std::shared_ptr<int> ptr0 (p);
611 std::shared_ptr<void> ptr1;
612
613 ptr1 = ptr0;
614
615 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
616 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
617 }
618
619 { // template <class U> operator=(const shared_ptr<U>&)
620 Derived<int>* const p = new Derived<int>;
621
622 std::shared_ptr<Derived<int> > ptr0 (p);
623 std::shared_ptr<Base<int> > ptr1;
624
625 ptr1 = ptr0;
626
627 rw_assert (ptr1.get ()->id == ptr0.get ()->id, 0, __LINE__,
628 "%d == %d, got %d", ptr1.get ()->id, ptr1.get ()->id,
629 ptr0.get ()->id);
630 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
631 }
632
633 { // template <class U> shared_ptr(U*)
634 Derived<int>* const pd = new Derived<int>;
635
636 const std::shared_ptr<Derived<int> > ptr_d (pd);
637
638 std::shared_ptr<Base_0<int> > ptr_0;
639 std::shared_ptr<Base_1<int> > ptr_1;
640 std::shared_ptr<Base<int> > ptr_b;
641 std::shared_ptr<void> ptr_v;
642
643 ptr_0 = ptr_d;
644 ptr_1 = ptr_d;
645 ptr_b = ptr_d;
646 ptr_v = ptr_d;
647
648 rw_assert (ptr_d.get () == ptr_0.get (), 0, __LINE__,
649 "(shared_ptr<Base_0>() = shared_ptr<Derived>(Derived* "
650 "= %#p)).get() == %#p, got %#p",
651 ptr_d.get (), (Base_0<int>*)ptr_d.get (), ptr_0.get ());
652
653 rw_assert (ptr_d.get () == ptr_1.get (), 0, __LINE__,
654 "(shared_ptr<Base_1>() = shared_ptr<Derived>(Derived* "
655 "= %#p)).get() == %#p, got %#p",
656 ptr_d.get (), (Base_1<int>*)ptr_d.get (), ptr_1.get ());
657
658 rw_assert (ptr_d.get () == ptr_b.get (), 0, __LINE__,
659 "(shared_ptr<Base>() = shared_ptr<Derived>(Derived* = %#p))"
660 ".get() == %#p, got %#p",
661 ptr_d.get (), (Base<int>*)ptr_d.get (), ptr_b.get ());
662
663 rw_assert (ptr_d.get () == ptr_v.get (), 0, __LINE__,
664 "(shared_ptr<void>() = shared_ptr<Derived>(Derived* = %#p))"
665 ".get() == %#p, got %#p",
666 ptr_d.get (), (void*)ptr_d.get (), ptr_v.get ());
667
668 rw_assert (5 == ptr_d.use_count (), 0, __LINE__, "");
669 rw_assert (ptr_0.use_count () == ptr_d.use_count (), 0, __LINE__, "");
670 rw_assert (ptr_1.use_count () == ptr_d.use_count (), 0, __LINE__, "");
671 rw_assert (ptr_b.use_count () == ptr_d.use_count (), 0, __LINE__, "");
672 rw_assert (ptr_v.use_count () == ptr_d.use_count (), 0, __LINE__, "");
673 }
674}
675
676
678static void
679test_modifiers ()
680{
681 rw_info (0, "util.smartptr.shared.mod", 0,
682 "shared_ptr modifiers");
683
684 rw_warn (0, 0, 0,
685 "shared_ptr modifiers not exercised");
686}
687
688
690static void
691test_observers ()
692{
693 rw_info (0, "util.smartptr.shared.obs", 0,
694 "shared_ptr observers");
695
696 { // operator*()
697 std::shared_ptr<void> ptr;
698 rw_assert (0 == ptr.get (), 0, __LINE__, "");
699 rw_assert (0 == ptr.use_count (), 0, __LINE__, "");
700 }
701
702 rw_warn (0, 0, 0,
703 "shared_ptr observers not exercised");
704}
705
706
708static void
709test_comparison ()
710{
711 rw_info (0, "util.smartptr.shared.cmp", 0,
712 "shared_ptr comparison");
713
714 { // operator==
715 std::shared_ptr<void> ptr;
716 rw_assert (ptr == ptr, 0, __LINE__, "");
717
718 rw_assert (!(ptr != ptr), 0, __LINE__, "");
719 }
720
721 { // operator==
722 std::shared_ptr<void> ptr0;
723 std::shared_ptr<int> ptr1;
724 rw_assert (ptr0 == ptr1, 0, __LINE__, "");
725 rw_assert (ptr1 == ptr0, 0, __LINE__, "");
726
727 rw_assert (!(ptr0 != ptr1), 0, __LINE__, "");
728 rw_assert (!(ptr1 != ptr0), 0, __LINE__, "");
729 }
730
731#if ILL_FORMED == -1 || ILL_FORMED == __LINE__
732
733 {
734 std::shared_ptr<char> ptr0;
735 std::shared_ptr<int> ptr1;
736
737 rw_assert (ptr0 == ptr1, 0, __LINE__, "");
738 rw_assert (ptr1 == ptr0, 0, __LINE__, "");
739
740 rw_assert (!(ptr0 != ptr1), 0, __LINE__, "");
741 rw_assert (!(ptr1 != ptr0), 0, __LINE__, "");
742 }
743
744#endif // ILL_FORMED
745
746 { // operator==
747 int* const p = new int (__LINE__);
748 std::shared_ptr<int> ptr0 (p);
749 std::shared_ptr<int> ptr1 (ptr0);
750
751 rw_assert (ptr0 == ptr1, 0, __LINE__, "");
752 rw_assert (ptr1 == ptr0, 0, __LINE__, "");
753
754 rw_assert (!(ptr0 != ptr1), 0, __LINE__, "");
755 rw_assert (!(ptr1 != ptr0), 0, __LINE__, "");
756 }
757
758 { // operator==
759 int* const p0 = new int (__LINE__);
760 int* const p1 = new int (__LINE__);
761 std::shared_ptr<int> ptr0 (p0);
762 std::shared_ptr<int> ptr1 (p1);
763
764 rw_assert (!(ptr0 == ptr1), 0, __LINE__, "");
765 rw_assert (!(ptr1 == ptr0), 0, __LINE__, "");
766
767 rw_assert (ptr0 != ptr1, 0, __LINE__, "");
768 rw_assert (ptr1 != ptr0, 0, __LINE__, "");
769 }
770
771 { // operator<
772 Derived<int>* const p = new Derived<int>;
773
774 std::shared_ptr<Derived<int> > ptr2 (p);
775 std::shared_ptr<Base_1<int> > ptr1 (ptr2);
776 std::shared_ptr<Base_0<int> > ptr0 (ptr2);
777 std::shared_ptr<Base<int> > ptr_b1 (ptr1);
778 std::shared_ptr<Base<int> > ptr_b0 (ptr0);
779
780 const bool eq_1_2 = (ptr1.get () == ptr2.get ()) == (ptr1 == ptr2);
781 const bool eq_0_2 = (ptr0.get () == ptr2.get ()) == (ptr0 == ptr2);
782
783 // rw_assert (eq_0_1, 0, __LINE__, "");
784 rw_assert (eq_1_2, 0, __LINE__, "");
785 rw_assert (eq_0_2, 0, __LINE__, "");
786 }
787
788 { // operator<
789 std::shared_ptr<void> ptr;
790
791 rw_assert (!(ptr < ptr), 0, __LINE__, "");
792 }
793
794 { // operator<
795 std::shared_ptr<void> ptr;
796 std::shared_ptr<void> cpy (ptr);
797
798 rw_assert (!(ptr < cpy) && !(cpy < ptr), 0, __LINE__, "");
799 }
800
801 { // operator<
802 int* const p = new int (__LINE__);
803 std::shared_ptr<void> ptr (p);
804 std::shared_ptr<void> cpy (ptr);
805
806 rw_assert (!(ptr < cpy) && !(cpy < ptr), 0, __LINE__, "");
807 }
808
809 { // operator<
810 Derived<int>* const p = new Derived<int>;
811
812 std::shared_ptr<Derived<int> > ptr2 (p);
813 std::shared_ptr<Base_1<int> > ptr1 (ptr2);
814 std::shared_ptr<Base_0<int> > ptr0 (ptr2);
815 std::shared_ptr<void> ptr (ptr0);
816
817 rw_assert (!(ptr < ptr0) && !(ptr0 < ptr), 0, __LINE__, "");
818 rw_assert (!(ptr < ptr1) && !(ptr1 < ptr), 0, __LINE__, "");
819 rw_assert (!(ptr < ptr2) && !(ptr2 < ptr ), 0, __LINE__, "");
820 rw_assert (!(ptr0 < ptr1) && !(ptr0 < ptr1), 0, __LINE__, "");
821 rw_assert (!(ptr0 < ptr2) && !(ptr0 < ptr2), 0, __LINE__, "");
822 rw_assert (!(ptr1 < ptr2) && !(ptr1 < ptr2), 0, __LINE__, "");
823 }
824
825 { // operator<
826 short* const p0 = new short (__LINE__);
827 float* const p1 = new float (__LINE__);
828
829 std::shared_ptr<short> ptr0 (p0);
830 std::shared_ptr<float> ptr1 (p1);
831
832 rw_assert (ptr0 < ptr1 || ptr1 < ptr0, 0, __LINE__, "");
833 }
834}
835
836
838static void
839test_io ()
840{
841 rw_info (0, "util.smartptr.shared.io", 0,
842 "shared_ptr I/O");
843
844 rw_warn (0, 0, 0,
845 "shared_ptr I/O not exercised");
846}
847
848
850static void
851test_specialized ()
852{
853 rw_info (0, "util.smartptr.shared.spec", 0,
854 "shared_ptr specialized algorithms");
855
856 rw_warn (0, 0, 0,
857 "shared_ptr specialized algorithms not exercised");
858}
859
860
862static void
863test_casts ()
864{
865 rw_info (0, "util.smartptr.shared.cast", 0,
866 "shared_ptr casts");
867
868 {
869 // dynamic_pointer_cast
870
871 }
872
873 rw_warn (0, 0, 0,
874 "shared_ptr casts not exercised");
875}
876
877
879static void
880test_deleter ()
881{
882 rw_info (0, "util.smartptr.shared.getdeleter", 0,
883 "shared_ptr get_deleter");
884
885 rw_warn (0, 0, 0,
886 "shared_ptr get_deleter not exercised");
887}
888
889
891static int no_ctor;
892static int no_copy_ctor;
893static int no_dtor;
894static int no_assign;
895static int no_modifiers;
896static int no_observers;
897static int no_comparison;
898static int no_io;
899static int no_specialized;
900static int no_casts;
901static int no_deleter;
902
903
904static int
905run_test (int, char*[])
906{
907#define TEST(what) \
908 if (no_ ## what) { \
909 rw_note (0, 0, __LINE__, "%s test disabled", #what); \
910 } \
911 else { \
912 test_ ## what (); \
913 } typedef void unused_typedef
914
915 TEST (ctor);
916 TEST (copy_ctor);
917 TEST (dtor);
918 TEST (assign);
919 TEST (modifiers);
920 TEST (observers);
921 TEST (comparison);
922 TEST (io);
923 TEST (specialized);
924 TEST (casts);
925 TEST (deleter);
926
927 return 0;
928}
929
930
932int main (int argc, char *argv[])
933{
934 return rw_test (argc, argv, __FILE__,
935 "util.smartptr.shared",
936 0 /* no comment */, run_test,
937 "|-no-ctor# "
938 "|-no-copy_ctor# "
939 "|-no-dtor# "
940 "|-no-assign# "
941 "|-no-modifiers# "
942 "|-no-observers# "
943 "|-no-comparison# "
944 "|-no-io# "
945 "|-no-specialized# "
946 "|-no-casts# "
947 "|-no-deleter# ",
948 &no_ctor,
949 &no_copy_ctor,
950 &no_dtor,
951 &no_assign,
952 &no_modifiers,
953 &no_observers,
954 &no_comparison,
955 &no_io,
956 &no_specialized,
957 &no_casts,
958 &no_deleter);
959}
960
961#else // _RWSTD_NO_EXT_CXX_0X
962
963static int
964run_test (int, char*[])
965{
966 rw_warn (0, 0, __LINE__,
967 "test disabled because _RWSTD_NO_EXT_CXX_0X is defined");
968 return 0;
969}
970
971int main (int argc, char *argv[])
972{
973 return rw_test (argc, argv, __FILE__,
974 "util.smartptr.shared",
975 0 /* no comment */,
976 run_test,
977 0);
978}
979
980#endif // _RWSTD_NO_EXT_CXX_0X
#define T
Definition 17.names.cpp:163
#define d
Definition 17.names.cpp:115
#define ptr
Definition 17.names.cpp:525
#define p1
Definition 17.names.cpp:239
#define p
Definition 17.names.cpp:132
int next_id
#define TEST(what)
void test_io(charT *, Traits *, Allocator *, const StringTestCaseData< charT > &tdata)
void test_assign(int line, int exceptions, const Iterator &dummy, int nelems, const char *seq, std::size_t seqlen, const char *asn, std::size_t asnlen, const char *res, std::size_t reslen)
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
void test_copy_ctor(const T *, const char *tname)
void test_ctor(const charT *, const Traits *, const char *cname, const char *tname)
#define main
volatile double * pd
int dtor
#define _RWSTD_UNUSED(arg)
Definition _defs.h:972
@ no_ctor
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_info(int, const char *, int, const char *,...)
Definition driver.cpp:1681
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
_TEST_EXPORT int rw_warn(int, const char *, int, const char *,...)
Definition driver.cpp:1647
static int n_dtors
virtual ~Base_0()
static int n_dtors
virtual ~Base_1()
static int n_dtors
virtual ~Deleter()
static int n_funcalls
static int n_def_ctors
void operator()(T *p)
Deleter(const Deleter &)
static int n_dtors
static int n_cpy_ctors
virtual ~Derived()
static int n_dtors