Add testing support to vtkm, and add tests for primitive types.

This commit is contained in:
Robert Maynard 2014-02-10 13:58:41 -05:00
parent c07301a993
commit 24f561f0fe
10 changed files with 4018 additions and 0 deletions

39
vtkm/CMakeLists.txt Normal file

@ -0,0 +1,39 @@
##============================================================================
## 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.
##
## Copyright 2014 Sandia Corporation.
## Copyright 2014 UT-Battelle, LLC.
## Copyright 2014. Los Alamos National Security
##
## Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
include_directories(${Boost_INCLUDE_DIRS})
set(headers
Types.h
TypeTraits.h
VectorTraits.h
)
vtkm_declare_headers(${headers})
#-----------------------------------------------------------------------------
#first add all the components vtkm that are shared between control and exec
add_subdirectory(testing)
add_subdirectory(internal)
#-----------------------------------------------------------------------------
#add the control and exec folders
# add_subdirectory(cont)

@ -0,0 +1,25 @@
##============================================================================
## 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.
##
## Copyright 2014 Sandia Corporation.
## Copyright 2014 UT-Battelle, LLC.
## Copyright 2014. Los Alamos National Security
##
## Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
set(unit_tests
UnitTestConfigureFor32.cxx
UnitTestConfigureFor64.cxx
)
vtkm_unit_tests(SOURCES ${unit_tests})

@ -0,0 +1,57 @@
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/internal/ConfigureFor32.h>
#include <vtkm/Types.h>
#include <vtkm/testing/Testing.h>
// Size of 32 bits.
#define EXPECTED_SIZE 4
#if VTKM_SIZE_ID != EXPECTED_SIZE
#error VTKM_SIZE_ID an unexpected size.
#endif
#if VTKM_SIZE_SCALAR != EXPECTED_SIZE
#error VTKM_SIZE_SCALAR an unexpected size.
#endif
namespace {
void TestTypeSizes()
{
VTKM_TEST_ASSERT(VTKM_SIZE_ID == EXPECTED_SIZE,
"VTKM_SIZE_ID an unexpected size.");
VTKM_TEST_ASSERT(sizeof(vtkm::Id) == EXPECTED_SIZE,
"vtkm::Id an unexpected size.");
VTKM_TEST_ASSERT(VTKM_SIZE_SCALAR == EXPECTED_SIZE,
"VTKM_SIZE_SCALAR an unexpected size.");
VTKM_TEST_ASSERT(sizeof(vtkm::Scalar) == EXPECTED_SIZE,
"vtkm::Scalar an unexpected size.");
}
}
int UnitTestConfigureFor32(int, char *[])
{
return vtkm::testing::Testing::Run(TestTypeSizes);
}

@ -0,0 +1,57 @@
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/internal/ConfigureFor64.h>
#include <vtkm/Types.h>
#include <vtkm/testing/Testing.h>
// Size of 64 bits.
#define EXPECTED_SIZE 8
#if VTKM_SIZE_ID != EXPECTED_SIZE
#error VTKM_SIZE_ID an unexpected size.
#endif
#if VTKM_SIZE_SCALAR != EXPECTED_SIZE
#error VTKM_SIZE_SCALAR an unexpected size.
#endif
namespace {
void TestTypeSizes()
{
VTKM_TEST_ASSERT(VTKM_SIZE_ID == EXPECTED_SIZE,
"VTKM_SIZE_ID an unexpected size.");
VTKM_TEST_ASSERT(sizeof(vtkm::Id) == EXPECTED_SIZE,
"vtkm::Id an unexpected size.");
VTKM_TEST_ASSERT(VTKM_SIZE_SCALAR == EXPECTED_SIZE,
"VTKM_SIZE_SCALAR an unexpected size.");
VTKM_TEST_ASSERT(sizeof(vtkm::Scalar) == EXPECTED_SIZE,
"vtkm::Scalar an unexpected size.");
}
}
int UnitTestConfigureFor64(int, char *[])
{
return vtkm::testing::Testing::Run(TestTypeSizes);
}

@ -0,0 +1,34 @@
##============================================================================
## 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.
##
## Copyright 2014 Sandia Corporation.
## Copyright 2014 UT-Battelle, LLC.
## Copyright 2014. Los Alamos National Security
##
## Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
set(headers
Testing.h
OptionParser.h
)
VTKM_declare_headers(${headers})
set(unit_tests
UnitTestTesting.cxx
UnitTestTypes.cxx
UnitTestTypeTraits.cxx
)
VTKM_unit_tests(SOURCES ${unit_tests})

2821
vtkm/testing/OptionParser.h Normal file

File diff suppressed because it is too large Load Diff

329
vtkm/testing/Testing.h Normal file

@ -0,0 +1,329 @@
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtkm_testing_Testing_h
#define vtkm_testing_Testing_h
#include <vtkm/Types.h>
#include <vtkm/TypeTraits.h>
#include <vtkm/VectorTraits.h>
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
// Try to enforce using the correct testing version. (Those that include the
// control environment have more possible exceptions.) This is not guaranteed
// to work. To make it more likely, place the Testing.h include last.
#ifdef vtkm_cont_Error_h
#ifndef vtkm_cont_testing_Testing_h
#error Use vtkm::cont::testing::Testing instead of vtkm::testing::Testing.
#else
#define VTKM_TESTING_IN_CONT
#endif
#endif
/// \def VTKM_TEST_ASSERT(condition, message)
///
/// Asserts a condition for a test to pass. A passing condition is when \a
/// condition resolves to true. If \a condition is false, then the test is
/// aborted and failure is returned.
#define VTKM_TEST_ASSERT(condition, message) \
::vtkm::testing::Testing::Assert( \
condition, __FILE__, __LINE__, message, #condition)
/// \def VTKM_TEST_FAIL(message)
///
/// Causes a test to fail with the given \a message.
#define VTKM_TEST_FAIL(message) \
throw ::vtkm::testing::Testing::TestFailure(__FILE__, __LINE__, message)
namespace vtkm {
namespace testing {
struct Testing
{
public:
class TestFailure
{
public:
VTKM_CONT_EXPORT TestFailure(const std::string &file,
vtkm::Id line,
const std::string &message)
: File(file), Line(line), Message(message) { }
VTKM_CONT_EXPORT TestFailure(const std::string &file,
vtkm::Id line,
const std::string &message,
const std::string &condition)
: File(file), Line(line)
{
this->Message.append(message);
this->Message.append(" (");
this->Message.append(condition);
this->Message.append(")");
}
VTKM_CONT_EXPORT const std::string &GetFile() const { return this->File; }
VTKM_CONT_EXPORT vtkm::Id GetLine() const { return this->Line; }
VTKM_CONT_EXPORT const std::string &GetMessage() const {
return this->Message;
}
private:
std::string File;
vtkm::Id Line;
std::string Message;
};
static VTKM_CONT_EXPORT void Assert(bool condition,
const std::string &file,
vtkm::Id line,
const std::string &message,
const std::string &conditionString)
{
if (condition)
{
// Do nothing.
}
else
{
throw TestFailure(file, line, message, conditionString);
}
}
#ifndef VTKM_TESTING_IN_CONT
/// Calls the test function \a function with no arguments. Catches any errors
/// generated by VTKM_TEST_ASSERT or VTKM_TEST_FAIL, reports the error, and
/// returns "1" (a failure status for a program's main). Returns "0" (a
/// success status for a program's main).
///
/// The intention is to implement a test's main function with this. For
/// example, the implementation of UnitTestFoo might look something like
/// this.
///
/// \code
/// #include <vtkm/testing/Testing.h>
///
/// namespace {
///
/// void TestFoo()
/// {
/// // Do actual test, which checks in VTKM_TEST_ASSERT or VTKM_TEST_FAIL.
/// }
///
/// } // anonymous namespace
///
/// int UnitTestFoo(int, char *[])
/// {
/// return vtkm::testing::Testing::Run(TestFoo);
/// }
/// \endcode
///
template<class Func>
static VTKM_CONT_EXPORT int Run(Func function)
{
try
{
function();
}
catch (TestFailure error)
{
std::cout << "***** Test failed @ "
<< error.GetFile() << ":" << error.GetLine() << std::endl
<< error.GetMessage() << std::endl;
return 1;
}
catch (...)
{
std::cout << "***** Unidentified exception thrown." << std::endl;
return 1;
}
return 0;
}
#endif
/// Check functors to be used with the TryAllTypes method.
///
struct TypeCheckAlwaysTrue {
template <typename T, class Functor>
void operator()(T t, Functor function) const { function(t); }
};
struct TypeCheckInteger {
template <typename T, class Functor>
void operator()(T t, Functor function) const {
this->DoInteger(typename vtkm::TypeTraits<T>::NumericTag(), t, function);
}
private:
template <class Tag, typename T, class Functor>
void DoInteger(Tag, T, const Functor&) const { }
template <typename T, class Functor>
void DoInteger(vtkm::TypeTraitsIntegerTag, T t, Functor function) const {
function(t);
}
};
struct TypeCheckReal {
template <typename T, class Functor>
void operator()(T t, Functor function) const {
this->DoReal(typename vtkm::TypeTraits<T>::NumericTag(), t, function);
}
private:
template <class Tag, typename T, class Functor>
void DoReal(Tag, T, const Functor&) const { }
template <typename T, class Functor>
void DoReal(vtkm::TypeTraitsRealTag, T t, Functor function) const {
function(t);
}
};
struct TypeCheckScalar {
template <typename T, class Functor>
void operator()(T t, Functor func) const {
this->DoScalar(typename vtkm::TypeTraits<T>::DimensionalityTag(), t, func);
}
private:
template <class Tag, typename T, class Functor>
void DoScalar(Tag, const T &, const Functor &) const { }
template <typename T, class Functor>
void DoScalar(vtkm::TypeTraitsScalarTag, T t, Functor function) const {
function(t);
}
};
struct TypeCheckVector {
template <typename T, class Functor>
void operator()(T t, Functor func) const {
this->DoVector(typename vtkm::TypeTraits<T>::DimensionalityTag(), t, func);
}
private:
template <class Tag, typename T, class Functor>
void DoVector(Tag, const T &, const Functor &) const { }
template <typename T, class Functor>
void DoVector(vtkm::TypeTraitsVectorTag, T t, Functor function) const {
function(t);
}
};
template<class FunctionType>
struct InternalPrintOnInvoke {
InternalPrintOnInvoke(FunctionType function, std::string toprint)
: Function(function), ToPrint(toprint) { }
template <typename T> void operator()(T t) {
std::cout << this->ToPrint << std::endl;
this->Function(t);
}
private:
FunctionType Function;
std::string ToPrint;
};
/// Runs templated \p function on all the basic types defined in VTKm. This is
/// helpful to test templated functions that should work on all types. If the
/// function is supposed to work on some subset of types, then \p check can
/// be set to restrict the types used. This Testing class contains several
/// helpful check functors.
///
template<class FunctionType, class CheckType>
static void TryAllTypes(FunctionType function, CheckType check)
{
vtkm::Id id = 0;
check(id, InternalPrintOnInvoke<FunctionType>(
function, "*** vtkm::Id ***************"));
vtkm::Id3 id3 = vtkm::make_Id3(0, 0, 0);
check(id3, InternalPrintOnInvoke<FunctionType>(
function, "*** vtkm::Id3 **************"));
vtkm::Scalar scalar = 0.0;
check(scalar, InternalPrintOnInvoke<FunctionType>(
function, "*** vtkm::Scalar ***********"));
vtkm::Vector2 vector2 = vtkm::make_Vector2(0.0, 0.0);
check(vector2, InternalPrintOnInvoke<FunctionType>(
function, "*** vtkm::Vector2 **********"));
vtkm::Vector3 vector3 = vtkm::make_Vector3(0.0, 0.0, 0.0);
check(vector3, InternalPrintOnInvoke<FunctionType>(
function, "*** vtkm::Vector3 **********"));
vtkm::Vector4 vector4 = vtkm::make_Vector4(0.0, 0.0, 0.0, 0.0);
check(vector4, InternalPrintOnInvoke<FunctionType>(
function, "*** vtkm::Vector4 **********"));
}
template<class FunctionType>
static void TryAllTypes(FunctionType function)
{
TryAllTypes(function, TypeCheckAlwaysTrue());
}
};
}
} // namespace vtkm::internal
/// Helper function to test two quanitites for equality accounting for slight
/// variance due to floating point numerical inaccuracies.
///
template<typename VectorType>
VTKM_EXEC_CONT_EXPORT bool test_equal(VectorType vector1,
VectorType vector2,
vtkm::Scalar tolerance = 0.0001)
{
typedef typename vtkm::VectorTraits<VectorType> Traits;
for (int component = 0; component < Traits::NUM_COMPONENTS; component++)
{
vtkm::Scalar value1 = Traits::GetComponent(vector1, component);
vtkm::Scalar value2 = Traits::GetComponent(vector2, component);
if ((fabs(value1) < 2*tolerance) && (fabs(value2) < 2*tolerance))
{
continue;
}
vtkm::Scalar ratio = value1/value2;
if ((ratio > vtkm::Scalar(1.0) - tolerance)
&& (ratio < vtkm::Scalar(1.0) + tolerance))
{
// This component is OK. The condition is checked in this way to
// correctly handle non-finites that fail all comparisons. Thus, if a
// non-finite is encountered, this condition will fail and false will be
// returned.
}
else
{
return false;
}
}
return true;
}
/// Helper function for printing out vectors during testing.
///
template<typename T, int Size>
VTKM_EXEC_CONT_EXPORT
std::ostream &operator<<(std::ostream &stream, const vtkm::Tuple<T,Size> &tuple)
{
stream << "[";
for (int component = 0; component < Size-1; component++)
{
stream << tuple[component] << ",";
}
return stream << tuple[Size-1] << "]";
}
#endif //vtkm_testing_Testing_h

@ -0,0 +1,78 @@
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
// This meta-test makes sure that the testing environment is properly reporting
// errors.
#include <vtkm/testing/Testing.h>
namespace {
void Fail()
{
VTKM_TEST_FAIL("I expect this error.");
}
void BadAssert()
{
VTKM_TEST_ASSERT(0 == 1, "I expect this error.");
}
void GoodAssert()
{
VTKM_TEST_ASSERT(1 == 1, "Always true.");
}
void TestTestEqual()
{
VTKM_TEST_ASSERT(test_equal(vtkm::Scalar(2.0), vtkm::Scalar(1.9999999)),
"These should be close enough.");
VTKM_TEST_ASSERT(!test_equal(vtkm::Scalar(2.0), vtkm::Scalar(1.999)),
"These should not be close enough.");
}
// All tests that should not raise a failure.
void CleanTests()
{
GoodAssert();
TestTestEqual();
}
} // anonymous namespace
int UnitTestTesting(int, char *[])
{
std::cout << "This call should fail." << std::endl;
if (vtkm::testing::Testing::Run(Fail) == 0)
{
std::cout << "Did not get expected fail!" << std::endl;
return 1;
}
std::cout << "This call should fail." << std::endl;
if (vtkm::testing::Testing::Run(BadAssert) == 0)
{
std::cout << "Did not get expected fail!" << std::endl;
return 1;
}
std::cout << "This call should pass." << std::endl;
// This is what your main function typically looks like.
return vtkm::testing::Testing::Run(CleanTests);
}

@ -0,0 +1,83 @@
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/TypeTraits.h>
#include <vtkm/VectorTraits.h>
#include <vtkm/testing/Testing.h>
namespace {
struct TypeTraitTest
{
template <typename T> void operator()(T t) {
// If you get compiler errors here, it could be a TypeTraits instance
// has missing or malformed tags.
this->TestDimensionality(t, typename vtkm::TypeTraits<T>::DimensionalityTag());
this->TestNumeric(t, typename vtkm::TypeTraits<T>::NumericTag());
}
private:
template <typename T>
void TestDimensionality(T, vtkm::TypeTraitsScalarTag) {
std::cout << " scalar" << std::endl;
VTKM_TEST_ASSERT(vtkm::VectorTraits<T>::NUM_COMPONENTS == 1,
"Scalar type does not have one component.");
}
template <typename T>
void TestDimensionality(T, vtkm::TypeTraitsVectorTag) {
std::cout << " vector" << std::endl;
VTKM_TEST_ASSERT(vtkm::VectorTraits<T>::NUM_COMPONENTS > 1,
"Vector type does not have multiple components.");
}
template <typename T>
void TestNumeric(T, vtkm::TypeTraitsIntegerTag) {
std::cout << " integer" << std::endl;
typedef typename vtkm::VectorTraits<T>::ComponentType VT;
VT value = VT(2.001);
VTKM_TEST_ASSERT(value == 2, "Integer does not round to integer.");
}
template <typename T>
void TestNumeric(T, vtkm::TypeTraitsRealTag) {
std::cout << " real" << std::endl;
typedef typename vtkm::VectorTraits<T>::ComponentType VT;
VT value = VT(2.001);
VTKM_TEST_ASSERT(test_equal(float(value), float(2.001)),
"Real does not hold floaing point number.");
}
};
static void TestTypeTraits()
{
TypeTraitTest test;
vtkm::testing::Testing::TryAllTypes(test);
std::cout << "vtkm::Tuple<vtkm::Scalar, 5>" << std::endl;
test(vtkm::Tuple<vtkm::Scalar, 5>());
}
} // anonymous namespace
//-----------------------------------------------------------------------------
int UnitTestTypeTraits(int, char *[])
{
return vtkm::testing::Testing::Run(TestTypeTraits);
}

@ -0,0 +1,495 @@
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
//============================================================================
// 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.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. Los Alamos National Security
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/Types.h>
#include <vtkm/testing/Testing.h>
namespace {
//general type test
template <typename T> void TypeTest()
{
//grab the number of elements of T
T a, b, c;
typename T::ComponentType s(5);
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{
a[i]=typename T::ComponentType((i+1)*2);
b[i]=typename T::ComponentType(i+1);
c[i]=typename T::ComponentType((i+1)*2);
}
//verify prefix and postfix increment and decrement
++c[T::NUM_COMPONENTS-1];
c[T::NUM_COMPONENTS-1]++;
--c[T::NUM_COMPONENTS-1];
c[T::NUM_COMPONENTS-1]--;
//make c nearly like a to verify == and != are correct.
c[T::NUM_COMPONENTS-1]=(c[T::NUM_COMPONENTS-1]-1);
T plus = a + b;
T correct_plus;
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{ correct_plus[i] = a[i] + b[i]; }
VTKM_TEST_ASSERT(test_equal(plus, correct_plus),"Tuples not added correctly.");
T minus = a - b;
T correct_minus;
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{ correct_minus[i] = a[i] - b[i]; }
VTKM_TEST_ASSERT(test_equal(minus, correct_minus),"Tuples not subtracted correctly.");
T mult = a * b;
T correct_mult;
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{ correct_mult[i] = a[i] * b[i]; }
VTKM_TEST_ASSERT(test_equal(mult, correct_mult),"Tuples not multiplied correctly.");
T div = a / b;
T correct_div;
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{ correct_div[i] = a[i] / b[i]; }
VTKM_TEST_ASSERT(test_equal(div,correct_div),"Tuples not divided correctly.");
mult = s * a;
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{ correct_mult[i] = s * a[i]; }
VTKM_TEST_ASSERT(test_equal(mult, correct_mult),
"Scalar and Tuple did not multiply correctly.");
mult = a * s;
VTKM_TEST_ASSERT(test_equal(mult, correct_mult),
"Tuple and Scalar to not multiply correctly.");
typename T::ComponentType d = vtkm::dot(a, b);
typename T::ComponentType correct_d = 0;
for(int i=0; i < T::NUM_COMPONENTS; ++i)
{correct_d += a[i] * b[i]; }
VTKM_TEST_ASSERT(test_equal(d, correct_d), "dot(Tuple) wrong");
VTKM_TEST_ASSERT(!(a < b), "operator< wrong");
VTKM_TEST_ASSERT((b < a), "operator< wrong");
VTKM_TEST_ASSERT(!(a < a), "operator< wrong");
VTKM_TEST_ASSERT((a < plus), "operator< wrong");
VTKM_TEST_ASSERT((minus < plus), "operator< wrong");
VTKM_TEST_ASSERT((c < a), "operator< wrong");
VTKM_TEST_ASSERT(!(a == b), "operator== wrong");
VTKM_TEST_ASSERT((a == a), "operator== wrong");
VTKM_TEST_ASSERT((a != b), "operator!= wrong");
VTKM_TEST_ASSERT(!(a != a), "operator!= wrong");
//test against a tuple that shares some values
VTKM_TEST_ASSERT( !(c == a), "operator == wrong");
VTKM_TEST_ASSERT( !(a == c), "operator == wrong");
VTKM_TEST_ASSERT( (c != a), "operator != wrong");
VTKM_TEST_ASSERT( (a != c), "operator != wrong");
}
template<> void TypeTest<vtkm::Vector2>()
{
vtkm::Vector2 a = vtkm::make_Vector2(2, 4);
vtkm::Vector2 b = vtkm::make_Vector2(1, 2);
vtkm::Scalar s = 5;
vtkm::Vector2 plus = a + b;
VTKM_TEST_ASSERT(test_equal(plus, vtkm::make_Vector2(3, 6)),
"Vectors do not add correctly.");
vtkm::Vector2 minus = a - b;
VTKM_TEST_ASSERT(test_equal(minus, vtkm::make_Vector2(1, 2)),
"Vectors to not subtract correctly.");
vtkm::Vector2 mult = a * b;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector2(2, 8)),
"Vectors to not multiply correctly.");
vtkm::Vector2 div = a / b;
VTKM_TEST_ASSERT(test_equal(div, vtkm::make_Vector2(2, 2)),
"Vectors to not divide correctly.");
mult = s * a;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector2(10, 20)),
"Vector and scalar to not multiply correctly.");
mult = a * s;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector2(10, 20)),
"Vector and scalar to not multiply correctly.");
vtkm::Scalar d = vtkm::dot(a, b);
VTKM_TEST_ASSERT(test_equal(d, vtkm::Scalar(10)), "dot(Vector2) wrong");
VTKM_TEST_ASSERT(!(a < b), "operator< wrong");
VTKM_TEST_ASSERT((b < a), "operator< wrong");
VTKM_TEST_ASSERT(!(a < a), "operator< wrong");
VTKM_TEST_ASSERT((a < plus), "operator< wrong");
VTKM_TEST_ASSERT((minus < plus), "operator< wrong");
VTKM_TEST_ASSERT(!(a == b), "operator== wrong");
VTKM_TEST_ASSERT((a == a), "operator== wrong");
VTKM_TEST_ASSERT((a != b), "operator!= wrong");
VTKM_TEST_ASSERT(!(a != a), "operator!= wrong");
//test against a tuple that shares some values
const vtkm::Vector2 c = vtkm::make_Vector2(2,3);
VTKM_TEST_ASSERT((c < a), "operator< wrong");
VTKM_TEST_ASSERT( !(c == a), "operator == wrong");
VTKM_TEST_ASSERT( !(a == c), "operator == wrong");
VTKM_TEST_ASSERT( (c != a), "operator != wrong");
VTKM_TEST_ASSERT( (a != c), "operator != wrong");
}
template<> void TypeTest<vtkm::Vector3>()
{
vtkm::Vector3 a = vtkm::make_Vector3(2, 4, 6);
vtkm::Vector3 b = vtkm::make_Vector3(1, 2, 3);
vtkm::Scalar s = 5;
vtkm::Vector3 plus = a + b;
VTKM_TEST_ASSERT(test_equal(plus, vtkm::make_Vector3(3, 6, 9)),
"Vectors do not add correctly.");
vtkm::Vector3 minus = a - b;
VTKM_TEST_ASSERT(test_equal(minus, vtkm::make_Vector3(1, 2, 3)),
"Vectors to not subtract correctly.");
vtkm::Vector3 mult = a * b;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector3(2, 8, 18)),
"Vectors to not multiply correctly.");
vtkm::Vector3 div = a / b;
VTKM_TEST_ASSERT(test_equal(div, vtkm::make_Vector3(2, 2, 2)),
"Vectors to not divide correctly.");
mult = s * a;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector3(10, 20, 30)),
"Vector and scalar to not multiply correctly.");
mult = a * s;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector3(10, 20, 30)),
"Vector and scalar to not multiply correctly.");
vtkm::Scalar d = vtkm::dot(a, b);
VTKM_TEST_ASSERT(test_equal(d, vtkm::Scalar(28)), "dot(Vector3) wrong");
VTKM_TEST_ASSERT(!(a < b), "operator< wrong");
VTKM_TEST_ASSERT((b < a), "operator< wrong");
VTKM_TEST_ASSERT(!(a < a), "operator< wrong");
VTKM_TEST_ASSERT((a < plus), "operator< wrong");
VTKM_TEST_ASSERT((minus < plus), "operator< wrong");
VTKM_TEST_ASSERT(!(a == b), "operator== wrong");
VTKM_TEST_ASSERT((a == a), "operator== wrong");
VTKM_TEST_ASSERT((a != b), "operator!= wrong");
VTKM_TEST_ASSERT(!(a != a), "operator!= wrong");
//test against a tuple that shares some values
const vtkm::Vector3 c = vtkm::make_Vector3(2,4,5);
VTKM_TEST_ASSERT((c < a), "operator< wrong");
VTKM_TEST_ASSERT( !(c == a), "operator == wrong");
VTKM_TEST_ASSERT( !(a == c), "operator == wrong");
VTKM_TEST_ASSERT( (c != a), "operator != wrong");
VTKM_TEST_ASSERT( (a != c), "operator != wrong");
}
template<> void TypeTest<vtkm::Vector4>()
{
vtkm::Vector4 a = vtkm::make_Vector4(2, 4, 6, 8);
vtkm::Vector4 b = vtkm::make_Vector4(1, 2, 3, 4);
vtkm::Scalar s = 5;
vtkm::Vector4 plus = a + b;
VTKM_TEST_ASSERT(test_equal(plus, vtkm::make_Vector4(3, 6, 9, 12)),
"Vectors do not add correctly.");
vtkm::Vector4 minus = a - b;
VTKM_TEST_ASSERT(test_equal(minus, vtkm::make_Vector4(1, 2, 3, 4)),
"Vectors to not subtract correctly.");
vtkm::Vector4 mult = a * b;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector4(2, 8, 18, 32)),
"Vectors to not multiply correctly.");
vtkm::Vector4 div = a / b;
VTKM_TEST_ASSERT(test_equal(div, vtkm::make_Vector4(2, 2, 2, 2)),
"Vectors to not divide correctly.");
mult = s * a;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector4(10, 20, 30, 40)),
"Vector and scalar to not multiply correctly.");
mult = a * s;
VTKM_TEST_ASSERT(test_equal(mult, vtkm::make_Vector4(10, 20, 30, 40)),
"Vector and scalar to not multiply correctly.");
vtkm::Scalar d = vtkm::dot(a, b);
VTKM_TEST_ASSERT(test_equal(d, vtkm::Scalar(60)), "dot(Vector4) wrong");
VTKM_TEST_ASSERT(!(a < b), "operator< wrong");
VTKM_TEST_ASSERT((b < a), "operator< wrong");
VTKM_TEST_ASSERT(!(a < a), "operator< wrong");
VTKM_TEST_ASSERT((a < plus), "operator< wrong");
VTKM_TEST_ASSERT((minus < plus), "operator< wrong");
VTKM_TEST_ASSERT(!(a == b), "operator== wrong");
VTKM_TEST_ASSERT((a == a), "operator== wrong");
VTKM_TEST_ASSERT((a != b), "operator!= wrong");
VTKM_TEST_ASSERT(!(a != a), "operator!= wrong");
//test against a tuple that shares some values
const vtkm::Vector4 c = vtkm::make_Vector4(2,4,6,7);
VTKM_TEST_ASSERT((c < a), "operator< wrong");
VTKM_TEST_ASSERT( !(c == a), "operator == wrong");
VTKM_TEST_ASSERT( !(a == c), "operator == wrong");
VTKM_TEST_ASSERT( (c != a), "operator != wrong");
VTKM_TEST_ASSERT( (a != c), "operator != wrong");
}
template<> void TypeTest<vtkm::Id3>()
{
vtkm::Id3 a = vtkm::make_Id3(2, 4, 6);
vtkm::Id3 b = vtkm::make_Id3(1, 2, 3);
vtkm::Id s = 5;
vtkm::Id3 plus = a + b;
if ((plus[0] != 3) || (plus[1] != 6) || (plus[2] != 9))
{
VTKM_TEST_FAIL("Vectors do not add correctly.");
}
vtkm::Id3 minus = a - b;
if ((minus[0] != 1) || (minus[1] != 2) || (minus[2] != 3))
{
VTKM_TEST_FAIL("Vectors to not subtract correctly.");
}
vtkm::Id3 mult = a * b;
if ((mult[0] != 2) || (mult[1] != 8) || (mult[2] != 18))
{
VTKM_TEST_FAIL("Vectors to not multiply correctly.");
}
vtkm::Id3 div = a / b;
if ((div[0] != 2) || (div[1] != 2) || (div[2] != 2))
{
VTKM_TEST_FAIL("Vectors to not divide correctly.");
}
mult = s * a;
if ((mult[0] != 10) || (mult[1] != 20) || (mult[2] != 30))
{
VTKM_TEST_FAIL("Vector and scalar to not multiply correctly.");
}
mult = a * s;
if ((mult[0] != 10) || (mult[1] != 20) || (mult[2] != 30))
{
VTKM_TEST_FAIL("Vector and scalar to not multiply correctly.");
}
if (vtkm::dot(a, b) != 28)
{
VTKM_TEST_FAIL("dot(Id3) wrong");
}
VTKM_TEST_ASSERT(!(a < b), "operator< wrong");
VTKM_TEST_ASSERT((b < a), "operator< wrong");
VTKM_TEST_ASSERT(!(a < a), "operator< wrong");
VTKM_TEST_ASSERT((a < plus), "operator< wrong");
VTKM_TEST_ASSERT((minus < plus), "operator< wrong");
if (a == b)
{
VTKM_TEST_FAIL("operator== wrong");
}
if (!(a == a))
{
VTKM_TEST_FAIL("operator== wrong");
}
if (!(a != b))
{
VTKM_TEST_FAIL("operator!= wrong");
}
if (a != a)
{
VTKM_TEST_FAIL("operator!= wrong");
}
//test against a tuple that shares some values
const vtkm::Id3 c = vtkm::make_Id3(2,4,5);
VTKM_TEST_ASSERT((c < a), "operator< wrong");
if (c == a) { VTKM_TEST_FAIL("operator== wrong"); }
if (a == c) { VTKM_TEST_FAIL("operator== wrong"); }
if (!(c != a)) { VTKM_TEST_FAIL("operator!= wrong"); }
if (!(a != c)) { VTKM_TEST_FAIL("operator!= wrong"); }
}
template<> void TypeTest<vtkm::Scalar>()
{
vtkm::Scalar a = 4;
vtkm::Scalar b = 2;
vtkm::Scalar plus = a + b;
if (plus != 6)
{
VTKM_TEST_FAIL("Scalars do not add correctly.");
}
vtkm::Scalar minus = a - b;
if (minus != 2)
{
VTKM_TEST_FAIL("Scalars to not subtract correctly.");
}
vtkm::Scalar mult = a * b;
if (mult != 8)
{
VTKM_TEST_FAIL("Scalars to not multiply correctly.");
}
vtkm::Scalar div = a / b;
if (div != 2)
{
VTKM_TEST_FAIL("Scalars to not divide correctly.");
}
if (a == b)
{
VTKM_TEST_FAIL("operator== wrong");
}
if (!(a != b))
{
VTKM_TEST_FAIL("operator!= wrong");
}
if (vtkm::dot(a, b) != 8)
{
VTKM_TEST_FAIL("dot(Scalar) wrong");
}
}
template<> void TypeTest<vtkm::Id>()
{
vtkm::Id a = 4;
vtkm::Id b = 2;
vtkm::Id plus = a + b;
if (plus != 6)
{
VTKM_TEST_FAIL("Scalars do not add correctly.");
}
vtkm::Id minus = a - b;
if (minus != 2)
{
VTKM_TEST_FAIL("Scalars to not subtract correctly.");
}
vtkm::Id mult = a * b;
if (mult != 8)
{
VTKM_TEST_FAIL("Scalars to not multiply correctly.");
}
vtkm::Id div = a / b;
if (div != 2)
{
VTKM_TEST_FAIL("Scalars to not divide correctly.");
}
if (a == b)
{
VTKM_TEST_FAIL("operator== wrong");
}
if (!(a != b))
{
VTKM_TEST_FAIL("operator!= wrong");
}
if (vtkm::dot(a, b) != 8)
{
VTKM_TEST_FAIL("dot(Id) wrong");
}
}
struct TypeTestFunctor
{
template <typename T> void operator()(const T&) const {
TypeTest<T>();
}
};
void TestTypes()
{
vtkm::testing::Testing::TryAllTypes(TypeTestFunctor());
//try with some custom tuple types
TypeTestFunctor()( vtkm::Tuple<vtkm::Scalar,6>() );
TypeTestFunctor()( vtkm::Tuple<vtkm::Id,4>() );
TypeTestFunctor()( vtkm::Tuple<unsigned char,4>() );
TypeTestFunctor()( vtkm::Tuple<vtkm::Id,1>() );
TypeTestFunctor()( vtkm::Tuple<vtkm::Scalar,1>() );
}
} // anonymous namespace
int UnitTestTypes(int, char *[])
{
return vtkm::testing::Testing::Run(TestTypes);
}