File size: 4,767 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
class ScriptInvokerTests : TestFramework
{
	ref ScriptInvoker m_Invoker;
	int m_InvokeCount;
	
	//---------------------------------------------------------------------------
	// Ctor - Decides the tests to run
	//---------------------------------------------------------------------------
	void ScriptInvokerTests()
	{
		m_Invoker = new ScriptInvoker();
		
		//AddInitTest("TestFirstUnique");
		//AddInitTest("TestSecondUnique");
		//AddInitTest("TestInsertRemoveUnique");
		//AddInitTest("TestInsertUniqueImmediate");
		//AddInitTest("TestClearRunning");
		//AddInitTest("TestInvokeRunning");
		AddInitTest("TestInsertRunning");
	}
	
	//---------------------------------------------------------------------------
	// Dtor
	//---------------------------------------------------------------------------
	void ~ScriptInvokerTests()
	{
		
	}
	
	//---------------------------------------------------------------------------
	// Tests
	//---------------------------------------------------------------------------
	// Test first insert flagged as unique
	TFResult TestFirstUnique()
	{
		InvokeReset();
		
		bool insert1 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
		Assert(insert1);
		bool insert2 = m_Invoker.Insert(InvokeLog);
		Assert(!insert2);
		
		m_Invoker.Invoke("TestFirstUnique");
		bool count = Assert(m_InvokeCount == 1);
		
		InvokeReset();
		
		return BTFR(insert1 && !insert2 && count);
	}
	
	//---------------------------------------------------------------------------
	// Test second insert flagged as unique
	TFResult TestSecondUnique()
	{
		InvokeReset();
		
		bool insert1 = m_Invoker.Insert(InvokeLog);
		Assert(insert1);
		bool insert2 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
		Assert(!insert2);
		
		m_Invoker.Invoke("TestSecondUnique");
		bool count = Assert(m_InvokeCount == 1);
		
		InvokeReset();
		
		return BTFR(insert1 && !insert2 && count);
	}
	
	//---------------------------------------------------------------------------
	// Test inserting and removing of unique
	TFResult TestInsertRemoveUnique()
	{
		InvokeReset();
		
		bool insert1 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
		Assert(insert1);
		bool remove1 = m_Invoker.Remove(InvokeLog);
		Assert(remove1);
		bool insert2 = m_Invoker.Insert(InvokeLog);
		Assert(insert2);
		
		m_Invoker.Invoke("TestInsertRemoveUnique");
		bool count = Assert(m_InvokeCount == 1);
		
		InvokeReset();
		
		return BTFR(insert1 && remove1 && insert2 && count);
	}
	
	//---------------------------------------------------------------------------
	// Test inserting and of unique and immediate
	TFResult TestInsertUniqueImmediate()
	{
		InvokeReset();
		
		bool insert1 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
		Assert(insert1);
		bool insert2 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.IMMEDIATE);
		Assert(insert2);
		
		m_Invoker.Invoke("TestInsertUniqueImmediate");
		bool count = Assert(m_InvokeCount == 1);
		
		InvokeReset();
		
		return BTFR(insert1 && !insert2 && count);
	}
	
	//---------------------------------------------------------------------------
	// Test clearing while invoking
	TFResult TestClearRunning()
	{
		InvokeReset();
		
		m_Invoker.Insert(InvokeClear);
		m_Invoker.Insert(InvokeLog);
		
		m_Invoker.Invoke("TestClearRunning");
		bool count = Assert(m_InvokeCount == 1);
		
		InvokeReset();
		
		return BTFR(count);
	}
	
	//---------------------------------------------------------------------------
	// Test invoke while invoking (will result in overflow)
	TFResult TestInvokeRunning()
	{
		InvokeReset();
		
		m_Invoker.Insert(InvokeInvoke);
		
		m_Invoker.Invoke("TestInvokeRunning");
		
		InvokeReset();
		
		return BTFR(false); // This can never succeed, it will never even reach this point
	}
	
	//---------------------------------------------------------------------------
	// Test insert while invoking
	TFResult TestInsertRunning()
	{
		InvokeReset();
		
		m_Invoker.Insert(InvokeInsert);
		
		m_Invoker.Invoke("TestInvokeRunning");
		Print(m_InvokeCount);
		bool count = Assert(m_InvokeCount == 129);
		
		InvokeReset();
		
		return BTFR(count);
	}

	
	//---------------------------------------------------------------------------
	// Helpers
	//---------------------------------------------------------------------------
	void InvokeLog(string s)
	{
		//Debug.TFLog(s, this, "InvokeLog");
		++m_InvokeCount;
	}
	
	void InvokeReset()
	{
		m_Invoker.Clear();
		m_InvokeCount = 0;
	}
	
	void InvokeClear(string s)
	{
		InvokeLog(s);
		m_Invoker.Clear();
	}
	
	void InvokeInvoke(string s)
	{
		InvokeLog(s);
		m_Invoker.Invoke(s);
	}
	
	void InvokeInsert(string s)
	{
		InvokeLog(s);
		m_Invoker.Insert(InvokeInsert, EScriptInvokerInsertFlags.IMMEDIATE);
	}
}