Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/testtools/micromock/unittests/micromocktest/timediscretemicromocktest_stim.cpp

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include "stdafx.h"
5 #include "hypothetic_module.h"
6 #include <string>
7
8
9 using namespace std;
10
11 //MICROMOCK_ENUM_TO_STRING_DECLARE(MICROMOCK_EXCEPTION_TAG);
12
13 #ifdef _MSC_VER
14
15 MICROMOCK_ENUM_TO_STRING(MICROMOCK_EXCEPTION_TAG,
16 L"MICROMOCK_EXCEPTION_INVALID_VALIDATE_BUFFERS",
17 L"MICROMOCK_EXCEPTION_ALLOCATION_FAILURE",
18 L"MICROMOCK_EXCEPTION_INVALID_ARGUMENT",
19 L"MICROMOCK_EXCEPTION_INVALID_CALL_MODIFIER_COMBINATION",
20 L"MICROMOCK_EXCEPTION_MOCK_NOT_FOUND",
21 L"MICROMOCK_EXCEPTION_SET_TIME_BEFORE_CALL",
22 L"MICROMOCK_EXCEPTION_SET_ARRAY_SIZE_BEFORE_CALL",
23 L"MICROMOCK_EXCEPTION_INTERNAL_ERROR");
24
25
26
27 /*tests table of content*/
28
29 /*
30 TEST_FUNCTION(TimeDiscreteMicroMock_LoopbackAll)
31 TEST_FUNCTION(TimeDiscreteMicroMock_FileOrderIsPreserved_1)
32 TEST_FUNCTION(TimeDiscreteMicroMock_FileOrderIsPreserved_2)
33 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved)
34 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_2)
35 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_3)
36 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_4)
37 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_5)
38 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_understands_NULL_buffer)
39 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_understands_buffer_of_1_element)
40 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_understands_buffer_of_5_elements)
41 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_NULL_NULL)
42 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_NULL_1int)
43 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_NULL_3int)
44 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_1int_NULL)
45 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_1int_1int)
46 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_1int_3int)
47 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_3int_NULL)
48 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_3int_1int)
49 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_3int_4int)
50 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_can_be_reused)
51 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_can_be_reused_at_different_times)
52 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_can_copy_an_array_of_objects)
53 TEST_FUNCTION(TimeDiscreteMicroMock_FailsWhenUserCallsDirectly_setArraySize)
54 TEST_FUNCTION(TimeDiscreteMicroMock_FailsWhenUserCallsDirectly_setTime)
55 TEST_FUNCTION(TimeDiscreteMicroMock_Const_ParameterBuffersAreSaved_understands_NULL_buffer)
56 TEST_FUNCTION(TimeDiscreteMicroMock_Const_ParameterBuffersAreSaved_understands_buffer_of_1_element)
57 TEST_FUNCTION(TimeDiscreteMicroMock_Const_ParameterBuffersAreSaved_understands_buffer_of_5_elements)
58 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_baseline)
59 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_using_without_strict_with_real_time)
60 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_time_mismatch_is_detected)
61 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_time_mismatch_is_detected_without_STRICT)
62 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_IgnoreArgument)
63 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_IgnoreAllArguments)
64 */
65
66
67 /*so sometimes some types that look like pointers and are not pointers have to be explictly implemented (specialized)*/
68 template <> class valueHolder <pVoidFunction>
69 {
70 private:
71 pVoidFunction theValue;
72
73 public:
74 valueHolder(pVoidFunction t)
75 {
76 theValue = t;
77 }
78
79 void operator=(pVoidFunction t)
80 {
81 theValue = t;
82 }
83
84 operator pVoidFunction()
85 {
86 return theValue;
87 }
88 };
89
90 template<>
91 static bool operator==<char*>(const CMockValue<char*>& lhs, const CMockValue<char*>& rhs)
92 {
93 bool result = false;
94 if (lhs.GetValue() == rhs.GetValue())
95 {
96 result = true;
97 } else
98 {
99 if ((NULL != lhs.GetValue()) &&
100 (NULL != rhs.GetValue()) &&
101 (strcmp(lhs.GetValue(), rhs.GetValue()) == 0))
102 {
103 result = true;
104 }
105 }
106 return result;
107 }
108
109 extern "C" void whenzero(void);
110 extern "C" int whenizero(void);
111 extern "C" int whenone(_In_ int i);
112 extern "C" int whentwo(_In_ pChar s, _In_ int i);
113 extern "C" int whenthree(_In_ char c, _In_ pChar s, _In_ int i);
114 extern "C" int whenfour(_In_ unsigned short int si, _In_ char c, _In_ pChar s, _In_ int i);
115 extern "C" int whenfive(_In_ pVoidFunction pVoid, _In_ unsigned short int si, _In_ char c, _In_ pChar s, _In_ int i);
116 extern "C" int whensix(_In_ char c1, _In_ char c2, _In_ char c3, _In_ char c4, _In_ char c5, _In_ char c6);
117
118 DECLARE_STIM_STATIC_TD_METHOD_0(,void, zero);
119 DECLARE_STIM_STATIC_TD_METHOD_0(,int, izero);
120 DECLARE_STIM_STATIC_TD_METHOD_1(,int, one, _In_ int, i);
121 DECLARE_STIM_STATIC_TD_METHOD_2(,int, two, _In_ pChar, s, int, i);
122 DECLARE_STIM_STATIC_TD_METHOD_3(,int, three, _In_ char, c, _In_ pChar, s, _In_ int, i);
123 DECLARE_STIM_STATIC_TD_METHOD_4(,int, four, _In_ unsigned short int, si, _In_ char, c, _In_ pChar, s, _In_ int, i);
124 DECLARE_STIM_STATIC_TD_METHOD_5(,int, five, _In_ pVoidFunction, pVoid, _In_ unsigned short int, si, _In_ char, c, _In_ pChar, s, _In_ int, i);
125 DECLARE_STIM_STATIC_TD_METHOD_6(,int, six, _In_ char, c1, _In_ char, c2, _In_ char, c3, _In_ char, c4, _In_ char, c5, _In_ char, c6);
126
127
128
129
130 static int posIndex=0;
131 static int posRecorder[2];
132 extern "C" void takes1int_1(int i)
133 {
134 if(posIndex<2)
135 {
136 posRecorder[posIndex++]=i;
137 }
138 else
139 {
140 ASSERT_FAIL(_T("too much posIndex for me"));
141 }
142 }
143
144 extern "C" void takes1int_2(int i)
145 {
146 if(posIndex<2)
147 {
148 posRecorder[posIndex++]=i;
149 }
150 else
151 {
152 ASSERT_FAIL(_T("too much posIndex for me"));
153 }
154 }
155
156 DECLARE_STIM_STATIC_TD_METHOD_1(,void, takes1int_1, _In_ int, i);
157 DECLARE_STIM_STATIC_TD_METHOD_1(,void, takes1int_2, _In_ int, i);
158
159 TD_MOCK_CLASS(MyMocks3)
160 {
161 public:
162 STIM_STATIC_TD_METHOD_0(,void, zero);
163 STIM_STATIC_TD_METHOD_0(,int, izero);
164 STIM_STATIC_TD_METHOD_1(,int, one, _In_ int, i);
165 STIM_STATIC_TD_METHOD_2(,int, two, _In_ pChar, s, _In_ int, i);
166 STIM_STATIC_TD_METHOD_3(,int, three, _In_ char, c, _In_ pChar, s, _In_ int, i);
167 STIM_STATIC_TD_METHOD_4(,int, four, _In_ unsigned short int, si, _In_ char, c, _In_ pChar, s, _In_ int, i);
168 STIM_STATIC_TD_METHOD_5(,int, five, _In_ pVoidFunction, pVoid, _In_ unsigned short int, si, _In_ char, c, _In_ pChar, s, _In_ int, i);
169 STIM_STATIC_TD_METHOD_6(,int, six, char, c1, char, c2, char, c3, char, c4, char, c5, char, c6);
170
171 STIM_STATIC_TD_METHOD_1(,void, takes1int_1, _In_ int, i);
172 STIM_STATIC_TD_METHOD_1(,void, takes1int_2, _In_ int, i);
173
174 MOCK_STATIC_TD_METHOD_0(, void, whenzero)
175 MOCK_VOID_METHOD_END()
176
177 MOCK_STATIC_TD_METHOD_0(, int, whenizero)
178 MOCK_METHOD_END(int, 0)
179
180 MOCK_STATIC_TD_METHOD_1(, int, whenone, _In_ int, i)
181 MOCK_METHOD_END(int, 0)
182
183 MOCK_STATIC_TD_METHOD_2(, int, whentwo, _In_ pChar, s, _In_ int, i)
184 MOCK_METHOD_END(int, 0)
185
186 MOCK_STATIC_TD_METHOD_3(, int, whenthree, _In_ char, c, _In_ pChar, s, _In_ int, i);
187 MOCK_METHOD_END(int, 0)
188
189 MOCK_STATIC_TD_METHOD_4(, int, whenfour, _In_ unsigned short int, si, _In_ char, c, _In_ pChar, s, _In_ int, i);
190 MOCK_METHOD_END(int, 0)
191
192 MOCK_STATIC_TD_METHOD_5(, int, whenfive, _In_ pVoidFunction, pVoid, _In_ unsigned short int, si, _In_ char, c, _In_ pChar, s, _In_ int, i);
193 MOCK_METHOD_END(int, 0)
194
195 MOCK_STATIC_TD_METHOD_6(,int, whensix, _In_ char, c1, _In_ char, c2, _In_ char, c3, _In_ char, c4, _In_ char, c5, _In_ char, c6);
196 MOCK_METHOD_END(int, 0)
197 };
198
199 DECLARE_GLOBAL_MOCK_METHOD_0(MyMocks3, , void, whenzero)
200 DECLARE_GLOBAL_MOCK_METHOD_0(MyMocks3, , int, whenizero)
201 DECLARE_GLOBAL_MOCK_METHOD_1(MyMocks3, , int, whenone, int, i)
202 DECLARE_GLOBAL_MOCK_METHOD_2(MyMocks3, , int, whentwo, pChar, s, int, i)
203 DECLARE_GLOBAL_MOCK_METHOD_3(MyMocks3, , int, whenthree, char, c, pChar, s, int, i)
204 DECLARE_GLOBAL_MOCK_METHOD_4(MyMocks3, , int, whenfour, unsigned short int, si, char, c, pChar, s, int, i)
205 DECLARE_GLOBAL_MOCK_METHOD_5(MyMocks3, , int, whenfive, pVoidFunction, pVoid, unsigned short int, si, char, c, pChar, s, int, i)
206 DECLARE_GLOBAL_MOCK_METHOD_6(MyMocks3, , int, whensix, char, c1, char, c2, char, c3, char, c4, char, c5, char, c6)
207
208 TCHAR g_STIM_FunctionThatTakesAStringParameter[2][1000]; /*1000 will be enough for our testing purposes*/
209 UINT32 g_STIM_FunctionThatTakesAStringParameterCalls=0;
210 void STIM_FunctionThatTakesAString(_In_z_ TCHAR* someString)
211 {
212 if(g_STIM_FunctionThatTakesAStringParameterCalls<2)
213 {
214 _tcscpy_s(g_STIM_FunctionThatTakesAStringParameter[g_STIM_FunctionThatTakesAStringParameterCalls++], someString);
215 }
216 else
217 {
218 ASSERT_FAIL(_T("too many calls / test method"));
219 }
220 }
221
222 int g_STIM_FunctionThatTakesAPointerToInt[2]; /*1000 will be enough for our testing purposes*/
223 UINT32 g_STIM_FunctionThatTakesAPointerToIntCalls=0;
224 void STIM_FunctionThatTakesAPointerToInt(_In_ int* a)
225 {
226 if(g_STIM_FunctionThatTakesAPointerToIntCalls<2)
227 {
228 g_STIM_FunctionThatTakesAPointerToInt[g_STIM_FunctionThatTakesAPointerToIntCalls++]=*a;
229 }
230 else
231 {
232 ASSERT_FAIL(_T("too many calls / test method"));
233 }
234 }
235
236 int g_STIM_FunctionThatTakesAInt[2]; /*1000 will be enough for our testing purposes*/
237 UINT32 g_STIM_FunctionThatTakesAIntCalls=0;
238 void STIM_FunctionThatTakesAInt(_In_ int a)
239 {
240 if(g_STIM_FunctionThatTakesAIntCalls<2)
241 {
242 g_STIM_FunctionThatTakesAInt[g_STIM_FunctionThatTakesAIntCalls++]=a;
243 }
244 else
245 {
246 ASSERT_FAIL(_T("too many calls / test method"));
247 }
248 }
249
250 int g_STIM_FunctionThatTakesAnArray[2][10];
251 bool g_STIM_FunctionThatTakesAnArrayWasParameterNULL=false;
252 int g_STIM_FunctionThatTakesAnArrayNParameters=0;
253 int g_STIM_FunctionThatTakesAnArray_nCalls=0;
254 void STIM_FunctionThatTakesAnArray(_In_reads_opt_(g_STIM_FunctionThatTakesAnArrayNParameters) int* array)
255 {
256 //could very well be: while(array[i]!=0) cout<<array[i++];
257 if(array==NULL)
258 {
259 g_STIM_FunctionThatTakesAnArrayWasParameterNULL = true;
260 }
261 else
262 {
263 if(g_STIM_FunctionThatTakesAnArray_nCalls<2)
264 {
265 for(int i=0;i<g_STIM_FunctionThatTakesAnArrayNParameters;i++)
266 {
267
268 g_STIM_FunctionThatTakesAnArray[g_STIM_FunctionThatTakesAnArray_nCalls][i]=array[i];
269 }
270 g_STIM_FunctionThatTakesAnArray_nCalls++;
271 }
272 else
273 {
274 ASSERT_FAIL(_T("catastrophic number of calls"));
275 }
276 }
277 }
278
279 int g_STIM_FunctionThatTakesAConstArray[2][10];
280 bool g_STIM_FunctionThatTakesAConstArrayWasParameterNULL=false;
281 int g_STIM_FunctionThatTakesAConstArrayNParameters=0;
282 int g_STIM_FunctionThatTakesAConstArray_nCalls=0;
283 void STIM_FunctionThatTakesAConstArray(_In_reads_opt_(g_STIM_FunctionThatTakesAConstArrayNParameters) const int* array)
284 {
285 //could very well be: while(array[i]!=0) cout<<array[i++];
286 if(array==NULL)
287 {
288 g_STIM_FunctionThatTakesAConstArrayWasParameterNULL = true;
289 }
290 else
291 {
292 if(g_STIM_FunctionThatTakesAConstArray_nCalls<2)
293 {
294 for(int i=0;i<g_STIM_FunctionThatTakesAConstArrayNParameters;i++)
295 {
296
297 g_STIM_FunctionThatTakesAConstArray[g_STIM_FunctionThatTakesAConstArray_nCalls][i]=array[i];
298 }
299 g_STIM_FunctionThatTakesAConstArray_nCalls++;
300 }
301 else
302 {
303 ASSERT_FAIL(_T("catastrophic number of calls"));
304 }
305 }
306 }
307
308 int* STIM_FunctionThatTakesTwoArrays_array1_called_pointer;
309 int STIM_FunctionThatTakesTwoArrays_array1_copy[100];
310 size_t STIM_FunctionThatTakesTwoArrays_nArray1;
311 int* STIM_FunctionThatTakesTwoArrays_array2_called_pointer;
312 int STIM_FunctionThatTakesTwoArrays_array2_copy[100];
313 size_t STIM_FunctionThatTakesTwoArrays_nArray2;
314 void STIM_FunctionThatTakesTwoArrays(_In_reads_opt_(nArray1) int* array1, _In_ size_t nArray1, _In_reads_opt_(nArray2) int* array2, _In_ size_t nArray2 )
315 {
316 STIM_FunctionThatTakesTwoArrays_array1_called_pointer = array1;
317 STIM_FunctionThatTakesTwoArrays_nArray1 = nArray1;
318 if(array1!=NULL)
319 {
320 for(size_t i=0;i<nArray1;i++)
321 {
322 STIM_FunctionThatTakesTwoArrays_array1_copy[i]=array1[i];
323 }
324 }
325
326 STIM_FunctionThatTakesTwoArrays_array2_called_pointer = array2;
327 STIM_FunctionThatTakesTwoArrays_nArray2 = nArray2;
328 if(array2!=NULL)
329 {
330 for(size_t i=0;i<nArray2;i++)
331 {
332 STIM_FunctionThatTakesTwoArrays_array2_copy[i]=array2[i];
333 }
334 }
335 }
336
337
338 void someTask(void)
339 {
340 }
341
342 static int s_someTaskThatCallsWhenTwoAtTimeEqualThree_nCalls=0;
343 void someTaskThatCallsWhenTwoAtTimeEqualThree(void)
344 {
345 if(s_someTaskThatCallsWhenTwoAtTimeEqualThree_nCalls == 3)
346 {
347 whentwo("two", -4);
348 }
349 s_someTaskThatCallsWhenTwoAtTimeEqualThree_nCalls ++;
350 }
351
352
353 class someSortOfT
354 {
355 private:
356 tstring someString;
357 char* someChar;
358 public:
359 //default constructor
360 someSortOfT():someChar(NULL)
361 {
362 }
363
364 //copy constructor
365 someSortOfT(const someSortOfT & t):someString(t.someString)
366 {
367 if(t.someChar==NULL)
368 {
369 someChar = NULL;
370 }
371 else
372 {
373 size_t strLen = strlen(t.someChar);
374 someChar = (char*) malloc(strLen+1);
375 if(someChar==NULL)
376 {
377 ASSERT_FAIL(_T("catastrophic memory error"));
378 }
379 else
380 {
381 strcpy_s(someChar, strLen+1, t.someChar);
382 }
383 }
384
385 }
386
387 //init constructor
388 someSortOfT(tstring s, _In_opt_z_ char* p)
389 {
390 someString = s;
391 if(p==NULL)
392 {
393 someChar = NULL;
394 }
395 else
396 {
397 size_t strLen = strlen(p);
398 someChar = (char*) malloc(strLen+1);
399 if(someChar==NULL)
400 {
401 ASSERT_FAIL(_T("catastrophic memory error"));
402 }
403 else
404 {
405 strcpy_s(someChar, strLen+1, p);
406 }
407 }
408 }
409
410 someSortOfT& operator=(const someSortOfT & right)
411 {
412 if(this==&right)
413 {
414 //nice try
415 }
416 else
417 {
418 someString = right.someString;
419
420 if(someChar!=NULL)
421 {
422 free(someChar);
423 someChar=NULL;
424 }
425
426 if(right.someChar!=NULL)
427 {
428 size_t strLen = strlen(right.someChar);
429 someChar = (char*)malloc(strLen+1);
430 if(someChar==NULL)
431 {
432 ASSERT_FAIL(_T("catastrphic memory error"));
433 }
434 else
435 {
436 strcpy_s(someChar, strLen+1, right.someChar);
437 }
438 }
439
440 }
441 return *this;
442 }
443
444 bool operator==(const someSortOfT& right) const
445 {
446 if(this==&right)
447 {
448 return true;
449 }
450 else
451 {
452 if(someString!=right.someString)
453 {
454 return false;
455 }
456 else
457 {
458 if(someChar==NULL)
459 {
460 if(right.someChar==NULL)
461 {
462 return true;
463 }
464 else
465 {
466 return false;
467 }
468 }
469 else
470 {
471 if(right.someChar == NULL)
472 {
473 return false;
474 }
475 else
476 {
477 return (strcmp(someChar, right.someChar)==0);
478 }
479 }
480 }
481 }
482 }
483
484 virtual ~someSortOfT()
485 {
486 if(someChar!=NULL)
487 {
488 free(someChar);
489 someChar=NULL;
490 }
491 }
492 const char* getSomeChar() const
493 {
494 return someChar;
495 }
496 tstring getSomeString() const
497 {
498 return someString;
499 }
500 };
501
502 namespace Microsoft
503 {
504 namespace VisualStudio
505 {
506 namespace CppUnitTestFramework
507 {
508 template <> static std::wstring ToString <someSortOfT>(const someSortOfT& q)
509 {
510 //return q.getSomeString();
511 std::string source1(q.getSomeChar());
512 std::wstring a(source1.begin(), source1.end());
513
514 std::string source2(q.getSomeString());
515 std::wstring b(source2.begin(), source2.end());
516
517 std::wstring toBeReturned (a + b);
518
519 return toBeReturned;
520 }
521 }
522 }
523 }
524
525
526 someSortOfT g_STIM_FunctionThatTakesAnArrayOfObjects[10][10];
527 bool g_STIM_FunctionThatTakesAnArrayOfObjectsWasParameterNULL=false;
528 int g_STIM_FunctionThatTakesAnArrayOfObjectsNParameters=2;
529 int g_STIM_FunctionThatTakesAnArrayOfObjects_nCalls=0;
530 void STIM_FunctionThatTakesAnArrayOfObjects(_In_opt_count_(g_STIM_FunctionThatTakesAnArrayOfObjectsNParameters) someSortOfT* array)
531 {
532
533 if(array==NULL)
534 {
535 g_STIM_FunctionThatTakesAnArrayOfObjectsWasParameterNULL = true;
536 }
537 else
538 {
539 if(g_STIM_FunctionThatTakesAnArrayOfObjects_nCalls<10)
540 {
541 for(int i=0;i<g_STIM_FunctionThatTakesAnArrayOfObjectsNParameters;i++)
542 {
543 g_STIM_FunctionThatTakesAnArrayOfObjects[g_STIM_FunctionThatTakesAnArrayOfObjects_nCalls][i]=array[i];
544 }
545 g_STIM_FunctionThatTakesAnArrayOfObjects_nCalls++;
546 }
547 else
548 {
549 ASSERT_FAIL(_T("catastrophic number of calls"));
550 }
551 }
552 }
553
554 DECLARE_STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAString, _In_z_ TCHAR*, someString);
555 DECLARE_STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAPointerToInt, _In_ int*, a);
556 DECLARE_STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAInt, _In_ int, a);
557 DECLARE_STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAnArray, _In_reads_opt_(g_STIM_FunctionThatTakesAnArrayNParameters) int*, a);
558 DECLARE_STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAConstArray, _In_reads_opt_(g_STIM_FunctionThatTakesAConstArrayNParameters) const int*, a);
559 //DECLARE_STIM_STATIC_TD_METHOD_4(, void, STIM_FunctionThatTakesTwoArrays, _In_ int*, array1, _In_ size_t, nArray1, _In_ int*,array2, _In_ size_t, nArray2 );
560 DECLARE_STIM_STATIC_TD_METHOD_4(, void, STIM_FunctionThatTakesTwoArrays, _In_reads_opt_(nArray1) int*, array1, _In_ size_t, nArray1, _In_reads_opt_(nArray2) int*,array2, _In_ size_t, nArray2 );
561 DECLARE_STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAnArrayOfObjects, _In_reads_opt_(g_STIM_FunctionThatTakesAnArrayOfObjectsNParameters) someSortOfT*, array);
562
563 TD_MOCK_CLASS(CMocksForParameterBuffers)
564 {
565 public:
566 STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAString, _In_z_ TCHAR*, someString);
567 STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAPointerToInt, _In_ int*, a);
568 STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAInt, _In_ int, a);
569 STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAnArray, _In_reads_opt_(g_STIM_FunctionThatTakesAnArrayNParameters) int*, a);
570 STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAConstArray, _In_reads_opt_(g_STIM_FunctionThatTakesAConstArrayNParameters) const int*, a);
571 STIM_STATIC_TD_METHOD_4(, void, STIM_FunctionThatTakesTwoArrays, _In_reads_opt_(nArray1) int*, array1, _In_ size_t, nArray1, _In_reads_opt_(nArray2) int*,array2, _In_ size_t, nArray2 );
572 STIM_STATIC_TD_METHOD_1(, void, STIM_FunctionThatTakesAnArrayOfObjects, _In_reads_opt_(g_STIM_FunctionThatTakesAnArrayOfObjectsNParameters) someSortOfT*, array);
573 };
574
575 static HANDLE g_testByTest=NULL;
576
577 #endif
578 BEGIN_TEST_SUITE(MicroMockStimTest)
579
580 #ifdef _MSC_VER
581 TEST_SUITE_INITIALIZE(a)
582 {
583 g_testByTest = CreateMutex(NULL, FALSE, NULL);
584 ASSERT_ARE_NOT_EQUAL_WITH_MSG(HANDLE, (HANDLE)NULL, g_testByTest, _T("Failed to create mutex for test by test"));
585 }
586
587 TEST_SUITE_CLEANUP(b)
588 {
589 (void)CloseHandle(g_testByTest);
590 }
591
592 TEST_FUNCTION_INITIALIZE(c)
593 {
594 if (WaitForSingleObject(g_testByTest, INFINITE) != WAIT_OBJECT_0)
595 {
596 ASSERT_FAIL(_T("Failed acquiring mutex for test by test."));
597 }
598
599 g_STIM_FunctionThatTakesAStringParameterCalls = 0;
600
601 g_STIM_FunctionThatTakesAnArrayWasParameterNULL = false;
602 g_STIM_FunctionThatTakesAnArrayNParameters = 0;
603 g_STIM_FunctionThatTakesAnArray_nCalls = 0;
604 g_STIM_FunctionThatTakesAConstArrayWasParameterNULL = false;
605 g_STIM_FunctionThatTakesAConstArrayNParameters = 0;
606 g_STIM_FunctionThatTakesAConstArray_nCalls = 0;
607 g_STIM_FunctionThatTakesAnArrayOfObjects_nCalls = 0;
608
609 s_someTaskThatCallsWhenTwoAtTimeEqualThree_nCalls = 0;
610 }
611
612 /*missing lock doesn't work with CppUnitTests TEST_FUNCTION_CLEANUP/ TEST_FUNCTION_INITIALIZE*/
613 #pragma warning(disable: 26135)
614 TEST_FUNCTION_CLEANUP(d)
615 {
616 if (ReleaseMutex(g_testByTest) == 0)
617 {
618 ASSERT_FAIL(_T("failure in test framework at ReleaseMutex"));
619 }
620 }
621
622 TEST_FUNCTION(TimeDiscreteMicroMock_LoopbackAll)
623 {
624 ///arrange
625 MyMocks3 mocks(theTask);
626 const int maxTick=60;
627 char *someString ="abc";
628 int i;
629
630 for(i=0;i<=maxTick;i++)
631 {
632 STIM_CALL_AT(mocks, i, zero());
633 STRICT_EXPECTED_CALL_AT(mocks, i, whenzero());
634 }
635
636 for(i=0;i<=maxTick;i++)
637 {
638 STIM_CALL_AT(mocks, i, izero());
639 STRICT_EXPECTED_CALL_AT(mocks,i, whenizero());
640 }
641
642 for(i=0;i<=maxTick;i++)
643 {
644 STIM_CALL_AT(mocks, i, one(i));
645 STRICT_EXPECTED_CALL_AT(mocks, i, whenone(i+1));
646 }
647
648 for(i=0;i<=maxTick;i+=2)
649 {
650 STIM_CALL_AT(mocks, i, two("a", i));
651 STRICT_EXPECTED_CALL_AT(mocks, i, whentwo("a", i+2));
652 }
653
654 for(i=0;i<=maxTick;i+=3)
655 {
656 STIM_CALL_AT(mocks, i, three('a', someString, i));
657 STRICT_EXPECTED_CALL_AT(mocks,i, whenthree('d', someString, i+3));
658 }
659
660 for(i=0;i<=maxTick;i+=4)
661 {
662 STIM_CALL_AT(mocks, i, four((unsigned short)i*10, 'a', someString, i));
663 STRICT_EXPECTED_CALL_AT(mocks,i, whenfour((unsigned short)i*10+4, 'e', someString, i+4));
664 }
665
666 for(i=0;i<=maxTick;i+=5)
667 {
668 STIM_CALL_AT(mocks, i, five(NULL, (unsigned short)i*10, 'a', someString, i));
669 STRICT_EXPECTED_CALL_AT(mocks,i, whenfive(NULL, (unsigned short)i*10+5, 'f', someString, i+5));
670 }
671
672 for(i=0;i<=maxTick;i+=6)
673 {
674 STIM_CALL_AT(mocks, i, six((char)i, (char)(i+1), (char)(i+2), (char)(i+3), (char)(i+4), (char)(i+5)));
675 STRICT_EXPECTED_CALL_AT(mocks,i, whensix((char)(i+6), (char)(i+7), (char)(i+8), (char)(i+9), (char)(i+10), (char)(i+11)));
676 }
677
678 ///act
679
680 mocks.RunUntilTick(maxTick);
681 ///assert - left to uM
682 }
683
684
685
686
687 TEST_FUNCTION(TimeDiscreteMicroMock_FileOrderIsPreserved_1)
688 {
689 ///arrange
690 MyMocks3 mocks(theTask);
691 posIndex=0;
692
693 STIM_CALL_AT(mocks, 0, takes1int_1(1));
694 STIM_CALL_AT(mocks, 0, takes1int_2(2));
695
696 ///act
697 mocks.RunUntilTick(0);
698
699 ///assert
700 ASSERT_ARE_EQUAL(int, 1, posRecorder[0]);
701 ASSERT_ARE_EQUAL(int, 2, posRecorder[1]);
702
703 }
704
705 TEST_FUNCTION(TimeDiscreteMicroMock_FileOrderIsPreserved_2)
706 {
707 ///arrange
708 MyMocks3 mocks(theTask);
709 posIndex=0;
710 STIM_CALL_AT(mocks, 0, takes1int_2(1));
711 STIM_CALL_AT(mocks, 0, takes1int_1(2));
712
713 ///act
714 mocks.RunUntilTick(0);
715
716 ///assert
717 ASSERT_ARE_EQUAL(int, 1, posRecorder[0]);
718 ASSERT_ARE_EQUAL(int, 2, posRecorder[1]);
719 }
720
721 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved)
722 {
723 ///arrange
724 CMocksForParameterBuffers m(someTask);
725
726 ///act
727 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(_T("alfa")));
728 m.RunUntilTick(0);
729
730 ///assert
731 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("alfa"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[0]);
732 }
733
734 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_2)
735 {
736 ///arrange
737 CMocksForParameterBuffers m(someTask);
738
739 ///act
740 {
741 TCHAR* stackString1 = _T("alfa");
742 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(stackString1));
743 }
744
745 {
746 TCHAR* stackString1 = _T("beta");
747 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(stackString1));
748 }
749 m.RunUntilTick(0);
750
751 ///assert
752 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("alfa"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[0]);
753 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("beta"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[1]);
754 }
755
756 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_3)
757 {
758 ///arrange
759 CMocksForParameterBuffers m(someTask);
760
761 ///act
762 TCHAR *var;
763 {
764 var= _T("alfa");
765 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(var));
766 }
767
768 {
769 var= _T("beta");
770 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(var));
771 }
772 m.RunUntilTick(0);
773
774 ///assert
775 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("alfa"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[0]);
776 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("beta"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[1]);
777 }
778
779 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_4)
780 {
781 ///arrange
782 CMocksForParameterBuffers m(someTask);
783
784 ///act
785 TCHAR var[100];
786 {
787 _tcscpy_s (var,_T("alfa"));
788 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(var));
789 }
790
791 {
792 _tcscpy_s (var, _T("beta"));
793 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAString(var));
794 }
795 m.RunUntilTick(0);
796
797 ///assert
798 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("alfa"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[0]);
799 ASSERT_ARE_EQUAL(LPCTSTR, (LPCTSTR)_T("beta"), (LPCTSTR)g_STIM_FunctionThatTakesAStringParameter[1]);
800 }
801
802 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_5)
803 {
804 ///arrange
805 CMocksForParameterBuffers m(someTask);
806
807 ///act
808 int var1, var2;
809 {
810 var1=1;
811 var2=11;
812 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAPointerToInt(&var1));
813 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAInt(var2));
814 }
815
816 {
817 var1=2;
818 var2=22;
819 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAPointerToInt(&var1));
820 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAInt(var2));
821 }
822 m.RunUntilTick(0);
823
824 ///assert
825 ASSERT_ARE_EQUAL(int, 1, g_STIM_FunctionThatTakesAPointerToInt[0]);
826 ASSERT_ARE_EQUAL(int, 2, g_STIM_FunctionThatTakesAPointerToInt[1]);
827 ASSERT_ARE_EQUAL(int, 11, g_STIM_FunctionThatTakesAInt[0]);
828 ASSERT_ARE_EQUAL(int, 22, g_STIM_FunctionThatTakesAInt[1]);
829 }
830
831 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_understands_NULL_buffer)
832 {
833 ///arrange
834 CMocksForParameterBuffers m(someTask);
835
836 ///act
837 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArray(NULL));
838 m.RunUntilTick(0);
839
840 ///assert
841 ASSERT_ARE_EQUAL(bool, true, g_STIM_FunctionThatTakesAnArrayWasParameterNULL);
842 }
843
844
845 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_understands_buffer_of_1_element)
846 {
847 ///arrange
848 CMocksForParameterBuffers m(someTask);
849 int var=7;
850 g_STIM_FunctionThatTakesAnArrayNParameters=1; /*since the STIM doesn't really know how many they are*/
851
852 ///act
853 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArray(&var));
854 m.RunUntilTick(0);
855
856 ///assert
857 ASSERT_ARE_EQUAL(int, 7, g_STIM_FunctionThatTakesAnArray[0][0]);
858 }
859
860 TEST_FUNCTION(TimeDiscreteMicroMock_ParameterBuffersAreSaved_understands_buffer_of_5_elements)
861 {
862 ///arrange
863 CMocksForParameterBuffers m(someTask);
864 int var[5]={1,2,3,4,5};
865 g_STIM_FunctionThatTakesAnArrayNParameters=5; /*since the STIM doesn't really know how many they are*/
866
867 ///act
868 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArray(var))
869 .setArraySize(1, 5);
870 m.RunUntilTick(0);
871
872 ///assert
873 ASSERT_ARE_EQUAL(int, 1, g_STIM_FunctionThatTakesAnArray[0][0]);
874 ASSERT_ARE_EQUAL(int, 2, g_STIM_FunctionThatTakesAnArray[0][1]);
875 ASSERT_ARE_EQUAL(int, 3, g_STIM_FunctionThatTakesAnArray[0][2]);
876 ASSERT_ARE_EQUAL(int, 4, g_STIM_FunctionThatTakesAnArray[0][3]);
877 ASSERT_ARE_EQUAL(int, 5, g_STIM_FunctionThatTakesAnArray[0][4]);
878 }
879
880 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_NULL_NULL)
881 {
882 ///arrange
883 CMocksForParameterBuffers m(someTask);
884
885 ///act
886 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(NULL, 0, NULL, 0));
887 m.RunUntilTick(0);
888
889 ///assert
890 ASSERT_ARE_EQUAL(bool, true, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
891 ASSERT_ARE_EQUAL(bool, true, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
892 }
893
894 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_NULL_1int)
895 {
896 ///arrange
897 CMocksForParameterBuffers m(someTask);
898 int v2=2;
899
900 ///act
901 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(NULL, 0, &v2, 1));
902 m.RunUntilTick(0);
903
904 //assert
905 ASSERT_ARE_EQUAL(bool, true, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
906 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
907 ASSERT_ARE_EQUAL(int, 2, STIM_FunctionThatTakesTwoArrays_array2_copy[0]);
908 }
909
910 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_NULL_3int)
911 {
912 ///arrange
913 CMocksForParameterBuffers m(someTask);
914 int v2[3]={21,22,23};
915
916 ///act
917 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(NULL, 0, v2, 3))
918 .setArraySize(3, 3);
919 m.RunUntilTick(0);
920
921 ///assert
922 ASSERT_ARE_EQUAL(bool, true, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
923 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
924 ASSERT_ARE_EQUAL(int, 21, STIM_FunctionThatTakesTwoArrays_array2_copy[0]);
925 ASSERT_ARE_EQUAL(int, 22, STIM_FunctionThatTakesTwoArrays_array2_copy[1]);
926 ASSERT_ARE_EQUAL(int, 23, STIM_FunctionThatTakesTwoArrays_array2_copy[2]);
927 }
928
929 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_1int_NULL)
930 {
931 ///arrange
932 CMocksForParameterBuffers m(someTask);
933 int v1=3;
934
935 ///act
936 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(&v1, 1, NULL, 0));
937 m.RunUntilTick(0);
938
939 ///assert
940 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
941 ASSERT_ARE_EQUAL(bool, true, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
942 ASSERT_ARE_EQUAL(int, 3, STIM_FunctionThatTakesTwoArrays_array1_copy[0]);
943 }
944
945 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_1int_1int)
946 {
947 ///arrange
948 CMocksForParameterBuffers m(someTask);
949 int v1=11;
950 int v2=22;
951
952 ///act
953 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(&v1, 1, &v2, 1));
954 m.RunUntilTick(0);
955
956 ///assert
957 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
958 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
959 ASSERT_ARE_EQUAL(int, 11, STIM_FunctionThatTakesTwoArrays_array1_copy[0]);
960 ASSERT_ARE_EQUAL(int, 22, STIM_FunctionThatTakesTwoArrays_array2_copy[0]);
961 }
962
963 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_1int_3int)
964 {
965 ///arrange
966 CMocksForParameterBuffers m(someTask);
967 int v1=11;
968 int v2[3]={22,23,24};
969
970 ///act
971 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(&v1, 1, v2, 3))
972 .setArraySize(3,3);
973 m.RunUntilTick(0);
974
975 ///assert
976 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
977 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
978 ASSERT_ARE_EQUAL(int, 11, STIM_FunctionThatTakesTwoArrays_array1_copy[0]);
979 ASSERT_ARE_EQUAL(int, 22, STIM_FunctionThatTakesTwoArrays_array2_copy[0]);
980 ASSERT_ARE_EQUAL(int, 23, STIM_FunctionThatTakesTwoArrays_array2_copy[1]);
981 ASSERT_ARE_EQUAL(int, 24, STIM_FunctionThatTakesTwoArrays_array2_copy[2]);
982 }
983
984 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_3int_NULL)
985 {
986 ///arrange
987 CMocksForParameterBuffers m(someTask);
988 int v1[3]={11,13,15};
989
990 ///act
991 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(v1, 3, NULL, 0))
992 .setArraySize(1,3);
993 m.RunUntilTick(0);
994
995 ///assert
996 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
997 ASSERT_ARE_EQUAL(bool, true, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
998 ASSERT_ARE_EQUAL(int, 11, STIM_FunctionThatTakesTwoArrays_array1_copy[0]);
999 ASSERT_ARE_EQUAL(int, 13, STIM_FunctionThatTakesTwoArrays_array1_copy[1]);
1000 ASSERT_ARE_EQUAL(int, 15, STIM_FunctionThatTakesTwoArrays_array1_copy[2]);
1001 }
1002
1003 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_3int_1int)
1004 {
1005 ///arrange
1006 CMocksForParameterBuffers m(someTask);
1007 int v1[3]={11,13,15};
1008 int v2=99;
1009
1010 ///act
1011 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(v1, 3, &v2, 1))
1012 .setArraySize(1,3);
1013 m.RunUntilTick(0);
1014
1015 ///assert
1016 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
1017 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
1018 ASSERT_ARE_EQUAL(int, 11, STIM_FunctionThatTakesTwoArrays_array1_copy[0]);
1019 ASSERT_ARE_EQUAL(int, 13, STIM_FunctionThatTakesTwoArrays_array1_copy[1]);
1020 ASSERT_ARE_EQUAL(int, 15, STIM_FunctionThatTakesTwoArrays_array1_copy[2]);
1021 ASSERT_ARE_EQUAL(int, 99, STIM_FunctionThatTakesTwoArrays_array2_copy[0]);
1022 }
1023
1024 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_with_2buffers_still_works_3int_4int)
1025 {
1026 ///arrange
1027 CMocksForParameterBuffers m(someTask);
1028 int v1[3]={11,13,15};
1029 int v2[4]={23,25,27, 29};
1030
1031 ///act
1032 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesTwoArrays(v1, 3, v2, 4))
1033 .setArraySize(1,3)
1034 .setArraySize(3,4);
1035 m.RunUntilTick(0);
1036
1037 ///assert
1038 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array1_called_pointer);
1039 ASSERT_ARE_EQUAL(bool, false, NULL==STIM_FunctionThatTakesTwoArrays_array2_called_pointer);
1040 ASSERT_ARE_EQUAL(int, 11, STIM_FunctionThatTakesTwoArrays_array1_copy[0]);
1041 ASSERT_ARE_EQUAL(int, 13, STIM_FunctionThatTakesTwoArrays_array1_copy[1]);
1042 ASSERT_ARE_EQUAL(int, 15, STIM_FunctionThatTakesTwoArrays_array1_copy[2]);
1043 ASSERT_ARE_EQUAL(int, 23, STIM_FunctionThatTakesTwoArrays_array2_copy[0]);
1044 ASSERT_ARE_EQUAL(int, 25, STIM_FunctionThatTakesTwoArrays_array2_copy[1]);
1045 ASSERT_ARE_EQUAL(int, 27, STIM_FunctionThatTakesTwoArrays_array2_copy[2]);
1046 ASSERT_ARE_EQUAL(int, 29, STIM_FunctionThatTakesTwoArrays_array2_copy[3]);
1047 }
1048
1049 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_can_be_reused)
1050 {
1051 ///arrange
1052 CMocksForParameterBuffers m(someTask);
1053 g_STIM_FunctionThatTakesAnArrayNParameters =2;
1054
1055 ///act
1056 int var1[2]={11,12};
1057 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArray(var1))
1058 .setArraySize(1, 2);
1059 var1[0]=22;
1060 var1[1]=23;
1061 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArray(var1))
1062 .setArraySize(1, 2);
1063 m.RunUntilTick(1);
1064
1065 //assert
1066 ASSERT_ARE_EQUAL(int, 11, g_STIM_FunctionThatTakesAnArray[0][0]);
1067 ASSERT_ARE_EQUAL(int, 12, g_STIM_FunctionThatTakesAnArray[0][1]);
1068
1069 ASSERT_ARE_EQUAL(int, 22, g_STIM_FunctionThatTakesAnArray[1][0]);
1070 ASSERT_ARE_EQUAL(int, 23, g_STIM_FunctionThatTakesAnArray[1][1]);
1071
1072 }
1073
1074 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_can_be_reused_at_different_times)
1075 {
1076 ///arrange
1077 CMocksForParameterBuffers m(someTask);
1078 g_STIM_FunctionThatTakesAnArrayNParameters =2;
1079
1080 ///act
1081 int var1[2]={11,12};
1082 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArray(var1))
1083 .setArraySize(1, 2);
1084 var1[0]=22;
1085 var1[1]=23;
1086 STIM_CALL_AT(m, 1, STIM_FunctionThatTakesAnArray(var1))
1087 .setArraySize(1, 2);
1088 m.RunUntilTick(1);
1089
1090 ///assert
1091 ASSERT_ARE_EQUAL(int, 11, g_STIM_FunctionThatTakesAnArray[0][0]);
1092 ASSERT_ARE_EQUAL(int, 12, g_STIM_FunctionThatTakesAnArray[0][1]);
1093 ASSERT_ARE_EQUAL(int, 22, g_STIM_FunctionThatTakesAnArray[1][0]);
1094 ASSERT_ARE_EQUAL(int, 23, g_STIM_FunctionThatTakesAnArray[1][1]);
1095 }
1096
1097 void justForStackFrame(CMocksForParameterBuffers& m)
1098 {
1099
1100 someSortOfT var1[2];
1101 var1[0]=someSortOfT(_T("thisIsTheString00"), "thisIsTheCharP00");
1102 var1[1]=someSortOfT(_T("thisIsTheString01"), "thisIsTheCharP01");
1103 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAnArrayOfObjects(var1))
1104 .setArraySize(1,2);
1105 }
1106
1107 /*found as a code review item*/
1108 TEST_FUNCTION(TimeDiscreteMicroMock_A_STIM_can_copy_an_array_of_objects)
1109 {
1110 ///arrange
1111 CMocksForParameterBuffers m(someTask);
1112
1113 ///act
1114 for(int i=0;i<1;i++)
1115 {
1116 justForStackFrame(m); /*hopefully the stack content is destroyed here*/
1117 }
1118 m.RunUntilTick(0);
1119
1120 ///assert
1121 someSortOfT var0=someSortOfT(_T("thisIsTheString00"), "thisIsTheCharP00");
1122 someSortOfT var1=someSortOfT(_T("thisIsTheString01"), "thisIsTheCharP01");
1123 ASSERT_ARE_EQUAL(someSortOfT, var0, g_STIM_FunctionThatTakesAnArrayOfObjects[0][0]);
1124 ASSERT_ARE_EQUAL(someSortOfT, var1, g_STIM_FunctionThatTakesAnArrayOfObjects[0][1]);
1125 }
1126
1127 /*found from code review*/
1128 TEST_FUNCTION(TimeDiscreteMicroMock_FailsWhenUserCallsDirectly_setArraySize)
1129 {
1130 CMocksForParameterBuffers m(someTask);
1131 try
1132 {
1133 m.STIM_FunctionThatTakesAnArrayOfObjects.setArraySize(1,2);
1134 }
1135 catch(CMicroMockException& c)
1136 {
1137 ASSERT_ARE_EQUAL_WITH_MSG(MICROMOCK_EXCEPTION, MICROMOCK_EXCEPTION_SET_ARRAY_SIZE_BEFORE_CALL, c.GetMicroMockExceptionCode(), _T("CMicroMockException was thrown, but the expected code was not right"));
1138 }
1139 catch(...)
1140 {
1141 ASSERT_FAIL(_T("unexpected exception thrown, was excepting CMicroMock with code MICROMOCK_EXCEPTION_SET_ARRAY_SIZE_BEFORE_CALL"));
1142 }
1143 }
1144
1145 /*found from code review*/
1146 TEST_FUNCTION(TimeDiscreteMicroMock_FailsWhenUserCallsDirectly_setTime)
1147 {
1148 CMocksForParameterBuffers m(someTask);
1149 try
1150 {
1151 m.STIM_FunctionThatTakesAnArrayOfObjects.SetTime(1,1);
1152 }
1153 catch(CMicroMockException& c)
1154 {
1155 ASSERT_ARE_EQUAL_WITH_MSG(MICROMOCK_EXCEPTION, MICROMOCK_EXCEPTION_SET_TIME_BEFORE_CALL, c.GetMicroMockExceptionCode(), _T("CMicroMockException was thrown, but the expected code was not right"));
1156 }
1157 catch(...)
1158 {
1159 ASSERT_FAIL(_T("unexpected exception thrown, was excepting CMicroMock with code MICROMOCK_EXCEPTION_SET_TIME_BEFORE_CALL"));
1160 }
1161 }
1162
1163 TEST_FUNCTION(TimeDiscreteMicroMock_Const_ParameterBuffersAreSaved_understands_NULL_buffer)
1164 {
1165 ///arrange
1166 CMocksForParameterBuffers m(someTask);
1167
1168 ///act
1169 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAConstArray(NULL));
1170 m.RunUntilTick(0);
1171
1172 ///assert
1173 ASSERT_ARE_EQUAL(bool, true, g_STIM_FunctionThatTakesAConstArrayWasParameterNULL);
1174 }
1175
1176
1177 TEST_FUNCTION(TimeDiscreteMicroMock_Const_ParameterBuffersAreSaved_understands_buffer_of_1_element)
1178 {
1179 ///arrange
1180 CMocksForParameterBuffers m(someTask);
1181 int var=7;
1182 g_STIM_FunctionThatTakesAConstArrayNParameters=1; /*since the STIM doesn't really know how many they are*/
1183
1184 ///act
1185 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAConstArray(&var));
1186 m.RunUntilTick(0);
1187
1188 ///assert
1189 ASSERT_ARE_EQUAL(int, 7, g_STIM_FunctionThatTakesAConstArray[0][0]);
1190 }
1191
1192 TEST_FUNCTION(TimeDiscreteMicroMock_Const_ParameterBuffersAreSaved_understands_buffer_of_5_elements)
1193 {
1194 ///arrange
1195 CMocksForParameterBuffers m(someTask);
1196 int var[5]={1,2,3,4,5};
1197 g_STIM_FunctionThatTakesAConstArrayNParameters=5; /*since the STIM doesn't really know how many they are*/
1198
1199 ///act
1200 STIM_CALL_AT(m, 0, STIM_FunctionThatTakesAConstArray(var))
1201 .setArraySize(1, 5);
1202 m.RunUntilTick(0);
1203
1204 ///assert
1205 ASSERT_ARE_EQUAL(int, 1, g_STIM_FunctionThatTakesAConstArray[0][0]);
1206 ASSERT_ARE_EQUAL(int, 2, g_STIM_FunctionThatTakesAConstArray[0][1]);
1207 ASSERT_ARE_EQUAL(int, 3, g_STIM_FunctionThatTakesAConstArray[0][2]);
1208 ASSERT_ARE_EQUAL(int, 4, g_STIM_FunctionThatTakesAConstArray[0][3]);
1209 ASSERT_ARE_EQUAL(int, 5, g_STIM_FunctionThatTakesAConstArray[0][4]);
1210 }
1211
1212 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_baseline)
1213 {
1214 MyMocks3 m(someTaskThatCallsWhenTwoAtTimeEqualThree);
1215 STRICT_EXPECTED_CALL_AT(m,3, whentwo("two", -4));
1216 m.RunUntilTick(10);
1217 }
1218
1219 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_using_without_strict_with_real_time)
1220 {
1221 MyMocks3 m(someTaskThatCallsWhenTwoAtTimeEqualThree);
1222 EXPECTED_CALL_AT(m, 3, whentwo("two_tee_at_two", +4)); /*EXPECTED just makes hte call, doesn't validate parameters*/
1223 m.RunUntilTick(10);
1224 }
1225
1226 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_time_mismatch_is_detected)
1227 {
1228 MyMocks3 m(someTaskThatCallsWhenTwoAtTimeEqualThree);
1229 STRICT_EXPECTED_CALL_AT(m, 4, whentwo("two", -4));
1230 m.RunUntilTick(10);
1231 ASSERT_ARE_EQUAL(tstring, tstring(_T("[Expected:whentwo(two,-4,4)][Actual:whentwo(two,-4,3)]")), m.CompareActualAndExpectedCalls());
1232 }
1233
1234 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_time_mismatch_is_detected_without_STRICT)
1235 {
1236 /*I want this to fail because time doesn't match*/
1237 MyMocks3 m(someTaskThatCallsWhenTwoAtTimeEqualThree);
1238 EXPECTED_CALL_AT(m, 4, whentwo("two_tee_at_two", +4));
1239 m.RunUntilTick(10);
1240 ASSERT_ARE_EQUAL(tstring, tstring(_T("[Expected:whentwo(two_tee_at_two,4,4)][Actual:whentwo(two,-4,3)]")), m.CompareActualAndExpectedCalls());
1241 }
1242
1243 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_IgnoreArgument)
1244 {
1245 /*this must fail, since time is not ignore-able*/
1246 MyMocks3 m(someTaskThatCallsWhenTwoAtTimeEqualThree);
1247 STRICT_EXPECTED_CALL_AT(m, 4, whentwo("two", -4))
1248 .IgnoreArgument(3);
1249 m.RunUntilTick(10);
1250 ASSERT_ARE_EQUAL(tstring, tstring(_T("[Expected:whentwo(two,-4,4)][Actual:whentwo(two,-4,3)]")), m.CompareActualAndExpectedCalls());
1251 }
1252
1253 TEST_FUNCTION(TimeDiscreteMicroMock_should_never_ignore_time_IgnoreAllArguments)
1254 {
1255 /*I want this to be user test code error*/
1256 MyMocks3 m(someTaskThatCallsWhenTwoAtTimeEqualThree);
1257 STRICT_EXPECTED_CALL_AT(m, 4, whentwo("two", -4))
1258 .IgnoreAllArguments();
1259 m.RunUntilTick(10);
1260 ASSERT_ARE_EQUAL(tstring, tstring(_T("[Expected:whentwo(two,-4,4)][Actual:whentwo(two,-4,3)]")), m.CompareActualAndExpectedCalls());
1261 }
1262 #endif
1263 END_TEST_SUITE(MicroMockStimTest);
1264