From b31cf6c8f4af0a19b7346290c934552c21587bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Wed, 11 Apr 2012 16:02:19 +0000 Subject: [PATCH] Added test for containers using scoped allocator [SVN r77915] --- proj/vc7ide/container.sln | 8 + .../vc7ide/scoped_allocator_usage_test.vcproj | 139 ++++++++++++++++++ test/scoped_allocator_usage_test.cpp | 95 ++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 proj/vc7ide/scoped_allocator_usage_test.vcproj create mode 100644 test/scoped_allocator_usage_test.cpp diff --git a/proj/vc7ide/container.sln b/proj/vc7ide/container.sln index 7bc2ee8..591f0fc 100644 --- a/proj/vc7ide/container.sln +++ b/proj/vc7ide/container.sln @@ -47,6 +47,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "scoped_allocator_adaptor_te ProjectSection(ProjectDependencies) = postProject EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "scoped_allocator_usage_test", "scoped_allocator_usage_test.vcproj", "{B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject Global GlobalSection(SolutionConfiguration) = preSolution Debug = Debug @@ -103,6 +107,10 @@ Global {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Debug.Build.0 = Debug|Win32 {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Release.ActiveCfg = Release|Win32 {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Release.Build.0 = Release|Win32 + {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Debug.ActiveCfg = Debug|Win32 + {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Debug.Build.0 = Debug|Win32 + {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Release.ActiveCfg = Release|Win32 + {B4E9FB12-7D7C-4461-83A9-5EB2C78E608F}.Release.Build.0 = Release|Win32 EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EndGlobalSection diff --git a/proj/vc7ide/scoped_allocator_usage_test.vcproj b/proj/vc7ide/scoped_allocator_usage_test.vcproj new file mode 100644 index 0000000..a19b214 --- /dev/null +++ b/proj/vc7ide/scoped_allocator_usage_test.vcproj @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/scoped_allocator_usage_test.cpp b/test/scoped_allocator_usage_test.cpp new file mode 100644 index 0000000..e7af116 --- /dev/null +++ b/test/scoped_allocator_usage_test.cpp @@ -0,0 +1,95 @@ +#include +#include + +#include +#include +#include + +template +class SimpleAllocator +{ +public: + typedef Ty value_type; + typedef typename std::allocator::pointer pointer; + typedef typename std::allocator::size_type size_type; + + SimpleAllocator(int value) + : _value(value) + {} + + template + SimpleAllocator(const SimpleAllocator &other) + : _value(other._value) + {} + + pointer allocate(size_type n) + { + return _allocator.allocate(n); + } + void deallocate(pointer p, size_type n) + { + _allocator.deallocate(p, n); + } +private: + int _value; + std::allocator _allocator; + + template friend class SimpleAllocator; +}; + +template +class ScopedAllocator : public boost::container::scoped_allocator_adaptor > +{ +private: + typedef boost::container::scoped_allocator_adaptor > Base; + +public: + ScopedAllocator(int value) + : Base(SimpleAllocator(value)) + {} +}; + +class Resource +{ +private: // Not copyable + Resource(const Resource &); + Resource &operator=(const Resource &); +public: + typedef SimpleAllocator allocator_type; + + Resource(BOOST_RV_REF(Resource)other) + : _value(other._value), _allocator(boost::move(other._allocator)) + { + other._value = -1; + } + + Resource(BOOST_RV_REF(Resource)other, const allocator_type &allocator) + : _value(other._value), _allocator(allocator) + { + other._value = -1; + } + + Resource(int value, const allocator_type &allocator) + : _value(value), _allocator(allocator) + {} +private: + int _value; + allocator_type _allocator; +}; + +typedef std::pair MapNode; + +typedef boost::container::scoped_allocator_adaptor > MapAllocator; + +typedef boost::container::map, MapAllocator> Map; + +int main() +{ + Map map1(std::less(), SimpleAllocator(5)); + + map1.emplace("foo", 42); + map1.emplace("bar", 11); + + //Map map2 = map1; + return 0; +}