-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathdelegate.cpp
More file actions
116 lines (102 loc) · 2.26 KB
/
delegate.cpp
File metadata and controls
116 lines (102 loc) · 2.26 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "pch.h"
using namespace winrt;
TEST_CASE("delegate")
{
// <>
{
bool invoked = false;
delegate<> d = [&] {invoked = true; };
d();
REQUIRE(invoked);
}
// <int>
{
int result = 0;
delegate<int> d = [&](int a) {result = a; };
d(123);
REQUIRE(result == 123);
}
// <int,int>
{
int result = 0;
delegate<int, int> d = [&](int a, int b) {result = a + b; };
d(4,5);
REQUIRE(result == 9);
}
// void()
{
bool invoked = false;
delegate<void()> d = [&] {invoked = true; };
d();
REQUIRE(invoked);
}
// void(int)
{
int result = 0;
delegate<void(int)> d = [&](int a) {result = a; };
d(123);
REQUIRE(result == 123);
}
// void(int,int)
{
int result = 0;
delegate<void(int,int)> d = [&](int a, int b) {result = a + b; };
d(4, 5);
REQUIRE(result == 9);
}
// int()
{
delegate<int()> d = [] { return 123; };
REQUIRE(d() == 123);
}
// int(int)
{
delegate<int(int)> d = [](int a) {return a; };
REQUIRE(d(123) == 123);
}
// int(int,int)
{
delegate<int(int, int)> d = [](int a, int b) {return a + b; };
REQUIRE(d(4, 5) == 9);
}
// void(int*) with function pointer
{
struct S
{
static void Invoke(int* p) { *p = 123; }
};
int value = 0;
delegate<void(int*)> d = &S::Invoke;
d(&value);
REQUIRE(value == 123);
}
// void(int*) with object and method pointer
{
struct S
{
void Invoke(int* p) { *p = 123; }
} s;
delegate<void(int*)> d{ &s, &S::Invoke };
int value = 0;
d(&value);
REQUIRE(value == 123);
}
// int() with function pointer
{
struct S
{
static int Value() { return 123; }
};
delegate<int()> d = &S::Value;
REQUIRE(d() == 123);
}
// int() with object and method pointer
{
struct S
{
int Value() { return 123; }
} s;
delegate<int()> d{ &s, &S::Value };
REQUIRE(d() == 123);
}
}