Tests for enable_if

[SVN r1401]
This commit is contained in:
Jaakko Järvi
2003-07-02 21:34:25 +00:00
parent 46f8fa5e6f
commit 037808a780
8 changed files with 386 additions and 0 deletions

53
test/enable_if_LICENSE Normal file
View File

@@ -0,0 +1,53 @@
Software License
Copyright 2003 <20> The Trustees of Indiana University. All rights
reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. All redistributions of source code must retain the above copyright
notice, the list of authors in the original source code, this list
of conditions and the disclaimer listed in this license;
2. All redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the disclaimer
listed in this license in the documentation and/or other materials
provided with the distribution;
3. Any documentation included with all redistributions must include
the following acknowledgement:
This product includes software developed by the Open Systems Laboratory
at Indiana University. For further information contact Andrew Lumsdaine
at lums@osl.iu.edu.
Alternatively, this acknowledgement may appear in the software itself,
and wherever such third-party acknowledgments normally appear.
4. The name Indiana University shall not be used to endorse or promote
products derived from this software without prior written permission
from Indiana University. For written permission, please contact the
Advanced Research and Technology Institute (ARTI) at 1100 Waterway
Blvd., Indianapolis, Indiana 46202.
5. Products derived from this software may not be called enable_if, nor
may Indiana University appear in their name, without prior written
permission of ARTI.
Indiana University provides no reassurances that the source code
provided does not infringe the patent or any other intellectual
property rights of any other entity. Indiana University disclaims any
liability to any recipient for claims brought by any other entity
based on infringement of intellectual property rights or otherwise.
LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH
NO WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA
UNIVERSITY GIVES NO WARRANTIES AND MAKES NO REPRESENTATION THAT
SOFTWARE IS FREE OF INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR
OTHER PROPRIETARY RIGHTS. INDIANA UNIVERSITY MAKES NO WARRANTIES THAT
SOFTWARE IS FREE FROM "BUGS", "VIRUSES", "TROJAN HORSES", "TRAP
DOORS", "WORMS", OR OTHER HARMFUL CODE. LICENSEE ASSUMES THE ENTIRE
RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR ASSOCIATED MATERIALS,
AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION GENERATED USING
SOFTWARE.

View File

@@ -0,0 +1,44 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
using boost::enable_if;
using boost::disable_if;
using boost::is_arithmetic;
struct container {
bool my_value;
template <class T>
container(const T&, const typename enable_if<is_arithmetic<T>::value, T>::type * = 0):
my_value(true) {}
template <class T>
container(const T&, const typename disable_if<is_arithmetic<T>::value, T>::type * = 0):
my_value(false) {}
};
int test_main(int, char*[])
{
BOOST_TEST(container(1).my_value);
BOOST_TEST(container(1.0).my_value);
BOOST_TEST(!container("1").my_value);
BOOST_TEST(!container(static_cast<void*>(0)).my_value);
return 0;
}

View File

@@ -0,0 +1,44 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
using boost::enable_if;
using boost::is_arithmetic;
template <int N> struct dummy {
dummy(int) {};
};
template<class T>
typename enable_if<is_arithmetic<T>::value, bool>::type
arithmetic_object(T t, dummy<0> = 0) { return true; }
template<class T>
typename enable_if<!is_arithmetic<T>::value, bool>::type
arithmetic_object(T t, dummy<1> = 0) { return false; }
int test_main(int, char*[])
{
BOOST_TEST(arithmetic_object(1));
BOOST_TEST(arithmetic_object(1.0));
BOOST_TEST(!arithmetic_object("1"));
BOOST_TEST(!arithmetic_object(static_cast<void*>(0)));
return 0;
}

74
test/enable_if_lazy.cpp Normal file
View File

@@ -0,0 +1,74 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
using boost::enable_if;
using boost::enable_if_lazy;
using boost::is_arithmetic;
// This class provides a reduced example of a traits class for computing
// the result of multiplying two types. The exists constant is true when a
// multiplication operator exists between two types, and the type member is
// defined to the return type of this operator. The return type member is
// not defined when a multiplication operator does not exist.
template <class T, class U>
struct mult_traits {
BOOST_STATIC_CONSTANT(bool, exists = false);
};
template <>
struct mult_traits<int, int> {
BOOST_STATIC_CONSTANT(bool, exists = true);
typedef int type;
};
template <>
struct mult_traits<double, double> {
BOOST_STATIC_CONSTANT(bool, exists = true);
typedef double type;
};
// Next, a forwarding function mult() is defined. It is enabled only when
// mult_traits<T, U>::exists is true. The first version, using normal
// enable_if, works with only some compilers (it does not work in g++):
#if 0
template <class T, class U>
typename enable_if<
mult_traits<T, U>::exists,
typename mult_traits<T, U>::type>::type
mult(const T& x, const U& y) {return x * y;}
#endif
// An improved version that works with more compilers uses enable_if_lazy.
// This template removes compiler errors from invalid code used as an
// argument to enable_if.
template <class T, class U>
typename enable_if_lazy<
mult_traits<T, U>::exists,
mult_traits<T, U> >::type
mult(const T& x, const U& y) {return x * y;}
int test_main(int, char*[])
{
BOOST_TEST(mult(1, 2) == 2);
BOOST_TEST(mult(1.0, 3.0) == 3.0);
return 0;
}

View File

@@ -0,0 +1,42 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
using boost::enable_if;
using boost::disable_if;
using boost::is_arithmetic;
struct container {
template <class T>
typename enable_if<is_arithmetic<T>::value, bool>::type
arithmetic_object(const T&, const int* /* disambiguate */ = 0) {return true;}
template <class T>
typename disable_if<is_arithmetic<T>::value, bool>::type
arithmetic_object(const T&) {return false;}
};
int test_main(int, char*[])
{
BOOST_TEST(container().arithmetic_object(1));
BOOST_TEST(container().arithmetic_object(1.0));
BOOST_TEST(!container().arithmetic_object("1"));
BOOST_TEST(!container().arithmetic_object(static_cast<void*>(0)));
return 0;
}

View File

@@ -0,0 +1,44 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
using boost::enable_if;
using boost::is_arithmetic;
namespace A {
template<class T>
typename enable_if<is_arithmetic<T>::value, bool>::type
arithmetic_object(T t) { return true; }
}
namespace B {
template<class T>
typename enable_if<!is_arithmetic<T>::value, bool>::type
arithmetic_object(T t) { return false; }
}
int test_main(int, char*[])
{
using namespace A;
using namespace B;
BOOST_TEST(arithmetic_object(1));
BOOST_TEST(arithmetic_object(1.0));
BOOST_TEST(!arithmetic_object("1"));
BOOST_TEST(!arithmetic_object(static_cast<void*>(0)));
return 0;
}

View File

@@ -0,0 +1,40 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
using boost::enable_if;
using boost::is_arithmetic;
template<class T>
typename enable_if<is_arithmetic<T>::value, bool>::type
arithmetic_object(T t) { return true; }
template<class T>
typename enable_if<!is_arithmetic<T>::value, bool>::type
arithmetic_object(T t) { return false; }
int test_main(int, char*[])
{
BOOST_TEST(arithmetic_object(1));
BOOST_TEST(arithmetic_object(1.0));
BOOST_TEST(!arithmetic_object("1"));
BOOST_TEST(!arithmetic_object(static_cast<void*>(0)));
return 0;
}

View File

@@ -0,0 +1,45 @@
//
// Copyright 2003 <20> The Trustees of Indiana University. All rights
// reserved.
//
// See the file enable_if_LICENSE for licensing conditions.
//
// Authors: Jaakko J<>rvi (jajarvi@osl.iu.edu)
// Jeremiah Willcock (jewillco@osl.iu.edu)
// Andrew Lumsdaine (lums@osl.iu.edu)
//
#include <boost/test/minimal.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
using boost::enable_if;
using boost::disable_if;
using boost::is_arithmetic;
template <class T, class Enable = void>
struct tester;
template <class T>
struct tester<T, typename enable_if<is_arithmetic<T>::value>::type> {
BOOST_STATIC_CONSTANT(bool, value = true);
};
template <class T>
struct tester<T, typename disable_if<is_arithmetic<T>::value>::type> {
BOOST_STATIC_CONSTANT(bool, value = false);
};
int test_main(int, char*[])
{
BOOST_TEST(tester<int>::value);
BOOST_TEST(tester<double>::value);
BOOST_TEST(!tester<char*>::value);
BOOST_TEST(!tester<void*>::value);
return 0;
}