| 1 | /* |
| 2 | Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2017 |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See |
| 5 | accompanying file LICENSE_1_0.txt or copy at |
| 6 | http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | See http://www.boost.org/ for latest version. |
| 9 | */ |
| 10 | |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <boost/algorithm/apply_permutation.hpp> |
| 14 | |
| 15 | #define BOOST_TEST_MAIN |
| 16 | |
| 17 | #include <boost/test/included/unit_test.hpp> |
| 18 | |
| 19 | namespace ba = boost::algorithm; |
| 20 | |
| 21 | |
| 22 | BOOST_AUTO_TEST_CASE(test_apply_permutation) |
| 23 | { |
| 24 | //Empty |
| 25 | { |
| 26 | std::vector<int> vec, order, result; |
| 27 | |
| 28 | ba::apply_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 29 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 30 | } |
| 31 | //1 element |
| 32 | { |
| 33 | std::vector<int> vec, order, result; |
| 34 | vec.push_back(x: 1); |
| 35 | order.push_back(x: 0); |
| 36 | result = vec; |
| 37 | |
| 38 | ba::apply_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 39 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 40 | } |
| 41 | //2 elements, no changes |
| 42 | { |
| 43 | std::vector<int> vec, order, result; |
| 44 | vec.push_back(x: 1); vec.push_back(x: 2); |
| 45 | order.push_back(x: 0); order.push_back(x: 1); |
| 46 | result = vec; |
| 47 | |
| 48 | ba::apply_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 49 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 50 | } |
| 51 | //2 elements, changed |
| 52 | { |
| 53 | std::vector<int> vec, order, result; |
| 54 | vec.push_back(x: 1); vec.push_back(x: 2); |
| 55 | order.push_back(x: 1); order.push_back(x: 0); |
| 56 | result.push_back(x: 2); result.push_back(x: 1); |
| 57 | |
| 58 | ba::apply_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 59 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 60 | } |
| 61 | //Multiple elements, no changes |
| 62 | { |
| 63 | std::vector<int> vec, order, result; |
| 64 | vec.push_back(x: 1); vec.push_back(x: 2); vec.push_back(x: 3); vec.push_back(x: 4); vec.push_back(x: 5); |
| 65 | order.push_back(x: 0); order.push_back(x: 1); order.push_back(x: 2); order.push_back(x: 3); order.push_back(x: 4); |
| 66 | result = vec; |
| 67 | |
| 68 | ba::apply_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 69 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 70 | } |
| 71 | //Multiple elements, changed |
| 72 | { |
| 73 | std::vector<int> vec, order, result; |
| 74 | vec.push_back(x: 1); vec.push_back(x: 2); vec.push_back(x: 3); vec.push_back(x: 4); vec.push_back(x: 5); |
| 75 | order.push_back(x: 4); order.push_back(x: 3); order.push_back(x: 2); order.push_back(x: 1); order.push_back(x: 0); |
| 76 | result.push_back(x: 5); result.push_back(x: 4); result.push_back(x: 3); result.push_back(x: 2); result.push_back(x: 1); |
| 77 | |
| 78 | ba::apply_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 79 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 80 | } |
| 81 | //Just test range interface |
| 82 | { |
| 83 | std::vector<int> vec, order, result; |
| 84 | vec.push_back(x: 1); vec.push_back(x: 2); vec.push_back(x: 3); vec.push_back(x: 4); vec.push_back(x: 5); |
| 85 | order.push_back(x: 0); order.push_back(x: 1); order.push_back(x: 2); order.push_back(x: 3); order.push_back(x: 4); |
| 86 | result = vec; |
| 87 | |
| 88 | ba::apply_permutation(item_range&: vec, ind_range&: order); |
| 89 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE(test_apply_reverse_permutation) |
| 94 | { |
| 95 | //Empty |
| 96 | { |
| 97 | std::vector<int> vec, order, result; |
| 98 | |
| 99 | ba::apply_reverse_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 100 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 101 | } |
| 102 | //1 element |
| 103 | { |
| 104 | std::vector<int> vec, order, result; |
| 105 | vec.push_back(x: 1); |
| 106 | order.push_back(x: 0); |
| 107 | result = vec; |
| 108 | |
| 109 | ba::apply_reverse_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 110 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 111 | } |
| 112 | //2 elements, no changes |
| 113 | { |
| 114 | std::vector<int> vec, order, result; |
| 115 | vec.push_back(x: 1); vec.push_back(x: 2); |
| 116 | order.push_back(x: 0); order.push_back(x: 1); |
| 117 | result = vec; |
| 118 | |
| 119 | ba::apply_reverse_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 120 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 121 | } |
| 122 | //2 elements, changed |
| 123 | { |
| 124 | std::vector<int> vec, order, result; |
| 125 | vec.push_back(x: 1); vec.push_back(x: 2); |
| 126 | order.push_back(x: 1); order.push_back(x: 0); |
| 127 | result.push_back(x: 2); result.push_back(x: 1); |
| 128 | |
| 129 | ba::apply_reverse_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 130 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 131 | } |
| 132 | //Multiple elements, no changes |
| 133 | { |
| 134 | std::vector<int> vec, order, result; |
| 135 | vec.push_back(x: 1); vec.push_back(x: 2); vec.push_back(x: 3); vec.push_back(x: 4); vec.push_back(x: 5); |
| 136 | order.push_back(x: 0); order.push_back(x: 1); order.push_back(x: 2); order.push_back(x: 3); order.push_back(x: 4); |
| 137 | result = vec; |
| 138 | |
| 139 | ba::apply_reverse_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 140 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 141 | } |
| 142 | //Multiple elements, changed |
| 143 | { |
| 144 | std::vector<int> vec, order, result; |
| 145 | vec.push_back(x: 1); vec.push_back(x: 2); vec.push_back(x: 3); vec.push_back(x: 4); vec.push_back(x: 5); |
| 146 | order.push_back(x: 4); order.push_back(x: 3); order.push_back(x: 2); order.push_back(x: 1); order.push_back(x: 0); |
| 147 | result.push_back(x: 5); result.push_back(x: 4); result.push_back(x: 3); result.push_back(x: 2); result.push_back(x: 1); |
| 148 | |
| 149 | ba::apply_reverse_permutation(item_begin: vec.begin(), item_end: vec.end(), ind_begin: order.begin(), ind_end: order.end()); |
| 150 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 151 | } |
| 152 | //Just test range interface |
| 153 | { |
| 154 | std::vector<int> vec, order, result; |
| 155 | vec.push_back(x: 1); vec.push_back(x: 2); vec.push_back(x: 3); vec.push_back(x: 4); vec.push_back(x: 5); |
| 156 | order.push_back(x: 0); order.push_back(x: 1); order.push_back(x: 2); order.push_back(x: 3); order.push_back(x: 4); |
| 157 | result = vec; |
| 158 | |
| 159 | ba::apply_reverse_permutation(item_range&: vec, ind_range&: order); |
| 160 | BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), result.begin(), result.end()); |
| 161 | } |
| 162 | } |
| 163 | |