From 7ec17497c773d61cf677b9da76ee1eaa59f0c383 Mon Sep 17 00:00:00 2001 From: Aleksey Gurtovoy Date: Sun, 10 Oct 2004 16:15:58 +0000 Subject: [PATCH] move enable_if tests into their own subdirectory [SVN r25649] --- enable_if/test/Jamfile | 33 +++++++ enable_if/test/constructors.cpp | 62 ++++++++++++ enable_if/test/dummy_arg_disambiguation.cpp | 46 +++++++++ enable_if/test/lazy.cpp | 82 ++++++++++++++++ enable_if/test/lazy_test.cpp | 100 ++++++++++++++++++++ enable_if/test/member_templates.cpp | 43 +++++++++ enable_if/test/namespace_disambiguation.cpp | 47 +++++++++ enable_if/test/no_disambiguation.cpp | 43 +++++++++ enable_if/test/partial_specializations.cpp | 67 +++++++++++++ 9 files changed, 523 insertions(+) create mode 100644 enable_if/test/Jamfile create mode 100644 enable_if/test/constructors.cpp create mode 100644 enable_if/test/dummy_arg_disambiguation.cpp create mode 100644 enable_if/test/lazy.cpp create mode 100644 enable_if/test/lazy_test.cpp create mode 100644 enable_if/test/member_templates.cpp create mode 100644 enable_if/test/namespace_disambiguation.cpp create mode 100644 enable_if/test/no_disambiguation.cpp create mode 100644 enable_if/test/partial_specializations.cpp diff --git a/enable_if/test/Jamfile b/enable_if/test/Jamfile new file mode 100644 index 0000000..f2e5ee3 --- /dev/null +++ b/enable_if/test/Jamfile @@ -0,0 +1,33 @@ +# Copyright David Abrahams 2003. +# 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) + +# For more information, see http://www.boost.org/ + +subproject libs/utility/enable_if/test ; + +# bring in rules for testing +import testing ; + +# Make tests run by default. +DEPENDS all : test ; + +{ + local test_monitor = @boost/libs/test/build/boost_test_exec_monitor ; + + # look in BOOST_ROOT for sources first, just in this Jamfile + local SEARCH_SOURCE = $(BOOST_ROOT) $(SEARCH_SOURCE) ; + + test-suite utility/enable_if + : + [ run libs/utility/enable_if/test/constructors.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/dummy_arg_disambiguation.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/lazy.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/lazy_test.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/member_templates.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/namespace_disambiguation.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/no_disambiguation.cpp $(test_monitor) ] + [ run libs/utility/enable_if/test/partial_specializations.cpp $(test_monitor) ] + ; +} diff --git a/enable_if/test/constructors.cpp b/enable_if/test/constructors.cpp new file mode 100644 index 0000000..557b2f0 --- /dev/null +++ b/enable_if/test/constructors.cpp @@ -0,0 +1,62 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include + +#include +#include + +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +struct container { + bool my_value; + + template + container(const T&, const typename enable_if, T>::type * = 0): + my_value(true) {} + + template + container(const T&, const typename disable_if, T>::type * = 0): + my_value(false) {} +}; + +// example from Howard Hinnant (tests enable_if template members of a templated class) +template +struct xstring +{ + template + xstring(It begin, It end, typename + disable_if >::type* = 0) + : data(end-begin) {} + + int data; +}; + + +int test_main(int, char*[]) +{ + + BOOST_CHECK(container(1).my_value); + BOOST_CHECK(container(1.0).my_value); + + BOOST_CHECK(!container("1").my_value); + BOOST_CHECK(!container(static_cast(0)).my_value); + + char sa[] = "123456"; + BOOST_CHECK(xstring(sa, sa+6).data == 6); + + + return 0; +} + diff --git a/enable_if/test/dummy_arg_disambiguation.cpp b/enable_if/test/dummy_arg_disambiguation.cpp new file mode 100644 index 0000000..bb9f733 --- /dev/null +++ b/enable_if/test/dummy_arg_disambiguation.cpp @@ -0,0 +1,46 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include + +#include +#include + +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +template struct dummy { + dummy(int) {}; +}; + +template +typename enable_if, bool>::type +arithmetic_object(T t, dummy<0> = 0) { return true; } + +template +typename disable_if, bool>::type +arithmetic_object(T t, dummy<1> = 0) { return false; } + + +int test_main(int, char*[]) +{ + + BOOST_CHECK(arithmetic_object(1)); + BOOST_CHECK(arithmetic_object(1.0)); + + BOOST_CHECK(!arithmetic_object("1")); + BOOST_CHECK(!arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/enable_if/test/lazy.cpp b/enable_if/test/lazy.cpp new file mode 100644 index 0000000..6e7d650 --- /dev/null +++ b/enable_if/test/lazy.cpp @@ -0,0 +1,82 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include + +#include +#include + +using boost::enable_if_c; +using boost::lazy_enable_if_c; + +// This class provides a reduced example of a traits class for +// computing the result of multiplying two types. The member typedef +// 'type' in this traits class defines the return type of this +// operator. The return type member is invalid unless both arguments +// for mult_traits are values that mult_traits expects (ints in this +// case). This kind of situation may arise if a traits class only +// makes sense for some set of types, not all C++ types. + +template struct is_int { + BOOST_STATIC_CONSTANT(bool, value = (boost::is_same::value)); +}; + +template +struct mult_traits { + typedef typename T::does_not_exist type; +}; + +template <> +struct mult_traits { + typedef int type; +}; + + +// Next, a forwarding function mult() is defined. It is enabled only +// when both arguments are of type int. The first version, using +// non-lazy enable_if_c does not work. + +#if 0 +template +typename enable_if_c< + is_int::value && is_int::value, + typename mult_traits::type +>::type +mult(const T& x, const U& y) {return x * y;} +#endif + +// A correct version uses lazy_enable_if_c. +// This template removes compiler errors from invalid code used as an +// argument to enable_if_c. + +#if 1 +template +typename lazy_enable_if_c< + is_int::value && is_int::value, + mult_traits +>::type +mult(const T& x, const U& y) {return x * y;} +#endif + +double mult(int i, double d) { return (double)i * d; } + +int test_main(int, char*[]) +{ + + + BOOST_CHECK(mult(1, 2) == 2); + + BOOST_CHECK(mult(1, 3.0) == 3.0); + + return 0; +} + diff --git a/enable_if/test/lazy_test.cpp b/enable_if/test/lazy_test.cpp new file mode 100644 index 0000000..d52d01e --- /dev/null +++ b/enable_if/test/lazy_test.cpp @@ -0,0 +1,100 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +// Testing all variations of lazy_enable_if. + +#include +#include + +#include +#include + +using boost::lazy_enable_if; +using boost::lazy_disable_if; + +using boost::lazy_enable_if_c; +using boost::lazy_disable_if_c; + + +template +struct is_int_or_double { + BOOST_STATIC_CONSTANT(bool, + value = (boost::is_same::value || + boost::is_same::value)); +}; + +template +struct some_traits { + typedef typename T::does_not_exist type; +}; + +template <> +struct some_traits { + typedef bool type; +}; + +template <> +struct some_traits { + typedef bool type; +}; + +template +struct make_bool { + typedef bool type; +}; + +template <> +struct make_bool {}; + +template <> +struct make_bool {}; + +namespace A { + + template + typename lazy_enable_if, some_traits >::type + foo(T t) { return true; } + + template + typename lazy_enable_if_c::value, some_traits >::type + foo2(T t) { return true; } +} + +namespace B { + template + typename lazy_disable_if, make_bool >::type + foo(T t) { return false; } + + template + typename lazy_disable_if_c::value, make_bool >::type + foo2(T t) { return false; } +} + +int test_main(int, char*[]) +{ + using namespace A; + using namespace B; + BOOST_CHECK(foo(1)); + BOOST_CHECK(foo(1.0)); + + BOOST_CHECK(!foo("1")); + BOOST_CHECK(!foo(static_cast(0))); + + BOOST_CHECK(foo2(1)); + BOOST_CHECK(foo2(1.0)); + + BOOST_CHECK(!foo2("1")); + BOOST_CHECK(!foo2(static_cast(0))); + + return 0; +} + diff --git a/enable_if/test/member_templates.cpp b/enable_if/test/member_templates.cpp new file mode 100644 index 0000000..23dd173 --- /dev/null +++ b/enable_if/test/member_templates.cpp @@ -0,0 +1,43 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include + +#include +#include + +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +struct container { + template + typename enable_if, bool>::type + arithmetic_object(const T&, const int* /* disambiguate */ = 0) {return true;} + + template + typename disable_if, bool>::type + arithmetic_object(const T&) {return false;} +}; + +int test_main(int, char*[]) +{ + + BOOST_CHECK(container().arithmetic_object(1)); + BOOST_CHECK(container().arithmetic_object(1.0)); + + BOOST_CHECK(!container().arithmetic_object("1")); + BOOST_CHECK(!container().arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/enable_if/test/namespace_disambiguation.cpp b/enable_if/test/namespace_disambiguation.cpp new file mode 100644 index 0000000..90a98a1 --- /dev/null +++ b/enable_if/test/namespace_disambiguation.cpp @@ -0,0 +1,47 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include +#include + +#include +#include + +using boost::enable_if; +using boost::mpl::not_; +using boost::is_arithmetic; + +namespace A { + template + typename enable_if, bool>::type + arithmetic_object(T t) { return true; } +} + +namespace B { + template + typename enable_if >, bool>::type + arithmetic_object(T t) { return false; } +} + +int test_main(int, char*[]) +{ + using namespace A; + using namespace B; + BOOST_CHECK(arithmetic_object(1)); + BOOST_CHECK(arithmetic_object(1.0)); + + BOOST_CHECK(!arithmetic_object("1")); + BOOST_CHECK(!arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/enable_if/test/no_disambiguation.cpp b/enable_if/test/no_disambiguation.cpp new file mode 100644 index 0000000..f4936e8 --- /dev/null +++ b/enable_if/test/no_disambiguation.cpp @@ -0,0 +1,43 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include +#include + +#include +#include + +using boost::mpl::not_; +using boost::enable_if; +using boost::is_arithmetic; + +template +typename enable_if, bool>::type +arithmetic_object(T t) { return true; } + +template +typename enable_if >, bool>::type +arithmetic_object(T t) { return false; } + + +int test_main(int, char*[]) +{ + + BOOST_CHECK(arithmetic_object(1)); + BOOST_CHECK(arithmetic_object(1.0)); + + BOOST_CHECK(!arithmetic_object("1")); + BOOST_CHECK(!arithmetic_object(static_cast(0))); + + return 0; +} + diff --git a/enable_if/test/partial_specializations.cpp b/enable_if/test/partial_specializations.cpp new file mode 100644 index 0000000..3dce799 --- /dev/null +++ b/enable_if/test/partial_specializations.cpp @@ -0,0 +1,67 @@ +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to 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) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + +#include + +#include +#include + +using boost::enable_if_c; +using boost::disable_if_c; +using boost::enable_if; +using boost::disable_if; +using boost::is_arithmetic; + +template +struct tester; + +template +struct tester::value>::type> { + BOOST_STATIC_CONSTANT(bool, value = true); +}; + +template +struct tester::value>::type> { + BOOST_STATIC_CONSTANT(bool, value = false); +}; + +template +struct tester2; + +template +struct tester2 >::type> { + BOOST_STATIC_CONSTANT(bool, value = true); +}; + +template +struct tester2 >::type> { + BOOST_STATIC_CONSTANT(bool, value = false); +}; + +int test_main(int, char*[]) +{ + + BOOST_CHECK(tester::value); + BOOST_CHECK(tester::value); + + BOOST_CHECK(!tester::value); + BOOST_CHECK(!tester::value); + + BOOST_CHECK(tester2::value); + BOOST_CHECK(tester2::value); + + BOOST_CHECK(!tester2::value); + BOOST_CHECK(!tester2::value); + + return 0; +} +