VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestAutoPtr.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.4 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2// vim:cindent:ts=4:et:sw=4:
3/* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is TestCOMPtrEq.cpp.
17 *
18 * The Initial Developer of the Original Code is
19 * L. David Baron.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 * L. David Baron <dbaron@dbaron.org> (original author)
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40#include "nsAutoPtr.h"
41#include <stdio.h>
42#include "nscore.h"
43
44class TestObjectBaseA {
45 public:
46 // Virtual dtor for deleting through base class pointer
47 virtual ~TestObjectBaseA() { };
48 int fooA;
49};
50
51class TestObjectBaseB {
52 public:
53 // Virtual dtor for deleting through base class pointer
54 virtual ~TestObjectBaseB() { };
55 int fooB;
56};
57
58class TestObject : public TestObjectBaseA, public TestObjectBaseB {
59 public:
60 TestObject()
61 {
62 printf(" Creating TestObject %p.\n",
63 NS_STATIC_CAST(void*, this));
64 }
65
66 // Virtual dtor for deleting through base class pointer
67 virtual ~TestObject()
68 {
69 printf(" Destroying TestObject %p.\n",
70 NS_STATIC_CAST(void*, this));
71 }
72};
73
74class TestRefObjectBaseA {
75 public:
76 int fooA;
77 // Must return |nsrefcnt| to keep |nsDerivedSafe| happy.
78 virtual nsrefcnt AddRef() = 0;
79 virtual nsrefcnt Release() = 0;
80};
81
82class TestRefObjectBaseB {
83 public:
84 int fooB;
85 virtual nsrefcnt AddRef() = 0;
86 virtual nsrefcnt Release() = 0;
87};
88
89class TestRefObject : public TestRefObjectBaseA, public TestRefObjectBaseB {
90 public:
91 TestRefObject()
92 : mRefCount(0)
93 {
94 printf(" Creating TestRefObject %p.\n",
95 NS_STATIC_CAST(void*, this));
96 }
97
98 ~TestRefObject()
99 {
100 printf(" Destroying TestRefObject %p.\n",
101 NS_STATIC_CAST(void*, this));
102 }
103
104 nsrefcnt AddRef()
105 {
106 ++mRefCount;
107 printf(" AddRef to %d on TestRefObject %p.\n",
108 mRefCount, NS_STATIC_CAST(void*, this));
109 return mRefCount;
110 }
111
112 nsrefcnt Release()
113 {
114 --mRefCount;
115 printf(" Release to %d on TestRefObject %p.\n",
116 mRefCount, NS_STATIC_CAST(void*, this));
117 if (mRefCount == 0) {
118 delete NS_CONST_CAST(TestRefObject*, this);
119 return 0;
120 }
121 return mRefCount;
122 }
123
124 protected:
125 PRUint32 mRefCount;
126
127};
128
129static void CreateTestObject(TestObject **aResult)
130{
131 *aResult = new TestObject();
132}
133
134static void CreateTestRefObject(TestRefObject **aResult)
135{
136 (*aResult = new TestRefObject())->AddRef();
137}
138
139static void DoSomethingWithTestObject(TestObject *aIn)
140{
141 printf(" Doing something with |TestObject| %p.\n",
142 NS_STATIC_CAST(void*, aIn));
143}
144
145static void DoSomethingWithConstTestObject(const TestObject *aIn)
146{
147 printf(" Doing something with |const TestObject| %p.\n",
148 NS_STATIC_CAST(const void*, aIn));
149}
150
151static void DoSomethingWithTestRefObject(TestRefObject *aIn)
152{
153 printf(" Doing something with |TestRefObject| %p.\n",
154 NS_STATIC_CAST(void*, aIn));
155}
156
157static void DoSomethingWithConstTestRefObject(const TestRefObject *aIn)
158{
159 printf(" Doing something with |const TestRefObject| %p.\n",
160 NS_STATIC_CAST(const void*, aIn));
161}
162
163static void DoSomethingWithTestObjectBaseB(TestObjectBaseB *aIn)
164{
165 printf(" Doing something with |TestObjectBaseB| %p.\n",
166 NS_STATIC_CAST(void*, aIn));
167}
168
169static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB *aIn)
170{
171 printf(" Doing something with |const TestObjectBaseB| %p.\n",
172 NS_STATIC_CAST(const void*, aIn));
173}
174
175static void DoSomethingWithTestRefObjectBaseB(TestRefObjectBaseB *aIn)
176{
177 printf(" Doing something with |TestRefObjectBaseB| %p.\n",
178 NS_STATIC_CAST(void*, aIn));
179}
180
181static void DoSomethingWithConstTestRefObjectBaseB(const TestRefObjectBaseB *aIn)
182{
183 printf(" Doing something with |const TestRefObjectBaseB| %p.\n",
184 NS_STATIC_CAST(const void*, aIn));
185}
186
187int main()
188{
189 {
190 printf("Should create one |TestObject|:\n");
191 nsAutoPtr<TestObject> pobj( new TestObject() );
192 printf("Should destroy one |TestObject|:\n");
193 }
194
195 {
196 printf("Should create one |TestObject|:\n");
197 nsAutoPtr<TestObject> pobj( new TestObject() );
198 printf("Should create one |TestObject| and then destroy one:\n");
199 pobj = new TestObject();
200 printf("Should destroy one |TestObject|:\n");
201 }
202
203 {
204 printf("Should create 3 |TestObject|s:\n");
205 nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
206 printf("Should create 5 |TestObject|s and then destroy 3:\n");
207 pobj = new TestObject[5];
208 printf("Should destroy 5 |TestObject|s:\n");
209 }
210
211 {
212 printf("Should create and AddRef one |TestRefObject|:\n");
213 nsRefPtr<TestRefObject> pobj( new TestRefObject() );
214 printf("Should Release and destroy one |TestRefObject|:\n");
215 }
216
217 {
218 printf("Should create and AddRef one |TestRefObject|:\n");
219 nsRefPtr<TestRefObject> pobj( new TestRefObject() );
220 printf("Should create and AddRef one |TestRefObject| and then Release and destroy one:\n");
221 pobj = new TestRefObject();
222 printf("Should Release and destroy one |TestRefObject|:\n");
223 }
224
225 {
226 printf("Should create and AddRef one |TestRefObject|:\n");
227 nsRefPtr<TestRefObject> p1( new TestRefObject() );
228 printf("Should AddRef one |TestRefObject|:\n");
229 nsRefPtr<TestRefObject> p2( p1 );
230 printf("Should Release twice and destroy one |TestRefObject|:\n");
231 }
232
233 printf("\nTesting equality (with all const-ness combinations):\n");
234
235 {
236 nsRefPtr<TestRefObject> p1( new TestRefObject() );
237 nsRefPtr<TestRefObject> p2( p1 );
238 printf("equality %s.\n",
239 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
240 }
241
242 {
243 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
244 nsRefPtr<TestRefObject> p2( p1 );
245 printf("equality %s.\n",
246 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
247 }
248
249 {
250 nsRefPtr<TestRefObject> p1( new TestRefObject() );
251 const nsRefPtr<TestRefObject> p2( p1 );
252 printf("equality %s.\n",
253 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
254 }
255
256 {
257 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
258 const nsRefPtr<TestRefObject> p2( p1 );
259 printf("equality %s.\n",
260 ((p1 == p2) && !(p1 != p2)) ? "OK" : "broken");
261 }
262
263 {
264 nsRefPtr<TestRefObject> p1( new TestRefObject() );
265 TestRefObject * p2 = p1;
266 printf("equality %s.\n",
267 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
268 }
269
270 {
271 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
272 TestRefObject * p2 = p1;
273 printf("equality %s.\n",
274 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
275 }
276
277#if 0 /* MSVC++ 6.0 can't be coaxed to accept this */
278 {
279 nsRefPtr<TestRefObject> p1( new TestRefObject() );
280 TestRefObject * const p2 = p1;
281 printf("equality %s.\n",
282 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
283 }
284
285 {
286 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
287 TestRefObject * const p2 = p1;
288 printf("equality %s.\n",
289 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
290 }
291#endif /* Things that MSVC++ 6.0 can't be coaxed to accept */
292
293 {
294 nsRefPtr<TestRefObject> p1( new TestRefObject() );
295 const TestRefObject * p2 = p1;
296 printf("equality %s.\n",
297 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
298 }
299
300 {
301 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
302 const TestRefObject * p2 = p1;
303 printf("equality %s.\n",
304 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
305 }
306
307 {
308 nsRefPtr<TestRefObject> p1( new TestRefObject() );
309 const TestRefObject * const p2 = p1;
310 printf("equality %s.\n",
311 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
312 }
313
314 {
315 const nsRefPtr<TestRefObject> p1( new TestRefObject() );
316 const TestRefObject * const p2 = p1;
317 printf("equality %s.\n",
318 ((p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1)) ? "OK" : "broken");
319 }
320
321 printf("\nTesting getter_Transfers and getter_AddRefs.\n");
322
323 {
324 nsAutoPtr<TestObject> ptr;
325 printf("Should create one |TestObject|:\n");
326 CreateTestObject(getter_Transfers(ptr));
327 printf("Should destroy one |TestObject|:\n");
328 }
329
330 {
331 nsRefPtr<TestRefObject> ptr;
332 printf("Should create and AddRef one |TestRefObject|:\n");
333 CreateTestRefObject(getter_AddRefs(ptr));
334 printf("Should Release and destroy one |TestRefObject|:\n");
335 }
336
337 printf("\nTesting casts and equality tests.\n");
338
339 if ((void*)(TestObject*)0x1000 ==
340 (void*)(TestObjectBaseB*)(TestObject*)0x1000)
341 printf("\n\nAll these tests are meaningless!\n\n\n");
342
343 {
344 nsAutoPtr<TestObject> p1(new TestObject());
345 TestObjectBaseB *p2 = p1;
346 printf("equality %s.\n",
347 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
348 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
349 ? "OK" : "broken");
350 }
351
352 {
353 TestObject *p1 = new TestObject();
354 nsAutoPtr<TestObjectBaseB> p2(p1);
355 printf("equality %s.\n",
356 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
357 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
358 ? "OK" : "broken");
359 }
360
361 {
362 nsRefPtr<TestRefObject> p1 = new TestRefObject();
363 // nsCOMPtr requires a |get| for something like this as well
364 nsRefPtr<TestRefObjectBaseB> p2 = p1.get();
365 printf("equality %s.\n",
366 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
367 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
368 ? "OK" : "broken");
369 }
370
371 {
372 nsRefPtr<TestRefObject> p1 = new TestRefObject();
373 TestRefObjectBaseB *p2 = p1;
374 printf("equality %s.\n",
375 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
376 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
377 ? "OK" : "broken");
378 }
379
380 {
381 TestRefObject *p1 = new TestRefObject();
382 nsRefPtr<TestRefObjectBaseB> p2 = p1;
383 printf("equality %s.\n",
384 ((NS_STATIC_CAST(void*, p1) != NS_STATIC_CAST(void*, p2)) &&
385 (p1 == p2) && !(p1 != p2) && (p2 == p1) && !(p2 != p1))
386 ? "OK" : "broken");
387 }
388
389 printf("\nTesting |forget()|.\n");
390
391 {
392 printf("Should create one |TestObject|:\n");
393 nsAutoPtr<TestObject> pobj( new TestObject() );
394 printf("Should do nothing:\n");
395 nsAutoPtr<TestObject> pobj2( pobj.forget() );
396 printf("Should destroy one |TestObject|:\n");
397 }
398
399 {
400 printf("Should create 3 |TestObject|s:\n");
401 nsAutoArrayPtr<TestObject> pobj( new TestObject[3] );
402 printf("Should do nothing:\n");
403 nsAutoArrayPtr<TestObject> pobj2( pobj.forget() );
404 printf("Should destroy 3 |TestObject|s:\n");
405 }
406
407 printf("\nTesting construction.\n");
408
409 {
410 printf("Should create one |TestObject|:\n");
411 nsAutoPtr<TestObject> pobj(new TestObject());
412 printf("Should destroy one |TestObject|:\n");
413 }
414
415 {
416 printf("Should create 3 |TestObject|s:\n");
417 nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
418 printf("Should destroy 3 |TestObject|s:\n");
419 }
420
421 {
422 printf("Should create and AddRef one |TestRefObject|:\n");
423 nsRefPtr<TestRefObject> pobj = new TestRefObject();
424 printf("Should Release and destroy one |TestRefObject|:\n");
425 }
426
427 printf("\nTesting calling of functions (including array access and casts).\n");
428
429 {
430 printf("Should create one |TestObject|:\n");
431 nsAutoPtr<TestObject> pobj(new TestObject());
432 printf("Should do something with one |TestObject|:\n");
433 DoSomethingWithTestObject(pobj);
434 printf("Should do something with one |TestObject|:\n");
435 DoSomethingWithConstTestObject(pobj);
436 printf("Should destroy one |TestObject|:\n");
437 }
438
439 {
440 printf("Should create 3 |TestObject|s:\n");
441 nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
442 printf("Should do something with one |TestObject|:\n");
443 DoSomethingWithTestObject(&pobj[2]);
444 printf("Should do something with one |TestObject|:\n");
445 DoSomethingWithConstTestObject(&pobj[1]);
446 printf("Should do something with one |TestObject|:\n");
447 DoSomethingWithTestObject(pobj + 2);
448 printf("Should do something with one |TestObject|:\n");
449 DoSomethingWithConstTestObject(pobj + 1);
450 printf("Should destroy 3 |TestObject|s:\n");
451 }
452
453 {
454 printf("Should create and AddRef one |TestRefObject|:\n");
455 nsRefPtr<TestRefObject> pobj = new TestRefObject();
456 printf("Should do something with one |TestRefObject|:\n");
457 DoSomethingWithTestRefObject(pobj);
458 printf("Should do something with one |TestRefObject|:\n");
459 DoSomethingWithConstTestRefObject(pobj);
460 printf("Should Release and destroy one |TestRefObject|:\n");
461 }
462
463 {
464 printf("Should create one |TestObject|:\n");
465 nsAutoPtr<TestObject> pobj(new TestObject());
466 printf("Should do something with one |TestObject|:\n");
467 DoSomethingWithTestObjectBaseB(pobj);
468 printf("Should do something with one |TestObject|:\n");
469 DoSomethingWithConstTestObjectBaseB(pobj);
470 printf("Should destroy one |TestObject|:\n");
471 }
472
473 {
474 printf("Should create 3 |TestObject|s:\n");
475 nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
476 printf("Should do something with one |TestObject|:\n");
477 DoSomethingWithTestObjectBaseB(&pobj[2]);
478 printf("Should do something with one |TestObject|:\n");
479 DoSomethingWithConstTestObjectBaseB(&pobj[1]);
480 printf("Should do something with one |TestObject|:\n");
481 DoSomethingWithTestObjectBaseB(pobj + 2);
482 printf("Should do something with one |TestObject|:\n");
483 DoSomethingWithConstTestObjectBaseB(pobj + 1);
484 printf("Should destroy 3 |TestObject|s:\n");
485 }
486
487 {
488 printf("Should create and AddRef one |TestRefObject|:\n");
489 nsRefPtr<TestRefObject> pobj = new TestRefObject();
490 printf("Should do something with one |TestRefObject|:\n");
491 DoSomethingWithTestRefObjectBaseB(pobj);
492 printf("Should do something with one |TestRefObject|:\n");
493 DoSomethingWithConstTestRefObjectBaseB(pobj);
494 printf("Should Release and destroy one |TestRefObject|:\n");
495 }
496
497 {
498 printf("Should create one |TestObject|:\n");
499 const nsAutoPtr<TestObject> pobj(new TestObject());
500 printf("Should do something with one |TestObject|:\n");
501 DoSomethingWithTestObject(pobj);
502 printf("Should do something with one |TestObject|:\n");
503 DoSomethingWithConstTestObject(pobj);
504 printf("Should destroy one |TestObject|:\n");
505 }
506
507 {
508 printf("Should create 3 |TestObject|s:\n");
509 const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
510 printf("Should do something with one |TestObject|:\n");
511 DoSomethingWithTestObject(&pobj[2]);
512 printf("Should do something with one |TestObject|:\n");
513 DoSomethingWithConstTestObject(&pobj[1]);
514 printf("Should do something with one |TestObject|:\n");
515 DoSomethingWithTestObject(pobj + 2);
516 printf("Should do something with one |TestObject|:\n");
517 DoSomethingWithConstTestObject(pobj + 1);
518 printf("Should destroy 3 |TestObject|s:\n");
519 }
520
521 {
522 printf("Should create and AddRef one |TestRefObject|:\n");
523 const nsRefPtr<TestRefObject> pobj = new TestRefObject();
524 printf("Should do something with one |TestRefObject|:\n");
525 DoSomethingWithTestRefObject(pobj);
526 printf("Should do something with one |TestRefObject|:\n");
527 DoSomethingWithConstTestRefObject(pobj);
528 printf("Should Release and destroy one |TestRefObject|:\n");
529 }
530
531 {
532 printf("Should create one |TestObject|:\n");
533 const nsAutoPtr<TestObject> pobj(new TestObject());
534 printf("Should do something with one |TestObject|:\n");
535 DoSomethingWithTestObjectBaseB(pobj);
536 printf("Should do something with one |TestObject|:\n");
537 DoSomethingWithConstTestObjectBaseB(pobj);
538 printf("Should destroy one |TestObject|:\n");
539 }
540
541 {
542 printf("Should create 3 |TestObject|s:\n");
543 const nsAutoArrayPtr<TestObject> pobj(new TestObject[3]);
544 printf("Should do something with one |TestObject|:\n");
545 DoSomethingWithTestObjectBaseB(&pobj[2]);
546 printf("Should do something with one |TestObject|:\n");
547 DoSomethingWithConstTestObjectBaseB(&pobj[1]);
548 printf("Should do something with one |TestObject|:\n");
549 DoSomethingWithTestObjectBaseB(pobj + 2);
550 printf("Should do something with one |TestObject|:\n");
551 DoSomethingWithConstTestObjectBaseB(pobj + 1);
552 printf("Should destroy 3 |TestObject|s:\n");
553 }
554
555 {
556 printf("Should create and AddRef one |TestRefObject|:\n");
557 const nsRefPtr<TestRefObject> pobj = new TestRefObject();
558 printf("Should do something with one |TestRefObject|:\n");
559 DoSomethingWithTestRefObjectBaseB(pobj);
560 printf("Should do something with one |TestRefObject|:\n");
561 DoSomethingWithConstTestRefObjectBaseB(pobj);
562 printf("Should Release and destroy one |TestRefObject|:\n");
563 }
564
565 return 0;
566}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use