Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
20.smartptr.weak.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
39template <class T>
40struct Base
41{
42 static int n_dtors;
43
44 virtual ~Base () {
45 ++n_dtors;
46 }
47};
48
49template <class T>
51
52
53template <class T>
54struct Derived: Base<T>
55{
56 static int n_dtors;
57
58 virtual ~Derived () {
59 ++n_dtors;
60 }
61};
62
63template <class T>
65
66
67template <class T>
68struct Derived2: Base<T>
69{
70 static int n_dtors;
71
72 virtual ~Derived2 () {
73 ++n_dtors;
74 }
75};
76
77template <class T>
79
80
81template <class T>
82struct Deleter
83{
84 static int n_def_ctors;
85 static int n_cpy_ctors;
86 static int n_dtors;
87 static int n_funcalls;
88
91 }
92
93 Deleter (const Deleter&) {
95 }
96
97 virtual ~Deleter () {
98 ++n_dtors;
99 }
100
101 void operator()(T *p) {
102 ++n_funcalls;
103 delete p;
104 }
105
106private:
107 void operator=(Deleter&); // not defined
108};
109
110template <class T>
112
113template <class T>
115
116template <class T>
118
119template <class T>
121
122
124static void
125test_ctor ()
126{
127 rw_info (0, "util.smartptr.weak.cons", 0,
128 "weak_ptr constructors");
129
130 { // default ctor
131 std::weak_ptr<char> weak;
132 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
133 }
134
135 { // shared_ptr converting ctor
136 std::shared_ptr<char> shared;
137 std::weak_ptr<char> weak (shared);
138 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
139 }
140
141 { // shared_ptr converting ctor
142 std::shared_ptr<char> shared;
143 std::weak_ptr<void> weak (shared);
144 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
145 }
146
147 { // shared_ptr converting ctor
148 int* const p = new int (__LINE__);
149 std::shared_ptr<int> shared (p);
150 std::weak_ptr<int> weak (shared);
151 rw_assert (shared.use_count () == weak.use_count (), 0, __LINE__, "");
152 }
153
154 { // shared_ptr converting ctor
155 int* const p = new int (__LINE__);
156 std::shared_ptr<int> shared0 (p);
157 std::shared_ptr<int> shared1 (shared0);
158 std::weak_ptr<int> weak (shared1);
159 rw_assert (shared1.use_count () == weak.use_count (), 0, __LINE__, "");
160 }
161
162
163 { // shared_ptr converting ctor
164 int* const p = new int (__LINE__);
165 std::shared_ptr<int> shared (p);
166 std::weak_ptr<void> weak (shared);
167 shared.reset ();
168
169 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
170 }
171
172 { // shared_ptr converting ctor
173 Derived<int>* const p = new Derived<int>;
174 std::shared_ptr<Derived<int> > shared (p);
175 std::weak_ptr<Base<int> > weak (shared);
176 shared.reset ();
177
178 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
179 }
180}
181
182
184static void
186{
187 rw_info (0, "util.smartptr.weak.copy", 0,
188 "weak_ptr copy constructors");
189
190 {
191 // weak_ptr (const weak_ptr&)
192 std::weak_ptr<void> weak;
193 std::weak_ptr<void> copy (weak);
194 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
195 rw_assert (0 == copy.use_count (), 0, __LINE__, "");
196 }
197
198 {
199 // weak_ptr (const weak_ptr&)
200 int* const p = new int (__LINE__);
201 std::shared_ptr<int> shared (p);
202 std::weak_ptr<int> weak (shared);
203 std::weak_ptr<int> copy (weak);
204
205 rw_assert (weak.use_count () == shared.use_count (), 0, __LINE__, "");
206 rw_assert (copy.use_count () == shared.use_count (), 0, __LINE__, "");
207 }
208
209 {
210 // weak_ptr (const weak_ptr&)
211 int* const p = new int (__LINE__);
212 std::shared_ptr<int> shared0 (p);
213 std::shared_ptr<int> shared1 (shared0);
214 std::weak_ptr<int> weak (shared1);
215 std::weak_ptr<int> copy (weak);
216
217 rw_assert (weak.use_count () == shared1.use_count (), 0, __LINE__, "");
218 rw_assert (copy.use_count () == shared1.use_count (), 0, __LINE__, "");
219 }
220
221 {
222 // weak_ptr (const weak_ptr&)
223 int* const p = new int (__LINE__);
224 std::shared_ptr<int> shared0 (p);
225 std::shared_ptr<int> shared1 (shared0);
226 std::weak_ptr<int> weak (shared1);
227 std::weak_ptr<void> copy (weak);
228
229 rw_assert (weak.use_count () == shared1.use_count (), 0, __LINE__, "");
230 rw_assert (copy.use_count () == shared1.use_count (), 0, __LINE__, "");
231 }
232
233 {
234 // weak_ptr (const weak_ptr&)
235 Derived<int>* const p = new Derived<int>;
236 std::shared_ptr<Derived<int> > shared0 (p);
237 std::shared_ptr<Derived<int> > shared1 (shared0);
238 std::weak_ptr<Derived<int> > weak (shared1);
239 std::weak_ptr<Base<int> > copy (weak);
240
241 rw_assert (weak.use_count () == shared1.use_count (), 0, __LINE__, "");
242 rw_assert (copy.use_count () == shared1.use_count (), 0, __LINE__, "");
243
244 shared1.reset ();
245 rw_assert (copy.use_count () == weak.use_count (), 0, __LINE__, "");
246 rw_assert (copy.use_count () == shared0.use_count (), 0, __LINE__, "");
247
248 shared0.reset ();
249 rw_assert (copy.use_count () == weak.use_count (), 0, __LINE__, "");
250 rw_assert (copy.use_count () == shared0.use_count (), 0, __LINE__, "");
251 }
252}
253
254
256static void
257test_dtor ()
258{
259 rw_info (0, "util.smartptr.weak.dest", 0,
260 "weak_ptr destructor");
261
262 {
263 // ~weak_ptr()
264 const int base_dtors = Base<int>::n_dtors;
265 {
266 std::weak_ptr<Base<int> > weak;
267 }
268 rw_assert (base_dtors == Base<int>::n_dtors, 0, __LINE__, "");
269 }
270
271 {
272 // ~weak_ptr()
273 const int base_dtors = Base<int>::n_dtors;
274 Base<int>* const p = new Base<int>;
275 std::shared_ptr<Base<int> > shared (p);
276 {
277 std::weak_ptr<Base<int> > weak (shared);
278 }
279 rw_assert (base_dtors == Base<int>::n_dtors, 0, __LINE__, "");
280 }
281
282 {
283 // ~weak_ptr()
284 const int base_dtors = Base<int>::n_dtors;
285 const int derived_dtors = Derived<int>::n_dtors;
286
287 Derived<int>* const p = new Derived<int>;
288 std::shared_ptr<Derived<int> > shared (p);
289
290 {
291 std::weak_ptr<Base<int> > weak (shared);
292 }
293 rw_assert (base_dtors == Base<int>::n_dtors, 0, __LINE__, "");
294 rw_assert (derived_dtors == Derived<int>::n_dtors, 0, __LINE__, "");
295 }
296
297 {
298 // ~weak_ptr()
299 const int base_dtors = Base<int>::n_dtors;
300 const int derived_dtors = Derived<int>::n_dtors;
301
302 Derived<int>* const p = new Derived<int>;
303 std::shared_ptr<Derived<int> > shared (p);
304
305 {
306 std::weak_ptr<Base<int> > weak (shared);
307 std::weak_ptr<Base<int> > copy (shared);
308 }
309 rw_assert (base_dtors == Base<int>::n_dtors, 0, __LINE__, "");
310 rw_assert (derived_dtors == Derived<int>::n_dtors, 0, __LINE__, "");
311 }
312
313 {
314 // ~weak_ptr()
315 const int base_dtors = Base<int>::n_dtors;
316 const int derived_dtors = Derived<int>::n_dtors;
317
318 Derived<int>* const p = new Derived<int>;
319 std::shared_ptr<Derived<int> > shared (p);
320
321 {
322 std::weak_ptr<Base<int> > weak (shared);
323 shared.reset ();
324 }
325 rw_assert (base_dtors + 1 == Base<int>::n_dtors, 0, __LINE__, "");
326 rw_assert (derived_dtors + 1 == Derived<int>::n_dtors, 0, __LINE__, "");
327 }
328
329 {
330 // ~weak_ptr()
331 const int base_dtors = Base<int>::n_dtors;
332 const int derived_dtors = Derived<int>::n_dtors;
333
334 std::weak_ptr<Derived<int> > weak;
335
336 {
337 Derived<int>* const p = new Derived<int>;
338 std::shared_ptr<Derived<int> > shared (p);
339
340 weak = shared;
341 }
342 rw_assert (base_dtors + 1 == Base<int>::n_dtors, 0, __LINE__, "");
343 rw_assert (derived_dtors + 1 == Derived<int>::n_dtors, 0, __LINE__, "");
344 }
345}
346
347
349static void
351{
352 rw_info (0, "util.smartptr.weak.assign", 0,
353 "weak_ptr assignment operators");
354
355#if 0
356
357 { // operator=(const weak_ptr&)
358 std::weak_ptr<void> ptr0;
359 std::weak_ptr<void> ptr1;
360
361 ptr1 = ptr0;
362
363 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
364 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
365 }
366
367 { // operator=(const weak_ptr&)
368 int* const p = new int (__LINE__);
369 std::weak_ptr<int> ptr0 (p);
370 std::weak_ptr<int> ptr1;
371
372 ptr1 = ptr0;
373
374 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
375 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
376 }
377
378 { // template <class U> operator=(const weak_ptr<U>&)
379 int* const p = new int (__LINE__);
380 std::weak_ptr<int> ptr0 (p);
381 std::weak_ptr<void> ptr1;
382
383 ptr1 = ptr0;
384
385 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
386 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
387 }
388
389 { // template <class U> operator=(const weak_ptr<U>&)
390 Derived<int>* const p = new Derived<int>;
391 std::weak_ptr<Derived<int> > ptr0 (p);
392 std::weak_ptr<Base<int> > ptr1;
393
394 ptr1 = ptr0;
395
396 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
397 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
398 }
399
400 { // template <class U> operator=(const weak_ptr<U>&)
401 Derived<int>* const p = new Derived<int>;
402 std::weak_ptr<Base<int> > ptr0 (p);
403 std::weak_ptr<void> ptr1;
404
405 ptr1 = ptr0;
406
407 rw_assert (ptr1.get () == ptr0.get (), 0, __LINE__, "");
408 rw_assert (ptr1.use_count () == ptr0.use_count (), 0, __LINE__, "");
409 }
410
411#endif // 0/1
412
413}
414
415
417static void
418test_modifiers ()
419{
420 rw_info (0, "util.smartptr.weak.mod", 0,
421 "weak_ptr modifiers");
422
423 rw_warn (0, 0, 0,
424 "weak_ptr modifiers not exercised");
425}
426
427
429static void
430test_observers ()
431{
432 rw_info (0, "util.smartptr.weak.obs", 0,
433 "weak_ptr observers");
434
435 { // use_count()
436 std::weak_ptr<void> weak;
437 rw_assert (0 == weak.use_count (), 0, __LINE__, "");
438 }
439
440 { // expired()
441 std::weak_ptr<void> weak;
442 rw_assert (weak.expired (), 0, __LINE__, "");
443 }
444
445 { // expired()
446 std::shared_ptr<void> shared;
447 std::weak_ptr<void> weak (shared);
448 rw_assert (weak.expired (), 0, __LINE__, "");
449 }
450
451 { // expired()
452 int* const p = new int (__LINE__);
453 std::shared_ptr<void> shared (p);
454 std::weak_ptr<void> weak (shared);
455 rw_assert (!weak.expired (), 0, __LINE__, "");
456 }
457
458 { // expired()
459 int* const p = new int (__LINE__);
460 std::shared_ptr<void> shared (p);
461 std::weak_ptr<void> weak (shared);
462 shared.reset ();
463 rw_assert (weak.expired (), 0, __LINE__, "");
464 }
465
466 { // lock()
467 std::weak_ptr<void> weak;
468 std::shared_ptr<void> shared (weak.lock ());
469
470 rw_assert (0 == weak.use_count (), 0, __LINE__,
471 "weak_ptr<void>::use_count() == 1, got %ld",
472 weak.use_count ());
473 rw_assert (0 == shared.use_count (), 0, __LINE__,
474 "shared_ptr<void>::use_count() == 1, got %ld",
475 shared.use_count ());
476 }
477
478 { // lock()
479 int* const p = new int (__LINE__);
480 std::shared_ptr<void> shared0 (p);
481 std::weak_ptr<void> weak (shared0);
482 std::shared_ptr<void> shared1 (weak.lock ());
483
484 rw_assert (2 == weak.use_count (), 0, __LINE__,
485 "weak_ptr<void>::use_count() == 2, got %ld",
486 weak.use_count ());
487 rw_assert (2 == shared1.use_count (), 0, __LINE__,
488 "shared_ptr<void>::use_count() == 2, got %ld",
489 shared1.use_count ());
490 }
491
492 { // lock()
493 int* const p = new int (__LINE__);
494 std::shared_ptr<void> shared0 (p);
495 std::weak_ptr<void> weak (shared0);
496 std::shared_ptr<void> shared1 (weak.lock ());
497 std::shared_ptr<void> shared2 (shared1);
498
499 rw_assert (3 == weak.use_count (), 0, __LINE__,
500 "weak_ptr<void>::use_count() == 3, got %ld",
501 weak.use_count ());
502 rw_assert (3 == shared0.use_count (), 0, __LINE__,
503 "shared_ptr<void>::use_count() == 3, got %ld",
504 shared1.use_count ());
505
506 shared0.reset ();
507
508 rw_assert (2 == weak.use_count (), 0, __LINE__,
509 "weak_ptr<void>::use_count() == 2, got %ld",
510 weak.use_count ());
511
512 shared1.reset ();
513
514 rw_assert (1 == weak.use_count (), 0, __LINE__,
515 "weak_ptr<void>::use_count() == 1, got %ld",
516 weak.use_count ());
517
518 shared2.reset ();
519
520 rw_assert (0 == weak.use_count (), 0, __LINE__,
521 "weak_ptr<void>::use_count() == 0, got %ld",
522 weak.use_count ());
523 }
524}
525
526
528static void
529test_comparison ()
530{
531 rw_info (0, "util.smartptr.weak.cmp", 0,
532 "weak_ptr comparison");
533
534 rw_warn (0, 0, 0,
535 "weak_ptr comparison not exercised");
536}
537
538
540static void
541test_specialized ()
542{
543 rw_info (0, "util.smartptr.weak.spec", 0,
544 "weak_ptr specialized algorithms");
545
546 rw_warn (0, 0, 0,
547 "weak_ptr specialized algorithms not exercised");
548}
549
550
552static int no_ctor;
553static int no_copy_ctor;
554static int no_dtor;
555static int no_assign;
556static int no_modifiers;
557static int no_observers;
558static int no_comparison;
559static int no_specialized;
560
561
562static int
563run_test (int, char*[])
564{
565#define TEST(what) \
566 if (no_ ## what) { \
567 rw_note (0, 0, __LINE__, "%s test disabled", #what); \
568 } \
569 else { \
570 test_ ## what (); \
571 } typedef void unused_typedef
572
573 TEST (ctor);
574 TEST (copy_ctor);
575 TEST (dtor);
576 TEST (assign);
577 TEST (modifiers);
578 TEST (observers);
579 TEST (comparison);
580 TEST (specialized);
581
582 return 0;
583}
584
585
587int main (int argc, char *argv[])
588{
589 return rw_test (argc, argv, __FILE__,
590 "util.smartptr.weak",
591 0 /* no comment */, run_test,
592 "|-no-ctor# "
593 "|-no-copy_ctor# "
594 "|-no-dtor# "
595 "|-no-assign# "
596 "|-no-modifiers# "
597 "|-no-observers# "
598 "|-no-comparison# "
599 "|-no-specialized# ",
600 &no_ctor,
601 &no_copy_ctor,
602 &no_dtor,
603 &no_assign,
604 &no_modifiers,
605 &no_observers,
606 &no_comparison,
607 &no_specialized);
608}
609
610#else // _RWSTD_NO_EXT_CXX_0X
611
612static int
613run_test (int, char* [])
614{
615 rw_warn (0, 0, __LINE__,
616 "test disabled because _RWSTD_NO_EXT_CXX_0X is defined");
617 return 0;
618}
619
620int main (int argc, char *argv[])
621{
622 return rw_test (argc, argv, __FILE__,
623 "util.smartptr.weak",
624 0 /* no comment */,
625 run_test,
626 0);
627}
628
629#endif // _RWSTD_NO_EXT_CXX_0X
#define T
Definition 17.names.cpp:163
#define p
Definition 17.names.cpp:132
#define TEST(what)
const std::string shared[nstrings]
const std::string shared1(64, '_')
const std::string shared0
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
int dtor
@ 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()
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
static int n_dtors
virtual ~Derived2()
virtual ~Derived()
static int n_dtors