Skip to content

Commit

Permalink
swap test
Browse files Browse the repository at this point in the history
  • Loading branch information
alfC committed Jul 15, 2024
1 parent 5871fd2 commit 21d6eda
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 12 deletions.
5 changes: 2 additions & 3 deletions include/boost/multi/adaptors/blas/test/swap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// -*-indent-tabs-mode:t;c-basic-offset:4;tab-width:4;autowrap:nil;-*-
// Copyright 2019-2023 Alfredo A. Correa
// Copyright 2019-2024 Alfredo A. Correa

#define BOOST_TEST_MODULE "C++ Unit Tests for Multi BLAS swap"
#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -50,7 +49,7 @@ BOOST_AUTO_TEST_CASE(lapack_potrf, *boost::unit_test::tolerance(0.00001)) {
}
{
using complex = std::complex<double>;
complex const I{0, 1};
complex const I{0, 1};
multi::array<complex, 2> A = {
{1.0 + 2. * I, 2.0, 3.0, 4.0 + 3.0 * I},
{ 5.0, 6.0, 7.0, 8.0},
Expand Down
18 changes: 9 additions & 9 deletions include/boost/multi/array_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,20 +1779,20 @@ class subarray : public const_subarray<T, D, ElementPtr, Layout> {
}
friend constexpr void swap(subarray&& self, subarray&& other) noexcept { std::move(self).swap(std::move(other)); }

template<class A, typename = std::enable_if_t<!std::is_base_of_v<subarray, std::decay_t<A>>>> friend constexpr void swap(subarray&& self, A&& other) noexcept { std::move(self).swap(std::forward<A>(other)); }
template<class A, typename = std::enable_if_t<!std::is_base_of_v<subarray, std::decay_t<A>>>> friend constexpr void swap(A&& other, subarray&& self) noexcept { std::move(self).swap(std::forward<A>(other)); }
// template<class A, typename = std::enable_if_t<!std::is_base_of_v<subarray, std::decay_t<A>>>> friend constexpr void swap(subarray&& self, A&& other) noexcept { std::move(self).swap(std::forward<A>(other)); }
// template<class A, typename = std::enable_if_t<!std::is_base_of_v<subarray, std::decay_t<A>>>> friend constexpr void swap(A&& other, subarray&& self) noexcept { std::move(self).swap(std::forward<A>(other)); }

template<class Array> constexpr void swap(Array&& other) && noexcept {
assert( std::move(*this).extension() == other.extension() ); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) : normal in a constexpr function
this->elements().swap(std::forward<Array>(other).elements());
// adl_swap_ranges(this->begin(), this->end(), adl_begin(std::forward<Array>(o)));
}
template<class A> constexpr void swap(A&& other) & noexcept {return swap(std::forward<A>(other));}
// template<class Array> constexpr void swap(Array&& other) && noexcept {
// assert( std::move(*this).extension() == other.extension() ); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) : normal in a constexpr function
// this->elements().swap(std::forward<Array>(other).elements());
// // adl_swap_ranges(this->begin(), this->end(), adl_begin(std::forward<Array>(o)));
// }
// template<class A> constexpr void swap(A&& other) & noexcept {return swap(std::forward<A>(other));}

// friend constexpr void swap(subarray&& self, subarray&& other) noexcept {std::move(self).swap(std::move(other));}

// template<class Array> constexpr void swap(subarray& self, Array&& other) noexcept {self.swap(std::forward<Array>(other));} // TODO(correaa) remove
template<class Array> constexpr void swap(Array&& other, subarray& self) noexcept {self.swap(std::forward<Array>(other));}
// template<class Array> constexpr void swap(Array&& other, subarray& self) noexcept {self.swap(std::forward<Array>(other));}

template<
class Range,
Expand Down
124 changes: 124 additions & 0 deletions test/swap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2020-2024 Alfredo A. Correa
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 10.
// https://www.boost.org/LICENSE_1_0.txt

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wundef"
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wsign-conversion"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wundef"
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif

#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MAIN
#endif

#include <boost/test/unit_test.hpp>

#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

#include <boost/multi/array.hpp> // for array, apply, array_types<>::ele...

#include <algorithm> // for copy, equal, fill_n, move
#include <iterator> // for size, back_insert_iterator, back...
#include <memory> // for unique_ptr, make_unique, allocat...
// IWYU pragma: no_include <type_traits> // for remove_reference<>::type
// IWYU pragma: no_include <map>
#include <utility> // for move
#include <vector> // for vector, operator==, vector<>::va...

namespace multi = boost::multi;

BOOST_AUTO_TEST_CASE(swap_array_1D) {
multi::array<int, 1> arr1 = { 0, 1, 2, 3};
multi::array<int, 1> arr2 = {100, 101, 102};

using std::swap;
swap(arr1, arr2);

BOOST_TEST( arr1[1] == 101 );
BOOST_TEST( arr2[1] == 1 );
}

BOOST_AUTO_TEST_CASE(swap_array_2D) {
multi::array<int, 2> arr1 = {
{00, 01, 02, 03},
{10, 11, 12, 13},
{20, 21, 22, 23},
};

multi::array<int, 2> arr2 = {
{100, 101, 102, 103},
{110, 111, 112, 113},
};

using std::swap;
swap(arr1, arr2);

BOOST_TEST( arr1[1][1] == 111 );
BOOST_TEST( arr2[1][1] == 11 );
}

BOOST_AUTO_TEST_CASE(swap_subarray_1D) {
multi::array<int, 1> arr1 = { 0, 1, 2, 3};
multi::array<int, 1> arr2 = {100, 101, 102, 103};

using std::swap;
swap(arr1(), arr2());

BOOST_TEST( arr1[1] == 101 );
BOOST_TEST( arr2[1] == 1 );
}

BOOST_AUTO_TEST_CASE(swap_subarray_2D) {
multi::array<int, 2> arr1 = {
{00, 01, 02, 03},
{10, 11, 12, 13},
{20, 21, 22, 23},
};

multi::array<int, 2> arr2 = {
{100, 101, 102, 103},
{110, 111, 112, 113},
{120, 121, 122, 123},
};

using std::swap;
swap(arr1(), arr2());

BOOST_TEST( arr1[1][1] == 111 );
BOOST_TEST( arr2[1][1] == 11 );
}

BOOST_AUTO_TEST_CASE(swap_const_subarray_2D) {
multi::array<int, 2> const arr1 = {
{00, 01, 02, 03},
{10, 11, 12, 13},
{20, 21, 22, 23},
};

multi::array<int, 2> arr2 = {
{100, 101, 102, 103},
{110, 111, 112, 113},
{120, 121, 122, 123},
};

// using std::swap;
// swap(arr1(), arr2());

BOOST_TEST( arr1[1][1] == 11 );
BOOST_TEST( arr2[1][1] == 111 );
}

0 comments on commit 21d6eda

Please sign in to comment.