-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParallelTests.cs
More file actions
124 lines (96 loc) · 4.03 KB
/
Copy pathParallelTests.cs
File metadata and controls
124 lines (96 loc) · 4.03 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
117
118
119
120
121
122
123
124
using GroveGames.BehaviourTree.Collections;
using GroveGames.BehaviourTree.Nodes;
using GroveGames.BehaviourTree.Nodes.Composites;
using Parallel = GroveGames.BehaviourTree.Nodes.Composites.Parallel;
namespace GroveGames.BehaviourTree.Tests.Nodes.Composites;
public class ParallelTests
{
private sealed class TestBlackboard : IBlackboard
{
public T? GetValue<T>(string key) => default;
public void SetValue<T>(string key, T value) where T : notnull { }
public void DeleteValue(string key) { }
public void Clear() { }
}
private sealed class TestNode : INode
{
public NodeState ReturnState { get; set; } = NodeState.Success;
public NodeState State => ReturnState;
public string Name => string.Empty;
public NodeState Evaluate(float deltaTime) => ReturnState;
public void Reset() { }
public void Abort() { }
public void StartEvaluate() { }
public void EndEvaluate() { }
public void SetParent(IParent parent) { }
public void SetName(string name)
{
}
}
private sealed class TestParent : IParent
{
public IBlackboard Blackboard { get; } = new TestBlackboard();
public IParent Attach(INode node) => this;
public IParent Attach(IChildTree tree) => this;
}
[Fact]
public void Evaluate_AnySuccessPolicy_ShouldReturnSuccessIfAnyChildSucceeds()
{
var parent = new TestParent();
var parallel = new Parallel(ParallelPolicy.AnySuccess);
var child1 = new TestNode { ReturnState = NodeState.Failure };
var child2 = new TestNode { ReturnState = NodeState.Success };
parallel.Attach(child1).Attach(child2);
var result = parallel.Evaluate(0.1f);
Assert.Equal(NodeState.Success, result);
Assert.Equal(NodeState.Success, parallel.State);
}
[Fact]
public void Evaluate_FirstFailurePolicy_ShouldReturnFailureIfAnyChildFails()
{
var parent = new TestParent();
var parallel = new Parallel(ParallelPolicy.FirstFailure);
var child1 = new TestNode { ReturnState = NodeState.Running };
var child2 = new TestNode { ReturnState = NodeState.Failure };
parallel.Attach(child1).Attach(child2);
var result = parallel.Evaluate(0.1f);
Assert.Equal(NodeState.Failure, result);
Assert.Equal(NodeState.Failure, parallel.State);
}
[Fact]
public void Evaluate_AllSuccessPolicy_ShouldReturnSuccessIfAllChildrenSucceed()
{
var parent = new TestParent();
var parallel = new Parallel(ParallelPolicy.AllSuccess);
var child1 = new TestNode { ReturnState = NodeState.Success };
var child2 = new TestNode { ReturnState = NodeState.Success };
parallel.Attach(child1).Attach(child2);
var result = parallel.Evaluate(0.1f);
Assert.Equal(NodeState.Success, result);
Assert.Equal(NodeState.Success, parallel.State);
}
[Fact]
public void Evaluate_AllSuccessPolicy_ShouldReturnRunningIfAnyChildIsRunning()
{
var parent = new TestParent();
var parallel = new Parallel(ParallelPolicy.AllSuccess);
var child1 = new TestNode { ReturnState = NodeState.Running };
var child2 = new TestNode { ReturnState = NodeState.Success };
parallel.Attach(child1).Attach(child2);
var result = parallel.Evaluate(0.1f);
Assert.Equal(NodeState.Running, result);
Assert.Equal(NodeState.Running, parallel.State);
}
[Fact]
public void Evaluate_AllSuccessPolicy_ShouldReturnFailureIfAllChildrenFail()
{
var parent = new TestParent();
var parallel = new Parallel(ParallelPolicy.AllSuccess);
var child1 = new TestNode { ReturnState = NodeState.Failure };
var child2 = new TestNode { ReturnState = NodeState.Failure };
parallel.Attach(child1).Attach(child2);
var result = parallel.Evaluate(0.1f);
Assert.Equal(NodeState.Failure, result);
Assert.Equal(NodeState.Failure, parallel.State);
}
}