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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestVariantSerialization.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 (C) 2008 The Trustees of Indiana University.
* Use, modification and distribution is subject to the Boost Software
* License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
*/
#include "vtkVariantBoostSerialization.h"
#include "vtkSmartPointer.h"
#include "vtkVariant.h"
#include "vtkVariantArray.h"
#include <vtksys/ios/sstream>
#include <string.h>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
int TestVariantSerialization(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
int errors = 0;
// Build a vtkVariantArray with a variety of values in it.
vtkSmartPointer<vtkVariantArray> sourceArray
= vtkSmartPointer<vtkVariantArray>::New();
sourceArray->SetName("Values");
sourceArray->SetNumberOfTuples(7);
sourceArray->SetValue(0, vtkVariant('V'));
sourceArray->SetValue(1, 3.14f);
sourceArray->SetValue(2, 2.71);
sourceArray->SetValue(3, "Test string");
sourceArray->SetValue(4, 17);
sourceArray->SetValue(5, 42l);
// This is the first two words from the Iliad in Greek -- approximately 'Mnviv aeide'
const vtkTypeUInt16 greek_text_utf16[] = { 0x039C, 0x03B7, 0x03BD, 0x03B9, 0x03BD,
' ',
0x03B1, 0x03B5, 0x03B9, 0x03B4, 0x03B5,
0 };
sourceArray->SetValue(6, vtkUnicodeString::from_utf16(greek_text_utf16));
// Serialize the array
std::ostringstream out_stream;
boost::archive::text_oarchive out(out_stream);
out << static_cast<const vtkVariantArray&>(*sourceArray);
// De-serialize the array
vtkSmartPointer<vtkVariantArray> sinkArray
= vtkSmartPointer<vtkVariantArray>::New();
std::istringstream in_stream(out_stream.str());
boost::archive::text_iarchive in(in_stream);
in >> *sinkArray;
// Check that the arrays are the same
if (strcmp(sourceArray->GetName(), sinkArray->GetName()))
{
cerr << "Sink array has name \"" << sinkArray->GetName()
<< "\", should be \"" << sourceArray->GetName() << "\".\n";
++errors;
}
if (sourceArray->GetNumberOfTuples() != sinkArray->GetNumberOfTuples())
{
cerr << "Sink array has " << sinkArray->GetNumberOfTuples()
<< " of elements, should be " << sourceArray->GetNumberOfTuples()
<< ".\n";
++errors;
return errors;
}
for (vtkIdType i = 0; i < sourceArray->GetNumberOfTuples(); ++i)
{
if (sourceArray->GetValue(i).GetType() != sinkArray->GetValue(i).GetType())
{
cerr << "Sink array value at index " << i << " has type "
<< sinkArray->GetValue(i).GetType() << ", should be"
<< sourceArray->GetValue(i).GetType() << ".\n";
++errors;
return errors;
}
}
#define VTK_VARIANT_ARRAY_DATA_CHECK(Index,Function,Kind) \
if (sourceArray->GetValue(Index).Function() \
!= sinkArray->GetValue(Index).Function()) \
{ \
cerr << Kind << " mismatch: \"" << sourceArray->GetValue(Index).Function() \
<< "\" vs. \"" << sinkArray->GetValue(Index).Function() << "\".\n"; \
++errors; \
}
VTK_VARIANT_ARRAY_DATA_CHECK(0,ToChar,"Character");
VTK_VARIANT_ARRAY_DATA_CHECK(1,ToFloat,"Float");
VTK_VARIANT_ARRAY_DATA_CHECK(2,ToDouble,"Double");
if (strcmp(sourceArray->GetValue(3).ToString(),
sinkArray->GetValue(3).ToString()))
{
cerr << "String mismatch: \"" << sourceArray->GetValue(3).ToString()
<< "\" vs. \"" << sinkArray->GetValue(3).ToString() << "\".\n";
++errors;
}
VTK_VARIANT_ARRAY_DATA_CHECK(4,ToInt,"Int");
VTK_VARIANT_ARRAY_DATA_CHECK(5,ToLong,"Long");
#undef VTK_VARIANT_ARRAY_DATA_CHECK
return errors;
}
|