311 { |
311 { |
312 /* The size of QScopedPointer should be the same as one pointer. */ |
312 /* The size of QScopedPointer should be the same as one pointer. */ |
313 QCOMPARE(sizeof(QScopedPointer<int>), sizeof(void *)); |
313 QCOMPARE(sizeof(QScopedPointer<int>), sizeof(void *)); |
314 } |
314 } |
315 |
315 |
|
316 struct RefCounted |
|
317 { |
|
318 RefCounted() |
|
319 : ref(0) |
|
320 { |
|
321 instanceCount.ref(); |
|
322 } |
|
323 |
|
324 RefCounted(RefCounted const &) |
|
325 : ref(0) |
|
326 { |
|
327 instanceCount.ref(); |
|
328 } |
|
329 |
|
330 ~RefCounted() |
|
331 { |
|
332 QVERIFY( ref == 0 ); |
|
333 instanceCount.deref(); |
|
334 } |
|
335 |
|
336 RefCounted &operator=(RefCounted const &) |
|
337 { |
|
338 return *this; |
|
339 } |
|
340 |
|
341 QAtomicInt ref; |
|
342 |
|
343 static QAtomicInt instanceCount; |
|
344 }; |
|
345 |
|
346 QAtomicInt RefCounted::instanceCount = 0; |
|
347 |
|
348 template <class A1, class A2, class B> |
|
349 void scopedPointerComparisonTest(const A1 &a1, const A2 &a2, const B &b) |
|
350 { |
|
351 // test equality on equal pointers |
|
352 QVERIFY(a1 == a2); |
|
353 QVERIFY(a2 == a1); |
|
354 |
|
355 // test inequality on equal pointers |
|
356 QVERIFY(!(a1 != a2)); |
|
357 QVERIFY(!(a2 != a1)); |
|
358 |
|
359 // test equality on unequal pointers |
|
360 QVERIFY(!(a1 == b)); |
|
361 QVERIFY(!(a2 == b)); |
|
362 QVERIFY(!(b == a1)); |
|
363 QVERIFY(!(b == a2)); |
|
364 |
|
365 // test inequality on unequal pointers |
|
366 QVERIFY(b != a1); |
|
367 QVERIFY(b != a2); |
|
368 QVERIFY(a1 != b); |
|
369 QVERIFY(a2 != b); |
|
370 } |
|
371 |
316 void tst_QScopedPointer::comparison() |
372 void tst_QScopedPointer::comparison() |
317 { |
373 { |
318 int *a = new int(42); |
374 QCOMPARE( int(RefCounted::instanceCount), 0 ); |
319 int *b = new int(43); |
375 |
320 |
376 { |
321 QScopedPointer<int> pa(a); |
377 RefCounted *a = new RefCounted; |
322 QScopedPointer<int> pa2(a); |
378 RefCounted *b = new RefCounted; |
323 QScopedPointer<int> pb(b); |
379 |
324 |
380 QCOMPARE( int(RefCounted::instanceCount), 2 ); |
325 // test equality on equal pointers |
381 |
326 QVERIFY(pa == pa2); |
382 QScopedPointer<RefCounted> pa1(a); |
327 QVERIFY(pa2 == pa); |
383 QScopedPointer<RefCounted> pa2(a); |
328 |
384 QScopedPointer<RefCounted> pb(b); |
329 // test unequality on equal pointers |
385 |
330 QVERIFY(!(pa != pa2)); |
386 scopedPointerComparisonTest(pa1, pa1, pb); |
331 QVERIFY(!(pa2 != pa)); |
387 scopedPointerComparisonTest(pa2, pa2, pb); |
332 |
388 scopedPointerComparisonTest(pa1, pa2, pb); |
333 // test on unequal pointers |
389 |
334 QVERIFY(!(pa == pb)); |
390 pa2.take(); |
335 QVERIFY(!(pb == pa)); |
391 |
336 QVERIFY(pb != pa); |
392 QCOMPARE( int(RefCounted::instanceCount), 2 ); |
337 QVERIFY(pa != pb); |
393 } |
338 |
394 |
339 pa2.take(); |
395 QCOMPARE( int(RefCounted::instanceCount), 0 ); |
|
396 |
|
397 { |
|
398 RefCounted *a = new RefCounted[42]; |
|
399 RefCounted *b = new RefCounted[43]; |
|
400 |
|
401 QCOMPARE( int(RefCounted::instanceCount), 85 ); |
|
402 |
|
403 QScopedArrayPointer<RefCounted> pa1(a); |
|
404 QScopedArrayPointer<RefCounted> pa2(a); |
|
405 QScopedArrayPointer<RefCounted> pb(b); |
|
406 |
|
407 scopedPointerComparisonTest(pa1, pa2, pb); |
|
408 |
|
409 pa2.take(); |
|
410 |
|
411 QCOMPARE( int(RefCounted::instanceCount), 85 ); |
|
412 } |
|
413 |
|
414 QCOMPARE( int(RefCounted::instanceCount), 0 ); |
|
415 |
|
416 { |
|
417 // QScopedSharedPointer is an internal helper class -- it is unsupported! |
|
418 |
|
419 RefCounted *a = new RefCounted; |
|
420 RefCounted *b = new RefCounted; |
|
421 |
|
422 QCOMPARE( int(RefCounted::instanceCount), 2 ); |
|
423 |
|
424 QSharedDataPointer<RefCounted> pa1(a); |
|
425 QSharedDataPointer<RefCounted> pa2(a); |
|
426 QSharedDataPointer<RefCounted> pb(b); |
|
427 |
|
428 QCOMPARE( int(a->ref), 2 ); |
|
429 QCOMPARE( int(b->ref), 1 ); |
|
430 QCOMPARE( int(RefCounted::instanceCount), 2 ); |
|
431 |
|
432 scopedPointerComparisonTest(pa1, pa2, pb); |
|
433 |
|
434 QCOMPARE( int(RefCounted::instanceCount), 2 ); |
|
435 } |
|
436 |
|
437 QCOMPARE( int(RefCounted::instanceCount), 0 ); |
340 } |
438 } |
341 |
439 |
342 QTEST_MAIN(tst_QScopedPointer) |
440 QTEST_MAIN(tst_QScopedPointer) |
343 #include "tst_qscopedpointer.moc" |
441 #include "tst_qscopedpointer.moc" |