TestXdmfArray.cpp 9.5 KB
Newer Older
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
1
#include "XdmfArray.hpp"
2
#include "XdmfArrayType.hpp"
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
3

4 5
#include <iostream>

6
int main(int, char **)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
7 8
{

9
  int values[] = {1, 2, 3, 4};
10

11 12 13
  //
  // COPIES
  //
14

15 16 17
  /**
   * Array stride = 1, Values stride = 1
   */
18
  shared_ptr<XdmfArray> array = XdmfArray::New();
19 20 21 22
  assert(array->getSize() == 0);
  assert(array->getArrayType() == XdmfArrayType::Uninitialized());
  assert(array->getValuesString() == "");
  assert(array->getValuesInternal() == NULL);
23 24 25
  std::vector<unsigned int> dimensions = array->getDimensions();
  assert(dimensions.size() == 1);
  assert(dimensions[0] == 0);
26
  assert(array->getDimensionsString().compare("0") == 0);
27 28 29
  array->insert(0, &values[0], 4, 1, 1);
  assert(array->getSize() == 4);
  assert(array->getArrayType() == XdmfArrayType::Int32());
30
  assert(array->getValuesString().compare("1 2 3 4") == 0);
31 32
  const int * const arrayPointer = 
    static_cast<int *>(array->getValuesInternal());
33 34 35 36 37
  assert(arrayPointer[0] == 1);
  assert(arrayPointer[1] == 2);
  assert(arrayPointer[2] == 3);
  assert(arrayPointer[3] == 4);
  // Assert we copied values correctly
38
  shared_ptr<std::vector<int> > storedValues =
39 40 41 42 43 44 45 46 47 48 49
    array->getValuesInternal<int>();
  assert((*storedValues)[0] == 1);
  assert((*storedValues)[1] == 2);
  assert((*storedValues)[2] == 3);
  assert((*storedValues)[3] == 4);
  // Assert we can copy values out correctly
  std::vector<int> outValues(4);
  array->getValues(0, &outValues[0], 4);
  for(unsigned int i=0; i<outValues.size(); ++i) {
    assert(outValues[i] == values[i]);
  }
50

51 52 53
  /**
   * Array stride = 2, Values stride = 1
   */
54
  shared_ptr<XdmfArray> array2 = XdmfArray::New();
55 56 57
  array2->insert(0, &values[0], 2, 2, 1);
  assert(array2->getSize() == 3);
  assert(array2->getArrayType() == XdmfArrayType::Int32());
58
  assert(array2->getValuesString().compare("1 0 2") == 0);
59 60 61 62
  storedValues = array2->getValuesInternal<int>();
  assert((*storedValues)[0] == 1);
  assert((*storedValues)[1] == 0);
  assert((*storedValues)[2] == 2);
63

64 65 66
  /**
   * Array stride = 1, Values stride = 2
   */
67
  shared_ptr<XdmfArray> array3 = XdmfArray::New();
68 69 70
  array3->insert(0, &values[0], 2, 1, 2);
  assert(array3->getSize() == 2);
  assert(array3->getArrayType() == XdmfArrayType::Int32());
71
  assert(array3->getValuesString().compare("1 3") == 0);
72 73 74
  storedValues = array3->getValuesInternal<int>();
  assert((*storedValues)[0] == 1);
  assert((*storedValues)[1] == 3);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
75

76 77 78
  /**
   * Array stride = 2, Values stride = 2
   */
79
  shared_ptr<XdmfArray> array4 = XdmfArray::New();
80 81
  array4->insert(0, &values[0], 2, 2, 2);
  assert(array4->getSize() == 3);
82
  assert(array4->getValuesString().compare("1 0 3") == 0);
83 84 85 86
  storedValues = array4->getValuesInternal<int>();
  assert((*storedValues)[0] == 1);
  assert((*storedValues)[1] == 0);
  assert((*storedValues)[2] == 3);
87

88 89 90 91
  /**
   * Copy values from another XdmfArray
   * Array stride = 1, Values stride = 1
   */
92
  shared_ptr<XdmfArray> array5 = XdmfArray::New();
93 94
  array5->insert(0, array, 1, 3);
  assert(array5->getSize() == 3);
95
  assert(array5->getArrayType() == array->getArrayType());
96 97 98
  storedValues = array5->getValuesInternal<int>();
  assert(array5->getSize() == 3);
  assert(array->getSize() == 4);
99 100 101 102 103 104 105 106 107 108
  assert(array5->getValuesString().compare("2 3 4") == 0);
  array5->insert(1, array, 1, 3);
  assert(array5->getValuesString().compare("2 2 3 4") == 0);
  array5->clear();
  assert(array5->getSize() == 0);
  array5->insert(0, array, 1, 3, 2, 1);
  assert(array5->getValuesString().compare("2 0 3 0 4") == 0);
  array5->clear();
  array5->insert(0, array, 1, 2, 2, 2);
  assert(array5->getValuesString().compare("2 0 4") == 0);
109

110 111 112
  /**
   * Single Insertion
   */
113
  shared_ptr<XdmfArray> array10 = XdmfArray::New();
114 115 116
  array10->insert<unsigned int>(0, 1);
  assert(array10->getSize() == 1);
  assert(array10->getValue<unsigned int>(0) == 1);
117

118 119 120
  //
  // PUSHBACK
  //
121
  shared_ptr<XdmfArray> pushBackArray = XdmfArray::New();
122 123 124
  assert(pushBackArray->getSize() == 0);
  pushBackArray->pushBack(10);
  assert(pushBackArray->getSize() == 1);
125

126 127 128
  //
  // SETS
  //
129

130 131 132 133 134 135
  /**
   * Simple Set
   */
  array5->setValuesInternal(values, 2, 0);
  assert(array5->getSize() == 2);
  assert(array5->getArrayType() == XdmfArrayType::Int32());
136
  assert(array5->getValuesString().compare("1 2") == 0);
137 138
  const int * const array5Pointer = 
    static_cast<int *>(array5->getValuesInternal());
139 140 141 142 143
  assert(array5Pointer[0] == 1);
  assert(array5Pointer[1] == 2);
  // Assert we can copy values out correctly
  array->getValues(1, &outValues[0], 3);
  assert(outValues[0] == values[1]);
144

145 146 147 148 149
  /**
   * Copy after Set
   */
  array5->setValuesInternal(&values[1], 3, 0);
  assert(array5->getSize() == 3);
150
  assert(array5->getValuesString().compare("2 3 4") == 0);
151 152
   int zero = 0;
   array5->insert(3, &zero, 1, 1, 0);
153
  assert(array5->getSize() == 4);
154
  assert(array5->getValuesString().compare("2 3 4 0") == 0);
155

156 157 158 159 160 161 162 163 164 165
  /**
   * Set and Ownership transfer
   */
  double * doubleValues = new double[3];
  doubleValues[0] = 0;
  doubleValues[1] = 1.1;
  doubleValues[2] = 10.1;
  array5->setValuesInternal(doubleValues, 3, 1);
  assert(array5->getSize() == 3);
  assert(array5->getArrayType() == XdmfArrayType::Float64());
166

167 168 169
  //
  // SHARED ASSIGNMENTS
  //
170

171 172 173
  /**
   * Shared vector assignment
   */
174
  shared_ptr<std::vector<char> > values2(new std::vector<char>());
175 176 177 178 179
  values2->push_back(-2);
  values2->push_back(-1);
  values2->push_back(0);
  values2->push_back(1);
  values2->push_back(2);
180
  shared_ptr<XdmfArray> array6 = XdmfArray::New();
181 182 183
  array6->setValuesInternal(values2);
  assert(array6->getSize() == 5);
  assert(array6->getArrayType() == XdmfArrayType::Int8());
184
  assert(array6->getValuesString().compare("-2 -1 0 1 2") == 0);
185
  // Assert we have the same values!
186
  shared_ptr<std::vector<char> > storedValues2 =
187 188 189 190 191 192 193 194 195 196
    array6->getValuesInternal<char>();
  int i = 0;
  for(std::vector<char>::const_iterator iter = storedValues2->begin();
      iter != storedValues2->end();
      ++iter, ++i) {
    assert(*iter == values2->operator[](i));
  }
  // Now modify original array
  values2->push_back(8);
  assert(array6->getSize() == 6);
197
  assert(array6->getValuesString().compare("-2 -1 0 1 2 8") == 0);
198 199 200 201 202 203 204 205
  // Assert we have the same values!
  i = 0;
  for(std::vector<char>::const_iterator iter = storedValues2->begin();
      iter != storedValues2->end();
      ++iter, ++i) {
    assert(*iter == values2->operator[](i));
  }
  // Assert we can't get an int vector out of our array.
206
  shared_ptr<std::vector<int> > storedValues2Int =
207 208
    array6->getValuesInternal<int>();
  assert(storedValues2Int == NULL);
209

210 211 212
  //
  // SWAPS
  //
213

214 215 216 217 218 219 220
  /**
   * Swap values from a vector
   */
  std::vector<short> values3;
  values3.push_back(-1);
  values3.push_back(0);
  values3.push_back(1);
221
  shared_ptr<XdmfArray> array7 = XdmfArray::New();
222 223 224 225
  array7->swap(values3);
  assert(values3.size() == 0);
  assert(array7->getSize() == 3);
  assert(array7->getArrayType() == XdmfArrayType::Int16());
226
  shared_ptr<std::vector<short> > storedValues3 =
227 228 229 230
    array7->getValuesInternal<short>();
  assert((*storedValues3)[0] == -1);
  assert((*storedValues3)[1] == 0);
  assert((*storedValues3)[2] == 1);
231

232 233 234 235 236 237 238 239
  /**
   * Swap values from a shared vector
   */
  array7->release();
  array7->swap(values2);
  assert(storedValues2->size() == 0);
  assert(array7->getSize() == 6);
  assert(array7->getArrayType() == XdmfArrayType::Int8());
240

241 242 243 244 245 246 247
  /**
   * Swap values from an XdmfArray (with copy)
   */
  array7->release();
  array7->swap(array4);
  assert(array4->getSize() == 0);
  assert(array7->getSize() == 3);
248

249 250 251 252 253 254 255 256 257
  /**
   * Swap values from an XdmfArray (with pointer)
   */
  array5->setValuesInternal(&values[1], 3, 0);
  assert(array5->getSize() == 3);
  array7->release();
  array7->swap(array5);
  assert(array5->getSize() == 0);
  assert(array7->getSize() == 3);
258

259 260 261
  //
  // Various STL like functions
  //
262

263
  /**
264
   * Resize
265
   */
266
  shared_ptr<XdmfArray> array8 = XdmfArray::New();
267 268
  array8->insert(0, &values[0], 4, 1, 1);
  array8->resize(5, 0);
269
  assert(array8->getValuesString().compare("1 2 3 4 0") == 0);
270
  array8->resize(3, 0);
271
  assert(array8->getValuesString().compare("1 2 3") == 0);
272
  array8->resize(8, 1.1);
273
  assert(array8->getValuesString().compare("1 2 3 1 1 1 1 1") == 0);
274

275 276 277 278 279 280
  /**
   * Erase
   */
  array8->erase(0);
  assert(array8->getValuesString().compare("2 3 1 1 1 1 1") == 0);

281 282 283 284 285
  /**
   * Reserve / Capacity
   */
  array8->reserve(50);
  assert(array8->getCapacity() >= 50);
286

287 288 289
  assert(array8->getName().compare("") == 0);
  array8->setName("BLAH");
  assert(array8->getName().compare("BLAH") == 0);
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304
  /**
   * Dimensions
   */
  dimensions.resize(2);
  dimensions[0] = 3;
  dimensions[1] = 3;
  shared_ptr<XdmfArray> array9 = XdmfArray::New();
  array9->initialize(XdmfArrayType::Float64(),
                     dimensions);
  assert(array9->getDimensionsString() == "3 3");
  assert(array9->getSize() == 9);
  array9->insert<double>(9, 1.0);
  assert(array9->getDimensionsString() == "10");

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  //
  // STRINGS
  //
  shared_ptr<XdmfArray> stringArray = XdmfArray::New();
  stringArray->resize<std::string>(3, "");
  std::string firstValue = stringArray->getValue<std::string>(0);
  assert(firstValue.compare("") == 0);
  stringArray->insert<std::string>(0, "foo");
  stringArray->insert<std::string>(1, "bar");
  stringArray->insert<std::string>(2, "cat");
  firstValue = stringArray->getValue<std::string>(0);
  assert(firstValue.compare("foo") == 0);
  std::string allValues = stringArray->getValuesString();
  assert(allValues.compare("foo bar cat") == 0);
  stringArray->insert<int>(3, 1);
  allValues = stringArray->getValuesString();
  assert(allValues.compare("foo bar cat 1") == 0);
  int num = stringArray->getValue<int>(3);
  assert(num == 1);

325
  return 0;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
326
}