// Copyright Paul A. Bristow 2015. // Copyright Christopher Kormanyos 2015. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) //! \file /*!\brief Example of a Very Contrived Test for a list of types and their respective values.\n It checks that the numeric_limits digits for the type is in accord with the expected value that is contained in a type trait that can be selected by the test_type.\n Method suggested by Gennadiy Rozental in email to Paul Bristow 3 Dec 2015. */ //! \sa http://www.boost.org/doc/libs/1_59_0/libs/test/doc/html/boost_test/tests_organization/test_cases/test_organization_templates.html#ref_BOOST_AUTO_TEST_CASE_TEMPLATE #define BOOST_TEST_MODULE test_types_values #define BOOST_LIB_DIAGNOSTIC #include #include // for mpl::list //#include #include #include #include // List of types to test. typedef boost::mpl::list test_types; template struct ExpectedTDigits; template<> struct ExpectedTDigits { static const int value = 8 - 1; }; template<> struct ExpectedTDigits { static const int value = 16 - 1; }; template<> struct ExpectedTDigits { static const int value = 32 - 1; }; template<> struct ExpectedTDigits { static const int value = 32 - 1; }; template<> struct ExpectedTDigits { static const int value = 64 - 1; }; // But a serious downside of this scheme is that it can't work with std::string or User-defined types. // This is because these types can't be made static which is essential for this to work. template struct ExpectedTstring; template<> struct ExpectedTstring { static const std::string value = "char"; }; // Error C2864 'ExpectedTstring::value': a static data member with an in-class initializer must have non-volatile const integral type BOOST_AUTO_TEST_CASE_TEMPLATE(my_test, T, test_types) { // Test for a value that is same for all T - works fine. BOOST_CHECK_EQUAL(std::numeric_limits::radix, 2); // Find the expected value for the type T. BOOST_CHECK_EQUAL(std::numeric_limits::digits, ExpectedTDigits::value); } // BOOST_AUTO_TEST_CASE_TEMPLATE(my_test, T, test_types) /* Output: Running 5 test cases... *** No errors detected */