TestXdmfBinaryController.cpp 2.57 KB
Newer Older
1
#include <fstream>
2
#include <iostream>
3
#include <vector>
4
#include <assert.h>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "XdmfArray.hpp"
#include "XdmfArrayType.hpp"
#include "XdmfBinaryController.hpp"
#include "XdmfReader.hpp"
#include "XdmfWriter.hpp"

int main(int, char **)
{
  
  //
  // write binary file
  //

  std::vector<int> outputData;
  outputData.push_back(1);
  outputData.push_back(0);
  outputData.push_back(-1);  
  outputData.push_back(100);
                       
  std::ofstream output("binary.bin",
                       std::ofstream::binary);
26 27 28 29 30 31 32 33 34 35 36

  // Dummy data to test seek

  std::vector<int> dummyData;
  dummyData.push_back(5);
  dummyData.push_back(-5);
  dummyData.push_back(12);

  output.write(reinterpret_cast<char *>(&(dummyData[0])),
               sizeof(int) * dummyData.size());

37 38 39 40 41 42
  output.write(reinterpret_cast<char *>(&(outputData[0])),
               sizeof(int) * outputData.size());
  output.close();

  //
  // read binary file using XdmfBinaryController
43
  // Checking use of seek
44 45 46 47 48
  //
  shared_ptr<XdmfBinaryController> binaryController = 
    XdmfBinaryController::New("binary.bin",
                              XdmfArrayType::Int32(),
                              XdmfBinaryController::NATIVE,
49
                              sizeof(int) * dummyData.size(),
50 51 52 53 54
                              std::vector<unsigned int>(1, 4));
  
  shared_ptr<XdmfArray> testArray = XdmfArray::New();
  testArray->setHeavyDataController(binaryController);
  testArray->read();
55 56 57 58 59 60 61

  std::cout << testArray->getSize() << " ?= " << 4 << std::endl;
  std::cout << testArray->getValue<int>(0) << " ?= " << outputData[0] << std::endl;
  std::cout << testArray->getValue<int>(1) << " ?= " << outputData[1] << std::endl;
  std::cout << testArray->getValue<int>(2) << " ?= " << outputData[2] << std::endl;
  std::cout << testArray->getValue<int>(3) << " ?= " << outputData[3] << std::endl;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  assert(testArray->getSize() == 4);
  assert(testArray->getValue<int>(0) == outputData[0]);
  assert(testArray->getValue<int>(1) == outputData[1]);
  assert(testArray->getValue<int>(2) == outputData[2]);
  assert(testArray->getValue<int>(3) == outputData[3]);
  
  testArray->release();

  //
  // output array to disk
  //
  shared_ptr<XdmfWriter> writer = XdmfWriter::New("TestXdmfBinary.xmf");
  writer->setMode(XdmfWriter::DistributedHeavyData);
  testArray->accept(writer);

  //
  // read array in
  //
  shared_ptr<XdmfReader> reader = XdmfReader::New();
  shared_ptr<XdmfArray> array = 
    shared_dynamic_cast<XdmfArray>(reader->read("TestXdmfBinary.xmf"));
83
  std::cout << array << " ?!= " << "NULL" << std::endl;
84 85 86 87
  assert(array != NULL);

  return 0;
}