forked from boostorg/unordered
Some tests.
[SVN r2892]
This commit is contained in:
17
test/container/Jamfile.v2
Normal file
17
test/container/Jamfile.v2
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
# Copyright Daniel James 2005. Use, modification, and distribution are
|
||||
# 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)
|
||||
|
||||
import testing ;
|
||||
|
||||
project unordered-test/container
|
||||
: requirements
|
||||
<toolset>intel-linux:"<cxxflags>-strict_ansi -cxxlib-icc"
|
||||
;
|
||||
|
||||
test-suite container-tests
|
||||
:
|
||||
[ run set_compile.cpp ]
|
||||
[ run map_compile.cpp ]
|
||||
;
|
114
test/container/compile_tests.hpp
Normal file
114
test/container/compile_tests.hpp
Normal file
@ -0,0 +1,114 @@
|
||||
#include <boost/concept_check.hpp>
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/mpl/bool.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/iterator/iterator_traits.hpp>
|
||||
#include <boost/limits.hpp>
|
||||
#include "../helpers/check_return_type.hpp"
|
||||
|
||||
typedef long double comparison_type;
|
||||
|
||||
template <class T> void sink(T const&) {}
|
||||
|
||||
template <class X, class T>
|
||||
void container_test(X& r, T& value)
|
||||
{
|
||||
typedef typename X::iterator iterator;
|
||||
typedef typename X::const_iterator const_iterator;
|
||||
typedef typename X::difference_type difference_type;
|
||||
typedef typename X::size_type size_type;
|
||||
|
||||
typedef typename boost::iterator_value<iterator>::type iterator_value_type;
|
||||
typedef typename boost::iterator_value<const_iterator>::type const_iterator_value_type;
|
||||
typedef typename boost::iterator_difference<iterator>::type iterator_difference_type;
|
||||
typedef typename boost::iterator_difference<const_iterator>::type const_iterator_difference_type;
|
||||
|
||||
typedef typename X::value_type value_type;
|
||||
typedef typename X::reference reference;
|
||||
typedef typename X::const_reference const_reference;
|
||||
|
||||
// value_type
|
||||
|
||||
BOOST_MPL_ASSERT((boost::is_same<T, value_type>));
|
||||
boost::function_requires<boost::CopyConstructibleConcept<X> >();
|
||||
|
||||
// reference_type / const_reference_type
|
||||
|
||||
// TODO: 'lvalue of T'
|
||||
BOOST_MPL_ASSERT((boost::is_same<T&, reference>));
|
||||
// TODO: 'const lvalue of T'
|
||||
BOOST_MPL_ASSERT((boost::is_same<T const&, const_reference>));
|
||||
|
||||
// iterator
|
||||
|
||||
boost::function_requires<boost::InputIteratorConcept<iterator> >();
|
||||
BOOST_MPL_ASSERT((boost::is_same<T, iterator_value_type>));
|
||||
BOOST_MPL_ASSERT((boost::is_convertible<iterator, const_iterator>));
|
||||
|
||||
// const_iterator
|
||||
|
||||
// TODO: Test that it's a constant iterator?
|
||||
boost::function_requires<boost::InputIteratorConcept<const_iterator> >();
|
||||
BOOST_MPL_ASSERT((boost::is_same<T, const_iterator_value_type>));
|
||||
|
||||
// difference_type
|
||||
|
||||
BOOST_MPL_ASSERT((boost::mpl::bool_<
|
||||
std::numeric_limits<difference_type>::is_signed>));
|
||||
BOOST_MPL_ASSERT((boost::mpl::bool_<
|
||||
std::numeric_limits<difference_type>::is_integer>));
|
||||
BOOST_MPL_ASSERT((boost::is_same<difference_type,
|
||||
iterator_difference_type>));
|
||||
BOOST_MPL_ASSERT((boost::is_same<difference_type,
|
||||
const_iterator_difference_type>));
|
||||
|
||||
// size_type
|
||||
|
||||
BOOST_MPL_ASSERT_NOT((boost::mpl::bool_<
|
||||
std::numeric_limits<size_type>::is_signed>));
|
||||
BOOST_MPL_ASSERT((boost::mpl::bool_<
|
||||
std::numeric_limits<size_type>::is_integer>));
|
||||
|
||||
// size_type can represent any non-negative value type of difference_type
|
||||
// I'm not sure about either of these tests...
|
||||
size_type max_diff((std::numeric_limits<difference_type>::max)());
|
||||
difference_type converted_diff(max_diff);
|
||||
BOOST_TEST((std::numeric_limits<difference_type>::max)()
|
||||
== converted_diff);
|
||||
|
||||
BOOST_TEST(
|
||||
static_cast<comparison_type>(
|
||||
(std::numeric_limits<size_type>::max)()) >
|
||||
static_cast<comparison_type>(
|
||||
(std::numeric_limits<difference_type>::max)()));
|
||||
|
||||
// I don't test the runtime post-conditions here.
|
||||
X u;
|
||||
BOOST_TEST(u.size() == 0);
|
||||
BOOST_TEST(X().size() == 0);
|
||||
|
||||
X a,b;
|
||||
|
||||
sink(X(a));
|
||||
X u2(a);
|
||||
X u3 = a;
|
||||
|
||||
X* ptr = new X();
|
||||
X& a1 = *ptr;
|
||||
(&a1)->~X();
|
||||
|
||||
X const a_const;
|
||||
test::check_return_type<iterator>::equals(a.begin());
|
||||
test::check_return_type<const_iterator>::equals(a_const.begin());
|
||||
test::check_return_type<iterator>::equals(a.end());
|
||||
test::check_return_type<const_iterator>::equals(a_const.end());
|
||||
|
||||
// No tests for ==, != since they're not required for unordered containers.
|
||||
|
||||
a.swap(b);
|
||||
test::check_return_type<X>::equals_ref(r = a);
|
||||
test::check_return_type<size_type>::equals(a.size());
|
||||
test::check_return_type<size_type>::equals(a.max_size());
|
||||
test::check_return_type<bool>::convertible(a.empty());
|
||||
}
|
45
test/container/map_compile.cpp
Normal file
45
test/container/map_compile.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
|
||||
// Copyright Daniel James 2006. Use, modification, and distribution are
|
||||
// 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)
|
||||
|
||||
// This test creates the containers with members that meet their minimum
|
||||
// requirements. Makes sure everything compiles and is defined correctly.
|
||||
|
||||
#include <boost/unordered_map.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include "../objects/minimal.hpp"
|
||||
#include "./compile_tests.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::pair<test::minimal::assignable const,
|
||||
test::minimal::copy_constructible> value_type;
|
||||
value_type value(
|
||||
test::minimal::assignable::create(),
|
||||
test::minimal::copy_constructible::create());
|
||||
|
||||
std::cout<<"Test unordered_map.\n";
|
||||
boost::unordered_map<
|
||||
test::minimal::assignable,
|
||||
test::minimal::copy_constructible,
|
||||
test::minimal::hash<test::minimal::assignable>,
|
||||
test::minimal::equal_to<test::minimal::assignable>,
|
||||
test::minimal::allocator<value_type> > map;
|
||||
|
||||
container_test(map, value);
|
||||
|
||||
std::cout<<"Test unordered_multimap.\n";
|
||||
boost::unordered_multimap<
|
||||
test::minimal::assignable,
|
||||
test::minimal::copy_constructible,
|
||||
test::minimal::hash<test::minimal::assignable>,
|
||||
test::minimal::equal_to<test::minimal::assignable>,
|
||||
test::minimal::allocator<value_type> > multimap;
|
||||
|
||||
container_test(multimap, value);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
39
test/container/set_compile.cpp
Normal file
39
test/container/set_compile.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
|
||||
// Copyright Daniel James 2006. Use, modification, and distribution are
|
||||
// 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)
|
||||
|
||||
// This test creates the containers with members that meet their minimum
|
||||
// requirements. Makes sure everything compiles and is defined correctly.
|
||||
|
||||
#include <boost/unordered_set.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include "../objects/minimal.hpp"
|
||||
#include "./compile_tests.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
test::minimal::assignable assignable = test::minimal::assignable::create();
|
||||
|
||||
std::cout<<"Test unordered_set.\n";
|
||||
boost::unordered_set<
|
||||
test::minimal::assignable,
|
||||
test::minimal::hash<test::minimal::assignable>,
|
||||
test::minimal::equal_to<test::minimal::assignable>,
|
||||
test::minimal::allocator<test::minimal::assignable> > set;
|
||||
|
||||
container_test(set, assignable);
|
||||
|
||||
std::cout<<"Test unordered_multiset.\n";
|
||||
boost::unordered_multiset<
|
||||
test::minimal::assignable,
|
||||
test::minimal::hash<test::minimal::assignable>,
|
||||
test::minimal::equal_to<test::minimal::assignable>,
|
||||
test::minimal::allocator<test::minimal::assignable> > multiset;
|
||||
|
||||
container_test(multiset, assignable);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
38
test/helpers/check_return_type.hpp
Normal file
38
test/helpers/check_return_type.hpp
Normal file
@ -0,0 +1,38 @@
|
||||
|
||||
// Copyright Daniel James 2005-2006. Use, modification, and distribution are
|
||||
// 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)
|
||||
|
||||
#if !defined(BOOST_UNORDERED_TEST_HELPERS_CHECK_RETURN_TYPE_HEADER)
|
||||
#define BOOST_UNORDERED_TEST_HELPERS_CHECK_RETURN_TYPE_HEADER
|
||||
|
||||
#include <boost/mpl/assert.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
|
||||
namespace test
|
||||
{
|
||||
template <class T1>
|
||||
struct check_return_type
|
||||
{
|
||||
template <class T2>
|
||||
static void equals(T2)
|
||||
{
|
||||
BOOST_MPL_ASSERT((boost::is_same<T1, T2>));
|
||||
}
|
||||
|
||||
template <class T2>
|
||||
static void equals_ref(T2&)
|
||||
{
|
||||
BOOST_MPL_ASSERT((boost::is_same<T1, T2>));
|
||||
}
|
||||
|
||||
template <class T2>
|
||||
static void convertible(T2)
|
||||
{
|
||||
BOOST_MPL_ASSERT((boost::is_convertible<T2, T1>));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
245
test/objects/minimal.hpp
Normal file
245
test/objects/minimal.hpp
Normal file
@ -0,0 +1,245 @@
|
||||
|
||||
// Copyright Daniel James 2006. Use, modification, and distribution are
|
||||
// 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)
|
||||
|
||||
#if !defined(BOOST_UNORDERED_OBJECTS_MINIMAL_HEADER)
|
||||
#define BOOST_UNORDERED_OBJECTS_MINIMAL_HEADER
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace test
|
||||
{
|
||||
namespace minimal
|
||||
{
|
||||
class copy_constructible;
|
||||
class assignable;
|
||||
template <class T> class hash;
|
||||
template <class T> class equal_to;
|
||||
template <class T> class pointer;
|
||||
template <class T> class const_pointer;
|
||||
template <class T> class allocator;
|
||||
|
||||
class copy_constructible
|
||||
{
|
||||
public:
|
||||
static copy_constructible create() { return copy_constructible(); }
|
||||
copy_constructible(copy_constructible const&) {}
|
||||
~copy_constructible() {}
|
||||
private:
|
||||
copy_constructible& operator=(copy_constructible const&);
|
||||
copy_constructible() {}
|
||||
};
|
||||
|
||||
class assignable
|
||||
{
|
||||
public:
|
||||
static assignable create() { return assignable(); }
|
||||
assignable(assignable const&) {}
|
||||
assignable& operator=(assignable const&) { return *this; }
|
||||
~assignable() {}
|
||||
private:
|
||||
assignable() {}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class hash
|
||||
{
|
||||
public:
|
||||
static hash create() { return hash(); }
|
||||
// TODO: hash has to be default constructible for the default
|
||||
// parameters. Maybe use an alternative version for testing
|
||||
// other member functions.
|
||||
//
|
||||
// Or maybe it's required to be default constructible?
|
||||
// The Container requirements include a default constructor.
|
||||
hash() {}
|
||||
hash(hash const&) {}
|
||||
// TODO: Required to be assignable?
|
||||
hash& operator=(hash const&) { return *this; }
|
||||
~hash() {}
|
||||
|
||||
std::size_t operator()(T const& x) const { return 0; }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class equal_to
|
||||
{
|
||||
public:
|
||||
static equal_to create() { return equal_to(); }
|
||||
// TODO: equal_to has to be default constructible for the default
|
||||
// parameters. Maybe use an alternative version for testing
|
||||
// other member functions.
|
||||
//
|
||||
// Or maybe it's required to be default constructible?
|
||||
// The Container requirements include a default constructor.
|
||||
equal_to() {}
|
||||
equal_to(equal_to const&) {}
|
||||
// TODO: Required to be assignable?
|
||||
equal_to& operator=(equal_to const&) { return *this; }
|
||||
~equal_to() {}
|
||||
|
||||
bool operator()(T const& x, T const& y) const { return true; }
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class Ptr, class T>
|
||||
class pointer_base
|
||||
{
|
||||
protected:
|
||||
pointer_base() : ptr_(0) {}
|
||||
explicit pointer_base(T* ptr) : ptr_(ptr) {}
|
||||
~pointer_base() {}
|
||||
Ptr& get() { return *static_cast<Ptr*>(this); }
|
||||
T* ptr_;
|
||||
public:
|
||||
typedef void (pointer_base::*bool_type)() const;
|
||||
void this_type_does_not_support_comparisons() const {}
|
||||
|
||||
T& operator*() const { return *ptr_; }
|
||||
T* operator->() const { return ptr_; }
|
||||
Ptr& operator++() { ++ptr_; return get(); }
|
||||
Ptr operator++(int) { Ptr tmp(get()); ++ptr_; return tmp; }
|
||||
Ptr operator+(int s) const { return Ptr(ptr_ + s); }
|
||||
T& operator[](int s) const { return ptr_[s]; }
|
||||
|
||||
operator bool_type() const {
|
||||
return ptr_ ?
|
||||
&pointer_base::this_type_does_not_support_comparisons
|
||||
: 0;
|
||||
}
|
||||
|
||||
bool operator!() const { return !ptr_; }
|
||||
bool operator==(Ptr const& x) const { return ptr_ == x.ptr_; }
|
||||
bool operator!=(Ptr const& x) const { return ptr_ != x.ptr_; }
|
||||
bool operator<(Ptr const& x) const { return ptr_ < x.ptr_; }
|
||||
bool operator>(Ptr const& x) const { return ptr_ > x.ptr_; }
|
||||
bool operator<=(Ptr const& x) const { return ptr_ <= x.ptr_; }
|
||||
bool operator>=(Ptr const& x) const { return ptr_ >= x.ptr_; }
|
||||
};
|
||||
}
|
||||
|
||||
template <class T> class pointer;
|
||||
template <class T> class const_pointer;
|
||||
|
||||
template <class T>
|
||||
class pointer : public detail::pointer_base<pointer<T>, T>
|
||||
{
|
||||
friend class allocator<T>;
|
||||
friend class detail::pointer_base<pointer<T>, T>;
|
||||
friend class const_pointer<T>;
|
||||
typedef detail::pointer_base<pointer<T>, T> base;
|
||||
|
||||
pointer(T* ptr) : base(ptr) {}
|
||||
|
||||
public:
|
||||
pointer() : base() {}
|
||||
|
||||
bool operator==(pointer const& x) const { return base::operator==(x); }
|
||||
bool operator!=(pointer const& x) const { return base::operator!=(x); }
|
||||
bool operator<(pointer const& x) const { return base::operator<(x);}
|
||||
bool operator>(pointer const& x) const { return base::operator>(x);}
|
||||
bool operator<=(pointer const& x) const { return base::operator<=(x);}
|
||||
bool operator>=(pointer const& x) const { return base::operator<=(x);}
|
||||
|
||||
bool operator==(const_pointer<T> const& x) const { return x == *this; }
|
||||
bool operator!=(const_pointer<T> const& x) const { return x != *this; }
|
||||
bool operator<(const_pointer<T> const& x) const { return x > *this; }
|
||||
bool operator>(const_pointer<T> const& x) const { return x < *this; }
|
||||
bool operator<=(const_pointer<T> const& x) const { return x >= *this; }
|
||||
bool operator>=(const_pointer<T> const& x) const { return x <= *this; }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class const_pointer : public detail::pointer_base<const_pointer<T>, T const>
|
||||
{
|
||||
friend class allocator<T>;
|
||||
friend class detail::pointer_base<const_pointer<T>, T const>;
|
||||
typedef detail::pointer_base<const_pointer<T>, T const> base;
|
||||
|
||||
const_pointer(T* ptr) : base(ptr) {}
|
||||
|
||||
public:
|
||||
const_pointer() : base() {}
|
||||
const_pointer(pointer<T> const& x) : base(x.ptr_) {}
|
||||
|
||||
bool operator==(const_pointer const& x) const { return base::operator==(x); }
|
||||
bool operator!=(const_pointer const& x) const { return base::operator!=(x); }
|
||||
bool operator<(const_pointer const& x) const { return base::operator<(x);}
|
||||
bool operator>(const_pointer const& x) const { return base::operator>(x);}
|
||||
bool operator<=(const_pointer const& x) const { return base::operator<=(x);}
|
||||
bool operator>=(const_pointer const& x) const { return base::operator<=(x);}
|
||||
|
||||
bool operator==(pointer<T> const& x) const { return operator==(const_pointer(x)); }
|
||||
bool operator!=(pointer<T> const& x) const { return operator!=(const_pointer(x)); }
|
||||
bool operator<(pointer<T> const& x) const { return operator<(const_pointer(x));}
|
||||
bool operator>(pointer<T> const& x) const { return operator>(const_pointer(x));}
|
||||
bool operator<=(pointer<T> const& x) const { return operator<=(const_pointer(x));}
|
||||
bool operator>=(pointer<T> const& x) const { return operator<=(const_pointer(x));}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class allocator
|
||||
{
|
||||
public:
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef pointer<T> pointer;
|
||||
typedef const_pointer<T> const_pointer;
|
||||
typedef T& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef T value_type;
|
||||
|
||||
template <class U> struct rebind { typedef allocator<U> other; };
|
||||
|
||||
allocator() {}
|
||||
allocator(allocator const&) {}
|
||||
template <class Y> allocator(allocator<Y> const&) {}
|
||||
~allocator() {}
|
||||
|
||||
pointer address(reference r) { return pointer(&r); }
|
||||
const_pointer address(const_reference r) { return const_pointer(&r); }
|
||||
|
||||
pointer allocate(size_type n) {
|
||||
return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
|
||||
}
|
||||
|
||||
pointer allocate(size_type n, const_pointer u)
|
||||
{
|
||||
return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
|
||||
}
|
||||
|
||||
void deallocate(pointer p, size_type n)
|
||||
{
|
||||
::operator delete((void*) p.ptr_);
|
||||
}
|
||||
|
||||
void construct(pointer p, T const& t) { new((void*)p.ptr_) T(t); }
|
||||
void destroy(pointer p) { ((T*)p.ptr_)->~T(); }
|
||||
|
||||
size_type max_size() const { return 1000; }
|
||||
private:
|
||||
allocator& operator=(allocator const&);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline bool operator==(allocator<T> const& x, allocator<T> const& y)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline bool operator!=(allocator<T> const& x, allocator<T> const& y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void swap(allocator<T>& x, allocator<T>& y)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user