//============================================================================ // 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 namespace { using NonDefaultCellSetList = vtkm::List, vtkm::cont::CellSetExplicit::StorageTag>>; template struct CheckFunctor { void operator()(const ExpectedCellType&, bool& called) const { called = true; } template void operator()(const UnexpectedType&, bool& called) const { VTKM_TEST_FAIL("CastAndCall functor called with wrong type."); called = false; } }; class DummyCellSet : public vtkm::cont::CellSet { }; void CheckEmptyUnknownCellSet() { vtkm::cont::UnknownCellSet empty; VTKM_TEST_ASSERT(empty.GetNumberOfCells() == 0, "UnknownCellSet should have no cells"); VTKM_TEST_ASSERT(empty.GetNumberOfFaces() == 0, "UnknownCellSet should have no faces"); VTKM_TEST_ASSERT(empty.GetNumberOfEdges() == 0, "UnknownCellSet should have no edges"); VTKM_TEST_ASSERT(empty.GetNumberOfPoints() == 0, "UnknownCellSet should have no points"); empty.PrintSummary(std::cout); using CellSet2D = vtkm::cont::CellSetStructured<2>; using CellSet3D = vtkm::cont::CellSetStructured<3>; VTKM_TEST_ASSERT(!empty.IsType(), "UnknownCellSet reports wrong type."); VTKM_TEST_ASSERT(!empty.IsType(), "UnknownCellSet reports wrong type."); VTKM_TEST_ASSERT(!empty.IsType(), "UnknownCellSet reports wrong type."); VTKM_TEST_ASSERT(!empty.CanConvert(), "UnknownCellSet reports wrong type."); VTKM_TEST_ASSERT(!empty.CanConvert(), "UnknownCellSet reports wrong type."); VTKM_TEST_ASSERT(!empty.CanConvert(), "UnknownCellSet reports wrong type."); bool gotException = false; try { CellSet2D instance = empty.AsCellSet(); } catch (vtkm::cont::ErrorBadType&) { gotException = true; } VTKM_TEST_ASSERT(gotException, "Empty UnknownCellSet should have thrown on casting"); auto empty2 = empty.NewInstance(); VTKM_TEST_ASSERT(empty.GetCellSetBase() == nullptr, "UnknownCellSet should contain a nullptr"); VTKM_TEST_ASSERT(empty2.GetCellSetBase() == nullptr, "UnknownCellSet should contain a nullptr"); } template void CheckUnknownCellSet(vtkm::cont::UnknownCellSet unknownCellSet) { VTKM_TEST_ASSERT(unknownCellSet.CanConvert()); VTKM_TEST_ASSERT(!unknownCellSet.CanConvert()); unknownCellSet.AsCellSet(); bool called = false; unknownCellSet.CastAndCallForTypes(CheckFunctor(), called); VTKM_TEST_ASSERT( called, "The functor was never called (and apparently a bad value exception not thrown)."); if (vtkm::ListHas::value) { called = false; CastAndCall(unknownCellSet, CheckFunctor(), called); VTKM_TEST_ASSERT( called, "The functor was never called (and apparently a bad value exception not thrown)."); } vtkm::cont::UncertainCellSet uncertainCellSet(unknownCellSet); called = false; uncertainCellSet.CastAndCall(CheckFunctor(), called); VTKM_TEST_ASSERT( called, "The functor was never called (and apparently a bad value exception not thrown)."); called = false; CastAndCall(uncertainCellSet, CheckFunctor(), called); VTKM_TEST_ASSERT( called, "The functor was never called (and apparently a bad value exception not thrown)."); } template void TryNewInstance(vtkm::cont::UnknownCellSet& originalCellSet) { vtkm::cont::UnknownCellSet newCellSet = originalCellSet.NewInstance(); VTKM_TEST_ASSERT(newCellSet.IsType(), "New cell set wrong type."); VTKM_TEST_ASSERT(originalCellSet.GetCellSetBase() != newCellSet.GetCellSetBase(), "NewInstance did not make a copy."); } template void TryCellSet(vtkm::cont::UnknownCellSet& unknownCellSet) { CheckUnknownCellSet(unknownCellSet); CheckUnknownCellSet>(unknownCellSet); TryNewInstance(unknownCellSet); } template void TryDefaultCellSet(CellSetType cellSet) { vtkm::cont::UnknownCellSet unknownCellSet(cellSet); TryCellSet(unknownCellSet); } template void TryNonDefaultCellSet(CellSetType cellSet) { vtkm::cont::UnknownCellSet unknownCellSet(cellSet); TryCellSet(unknownCellSet); } void TestDynamicCellSet() { std::cout << "Try default types with default type lists." << std::endl; std::cout << "*** 2D Structured Grid ******************" << std::endl; TryDefaultCellSet(vtkm::cont::CellSetStructured<2>()); std::cout << "*** 3D Structured Grid ******************" << std::endl; TryDefaultCellSet(vtkm::cont::CellSetStructured<3>()); std::cout << "*** Explicit Grid ***********************" << std::endl; TryDefaultCellSet(vtkm::cont::CellSetExplicit<>()); std::cout << std::endl << "Try non-default types." << std::endl; std::cout << "*** 1D Structured Grid ******************" << std::endl; TryNonDefaultCellSet(vtkm::cont::CellSetStructured<1>()); std::cout << "*** Explicit Grid Constant Shape ********" << std::endl; TryNonDefaultCellSet( vtkm::cont::CellSetExplicit::StorageTag>()); std::cout << std::endl << "Try empty DynamicCellSet." << std::endl; CheckEmptyUnknownCellSet(); } } // anonymous namespace int UnitTestUnknownCellSet(int argc, char* argv[]) { return vtkm::cont::testing::Testing::Run(TestDynamicCellSet, argc, argv); }