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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestDispatchers.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.
=========================================================================*/
// .NAME Test Dispatchers
// .SECTION Description
// Tests vtkDataArrayDispatcher
#include "vtkObjectFactory.h"
#include "vtkDataArrayDispatcher.h"
#include "vtkNew.h"
//classes we will be using in the test
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include <stdexcept>
#include <algorithm>
namespace
{
void test_expression(bool valid, std::string msg)
{
if(!valid)
{
throw std::runtime_error(msg);
}
}
template<typename T, typename U>
inline T* as(U* u)
{
return dynamic_cast<T*>(u);
}
//returns the length of an array
struct lengthCheckFunctor
{
template<typename T>
vtkIdType operator()(const vtkDataArrayDispatcherPointer<T>& array) const
{
return array.NumberOfComponents * array.NumberOfTuples;
}
};
//accumlates the length of all arrays used with this functor
struct storeLengthFunctor
{
vtkIdType length;
storeLengthFunctor():length(0){}
template<typename T>
void operator()(vtkDataArrayDispatcherPointer<T> array)
{
length += array.NumberOfComponents * array.NumberOfTuples;
}
};
//modifies an array to be sorted, only works with arrays
//that have one component
struct sortArray
{
template<typename T>
void operator()(vtkDataArrayDispatcherPointer<T> array) const
{
std::sort(array.RawPointer,array.RawPointer + array.NumberOfTuples);
}
};
bool TestDataArrayDispatchStatefull()
{
storeLengthFunctor functor;
vtkDataArrayDispatcher<storeLengthFunctor> dispatcher(functor);
//verify the dispatching
vtkNew<vtkDoubleArray> doubleArray;
vtkNew<vtkIntArray> intArray;
doubleArray->SetNumberOfComponents(4);
doubleArray->SetNumberOfTuples(10);
intArray->SetNumberOfTuples(13);
const vtkIdType doubleSize =
doubleArray->GetNumberOfComponents() * doubleArray->GetNumberOfTuples();
const vtkIdType intSize =
intArray->GetNumberOfComponents() * intArray->GetNumberOfTuples();
dispatcher.Go(as<vtkDataArray>(doubleArray.GetPointer()));
test_expression(functor.length==doubleSize,
"double array dispatch failed with statefull functor");
dispatcher.Go(intArray.GetPointer());
test_expression(functor.length==intSize+doubleSize,
"int array dispatch failed with statefull functor");
return true;
}
bool TestDataArrayDispatchStateless()
{
vtkDataArrayDispatcher<lengthCheckFunctor,int> dispatcher;
//verify the dispatching
vtkNew<vtkDoubleArray> doubleArray;
vtkNew<vtkIntArray> intArray;
doubleArray->SetNumberOfComponents(4);
doubleArray->SetNumberOfTuples(10);
intArray->SetNumberOfTuples(13);
const vtkIdType doubleSize =
doubleArray->GetNumberOfComponents() * doubleArray->GetNumberOfTuples();
const vtkIdType intSize =
intArray->GetNumberOfComponents() * intArray->GetNumberOfTuples();
vtkIdType result = dispatcher.Go(doubleArray.GetPointer());
test_expression(result == doubleSize,
"double array dispatch failed with stateless functor");
result = dispatcher.Go(intArray.GetPointer());
test_expression(result==intSize,
"int array dispatch failed with stateless functor");
return true;
}
bool TestDataArrayDispatchSort()
{
vtkDataArrayDispatcher<sortArray> dispatcher;
//verify the dispatching
vtkNew<vtkDoubleArray> doubleArray;
const int doubleSize=10;
doubleArray->SetNumberOfTuples(doubleSize);
for(int i=0; i < doubleSize; i++)
{
doubleArray->SetValue(i,doubleSize-i);
}
dispatcher.Go(doubleArray.GetPointer());
for(int i=0; i < doubleSize; i++)
{
test_expression(doubleArray->GetValue(i)==i+1,
"sort functor failed");
}
return true;
}
}
int TestDataArrayDispatcher(int /*argc*/, char* /*argv*/[])
{
bool passed = TestDataArrayDispatchStatefull();
passed &= TestDataArrayDispatchStateless();
passed &= TestDataArrayDispatchSort();
return passed ? 0 : 1;
}
|