Skip to content

Commit 4acd3ad

Browse files
Add tests for iterators
1 parent b02da54 commit 4acd3ad

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

memlayout/wrapper.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ struct Wrapper<Struct, pointer, Layout::aos> {
156156
constexpr Wrapper<Struct, const_reference> operator->() const { return operator[](0); }
157157
};
158158

159+
template <template <template <class> class> class Struct>
160+
struct Wrapper<Struct, const_pointer, Layout::aos> {
161+
static constexpr Layout layout_type = Layout::aos;
162+
163+
const_pointer<Struct<value>> data;
164+
using Data = const_pointer<Struct<value>>;
165+
166+
operator const Data&() const { return data; }
167+
168+
constexpr bool operator==(const Wrapper& other) const { return data == other.data; }
169+
constexpr bool operator!=(const Wrapper& other) const { return data != other.data; }
170+
constexpr bool operator<(const Wrapper& other) const { return data < other.data; }
171+
172+
constexpr Wrapper operator+(ptrdiff_t i) const { return {data + i}; }
173+
constexpr Wrapper operator-(ptrdiff_t i) const { return {data - i}; }
174+
constexpr ptrdiff_t operator-(const Wrapper& other) const { return {data - other.data}; }
175+
176+
constexpr Wrapper operator++() { return {++data}; }
177+
constexpr Wrapper operator+=(ptrdiff_t i) { return {data += i}; }
178+
constexpr Wrapper operator--() { return {--data}; }
179+
constexpr Wrapper operator-=(ptrdiff_t i) { return {data -= i}; }
180+
181+
constexpr Wrapper<Struct, const_reference> operator[] (size_t i) const { return data[i]; }
182+
constexpr Wrapper<Struct, const_reference> operator*() const { return *data; }
183+
constexpr Wrapper<Struct, const_reference> operator->() const { return operator[](0); }
184+
};
185+
159186
template <
160187
template <template <class> class> class Struct,
161188
template <class> class Container

tests/cpu/test_cpu.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <algorithm>
12
#include <vector>
23
#include <span>
34

@@ -158,3 +159,55 @@ TEST(VectorToSpan, SoA) {
158159
uint32_t expected_sum = 45;
159160
EXPECT_EQ(expected_sum, sum);
160161
}
162+
163+
TEST(Iterators, AoS) {
164+
memlayout::Wrapper<test::Point2D, std::vector, memlayout::Layout::aos> w{
165+
{0, 5}, {1, 6}, {2, 7}, {3, 8}, {4, 9}
166+
};
167+
uint32_t expected = 2;
168+
for (memlayout::Wrapper<test::Point2D, memlayout::reference> p : w) {
169+
p.x = expected;
170+
p.y = expected;
171+
}
172+
for (memlayout::Wrapper<test::Point2D, memlayout::const_reference> p : w) {
173+
EXPECT_EQ(p.x , expected);
174+
EXPECT_EQ(p.y , expected);
175+
}
176+
}
177+
TEST(Iterators, SoA) {
178+
memlayout::Wrapper<test::Point2D, std::vector, memlayout::Layout::soa> w{
179+
{{0, 1, 2, 3, 4}, {5, 6, 7, 8, 9}}
180+
};
181+
uint32_t expected = 2;
182+
for (memlayout::Wrapper<test::Point2D, memlayout::reference> p : w) {
183+
p.x = expected;
184+
p.y = expected;
185+
}
186+
for (memlayout::Wrapper<test::Point2D, memlayout::const_reference> p : w) {
187+
EXPECT_EQ(p.x , expected);
188+
EXPECT_EQ(p.y , expected);
189+
}
190+
}
191+
192+
TEST(Sort, AoS) {
193+
memlayout::Wrapper<test::Point2D, std::vector, memlayout::Layout::aos> w{
194+
{9, 5}, {1, 6}, {5, 7}, {3, 8}, {2, 9}
195+
};
196+
std::sort(w.begin(), w.end(), test::Comp{});
197+
EXPECT_EQ(w[0].y, 6);
198+
EXPECT_EQ(w[1].y, 9);
199+
EXPECT_EQ(w[2].y, 8);
200+
EXPECT_EQ(w[3].y, 7);
201+
EXPECT_EQ(w[4].y, 5);
202+
}
203+
TEST(Sort, SoA) {
204+
memlayout::Wrapper<test::Point2D, std::vector, memlayout::Layout::soa> w{
205+
{{9, 1, 5, 3, 2}, {5, 6, 7, 8, 9}}
206+
};
207+
std::sort(w.begin(), w.end(), test::Comp{});
208+
EXPECT_EQ(w[0].y, 6);
209+
EXPECT_EQ(w[1].y, 9);
210+
EXPECT_EQ(w[2].y, 8);
211+
EXPECT_EQ(w[3].y, 7);
212+
EXPECT_EQ(w[4].y, 5);
213+
}

tests/include/structs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef STRUCTS_H
22
#define STRUCTS_H
33

4+
#include <algorithm>
5+
46
#include "memlayout/wrapper.h"
57

68
namespace test {
@@ -14,6 +16,16 @@ struct Point2D {
1416
constexpr int32_t abs2() const { return x * x + y * y; }
1517
};
1618

19+
void swap(Point2D<memlayout::reference> a, Point2D<memlayout::reference> b) {
20+
std::swap(a.x, b.x);
21+
std::swap(a.y, b.y);
22+
}
23+
24+
struct Comp {
25+
using cref = const memlayout::Wrapper<Point2D, memlayout::const_reference>;
26+
constexpr bool operator()(cref& p, cref& q) const { return p.x < q.x; }
27+
};
28+
1729
template <template <class> class Container>
1830
struct Point3D {
1931
MEMLAYOUT_APPLY_UNARY(point2d, z)

0 commit comments

Comments
 (0)