-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelegateTests.cs
77 lines (65 loc) · 1.98 KB
/
DelegateTests.cs
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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace DelegatesLambdasAndExpressions
{
[TestClass]
public class DelegateTests
{
public delegate void VoidAction();
bool testVoidActionExecuted;
void MyAction()
{
testVoidActionExecuted = true;
}
[TestMethod]
public void VoidNoArgDelegate()
{
testVoidActionExecuted = false;
VoidAction voidAction = MyAction;
voidAction();
Assert.IsTrue(testVoidActionExecuted);
}
public delegate void VoidActionWithArg(int value);
int testDelegateArg;
void MyActionWithArg(int value)
{
testDelegateArg = value;
}
[TestMethod]
public void VoidWithArgDelegate()
{
testDelegateArg = 0;
var argument = (new Random()).Next(10, int.MaxValue);
VoidActionWithArg voidActionWithArg = MyActionWithArg;
voidActionWithArg(argument);
Assert.AreEqual(argument, testDelegateArg);
}
public delegate int NoArgFunction();
int noArgFunctionReturnValue;
int MyNoArgFunction()
{
return noArgFunctionReturnValue;
}
[TestMethod]
public void FunctionNoArgDelegate()
{
noArgFunctionReturnValue = (new Random()).Next(10, int.MaxValue);
NoArgFunction noArgFunction = MyNoArgFunction;
var returnValue = noArgFunction();
Assert.AreEqual(noArgFunctionReturnValue, returnValue);
}
public delegate int FunctionWithArg(int value);
int MyFunctionWithArg(int value)
{
return value;
}
[TestMethod]
public void FunctionWithArgDelegate()
{
var argument = (new Random()).Next(10, int.MaxValue);
FunctionWithArg functionWithArg = MyFunctionWithArg;
var returnValue = functionWithArg(argument);
Assert.AreEqual(argument, returnValue);
}
}
}