1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestTable.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkMath.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkVariantArray.h"
#include <ctime>
#include <vector>
const int size = 100;
const double prob = 1.0 - 1.0 / size;
const double highProb = 1.0 - 1.0 / (size * size);
void CheckEqual(
vtkTable* table, std::vector<std::vector<double>>& stdTable, const std::string& info)
{
// Check sizes
if (table->GetNumberOfRows() != static_cast<vtkIdType>(stdTable[0].size()))
{
cout << "TestTable CheckEqual() fails after operation '" << info << "()':" << endl;
cout << "Number of rows is incorrect (" << table->GetNumberOfRows() << " != " << stdTable.size()
<< ")" << endl;
exit(EXIT_FAILURE);
}
if (table->GetNumberOfColumns() != static_cast<vtkIdType>(stdTable.size()))
{
cout << "TestTable CheckEqual() fails after operation '" << info << "()':" << endl;
cout << "Number of columns is incorrect (" << table->GetNumberOfColumns()
<< " != " << stdTable.size() << ")" << endl;
exit(EXIT_FAILURE);
}
// Use GetValue() and GetValueByName() to check
for (int i = 0; i < table->GetNumberOfRows(); i++)
{
for (int j = 0; j < table->GetNumberOfColumns(); j++)
{
double tableVal = table->GetValue(i, j).ToDouble();
double stdTableVal = stdTable[j][i];
if (stdTableVal && tableVal != stdTableVal)
{
cout << "TestTable CheckEqual() fails after operation '" << info << "()':" << endl;
cout << "Values not equal at row " << i << " column " << j << ": ";
cout << "(" << tableVal << " != " << stdTableVal << ")" << endl;
exit(EXIT_FAILURE);
}
}
}
// Use GetColumn() and GetColumnByName() to check
for (int j = 0; j < table->GetNumberOfColumns(); j++)
{
vtkAbstractArray* arr;
if (vtkMath::Random() < 0.5)
{
arr = table->GetColumn(j);
}
else
{
arr = table->GetColumnByName(table->GetColumnName(j));
}
for (int i = 0; i < table->GetNumberOfRows(); i++)
{
double val;
if (arr->IsA("vtkVariantArray"))
{
val = vtkArrayDownCast<vtkVariantArray>(arr)->GetValue(i).ToDouble();
}
else if (arr->IsA("vtkStringArray"))
{
vtkVariant v(vtkArrayDownCast<vtkStringArray>(arr)->GetValue(i));
val = v.ToDouble();
}
else if (arr->IsA("vtkDataArray"))
{
val = vtkArrayDownCast<vtkDataArray>(arr)->GetTuple1(i);
}
else
{
cout << "TestTable CheckEqual() fails after operation '" << info << "()':" << endl;
cout << "Unknown array type" << endl;
exit(EXIT_FAILURE);
}
double stdTableVal = stdTable[j][i];
if (stdTableVal && val != stdTableVal)
{
cout << "TestTable CheckEqual() fails after operation '" << info << "()':" << endl;
cout << "Values not equal at row " << i << " column " << j << ": ";
cout << "(" << val << " != " << stdTableVal << ")" << endl;
exit(EXIT_FAILURE);
}
}
}
// Use GetRow() to check
for (int i = 0; i < table->GetNumberOfRows(); i++)
{
vtkVariantArray* arr = table->GetRow(i);
for (int j = 0; j < table->GetNumberOfColumns(); j++)
{
double val = arr->GetValue(j).ToDouble();
double stdTableVal = stdTable[j][i];
if (stdTableVal && val != stdTableVal)
{
cout << "TestTable CheckEqual() fails after operation '" << info << "()':" << endl;
cout << "Values not equal at row " << i << " column " << j << ": ";
cout << "(" << val << " != " << stdTableVal << ")" << endl;
exit(EXIT_FAILURE);
}
}
}
}
void FillTable(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
cout << "Creating columns." << endl;
vtkIdType columnId = 0;
bool noColumns = true;
while (noColumns || vtkMath::Random() < prob)
{
noColumns = false;
stdTable.emplace_back();
double r = vtkMath::Random();
vtkVariant name(columnId);
vtkAbstractArray* arr;
if (r < 0.25)
{
arr = vtkIntArray::New();
arr->SetName((name.ToString() + " (vtkIntArray)").c_str());
}
else if (r < 0.5)
{
arr = vtkDoubleArray::New();
arr->SetName((name.ToString() + " (vtkDoubleArray)").c_str());
}
else if (r < 0.75)
{
arr = vtkStringArray::New();
arr->SetName((name.ToString() + " (vtkStringArray)").c_str());
}
else
{
arr = vtkVariantArray::New();
arr->SetName((name.ToString() + " (vtkVariantArray)").c_str());
}
table->AddColumn(arr);
arr->Delete();
columnId++;
}
CheckEqual(table, stdTable, "FillTable");
}
void AddColumn(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
vtkVariant name(table->GetNumberOfColumns());
vtkNew<vtkDoubleArray> arr;
arr->SetNumberOfComponents(1);
arr->SetNumberOfTuples(table->GetNumberOfRows());
arr->SetName((name.ToString() + " (vtkDoubleArray)").c_str());
arr->FillComponent(0, 0.0);
table->AddColumn(arr);
std::vector<double> fillvec;
fillvec.resize(table->GetNumberOfRows(), 0.0);
stdTable.push_back(fillvec);
CheckEqual(table, stdTable, "AddColumn");
}
void InsertColumn(vtkTable* table, std::vector<std::vector<double>>& stdTable, int c0)
{
vtkVariant name(table->GetNumberOfColumns());
vtkNew<vtkDoubleArray> arr;
arr->SetNumberOfComponents(1);
arr->SetNumberOfTuples(table->GetNumberOfRows());
arr->SetName((name.ToString() + " (vtkDoubleArray)").c_str());
arr->FillComponent(0, 0.0);
table->InsertColumn(arr, c0);
std::vector<double> fillvec;
fillvec.resize(table->GetNumberOfRows(), 0.0);
stdTable.insert(stdTable.begin() + c0, fillvec);
CheckEqual(table, stdTable, "InsertColumn");
}
void InsertRows(
vtkTable* table, std::vector<std::vector<double>>& stdTable, const vtkIdType r0, const int n)
{
cout << "Inserting rows in middle of table." << endl;
const int ncols = table->GetNumberOfColumns();
// insert rows in middle of stdTable, fill with 0.0
for (int c = 0; c < ncols; c++)
{
for (int r = r0; r < r0 + n; r++)
{
stdTable[c].insert(stdTable[c].begin() + r0, 0.0);
}
}
// insert rows in middle of vtkTable
// then fill with defined value, i.e. 0.0
table->InsertRows(r0, n);
for (int r = r0; r < r0 + n; r++)
{
for (int c = 0; c < ncols; c++)
{
table->SetValue(r, c, 0.0);
}
}
// compare if the remainder of the tables still equal each other
CheckEqual(table, stdTable, "InsertRowsInMiddle");
}
void InsertEmptyRows(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
cout << "Inserting empty rows." << endl;
bool noRows = true;
while (noRows || vtkMath::Random() < prob)
{
noRows = false;
table->InsertNextBlankRow();
for (unsigned int i = 0; i < stdTable.size(); i++)
{
stdTable[i].push_back(0.0);
}
}
CheckEqual(table, stdTable, "InsertEmptyRows");
}
void InsertFullRows(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
cout << "Inserting full rows." << endl;
while (vtkMath::Random() < prob)
{
vtkVariantArray* rowArray = vtkVariantArray::New();
for (vtkIdType j = 0; j < table->GetNumberOfColumns(); j++)
{
rowArray->InsertNextValue(vtkVariant(j));
stdTable[j].push_back(j);
}
table->InsertNextRow(rowArray);
rowArray->Delete();
}
CheckEqual(table, stdTable, "InsertFullRows");
}
void RandomizeValues(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
cout << "Performing all kinds of inserts." << endl;
int id = 0;
while (vtkMath::Random() < highProb)
{
vtkIdType row = static_cast<vtkIdType>(vtkMath::Random(0, table->GetNumberOfRows()));
vtkIdType col = static_cast<vtkIdType>(vtkMath::Random(0, table->GetNumberOfColumns()));
vtkVariant v;
if (vtkMath::Random() < 0.25)
{
vtkVariant temp(id);
v = vtkVariant(temp.ToString());
}
else if (vtkMath::Random() < 0.5)
{
v = vtkVariant(id);
}
else
{
v = vtkVariant(static_cast<double>(id));
}
if (vtkMath::Random() < 0.5)
{
table->SetValue(row, col, v);
}
else
{
table->SetValueByName(row, table->GetColumnName(col), v);
}
stdTable[col][row] = id;
id++;
}
CheckEqual(table, stdTable, "RandomInserts");
}
void RemoveHalfOfRows(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
cout << "Removing half of the rows." << endl;
int numRowsToRemove = table->GetNumberOfRows() / 2;
for (int i = 0; i < numRowsToRemove; i++)
{
vtkIdType row = static_cast<vtkIdType>(vtkMath::Random(0, table->GetNumberOfRows()));
cout << "Removing row " << row << " from vtkTable with " << table->GetNumberOfRows() << " rows"
<< endl;
table->RemoveRow(row);
cout << "Removing row " << row << " from std::vector< std::vector<double> > with "
<< stdTable[0].size() << " rows " << endl;
for (unsigned int j = 0; j < stdTable.size(); j++)
{
std::vector<double>::iterator rowIt = stdTable[j].begin() + row;
stdTable[j].erase(rowIt);
}
}
CheckEqual(table, stdTable, "RemoveHalfRows");
}
void SqueezeRows(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
table->SqueezeRows();
CheckEqual(table, stdTable, "SqueezeRows");
}
void RemoveHalfOfColumns(vtkTable* table, std::vector<std::vector<double>>& stdTable)
{
cout << "Removing half of the columns." << endl;
int numColsToRemove = table->GetNumberOfColumns() / 2;
for (int i = 0; i < numColsToRemove; i++)
{
vtkIdType col = static_cast<vtkIdType>(vtkMath::Random(0, table->GetNumberOfColumns()));
if (vtkMath::Random() < 0.5)
{
table->RemoveColumn(col);
}
else
{
table->RemoveColumnByName(table->GetColumnName(col));
}
std::vector<std::vector<double>>::iterator colIt = stdTable.begin() + col;
stdTable.erase(colIt);
}
CheckEqual(table, stdTable, "RemoveHalfColumns");
}
int TestTable(int, char*[])
{
cout << "CTEST_FULL_OUTPUT" << endl;
long seed = time(nullptr);
cout << "Seed: " << seed << endl;
vtkMath::RandomSeed(seed);
// Make a table and a parallel vector of vectors containing the same data
vtkNew<vtkTable> table;
std::vector<std::vector<double>> stdTable;
FillTable(table, stdTable);
InsertEmptyRows(table, stdTable);
RandomizeValues(table, stdTable);
InsertFullRows(table, stdTable);
RandomizeValues(table, stdTable);
// add new column to the end of the table
AddColumn(table, stdTable);
RandomizeValues(table, stdTable);
// insert new column in the middle of the table
InsertColumn(table, stdTable, table->GetNumberOfColumns() / 2);
RandomizeValues(table, stdTable);
// insert new rows at the beginning of the table
InsertRows(table, stdTable, 0, 3);
RandomizeValues(table, stdTable);
// insert new rows in the middle of the table
InsertRows(table, stdTable, table->GetNumberOfRows() / 2, 3);
RandomizeValues(table, stdTable);
// insert new rows at the end of the table
InsertRows(table, stdTable, table->GetNumberOfRows() - 1, 3);
RandomizeValues(table, stdTable);
RemoveHalfOfRows(table, stdTable);
RandomizeValues(table, stdTable);
SqueezeRows(table, stdTable);
RandomizeValues(table, stdTable);
RemoveHalfOfColumns(table, stdTable);
RandomizeValues(table, stdTable);
return EXIT_SUCCESS;
}
|