//============================================================================ // Copyright (c) Kitware, Inc. // All rights reserved. // See LICENSE.txt 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. //============================================================================ #include #include #include #include #include #include #include #include #include namespace { // Likely to contain both supported and unsupported types. using TypesToTry = vtkm::List; constexpr vtkm::Id DIM_SIZE = 4; constexpr vtkm::Id ARRAY_SIZE = DIM_SIZE * DIM_SIZE * DIM_SIZE; template vtkm::cont::ArrayHandle> MakeCoordinates() { vtkm::cont::ArrayHandleUniformPointCoordinates coordArray{ vtkm::Id(DIM_SIZE) }; VTKM_TEST_ASSERT(coordArray.GetNumberOfValues() == ARRAY_SIZE); vtkm::cont::ArrayHandle> castArray; vtkm::cont::ArrayCopy(coordArray, castArray); return castArray; } template vtkm::cont::ArrayHandle MakeField() { vtkm::cont::ArrayHandle fieldArray; fieldArray.Allocate(ARRAY_SIZE); SetPortal(fieldArray.WritePortal()); return fieldArray; } template vtkm::cont::ArrayHandle> MakeVecField() { return MakeField>(); } template vtkm::cont::DataSet MakeDataSet() { VTKM_STATIC_ASSERT((std::is_same::DimensionalityTag, vtkm::TypeTraitsScalarTag>::value)); vtkm::cont::DataSet dataset; vtkm::cont::CellSetStructured<3> cellSet; cellSet.SetPointDimensions(vtkm::Id3(DIM_SIZE)); dataset.SetCellSet(cellSet); dataset.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coords", MakeCoordinates())); dataset.AddPointField("scalars", MakeField()); dataset.AddPointField("vectors", MakeVecField()); VTKM_TEST_ASSERT(dataset.GetNumberOfPoints() == ARRAY_SIZE); return dataset; } struct CheckCoords { template void operator()(const ArrayType& array) const { VTKM_TEST_ASSERT(test_equal_ArrayHandles(array, MakeCoordinates())); } }; template struct CheckField { template void operator()(const ArrayType& array) const { auto expectedArray = MakeField(); VTKM_TEST_ASSERT(test_equal_ArrayHandles(array, expectedArray)); } }; struct TryType { template void operator()(FieldType) const { using VecType = vtkm::Vec; std::cout << "Creating data." << std::endl; vtkm::cont::DataSet data = MakeDataSet(); std::cout << "Check original data." << std::endl; CheckCoords{}( data.GetCoordinateSystem().GetData().AsArrayHandle>()); CheckField{}( data.GetPointField("scalars").GetData().AsArrayHandle>()); CheckField{}( data.GetPointField("vectors").GetData().AsArrayHandle>()); VTKM_TEST_ASSERT((data.GetCoordinateSystem().IsSupportedType() == vtkm::ListHas::value)); VTKM_TEST_ASSERT((data.GetField("scalars").IsSupportedType() == vtkm::ListHas::value)); VTKM_TEST_ASSERT((data.GetField("vectors").IsSupportedType() == vtkm::ListHas::value)); std::cout << "Check as float default." << std::endl; CheckCoords{}(data.GetCoordinateSystem() .GetDataAsDefaultFloat() .AsArrayHandle>()); CheckField{}(data.GetPointField("scalars") .GetDataAsDefaultFloat() .AsArrayHandle>()); CheckField{}(data.GetPointField("vectors") .GetDataAsDefaultFloat() .AsArrayHandle>()); std::cout << "Check as expected type." << std::endl; vtkm::cont::CastAndCall(data.GetCoordinateSystem().GetDataWithExpectedTypes(), CheckCoords{}); vtkm::cont::CastAndCall(data.GetPointField("scalars").GetDataWithExpectedTypes(), CheckField{}); vtkm::cont::CastAndCall(data.GetPointField("vectors").GetDataWithExpectedTypes(), CheckField{}); std::cout << "Convert to expected and check." << std::endl; data.ConvertToExpected(); vtkm::cont::CastAndCall(data.GetCoordinateSystem(), CheckCoords{}); vtkm::cont::CastAndCall(data.GetPointField("scalars"), CheckField{}); vtkm::cont::CastAndCall(data.GetPointField("vectors"), CheckField{}); } }; void Run() { VTKM_TEST_ASSERT(vtkm::ListHas::value, "This test assumes that VTKM_DEFAULT_TYPE_LIST has vtkm::FloatDefault. " "If there is a reason for this condition, then a special condition needs " "to be added to skip this test."); VTKM_TEST_ASSERT(vtkm::ListHas::value, "This test assumes that VTKM_DEFAULT_TYPE_LIST has vtkm::Vec3f. " "If there is a reason for this condition, then a special condition needs " "to be added to skip this test."); vtkm::testing::Testing::TryTypes(TryType{}, TypesToTry{}); } } // anonymous namespace int UnitTestDataSetConvertToExpected(int argc, char* argv[]) { return vtkm::cont::testing::Testing::Run(Run, argc, argv); }