1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    otherStringArray.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 
16 #include "vtkDebugLeaks.h"
17 #include "vtkCharArray.h"
18 #include "vtkIdTypeArray.h"
19 #include "vtkStringArray.h"
20 #include "vtkIdList.h"
21 
22 #include <vtksys/ios/sstream>
23 
24 #define SIZE 1000
25 
doStringArrayTest(ostream & strm,int size)26 int doStringArrayTest(ostream& strm, int size)
27 {
28   int errors = 0;
29 
30   vtkStringArray *ptr = vtkStringArray::New();
31   vtkStdString *strings = new vtkStdString[SIZE];
32   for (int i = 0; i < SIZE; ++i)
33     {
34     char buf[1024];
35     sprintf(buf, "string entry %d", i);
36     strings[i] = vtkStdString(buf);
37     }
38 
39   strm << "\tResize(0)...";
40   ptr->Resize(0);
41   strm << "OK" << endl;
42 
43   strm << "\tResize(10)...";
44   ptr->Resize(10);
45   strm << "OK" << endl;
46 
47   strm << "\tResize(5)...";
48   ptr->Resize(5);
49   strm << "OK" << endl;
50 
51   strm << "\tResize(size)...";
52   ptr->Resize(size);
53   strm << "OK" << endl;
54 
55   strm << "\tSetNumberOfValues...";
56   ptr->SetNumberOfValues(100);
57   if (ptr->GetNumberOfValues() == 100) strm << "OK" << endl;
58   else
59     {
60     ++errors;
61     strm << "FAILED" << endl;
62     }
63 
64   strm << "\tSetVoidArray...";
65   ptr->SetVoidArray(strings, size, 1);
66   strm << "OK" << endl;
67 
68   strm << "\tGetValue...";
69   vtkStdString value = ptr->GetValue(123);
70   if (value == "string entry 123")
71     {
72     strm << "OK" << endl;
73     }
74   else
75     {
76     ++errors;
77     strm << "FAILED.  Expected 'string entry 123', got '"
78          << value << "'" << endl;
79 #ifdef DUMP_VALUES
80     for (int i = 0; i < ptr->GetNumberOfValues(); ++i)
81       {
82       strm << "\t\tValue " << i << ": " << ptr->GetValue(i) << endl;
83       }
84 #endif
85     }
86 
87   strm << "\tSetValue...";
88   ptr->SetValue(124, "jabberwocky");
89   if (ptr->GetValue(124) == "jabberwocky")
90     {
91     strm << "OK" << endl;
92     }
93   else
94     {
95     ++errors;
96     strm << "FAILED" << endl;
97     }
98 
99   strm << "\tInsertValue...";
100   ptr->InsertValue(500, "There and Back Again");
101   if (ptr->GetValue(500) == "There and Back Again")
102     {
103     strm << "OK" << endl;
104     }
105   else
106     {
107     ++errors;
108     strm << "FAILED" << endl;
109     }
110 
111   strm << "\tInsertNextValue...";
112   if (ptr->GetValue(ptr->InsertNextValue("3.141592653589")) ==
113       "3.141592653589")
114     {
115     strm << "OK" << endl;
116     }
117   else
118     {
119     ++errors;
120     strm << "FAILED" << endl;
121     }
122 
123   strm << "\tvtkAbstractArray::GetTuples(vtkIdList)...";
124   vtkIdList *indices = vtkIdList::New();
125   indices->InsertNextId(10);
126   indices->InsertNextId(20);
127   indices->InsertNextId(314);
128 
129   vtkStringArray *newValues = vtkStringArray::New();
130   newValues->SetNumberOfValues(3);
131   ptr->GetTuples(indices, newValues);
132 
133   if (newValues->GetValue(0) == "string entry 10" &&
134       newValues->GetValue(1) == "string entry 20" &&
135       newValues->GetValue(2) == "string entry 314")
136     {
137     strm << "OK" << endl;
138     }
139   else
140     {
141     ++errors;
142     strm << "FAILED.  Results:" << endl;
143     strm << "\tExpected: 'string entry 10'\tActual: '"
144          << newValues->GetValue(0) << "'" << endl;
145     strm << "\tExpected: 'string entry 20'\tActual: '"
146          << newValues->GetValue(1) << "'" << endl;
147     strm << "\tExpected: 'string entry 314'\tActual: '"
148          << newValues->GetValue(2) << "'" << endl;
149     }
150 
151   newValues->Reset();
152 
153   strm << "\tvtkAbstractArray::GetTuples(vtkIdType, vtkIdType)...";
154   newValues->SetNumberOfValues(3);
155   ptr->GetTuples(30, 32, newValues);
156   if (newValues->GetValue(0) == "string entry 30" &&
157       newValues->GetValue(1) == "string entry 31" &&
158       newValues->GetValue(2) == "string entry 32")
159     {
160     strm << "OK" << endl;
161     }
162   else
163     {
164     ++errors;
165     strm << "FAILED" << endl;
166     }
167 
168   strm << "\tvtkAbstractArray::InsertTuple...";
169   ptr->InsertTuple(150, 2, newValues);
170   if (ptr->GetValue(150) == "string entry 32")
171     {
172     strm << "OK" << endl;
173     }
174   else
175     {
176     ++errors;
177     strm << "FAILED" << endl;
178     }
179 
180   newValues->Delete();
181   indices->Delete();
182 
183   strm << "PrintSelf..." << endl;
184   strm << *ptr;
185 
186   ptr->Delete();
187   delete [] strings;
188   return errors;
189 }
190 
otherStringArrayTest(ostream & strm)191 int otherStringArrayTest(ostream& strm)
192 {
193   int errors = 0;
194     {
195     strm << "Test StringArray" << endl;
196     errors += doStringArrayTest(strm, SIZE);
197     }
198 
199     return errors;
200 }
201 
otherStringArray(int,char * [])202 int otherStringArray(int, char *[])
203 {
204   vtksys_ios::ostringstream vtkmsg_with_warning_C4701;
205 //  return otherArraysTest(vtkmsg_with_warning_C4701);
206   return otherStringArrayTest(cerr);
207 
208 }
209