From 6caf6a9def9e73371bf3228b5e5c740c9e172be1 Mon Sep 17 00:00:00 2001
From: CromwellEnage <32967088+CromwellEnage@users.noreply.github.com>
Date: Wed, 19 Sep 2018 13:24:01 -0400
Subject: [PATCH] Add test for allocators
Program by Glen Fernandes tests out support for stateful and minimal allocators.
---
doc/test_cases.html | 5 ++
test/Jamfile.v2 | 1 +
test/allocators.cpp | 183 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 189 insertions(+)
create mode 100755 test/allocators.cpp
diff --git a/doc/test_cases.html b/doc/test_cases.html
index f0ad9d36..a3b0c59b 100644
--- a/doc/test_cases.html
+++ b/doc/test_cases.html
@@ -114,6 +114,11 @@
| libs/multi_array/test/storage_order_convert.cpp |
diff --git a/test/Jamfile.v2 b/test/Jamfile.v2
index 0170da7b..107c679e 100644
--- a/test/Jamfile.v2
+++ b/test/Jamfile.v2
@@ -37,6 +37,7 @@ test-suite multi_array
[ run assign.cpp ../../test/build//boost_test_exec_monitor ]
[ run assign_to_array.cpp ../../test/build//boost_test_exec_monitor ]
[ run index_bases.cpp ../../test/build//boost_test_exec_monitor ]
+ [ run allocators.cpp ]
[ run storage_order_convert.cpp ../../test/build//boost_test_exec_monitor ]
[ run storage_order.cpp ../../test/build//boost_test_exec_monitor ]
[ run reshape.cpp ../../test/build//boost_test_exec_monitor ]
diff --git a/test/allocators.cpp b/test/allocators.cpp
new file mode 100755
index 00000000..b6cef969
--- /dev/null
+++ b/test/allocators.cpp
@@ -0,0 +1,183 @@
+// Copyright 2014 Glen Joseph Fernandes.
+// glenfe at live dot com
+
+// 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)
+
+// Boost.MultiArray Library
+// Authors: Ronald Garcia
+// Jeremy Siek
+// Andrew Lumsdaine
+// See http://www.boost.org/libs/multi_array for documentation.
+
+//
+// allocators.cpp - testing the code for allocators
+//
+
+#include
+#include
+#include
+#include
+#include
+
+template
+class stateful : public std::allocator
+{
+ public:
+ template
+ struct rebind
+ {
+ typedef stateful other;
+ };
+
+ stateful(int value) : state(value)
+ {
+ }
+
+ template
+ stateful(stateful const& other) : state(other.state)
+ {
+ }
+
+ int state;
+};
+
+template
+bool operator==(stateful const& a, stateful const& b)
+{
+ return a.state == b.state;
+}
+
+template
+bool operator!=(stateful const& a, stateful const& b)
+{
+ return !(a == b);
+}
+
+#if !defined(BOOST_NO_CXX11_ALLOCATOR)
+template
+class minimal
+{
+ public:
+ typedef T value_type;
+
+#if defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
+ minimal()
+ {
+ }
+#else
+ minimal() = default;
+#endif
+
+ template
+ minimal(minimal const&)
+ {
+ }
+
+ T* allocate(std::size_t size)
+ {
+ void* p = ::operator new(size * sizeof(T));
+ return static_cast(p);
+ }
+
+ void deallocate(T* ptr, std::size_t)
+ {
+ void* p = ptr;
+ ::operator delete(p);
+ }
+};
+
+template
+bool operator==(minimal const& a, minimal const& b)
+{
+ return true;
+}
+
+template
+bool operator!=(minimal const& a, minimal const& b)
+{
+ return !(a == b);
+}
+#endif // BOOST_NO_CXX11_ALLOCATOR
+
+void check_shape(double const&, std::size_t*, int*, unsigned long)
+{
+}
+
+template
+void
+ check_shape(
+ T const& data
+ , std::size_t* sizes
+ , int* strides
+ , unsigned long elements
+ )
+{
+ BOOST_TEST(data.num_elements() == elements);
+ BOOST_TEST(data.size() == *sizes);
+ BOOST_TEST((
+ std::equal(sizes, sizes + data.num_dimensions(), data.shape())
+ ));
+ BOOST_TEST((
+ std::equal(strides, strides + data.num_dimensions(), data.strides())
+ ));
+ check_shape(data[0], ++sizes, ++strides, elements / data.size());
+}
+
+int main(int, char*[])
+{
+ {
+ int strides[] = {9, 3, 1};
+ boost::array<
+ boost::multi_array::size_type
+ , 3
+ > sizes = {{3, 3, 3}};
+ boost::multi_array<
+ double
+ , 3
+ , stateful
+ >::size_type num_elements = 27;
+ boost::multi_array<
+ double
+ , 3
+ , stateful
+ >::extent_gen extents;
+ stateful alloc(5);
+ boost::multi_array<
+ double
+ , 3
+ , stateful
+ > data(extents[3][3][3], alloc);
+ check_shape(data, &sizes[0], strides, num_elements);
+ }
+
+#if !defined(BOOST_NO_CXX11_ALLOCATOR)
+ {
+ int strides[] = {9, 3, 1};
+ boost::array<
+ boost::multi_array::size_type
+ , 3
+ > sizes = {{3, 3, 3}};
+ boost::multi_array<
+ double
+ , 3
+ , minimal
+ >::size_type num_elements = 27;
+ boost::multi_array<
+ double
+ , 3
+ , minimal
+ >::extent_gen extents;
+ boost::multi_array<
+ double
+ , 3
+ , minimal
+ > data(extents[3][3][3]);
+ check_shape(data, &sizes[0], strides, num_elements);
+ }
+#endif // BOOST_NO_CXX11_ALLOCATOR
+
+ return boost::report_errors();
+}
+
|