1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    ArrayAPI.cxx
5 
6 -------------------------------------------------------------------------
7   Copyright 2008 Sandia Corporation.
8   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9   the U.S. Government retains certain rights in this software.
10 -------------------------------------------------------------------------
11 
12   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13   All rights reserved.
14   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15 
16      This software is distributed WITHOUT ANY WARRANTY; without even
17      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18      PURPOSE.  See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
22 #include <vtkDenseArray.h>
23 #include <vtkSmartPointer.h>
24 #include <vtkSparseArray.h>
25 
26 #include <vtksys/ios/iostream>
27 #include <vtksys/ios/sstream>
28 #include <vtksys/stl/stdexcept>
29 
30 #define test_expression(expression) \
31 { \
32   if(!(expression)) \
33     { \
34     vtksys_ios::ostringstream buffer; \
35     buffer << "Expression failed at line " << __LINE__ << ": " << #expression; \
36     throw std::runtime_error(buffer.str()); \
37     } \
38 }
39 
TestArrayAPI(int vtkNotUsed (argc),char * vtkNotUsed (argv)[])40 int TestArrayAPI(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
41 {
42   try
43     {
44     vtkSmartPointer<vtkArray> array;
45 
46     // Test to see that we can create every supported combination of storage- and value-type.
47     std::vector<int> storage_types;
48     storage_types.push_back(vtkArray::DENSE);
49     storage_types.push_back(vtkArray::SPARSE);
50 
51     std::vector<int> value_types;
52     value_types.push_back(VTK_CHAR);
53     value_types.push_back(VTK_UNSIGNED_CHAR);
54     value_types.push_back(VTK_SHORT);
55     value_types.push_back(VTK_UNSIGNED_SHORT);
56     value_types.push_back(VTK_INT);
57     value_types.push_back(VTK_UNSIGNED_INT);
58     value_types.push_back(VTK_LONG);
59     value_types.push_back(VTK_UNSIGNED_LONG);
60     value_types.push_back(VTK_DOUBLE);
61     value_types.push_back(VTK_ID_TYPE);
62     value_types.push_back(VTK_STRING);
63     value_types.push_back(VTK_VARIANT);
64 
65     std::vector<vtkVariant> sample_values;
66     sample_values.push_back(static_cast<char>(1));
67     sample_values.push_back(static_cast<unsigned char>(2));
68     sample_values.push_back(static_cast<short>(3));
69     sample_values.push_back(static_cast<unsigned short>(4));
70     sample_values.push_back(static_cast<int>(5));
71     sample_values.push_back(static_cast<unsigned int>(6));
72     sample_values.push_back(static_cast<long>(7));
73     sample_values.push_back(static_cast<unsigned long>(8));
74     sample_values.push_back(static_cast<double>(9.0));
75     sample_values.push_back(static_cast<vtkIdType>(10));
76     sample_values.push_back(vtkStdString("11"));
77     sample_values.push_back(vtkVariant(12.0));
78 
79     for(std::vector<int>::const_iterator storage_type = storage_types.begin(); storage_type != storage_types.end(); ++storage_type)
80       {
81       for(size_t value_type = 0; value_type != value_types.size(); ++value_type)
82         {
83         cerr << "creating array with storage type " << *storage_type << " and value type " << vtkImageScalarTypeNameMacro(value_types[value_type]) << endl;
84 
85         array.TakeReference(vtkArray::CreateArray(*storage_type, value_types[value_type]));
86         test_expression(array);
87 
88         test_expression(array->GetName() == "");
89         array->SetName("foo");
90         test_expression(array->GetName() == "foo");
91 
92         array->Resize(10);
93         array->SetVariantValue(5, sample_values[value_type]);
94         test_expression(array->GetVariantValue(5).IsValid());
95         test_expression(array->GetVariantValue(5) == sample_values[value_type]);
96         }
97       }
98 
99     // Do some spot-checking to see that the actual type matches what we expect ...
100     array.TakeReference(vtkArray::CreateArray(vtkArray::DENSE, VTK_DOUBLE));
101     test_expression(vtkDenseArray<double>::SafeDownCast(array));
102 
103     array.TakeReference(vtkArray::CreateArray(vtkArray::SPARSE, VTK_STRING));
104     test_expression(vtkSparseArray<vtkStdString>::SafeDownCast(array));
105 
106     return 0;
107     }
108   catch(std::exception& e)
109     {
110     cerr << e.what() << endl;
111     return 1;
112     }
113 }
114