mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Before we can use clang-tidy-17 Pull Request resolved: https://github.com/pytorch/pytorch/pull/137407 Approved by: https://github.com/Skylion007, https://github.com/aaronenyeshi
73 lines
2.3 KiB
C++
73 lines
2.3 KiB
C++
#include <c10/core/CompileTimeFunctionPointer.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
namespace test_is_compile_time_function_pointer {
|
|
static_assert(!c10::is_compile_time_function_pointer<void()>::value);
|
|
|
|
void dummy() {}
|
|
static_assert(
|
|
c10::is_compile_time_function_pointer<TORCH_FN_TYPE(dummy)>::value);
|
|
} // namespace test_is_compile_time_function_pointer
|
|
|
|
namespace test_access_through_type {
|
|
void dummy() {}
|
|
using dummy_ptr = TORCH_FN_TYPE(dummy);
|
|
static_assert(c10::is_compile_time_function_pointer<dummy_ptr>::value);
|
|
static_assert(dummy_ptr::func_ptr() == &dummy);
|
|
static_assert(std::is_same_v<void(), dummy_ptr::FuncType>);
|
|
} // namespace test_access_through_type
|
|
|
|
namespace test_access_through_value {
|
|
void dummy() {}
|
|
constexpr auto dummy_ptr = TORCH_FN(dummy);
|
|
static_assert(dummy_ptr.func_ptr() == &dummy);
|
|
static_assert(std::is_same_v<void(), decltype(dummy_ptr)::FuncType>);
|
|
} // namespace test_access_through_value
|
|
|
|
namespace test_access_through_type_also_works_if_specified_as_pointer {
|
|
void dummy() {}
|
|
using dummy_ptr = TORCH_FN_TYPE(&dummy);
|
|
static_assert(c10::is_compile_time_function_pointer<dummy_ptr>::value);
|
|
static_assert(dummy_ptr::func_ptr() == &dummy);
|
|
static_assert(std::is_same_v<void(), dummy_ptr::FuncType>);
|
|
} // namespace test_access_through_type_also_works_if_specified_as_pointer
|
|
|
|
namespace test_access_through_value_also_works_if_specified_as_pointer {
|
|
void dummy() {}
|
|
constexpr auto dummy_ptr = TORCH_FN(&dummy);
|
|
static_assert(dummy_ptr.func_ptr() == &dummy);
|
|
static_assert(std::is_same_v<void(), decltype(dummy_ptr)::FuncType>);
|
|
} // namespace test_access_through_value_also_works_if_specified_as_pointer
|
|
|
|
namespace test_run_through_type {
|
|
int add(int a, int b) {
|
|
return a + b;
|
|
}
|
|
using Add = TORCH_FN_TYPE(add);
|
|
template <class Func>
|
|
struct Executor {
|
|
int execute(int a, int b) {
|
|
return Func::func_ptr()(a, b);
|
|
}
|
|
};
|
|
|
|
TEST(CompileTimeFunctionPointerTest, runFunctionThroughType) {
|
|
Executor<Add> executor;
|
|
EXPECT_EQ(3, executor.execute(1, 2));
|
|
}
|
|
} // namespace test_run_through_type
|
|
|
|
namespace test_run_through_value {
|
|
int add(int a, int b) {
|
|
return a + b;
|
|
}
|
|
template <class Func>
|
|
int execute(Func, int a, int b) {
|
|
return Func::func_ptr()(a, b);
|
|
}
|
|
|
|
TEST(CompileTimeFunctionPointerTest, runFunctionThroughValue) {
|
|
EXPECT_EQ(3, execute(TORCH_FN(add), 1, 2));
|
|
}
|
|
} // namespace test_run_through_value
|