1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestSmartPointer.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // .NAME Test of vtkNew.
16 // .SECTION Description
17 // Tests instantiations of the vtkNew class template.
18 
19 #include "vtkDebugLeaks.h"
20 #include "vtkFloatArray.h"
21 #include "vtkIntArray.h"
22 #include "vtkNew.h"
23 #include "vtkSmartPointer.h"
24 #include "vtkWeakPointer.h"
25 
26 #include "vtkTestNewVar.h"
27 
TestNew(int,char * [])28 int TestNew(int,char *[])
29 {
30   bool error = false;
31   // This one should be cleaned up when the main function ends.
32   vtkNew<vtkIntArray> a;
33   if (a->GetReferenceCount() != 1)
34     {
35     error = true;
36     cerr << "Error, reference count should be 1, was " << a->GetReferenceCount()
37          << endl;
38     }
39   cout << "vtkNew streaming " << a << endl;
40 
41   vtkWeakPointer<vtkFloatArray> wf;
42   // Test scoping, and deletion.
43   if (wf == 0)
44     {
45     vtkNew<vtkFloatArray> f;
46     wf = f.GetPointer();
47     }
48   if (wf != 0)
49     {
50     error = true;
51     cerr << "Error, vtkNew failed to delete the object it contained."
52          << endl;
53     }
54 
55   // Now test interaction with the smart pointer.
56   vtkSmartPointer<vtkIntArray> si;
57   if (si == 0)
58     {
59     vtkNew<vtkIntArray> i;
60     si = i.Get();
61     }
62   if (si->GetReferenceCount() != 1)
63     {
64     error = true;
65     cerr << "Error, vtkNew failed to delete the object it contained, "
66          << "or the smart pointer failed to increment it. Reference count: "
67          << si->GetReferenceCount() << endl;
68     }
69 
70   vtkNew<vtkTestNewVar> newVarObj;
71   if (newVarObj->GetPointsRefCount() != 1)
72     {
73     error = true;
74     cerr << "The mmeber pointer failed to set the correct reference count: "
75          << newVarObj->GetPointsRefCount() << endl;
76     }
77 
78   vtkSmartPointer<vtkObject> points = newVarObj->GetPoints();
79   if (points->GetReferenceCount() != 2)
80     {
81     error = true;
82     cerr << "Error, vtkNew failed to keep the object it contained, "
83          << "or the smart pointer failed to increment it. Reference count: "
84          << points->GetReferenceCount() << endl;
85     }
86 
87   return error ? 1 : 0;
88 }
89