File: CompileTimeFunctionPointer_test.cpp

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (72 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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