|
| 1 | +#include <algorithm> |
| 2 | +#include <cassert> |
| 3 | +#include <cmath> |
| 4 | +#include <numeric> |
| 5 | +#include <vector> |
| 6 | +#include <catch.hpp> |
| 7 | + |
| 8 | +#include "DataFormats/Common/interface/MultiSpan.h" |
| 9 | + |
| 10 | +TEST_CASE("MultiSpan basic indexing", "[MultiSpan]") { |
| 11 | + edm::MultiSpan<int> emptyMultiSpan; |
| 12 | + edm::MultiSpan<int> ms; |
| 13 | + |
| 14 | + edm::MultiSpan<int> ms1; // MultiSpan with empty span as first span |
| 15 | + edm::MultiSpan<int> ms2; // MultiSpan with several empty spans |
| 16 | + edm::MultiSpan<int> ms3; // MultiSpan with empty span as last span |
| 17 | + |
| 18 | + std::vector<int> a = {1, 2, 3}; |
| 19 | + std::vector<int> b = {4, 5}; |
| 20 | + std::vector<int> c; |
| 21 | + |
| 22 | + ms.add(a); |
| 23 | + ms.add(b); |
| 24 | + |
| 25 | + ms1.add(c); |
| 26 | + ms1.add(b); |
| 27 | + ms1.add(a); |
| 28 | + |
| 29 | + ms2.add(b); |
| 30 | + ms2.add(c); |
| 31 | + ms2.add(c); |
| 32 | + ms2.add(c); |
| 33 | + ms2.add(b); |
| 34 | + ms2.add(a); |
| 35 | + ms2.add(c); |
| 36 | + |
| 37 | + ms3.add(a); |
| 38 | + ms3.add(c); |
| 39 | + |
| 40 | + using ElementType = decltype(ms[0]); |
| 41 | + // Check that the const-correctness of the MultiSpan |
| 42 | + static_assert(!std::is_assignable<ElementType, int>::value, |
| 43 | + "It should not be possible to assign to an element of MultiSpan; See PR #48826"); |
| 44 | + |
| 45 | + SECTION("Empty MultiSpan") { |
| 46 | + REQUIRE(emptyMultiSpan.size() == 0); |
| 47 | + REQUIRE(emptyMultiSpan.begin() == emptyMultiSpan.end()); |
| 48 | + REQUIRE_THROWS_AS(emptyMultiSpan[0], std::out_of_range); |
| 49 | + REQUIRE_THROWS_AS(emptyMultiSpan.globalIndex(0, 0), std::out_of_range); |
| 50 | + } |
| 51 | + |
| 52 | + SECTION("Size is correct") { REQUIRE(ms.size() == 5); } |
| 53 | + |
| 54 | + SECTION("Range check") { |
| 55 | + REQUIRE_THROWS_AS(ms[5], std::out_of_range); |
| 56 | + REQUIRE_THROWS_AS(ms.globalIndex(2, 0), std::out_of_range); |
| 57 | + REQUIRE_THROWS_AS(ms.globalIndex(1, 2), std::out_of_range); |
| 58 | + REQUIRE_THROWS_AS(ms.spanAndLocalIndex(5), std::out_of_range); |
| 59 | + } |
| 60 | + |
| 61 | + SECTION("Indexing returns correct values") { |
| 62 | + REQUIRE(ms[0] == 1); |
| 63 | + REQUIRE(ms[1] == 2); |
| 64 | + REQUIRE(ms[2] == 3); |
| 65 | + REQUIRE(ms[3] == 4); |
| 66 | + REQUIRE(ms[4] == 5); |
| 67 | + } |
| 68 | + |
| 69 | + SECTION("Global index from span index and local index") { |
| 70 | + REQUIRE(ms.globalIndex(0, 0) == 0); |
| 71 | + REQUIRE(ms.globalIndex(0, 2) == 2); |
| 72 | + REQUIRE(ms.globalIndex(1, 0) == 3); |
| 73 | + REQUIRE(ms.globalIndex(1, 1) == 4); |
| 74 | + } |
| 75 | + |
| 76 | + SECTION("Span and local index from global index") { |
| 77 | + auto [span0, local0] = ms.spanAndLocalIndex(0); |
| 78 | + REQUIRE(span0 == 0); |
| 79 | + REQUIRE(local0 == 0); |
| 80 | + |
| 81 | + auto [span1, local1] = ms.spanAndLocalIndex(4); |
| 82 | + REQUIRE(span1 == 1); |
| 83 | + REQUIRE(local1 == 1); |
| 84 | + } |
| 85 | + |
| 86 | + SECTION("Iterators work with range-based for") { |
| 87 | + std::vector<int> collected; |
| 88 | + for (auto val : ms) { |
| 89 | + collected.push_back(val); |
| 90 | + } |
| 91 | + REQUIRE(collected == std::vector<int>{1, 2, 3, 4, 5}); |
| 92 | + } |
| 93 | + |
| 94 | + SECTION("Random access iterator supports arithmetic") { |
| 95 | + auto it = ms.begin(); |
| 96 | + REQUIRE(*(it + 2) == 3); |
| 97 | + REQUIRE(*(ms.end() - 2) == 4); |
| 98 | + REQUIRE((ms.end() - ms.begin()) == 5); |
| 99 | + } |
| 100 | + |
| 101 | + SECTION("std::find works") { |
| 102 | + auto it = std::find(ms.begin(), ms.end(), 4); |
| 103 | + REQUIRE(it != ms.end()); |
| 104 | + REQUIRE(*it == 4); |
| 105 | + REQUIRE((it - ms.begin()) == 3); |
| 106 | + } |
| 107 | + |
| 108 | + SECTION("std::distance returns correct result") { |
| 109 | + auto dist = std::distance(ms.begin(), ms.end()); |
| 110 | + REQUIRE(dist == 5); |
| 111 | + } |
| 112 | + |
| 113 | + SECTION("std::copy copies all values") { |
| 114 | + std::vector<int> out(5); |
| 115 | + std::copy(ms.begin(), ms.end(), out.begin()); |
| 116 | + |
| 117 | + REQUIRE(ms[0] == out[0]); |
| 118 | + REQUIRE(ms[1] == out[1]); |
| 119 | + REQUIRE(ms[2] == out[2]); |
| 120 | + REQUIRE(ms[3] == out[3]); |
| 121 | + REQUIRE(ms[4] == out[4]); |
| 122 | + } |
| 123 | + |
| 124 | + SECTION("Check MultiSpan with empty span as first span") { |
| 125 | + REQUIRE(ms1.size() == 5); |
| 126 | + |
| 127 | + REQUIRE(ms1[0] == b[0]); |
| 128 | + REQUIRE(ms1[1] == b[1]); |
| 129 | + REQUIRE(ms1[2] == a[0]); |
| 130 | + REQUIRE(ms1[3] == a[1]); |
| 131 | + REQUIRE(ms1[4] == a[2]); |
| 132 | + |
| 133 | + REQUIRE(ms1.globalIndex(0, 0) == 0); |
| 134 | + REQUIRE(ms1.globalIndex(1, 1) == 3); |
| 135 | + |
| 136 | + std::vector<int> collected; |
| 137 | + for (auto val : ms1) { |
| 138 | + collected.push_back(val); |
| 139 | + } |
| 140 | + REQUIRE(collected == std::vector<int>{b[0], b[1], a[0], a[1], a[2]}); |
| 141 | + } |
| 142 | + |
| 143 | + SECTION("Check MultiSpan with serveral empty spans") { |
| 144 | + REQUIRE(ms2.size() == 7); |
| 145 | + |
| 146 | + REQUIRE(ms2[0] == b[0]); |
| 147 | + REQUIRE(ms2[1] == b[1]); |
| 148 | + REQUIRE(ms2[2] == b[0]); |
| 149 | + REQUIRE(ms2[3] == b[1]); |
| 150 | + REQUIRE(ms2[4] == a[0]); |
| 151 | + REQUIRE(ms2[5] == a[1]); |
| 152 | + REQUIRE(ms2[6] == a[2]); |
| 153 | + |
| 154 | + REQUIRE(ms2.globalIndex(0, 0) == 0); |
| 155 | + REQUIRE(ms2.globalIndex(1, 1) == 3); |
| 156 | + REQUIRE(ms2.globalIndex(2, 1) == 5); |
| 157 | + |
| 158 | + std::vector<int> collected; |
| 159 | + for (auto val : ms2) { |
| 160 | + collected.push_back(val); |
| 161 | + } |
| 162 | + REQUIRE(collected == std::vector<int>{b[0], b[1], b[0], b[1], a[0], a[1], a[2]}); |
| 163 | + } |
| 164 | + |
| 165 | + SECTION("Check MultiSpan with serveral empty spans") { |
| 166 | + REQUIRE(ms3.size() == 3); |
| 167 | + |
| 168 | + REQUIRE(ms3[0] == a[0]); |
| 169 | + REQUIRE(ms3[1] == a[1]); |
| 170 | + REQUIRE(ms3[2] == a[2]); |
| 171 | + |
| 172 | + std::vector<int> collected; |
| 173 | + for (auto val : ms3) { |
| 174 | + collected.push_back(val); |
| 175 | + } |
| 176 | + REQUIRE(collected == std::vector<int>{a[0], a[1], a[2]}); |
| 177 | + } |
| 178 | +} |
0 commit comments