Skip to content

Commit c51c2fe

Browse files
committed
Test refactoring: utility and first tests of batches
1 parent 1df22d6 commit c51c2fe

File tree

4 files changed

+539
-3
lines changed

4 files changed

+539
-3
lines changed

test/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ include_directories(${GTEST_INCLUDE_DIRS})
142142

143143
set(XSIMD_TESTS
144144
main.cpp
145-
xsimd_api_test.hpp
145+
test_batch.cpp
146+
test_utils.hpp
147+
#[[ xsimd_api_test.hpp
146148
xsimd_api_test.cpp
147149
xsimd_algorithms.cpp
148150
xsimd_basic_test.hpp
@@ -178,7 +180,7 @@ set(XSIMD_TESTS
178180
xsimd_tester.hpp
179181
xsimd_test_utils.hpp
180182
xsimd_trigonometric_test.hpp
181-
xsimd_trigonometric_test.cpp
183+
xsimd_trigonometric_test.cpp]]
182184
)
183185

184186
add_executable(test_xsimd ${XSIMD_TESTS} ${XSIMD_HEADERS})

test/downloadGTest.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ project(googletest-download NONE)
1515
include(ExternalProject)
1616
ExternalProject_Add(googletest
1717
GIT_REPOSITORY https://github.com/google/googletest.git
18-
GIT_TAG release-1.8.1
18+
GIT_TAG release-1.10.0
1919
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
2020
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
2121
CONFIGURE_COMMAND ""

test/test_batch.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* Copyright (c) QuantStack *
5+
* *
6+
* Distributed under the terms of the BSD 3-Clause License. *
7+
* *
8+
* The full license is in the file LICENSE, distributed with this software. *
9+
****************************************************************************/
10+
11+
#include <functional>
12+
13+
#include "test_utils.hpp"
14+
15+
template <class B>
16+
class batch : public testing::Test
17+
{
18+
protected:
19+
20+
using batch_type = B;
21+
using value_type = typename B::value_type;
22+
static constexpr size_t size = B::size;
23+
using array_type = std::array<value_type, size>;
24+
25+
array_type lhs;
26+
array_type rhs;
27+
28+
batch()
29+
{
30+
init_operands();
31+
}
32+
33+
void test_load_store() const
34+
{
35+
array_type res;
36+
batch_type b;
37+
b.load_unaligned(lhs.data());
38+
b.store_unaligned(res.data());
39+
EXPECT_EQ(res, lhs) << print_function_name("load_unaligned / store_unaligned");
40+
41+
alignas(XSIMD_DEFAULT_ALIGNMENT) array_type arhs(this->rhs);
42+
alignas(XSIMD_DEFAULT_ALIGNMENT) array_type ares;
43+
b.load_aligned(arhs.data());
44+
b.store_aligned(ares.data());
45+
EXPECT_EQ(ares, rhs) << print_function_name("load_aligned / store_aligned");
46+
}
47+
48+
void test_constructors() const
49+
{
50+
array_type tmp;
51+
std::fill(tmp.begin(), tmp.end(), value_type(2));
52+
batch_type b0(2);
53+
EXPECT_EQ(b0, tmp) << print_function_name("batch(value_type)");
54+
55+
batch_type b1(lhs.data());
56+
EXPECT_EQ(b1, lhs) << print_function_name("batch(value_type*)");
57+
}
58+
59+
void test_arithmetic() const
60+
{
61+
// batch + batch
62+
{
63+
array_type expected;
64+
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(), std::plus<value_type>());
65+
batch_type res = batch_lhs() + batch_rhs();
66+
EXPECT_BATCH_EQ(res, expected) << print_function_name("batch + batch");
67+
}
68+
}
69+
70+
private:
71+
72+
batch_type batch_lhs() const
73+
{
74+
return batch_type(lhs.data());
75+
}
76+
77+
batch_type batch_rhs() const
78+
{
79+
return batch_type(rhs.data());
80+
}
81+
82+
template <class T = value_type>
83+
xsimd::enable_integral_t<T, void> init_operands()
84+
{
85+
for (size_t i = 0; i < size; ++i)
86+
{
87+
bool negative_lhs = std::is_signed<T>::value && (i % 2 == 1);
88+
lhs[i] = value_type(i) * (negative_lhs ? -10 : 10);
89+
rhs[i] = value_type(i) + value_type(4);
90+
}
91+
}
92+
93+
template <class T = value_type>
94+
xsimd::enable_floating_point_t<T, void> init_operands()
95+
{
96+
for (size_t i = 0; i < size; ++i)
97+
{
98+
lhs[i] = value_type(i) / 4 + value_type(1.2) * std::sqrt(value_type(i + 0.25));
99+
rhs[i] = value_type(10.2) / (i + 2) + value_type(0.25);
100+
}
101+
}
102+
};
103+
104+
TYPED_TEST_SUITE_P(batch);
105+
106+
TYPED_TEST_P(batch, load_store)
107+
{
108+
this->test_load_store();
109+
}
110+
111+
TYPED_TEST_P(batch, constructors)
112+
{
113+
this->test_constructors();
114+
}
115+
116+
TYPED_TEST_P(batch, arithmetic)
117+
{
118+
this->test_arithmetic();
119+
}
120+
121+
REGISTER_TYPED_TEST_SUITE_P(
122+
batch,
123+
load_store,
124+
constructors,
125+
arithmetic
126+
);
127+
128+
129+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION
130+
INSTANTIATE_TYPED_TEST_SUITE_P(sse,
131+
batch,
132+
sse_types,
133+
simd_test_names);
134+
#endif

0 commit comments

Comments
 (0)