Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/testtools/micromock/inc/mockvalue.h

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 #ifndef MOCKVALUE_H
5 #define MOCKVALUE_H
6
7 #pragma once
8
9 #include "stdafx.h"
10 #include "mockvaluebase.h"
11
12 #include <sstream>
13
14
15 template<typename T>
16 class CMockValue :
17 public CMockValueBase
18 {
19 public:
20 CMockValue(_In_ const T& value) : m_Value(value), m_OriginalValue(m_Value)
21 {
22 }
23
24 virtual ~CMockValue()
25 {
26 }
27
28 virtual std::tstring ToString() const
29 {
30 std::tostringstream strStream;
31 strStream << m_Value;
32 return strStream.str();
33 }
34
35 virtual bool EqualTo(_In_ const CMockValueBase* right)
36 {
37 return (*this == *(reinterpret_cast<const CMockValue<T>*>(right)));
38 }
39
40 void SetValue(_In_ const T& value)
41 {
42 m_Value = value;
43 m_OriginalValue = value;
44 }
45
46 const T& GetValue() const
47 {
48 return m_Value;
49 }
50
51 protected:
52 T m_Value;
53 T m_OriginalValue;
54 };
55
56 template<>
57 class CMockValue <unsigned char>:
58 public CMockValueBase
59 {
60 public:
61 CMockValue(_In_ const unsigned char& value) : m_Value(value), m_OriginalValue(value)
62 {
63 }
64
65 virtual ~CMockValue()
66 {
67 }
68
69 virtual std::tstring ToString() const
70 {
71 std::tostringstream strStream;
72 strStream << (unsigned int)m_Value;
73 return strStream.str();
74 }
75
76 virtual bool EqualTo(_In_ const CMockValueBase* right)
77 {
78 return (this->m_Value == (reinterpret_cast<const CMockValue<unsigned char>*>(right))->m_Value);
79 }
80
81 void SetValue(_In_ const unsigned char& value)
82 {
83 m_Value = value;
84 m_OriginalValue = value;
85 }
86
87 const unsigned char& GetValue() const
88 {
89 return m_Value;
90 }
91
92 protected:
93 unsigned char m_Value;
94 unsigned char m_OriginalValue;
95 };
96
97 template<>
98 class CMockValue <void> :
99 public CMockValueBase
100 {
101 public:
102 CMockValue(void)
103 {
104 }
105
106 virtual ~CMockValue()
107 {
108 }
109
110 virtual std::tstring ToString() const
111 {
112 std::tostringstream strStream;
113 strStream << "void";
114 return strStream.str();
115 }
116
117 virtual bool EqualTo(_In_ const CMockValueBase* right)
118 {
119 (void)right;
120 return true;
121 }
122
123 void SetValue(void)
124 {
125
126 }
127
128 void GetValue() const
129 {
130 return;
131 }
132
133 protected:
134 unsigned char m_Value;
135 unsigned char m_OriginalValue;
136 };
137
138 template<>
139 class CMockValue <wchar_t *> :
140 public CMockValueBase
141 {
142 public:
143 CMockValue(_In_ const wchar_t * value)
144 {
145 if (value == NULL)
146 {
147 m_Value = L"NULL";
148 m_OriginalValue = L"NULL";
149 }
150 else
151 {
152 m_Value = value;
153 m_OriginalValue = value;
154 }
155 }
156
157 virtual ~CMockValue()
158 {
159 }
160
161 virtual std::tstring ToString() const
162 {
163 #ifdef _MSC_VER
164 #pragma warning(push)
165 #pragma warning(disable: 4244)
166 #endif // _MSC_VER
167 std::string temp(m_Value.begin(), m_Value.end());
168
169 std::tostringstream strStream;
170 strStream << temp.c_str();
171 return strStream.str();
172 #ifdef _MSC_VER
173 #pragma warning(pop)
174 #endif // _MSC_VER
175 }
176
177 virtual bool EqualTo(_In_ const CMockValueBase* right)
178 {
179 return (this->m_Value == (reinterpret_cast<const CMockValue<wchar_t *>*>(right))->m_Value);
180 }
181
182 void SetValue(_In_ const wchar_t* value)
183 {
184 if (value == NULL)
185 {
186 m_Value = L"NULL";
187 m_OriginalValue = L"NULL";
188 }
189 else
190 {
191 m_Value = value;
192 m_OriginalValue = value;
193 }
194 }
195
196 const wchar_t* GetValue() const
197 {
198 return m_Value.c_str();
199 }
200
201 protected:
202 std::wstring m_Value;
203 std::wstring m_OriginalValue;
204 };
205
206 template<>
207 class CMockValue <const wchar_t *> :
208 public CMockValueBase
209 {
210 public:
211 CMockValue(_In_ const wchar_t * value)
212 {
213 if (value == NULL)
214 {
215 m_Value = L"NULL";
216 m_OriginalValue = L"NULL";
217 }
218 else
219 {
220 m_Value = value;
221 m_OriginalValue = value;
222 }
223 }
224
225 virtual ~CMockValue()
226 {
227 }
228
229 virtual std::tstring ToString() const
230 {
231 #ifdef _MSC_VER
232 #pragma warning(push)
233 #pragma warning(disable: 4244)
234 #endif // _MSC_VER
235 std::string temp(m_Value.begin(), m_Value.end());
236
237 std::tostringstream strStream;
238 strStream << temp.c_str();
239 return strStream.str();
240 #ifdef _MSC_VER
241 #pragma warning(pop)
242 #endif // _MSC_VER
243 }
244
245 virtual bool EqualTo(_In_ const CMockValueBase* right)
246 {
247 return (this->m_Value == (reinterpret_cast<const CMockValue<const wchar_t *>*>(right))->m_Value);
248 }
249
250 void SetValue(_In_ const wchar_t* value)
251 {
252 if (value == NULL)
253 {
254 m_Value = L"NULL";
255 m_OriginalValue = L"NULL";
256 }
257 else
258 {
259 m_Value = value;
260 m_OriginalValue = value;
261 }
262 }
263
264 const wchar_t* GetValue() const
265 {
266 return m_Value.c_str();
267 }
268
269 protected:
270 std::wstring m_Value;
271 std::wstring m_OriginalValue;
272 };
273
274 template<typename T>
275 class CMockValue <T*> :
276 public CMockValueBase
277 {
278 public:
279 CMockValue(_In_ T* value)
280 {
281 m_Value = value;
282 m_OriginalValue = value;
283 }
284
285 virtual ~CMockValue()
286 {
287 }
288
289 virtual std::tstring ToString() const
290 {
291 std::tostringstream strStream;
292 if (NULL == m_Value)
293 {
294 strStream << _T("NULL");
295 }
296 else
297 {
298 strStream << m_Value;
299 }
300 return strStream.str();
301 }
302
303 virtual bool EqualTo(_In_ const CMockValueBase* right)
304 {
305 return (*this == *(reinterpret_cast<const CMockValue<T*>*>(right)));
306 }
307
308 void SetValue(_In_ T* value)
309 {
310 m_Value = value;
311 m_OriginalValue = value;
312 }
313
314 T* GetValue() const
315 {
316 return m_Value;
317 }
318
319 protected:
320 T* m_Value;
321 T* m_OriginalValue;
322 };
323
324 template<typename T, size_t N>
325 class CMockValue <T[N]> :
326 public CMockValueBase
327 {
328 public:
329 typedef struct arraySave_TAG
330 {
331 bool isNULL;
332 T* copy;
333 }arraySave;
334
335 typedef T T_Array_N[N];
336 CMockValue(_In_ T* value)
337 {
338 m_OriginalValue = value;
339 if (value == NULL)
340 {
341 m_Value.isNULL = true;
342 }
343 else
344 {
345 m_Value.isNULL = false;
346 m_Value.copy = new T[N]();
347 for (size_t i = 0; i < N;i++)
348 {
349 m_Value.copy[i] = value[i];
350 }
351 }
352 }
353
354 virtual ~CMockValue()
355 {
356 if (m_Value.isNULL ==false)
357 {
358 delete[] m_Value.copy;
359 }
360 }
361
362 virtual std::tstring ToString() const
363 {
364 std::tostringstream strStream;
365 if (m_Value.isNULL)
366 {
367 strStream << "NULL";
368 }
369 else
370 {
371 strStream << "{";
372 for (size_t i = 0; i < N; i++)
373 {
374 if (i > 0)
375 {
376 strStream << ",";
377 }
378
379 strStream << m_Value.copy[i];
380 }
381 strStream << "}";
382 }
383 return strStream.str();
384 }
385
386 virtual bool EqualTo(_In_ const CMockValueBase* right)
387 {
388 /*this will be interesting*/
389 return (*this == *(reinterpret_cast<const CMockValue<T_Array_N>*>(right)));
390 }
391
392 void SetValue(_In_ T* value)
393 {
394 m_OriginalValue = value;
395 if (m_Value.isNULL == true)
396 {
397 if (value == NULL)
398 {
399 /*do nothing*/
400 }
401 else
402 {
403 m_Value.isNULL = false;
404 m_Value.copy = new T[N];
405 for (size_t i = 0; i < N; i++)
406 {
407 m_Value.copy[i] = value[i];
408 }
409 }
410 }
411 else
412 {
413 if (value == NULL)
414 {
415 m_Value.isNULL = true;
416 delete[]m_Value.copy;
417 }
418 else
419 {
420 if (value == m_Value.copy)
421 {
422 /*do nothing, we are copying from us to us*/
423 }
424 else
425 {
426 m_Value.isNULL = false;
427 delete[] m_Value.copy;
428 m_Value.copy = new T[N];
429 for (size_t i = 0; i < N; i++)
430 {
431 m_Value.copy[i] = value[i];
432 }
433 }
434 }
435 }
436 }
437
438 const T* GetValue() const
439 {
440 if (m_Value.isNULL == true)
441 {
442 return NULL;
443 }
444 else
445 {
446 return m_Value.copy;
447 }
448 }
449
450 protected:
451 arraySave m_Value;
452 T* m_OriginalValue;
453 };
454
455 template<typename T, size_t N>
456 class CMockValue <const T[N]> :
457 public CMockValueBase
458 {
459 public:
460 typedef struct arraySave_TAG
461 {
462 bool isNULL;
463 T* copy;
464 }arraySave;
465
466 typedef T T_Array_N[N];
467 CMockValue(_In_ const T* value)
468 {
469 m_OriginalValue = value;
470 if (value == NULL)
471 {
472 m_Value.isNULL = true;
473 }
474 else
475 {
476 m_Value.isNULL = false;
477 m_Value.copy = new T[N]();
478 for (size_t i = 0; i < N; i++)
479 {
480 m_Value.copy[i] = value[i];
481 }
482 }
483 }
484
485 virtual ~CMockValue()
486 {
487 if (m_Value.isNULL == false)
488 {
489 delete[] m_Value.copy;
490 }
491 }
492
493 virtual std::tstring ToString() const
494 {
495 std::tostringstream strStream;
496 if (m_Value.isNULL)
497 {
498 strStream << "NULL";
499 }
500 else
501 {
502 strStream << "{";
503 for (size_t i = 0; i < N; i++)
504 {
505 if (i > 0)
506 {
507 strStream << ",";
508 }
509
510 strStream << m_Value.copy[i];
511 }
512 strStream << "}";
513 }
514 return strStream.str();
515 }
516
517 virtual bool EqualTo(_In_ const CMockValueBase* right)
518 {
519 /*this will be interesting*/
520 return (*this == *(reinterpret_cast<const CMockValue<const T_Array_N>*>(right)));
521 }
522
523 void SetValue(_In_ const T* value)
524 {
525 m_OriginalValue = value;
526 if (m_Value.isNULL == true)
527 {
528 if (value == NULL)
529 {
530 /*do nothing*/
531 }
532 else
533 {
534 m_Value.isNULL = false;
535 m_Value.copy = new T[N];
536 for (size_t i = 0; i < N; i++)
537 {
538 m_Value.copy[i] = value[i];
539 }
540 }
541 }
542 else
543 {
544 if (value == NULL)
545 {
546 m_Value.isNULL = true;
547 delete[]m_Value.copy;
548 }
549 else
550 {
551 if (value == m_Value.copy)
552 {
553 /*do nothing, we are copying from us to us*/
554 }
555 else
556 {
557 m_Value.isNULL = false;
558 delete[] m_Value.copy;
559 m_Value.copy = new T[N];
560 for (size_t i = 0; i < N; i++)
561 {
562 m_Value.copy[i] = value[i];
563 }
564 }
565 }
566 }
567 }
568
569 const T* GetValue() const
570 {
571 if (m_Value.isNULL == true)
572 {
573 return NULL;
574 }
575 else
576 {
577 return m_Value.copy;
578 }
579 }
580
581 protected:
582 arraySave m_Value;
583 const T* m_OriginalValue;
584 };
585
586
587 template<typename T>
588 static bool operator==(_In_ const CMockValue<T>& lhs, _In_ const CMockValue<T>& rhs)
589 {
590 return lhs.GetValue() == rhs.GetValue();
591 }
592
593 template<typename T, size_t N>
594 static bool operator==(_In_ const CMockValue<T[N]>& lhs, _In_ const CMockValue<T[N]>& rhs)
595 {
596 /*comparing two arrays needs to be handled differently than the build in == operator*/
597 return(strcmp(lhs.ToString().c_str(), rhs.ToString().c_str()) == 0);
598 }
599
600 #endif // MOCKVALUE_H