CbmRoot
Loading...
Searching...
No Matches
_GTestPartitionedSpan.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Felix Weiglhofer [committer] */
4#include "PartitionedSpan.h"
5#include "PartitionedVector.h"
6
7#include <gtest/gtest.h>
8
9using namespace cbm::algo;
10
11template<typename T>
12void EXPECT_CONTAINER_EQ(gsl::span<T> a, std::vector<i32> b)
13{
14 EXPECT_EQ(a.size(), b.size());
15 for (size_t i = 0; i < a.size(); ++i) {
16 EXPECT_EQ(a[i], b[i]);
17 }
18}
19
20class PartitionedSpanTest : public ::testing::Test {
21 protected:
22 std::vector<i32> fData;
23 std::vector<size_t> fSizes;
24 std::vector<size_t> fOffsets;
25 std::vector<size_t> fOffsetsInvalid;
26 std::vector<u32> fAddresses;
27 std::vector<u32> fAddressesInvalid;
28
29 void SetUp() override
30 {
31 fData = {1, 2, 3, 4, 5, 6, 7, 8, 9};
32 fSizes = {2, 4, 3};
33 fOffsets = {0, 2, 6, 9};
34 fOffsetsInvalid = {0, 2, 6};
35 fAddresses = {0x0, 0x100, 0x200};
36 fAddressesInvalid = {0x0, 0x100};
37 }
38
39 template<typename T> // T is either i32 or 'const i32'
40 void Ensure(const PartitionedSpan<T>& vec)
41 {
42 EXPECT_EQ(vec.NElements(), 9);
43 EXPECT_EQ(vec.NPartitions(), 3);
44
45 EXPECT_EQ(vec.Size(0), 2);
46
47 EXPECT_EQ(vec.Size(0), 2);
48 EXPECT_EQ(vec[0].size(), 2);
49 EXPECT_EQ(vec.Address(0), 0x0);
50 EXPECT_CONTAINER_EQ(vec[0], {1, 2});
51
52 auto part = vec.Partition(0);
53 EXPECT_EQ(part.first.size(), vec.Size(0));
54 EXPECT_EQ(part.second, vec.Address(0));
55
56 EXPECT_EQ(vec.Size(1), 4);
57 EXPECT_EQ(vec[1].size(), 4);
58 EXPECT_EQ(vec.Address(1), 0x100);
59 EXPECT_CONTAINER_EQ(vec[1], {3, 4, 5, 6});
60
61 part = vec.Partition(1);
62 EXPECT_EQ(part.first.size(), vec.Size(1));
63 EXPECT_EQ(part.second, vec.Address(1));
64
65 EXPECT_EQ(vec.Size(2), 3);
66 EXPECT_EQ(vec[2].size(), 3);
67 EXPECT_EQ(vec.Address(2), 0x200);
68 EXPECT_CONTAINER_EQ(vec[2], {7, 8, 9});
69
70 part = vec.Partition(2);
71 EXPECT_EQ(part.first.size(), vec.Size(2));
72 EXPECT_EQ(part.second, vec.Address(2));
73 }
74};
75
76TEST_F(PartitionedSpanTest, IsDefaultConstructable)
77{
79 EXPECT_EQ(vec.NElements(), 0);
80 EXPECT_EQ(vec.NPartitions(), 0);
81}
82
83TEST_F(PartitionedSpanTest, CanCreateWithPartitions)
84{
85 PartitionedSpan vec(fData, fOffsets, fAddresses);
86 Ensure(vec);
87}
88
89TEST_F(PartitionedSpanTest, IsConstructableFromPartitionedVector)
90{
91 PartitionedVector vec(std::move(fData), fSizes, fAddresses);
92
93 PartitionedSpan span(vec);
94 Ensure(span);
95}
96
97TEST_F(PartitionedSpanTest, IsCopyConstructable)
98{
99 PartitionedSpan vec(fData, fOffsets, fAddresses);
100
101 PartitionedSpan vecCopy(vec);
102 Ensure(vecCopy);
103}
104
105TEST_F(PartitionedSpanTest, IsConstCopyConstructable)
106{
107 PartitionedSpan vec(fData, fOffsets, fAddresses);
108
109 PartitionedSpan<const i32> vecCopy(vec);
110 Ensure(vecCopy);
111}
112
113TEST_F(PartitionedSpanTest, ThrowsOnNumAddressesMismatchesNumPartions)
114{
115 EXPECT_THROW(PartitionedSpan vec(fData, fOffsets, fAddressesInvalid), std::runtime_error);
116}
117
118TEST_F(PartitionedSpanTest, ThrowsOnOffsetsMismatchesDataSize)
119{
120 EXPECT_THROW(PartitionedSpan vec(fData, fOffsetsInvalid, fAddresses), std::runtime_error);
121}
122
123TEST_F(PartitionedSpanTest, ThrowsOnOutOfBounds)
124{
125 PartitionedSpan vec(fData, fOffsets, fAddresses);
126
127 EXPECT_THROW(vec[3], std::out_of_range);
128 EXPECT_THROW(vec.Partition(3), std::out_of_range);
129 EXPECT_THROW(vec.Address(3), std::out_of_range);
130 EXPECT_THROW(vec.Size(3), std::out_of_range);
131}
static constexpr size_t size()
Definition KfSimdPseudo.h:2
TEST_F(PartitionedSpanTest, IsDefaultConstructable)
void EXPECT_CONTAINER_EQ(gsl::span< T > a, std::vector< i32 > b)
std::vector< size_t > fSizes
std::vector< size_t > fOffsets
std::vector< size_t > fOffsetsInvalid
std::vector< u32 > fAddressesInvalid
std::vector< u32 > fAddresses
void Ensure(const PartitionedSpan< T > &vec)
std::pair< gsl::span< T >, u32 > Partition(size_t i) const
u32 Address(size_t i) const
size_t Size(size_t i) const
A vector that is partitioned into multiple subvectors.