| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef HWY_TESTS_HWY_GTEST_H_ |
| 17 |
#define HWY_TESTS_HWY_GTEST_H_ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
#include <string> |
| 25 |
#include <tuple> |
| 26 |
|
| 27 |
#include "gtest/gtest.h" |
| 28 |
#include "hwy/highway.h" |
| 29 |
|
| 30 |
namespace hwy { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#ifdef INSTANTIATE_TEST_SUITE_P |
| 35 |
#define HWY_GTEST_INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_SUITE_P |
| 36 |
#else |
| 37 |
#define HWY_GTEST_INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_CASE_P |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class TestWithParamTarget : public testing::TestWithParam<int64_t> { |
| 48 |
protected: |
| 49 |
void SetUp() override { SetSupportedTargetsForTest(GetParam()); } |
| 50 |
|
| 51 |
void TearDown() override { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#if (HWY_TARGETS & (HWY_TARGETS - 1)) != 0 |
| 56 |
EXPECT_TRUE(GetChosenTarget().IsInitialized()) |
| 57 |
<< "This hwy target parametric test doesn't use dynamic-dispatch and " |
| 58 |
"doesn't need to be parametric."; |
| 59 |
#endif |
| 60 |
SetSupportedTargetsForTest(0); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static inline std::string TestParamTargetName( |
| 67 |
const testing::TestParamInfo<int64_t>& info) { |
| 68 |
return TargetName(info.param); |
| 69 |
} |
| 70 |
|
| 71 |
#define HWY_TARGET_INSTANTIATE_TEST_SUITE_P(suite) \ |
| 72 |
HWY_GTEST_INSTANTIATE_TEST_SUITE_P( \ |
| 73 |
suite##Group, suite, \ |
| 74 |
testing::ValuesIn(::hwy::SupportedAndGeneratedTargets()), \ |
| 75 |
::hwy::TestParamTargetName) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
template <typename T> |
| 87 |
class TestWithParamTargetAndT |
| 88 |
: public ::testing::TestWithParam<std::tuple<int64_t, T>> { |
| 89 |
public: |
| 90 |
|
| 91 |
|
| 92 |
using HwyParamType = T; |
| 93 |
|
| 94 |
protected: |
| 95 |
void SetUp() override { |
| 96 |
SetSupportedTargetsForTest(std::get<0>( |
| 97 |
::testing::TestWithParam<std::tuple<int64_t, T>>::GetParam())); |
| 98 |
} |
| 99 |
|
| 100 |
void TearDown() override { |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#if (HWY_TARGETS & (HWY_TARGETS - 1)) != 0 |
| 105 |
EXPECT_TRUE(GetChosenTarget().IsInitialized()) |
| 106 |
<< "This hwy target parametric test doesn't use dynamic-dispatch and " |
| 107 |
"doesn't need to be parametric."; |
| 108 |
#endif |
| 109 |
SetSupportedTargetsForTest(0); |
| 110 |
} |
| 111 |
|
| 112 |
T GetParam() { |
| 113 |
return std::get<1>( |
| 114 |
::testing::TestWithParam<std::tuple<int64_t, T>>::GetParam()); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
template <typename T> |
| 119 |
std::string TestParamTargetNameAndT( |
| 120 |
const testing::TestParamInfo<std::tuple<int64_t, T>>& info) { |
| 121 |
return std::string(TargetName(std::get<0>(info.param))) + "_" + |
| 122 |
::testing::PrintToString(std::get<1>(info.param)); |
| 123 |
} |
| 124 |
|
| 125 |
#define HWY_TARGET_INSTANTIATE_TEST_SUITE_P_T(suite, generator) \ |
| 126 |
HWY_GTEST_INSTANTIATE_TEST_SUITE_P( \ |
| 127 |
suite##Group, suite, \ |
| 128 |
::testing::Combine( \ |
| 129 |
testing::ValuesIn(::hwy::SupportedAndGeneratedTargets()), \ |
| 130 |
generator), \ |
| 131 |
::hwy::TestParamTargetNameAndT<suite::HwyParamType>) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#define HWY_EXPORT_AND_TEST_P(suite, func_name) \ |
| 141 |
HWY_EXPORT(func_name); \ |
| 142 |
TEST_P(suite, func_name) { HWY_DYNAMIC_DISPATCH(func_name)(); } \ |
| 143 |
static_assert(true, "For requiring trailing semicolon") |
| 144 |
|
| 145 |
#define HWY_EXPORT_AND_TEST_P_T(suite, func_name) \ |
| 146 |
HWY_EXPORT(func_name); \ |
| 147 |
TEST_P(suite, func_name) { HWY_DYNAMIC_DISPATCH(func_name)(GetParam()); } \ |
| 148 |
static_assert(true, "For requiring trailing semicolon") |
| 149 |
|
| 150 |
#define HWY_BEFORE_TEST(suite) \ |
| 151 |
class suite : public hwy::TestWithParamTarget {}; \ |
| 152 |
HWY_TARGET_INSTANTIATE_TEST_SUITE_P(suite); \ |
| 153 |
static_assert(true, "For requiring trailing semicolon") |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
#endif |