]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Test/GoogleTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.cpp
MdePkg/Test: Add port of BaseSafeIntLib unit tests to GoogleTest
[mirror_edk2.git] / MdePkg / Test / GoogleTest / Library / BaseSafeIntLib / TestBaseSafeIntLib.cpp
1 /** @file
2 UEFI OS based application for unit testing the SafeIntLib.
3
4 Copyright (c) Microsoft Corporation.<BR>
5 Copyright (c) 2018 - 2022, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include <gtest/gtest.h>
10 extern "C" {
11 #include <Base.h>
12 #include <Library/SafeIntLib.h>
13 }
14
15 //
16 // Conversion function tests:
17 //
18 TEST(ConversionTestSuite, TestSafeInt8ToUint8) {
19 RETURN_STATUS Status;
20 INT8 Operand;
21 UINT8 Result;
22
23 //
24 // Positive UINT8 should result in just a cast
25 //
26 Operand = 0x5b;
27 Result = 0;
28 Status = SafeInt8ToUint8 (Operand, &Result);
29 ASSERT_EQ (Status, RETURN_SUCCESS);
30 ASSERT_EQ (0x5b, Result);
31
32 //
33 // Negative number should result in an error status
34 //
35 Operand = (-56);
36 Status = SafeInt8ToUint8 (Operand, &Result);
37 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
38 }
39
40 TEST(ConversionTestSuite, TestSafeInt8ToUint16) {
41 RETURN_STATUS Status;
42 INT8 Operand;
43 UINT16 Result;
44
45 //
46 // Positive UINT8 should result in just a cast
47 //
48 Operand = 0x5b;
49 Result = 0;
50 Status = SafeInt8ToUint16 (Operand, &Result);
51 ASSERT_EQ (Status, RETURN_SUCCESS);
52 ASSERT_EQ (0x5b, Result);
53
54 //
55 // Negative number should result in an error status
56 //
57 Operand = (-56);
58 Status = SafeInt8ToUint16 (Operand, &Result);
59 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
60 }
61
62 TEST(ConversionTestSuite, TestSafeInt8ToUint32) {
63 RETURN_STATUS Status;
64 INT8 Operand;
65 UINT32 Result;
66
67 //
68 // Positive UINT8 should result in just a cast
69 //
70 Operand = 0x5b;
71 Result = 0;
72 Status = SafeInt8ToUint32 (Operand, &Result);
73 ASSERT_EQ (Status, RETURN_SUCCESS);
74 ASSERT_EQ ((UINT32)0x5b, Result);
75
76 //
77 // Negative number should result in an error status
78 //
79 Operand = (-56);
80 Status = SafeInt8ToUint32 (Operand, &Result);
81 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
82 }
83
84 TEST(ConversionTestSuite, TestSafeInt8ToUintn) {
85 RETURN_STATUS Status;
86 INT8 Operand;
87 UINTN Result;
88
89 //
90 // Positive UINT8 should result in just a cast
91 //
92 Operand = 0x5b;
93 Result = 0;
94 Status = SafeInt8ToUintn (Operand, &Result);
95 ASSERT_EQ (Status, RETURN_SUCCESS);
96 ASSERT_EQ ((UINTN)0x5b, Result);
97
98 //
99 // Negative number should result in an error status
100 //
101 Operand = (-56);
102 Status = SafeInt8ToUintn (Operand, &Result);
103 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
104 }
105
106 TEST(ConversionTestSuite, TestSafeInt8ToUint64) {
107 RETURN_STATUS Status;
108 INT8 Operand;
109 UINT64 Result;
110
111 //
112 // Positive UINT8 should result in just a cast
113 //
114 Operand = 0x5b;
115 Result = 0;
116 Status = SafeInt8ToUint64 (Operand, &Result);
117 ASSERT_EQ (Status, RETURN_SUCCESS);
118 ASSERT_EQ ((UINT64)0x5b, Result);
119
120 //
121 // Negative number should result in an error status
122 //
123 Operand = (-56);
124 Status = SafeInt8ToUint64 (Operand, &Result);
125 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
126 }
127
128 TEST(ConversionTestSuite, TestSafeUint8ToInt8) {
129 RETURN_STATUS Status;
130 UINT8 Operand;
131 INT8 Result;
132
133 //
134 // Operand <= 0x7F (MAX_INT8) should result in a cast
135 //
136 Operand = 0x5b;
137 Result = 0;
138 Status = SafeUint8ToInt8 (Operand, &Result);
139 ASSERT_EQ (Status, RETURN_SUCCESS);
140 ASSERT_EQ (0x5b, Result);
141
142 //
143 // Operand larger than 0x7f should result in an error status
144 //
145 Operand = 0xaf;
146 Status = SafeUint8ToInt8 (Operand, &Result);
147 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
148 }
149
150 TEST(ConversionTestSuite, TestSafeUint8ToChar8) {
151 RETURN_STATUS Status;
152 UINT8 Operand;
153 CHAR8 Result;
154
155 //
156 // CHAR8 is typedefed as char, which by default is signed, thus
157 // CHAR8 is same as INT8, so same tests as above:
158 //
159
160 //
161 // Operand <= 0x7F (MAX_INT8) should result in a cast
162 //
163 Operand = 0x5b;
164 Result = 0;
165 Status = SafeUint8ToChar8 (Operand, &Result);
166 ASSERT_EQ (Status, RETURN_SUCCESS);
167 ASSERT_EQ (0x5b, Result);
168
169 //
170 // Operand larger than 0x7f should result in an error status
171 //
172 Operand = 0xaf;
173 Status = SafeUint8ToChar8 (Operand, &Result);
174 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
175 }
176
177 TEST(ConversionTestSuite, TestSafeInt16ToInt8) {
178 RETURN_STATUS Status;
179 INT16 Operand;
180 INT8 Result;
181
182 //
183 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
184 //
185 Operand = 0x5b;
186 Result = 0;
187 Status = SafeInt16ToInt8 (Operand, &Result);
188 ASSERT_EQ (Status, RETURN_SUCCESS);
189 ASSERT_EQ (0x5b, Result);
190
191 Operand = (-35);
192 Status = SafeInt16ToInt8 (Operand, &Result);
193 ASSERT_EQ (Status, RETURN_SUCCESS);
194 ASSERT_EQ ((-35), Result);
195
196 //
197 // Otherwise should result in an error status
198 //
199 Operand = 0x1234;
200 Status = SafeInt16ToInt8 (Operand, &Result);
201 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
202
203 Operand = (-17835);
204 Status = SafeInt16ToInt8 (Operand, &Result);
205 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
206 }
207
208 TEST(ConversionTestSuite, TestSafeInt16ToChar8) {
209 RETURN_STATUS Status;
210 INT16 Operand;
211 CHAR8 Result;
212
213 //
214 // CHAR8 is typedefed as char, which may be signed or unsigned based
215 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
216 //
217
218 //
219 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
220 //
221 Operand = 0x5b;
222 Result = 0;
223 Status = SafeInt16ToChar8 (Operand, &Result);
224 ASSERT_EQ (Status, RETURN_SUCCESS);
225 ASSERT_EQ (0x5b, Result);
226
227 Operand = 0;
228 Result = 0;
229 Status = SafeInt16ToChar8 (Operand, &Result);
230 ASSERT_EQ (Status, RETURN_SUCCESS);
231 ASSERT_EQ (0, Result);
232
233 Operand = MAX_INT8;
234 Result = 0;
235 Status = SafeInt16ToChar8 (Operand, &Result);
236 ASSERT_EQ (Status, RETURN_SUCCESS);
237 ASSERT_EQ (MAX_INT8, Result);
238
239 //
240 // Otherwise should result in an error status
241 //
242 Operand = (-35);
243 Status = SafeInt16ToChar8 (Operand, &Result);
244 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
245
246 Operand = 0x1234;
247 Status = SafeInt16ToChar8 (Operand, &Result);
248 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
249
250 Operand = (-17835);
251 Status = SafeInt16ToChar8 (Operand, &Result);
252 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
253 }
254
255 TEST(ConversionTestSuite, TestSafeInt16ToUint8) {
256 RETURN_STATUS Status;
257 INT16 Operand;
258 UINT8 Result;
259
260 //
261 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
262 //
263 Operand = 0x5b;
264 Result = 0;
265 Status = SafeInt16ToUint8 (Operand, &Result);
266 ASSERT_EQ (Status, RETURN_SUCCESS);
267 ASSERT_EQ (0x5b, Result);
268
269 //
270 // Otherwise should result in an error status
271 //
272 Operand = 0x1234;
273 Status = SafeInt16ToUint8 (Operand, &Result);
274 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
275
276 Operand = (-17835);
277 Status = SafeInt16ToUint8 (Operand, &Result);
278 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
279 }
280
281 TEST(ConversionTestSuite, TestSafeInt16ToUint16) {
282 RETURN_STATUS Status;
283 INT16 Operand = 0x5b5b;
284 UINT16 Result = 0;
285
286 //
287 // If Operand is non-negative, then it's a cast
288 //
289 Status = SafeInt16ToUint16 (Operand, &Result);
290 ASSERT_EQ (Status, RETURN_SUCCESS);
291 ASSERT_EQ (0x5b5b, Result);
292
293 //
294 // Otherwise should result in an error status
295 //
296 Operand = (-17835);
297 Status = SafeInt16ToUint16 (Operand, &Result);
298 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
299 }
300
301 TEST(ConversionTestSuite, TestSafeInt16ToUint32) {
302 RETURN_STATUS Status;
303 INT16 Operand;
304 UINT32 Result;
305
306 //
307 // If Operand is non-negative, then it's a cast
308 //
309 Operand = 0x5b5b;
310 Result = 0;
311 Status = SafeInt16ToUint32 (Operand, &Result);
312 ASSERT_EQ (Status, RETURN_SUCCESS);
313 ASSERT_EQ ((UINT32)0x5b5b, Result);
314
315 //
316 // Otherwise should result in an error status
317 //
318 Operand = (-17835);
319 Status = SafeInt16ToUint32 (Operand, &Result);
320 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
321 }
322
323 TEST(ConversionTestSuite, TestSafeInt16ToUintn) {
324 RETURN_STATUS Status;
325 INT16 Operand;
326 UINTN Result;
327
328 //
329 // If Operand is non-negative, then it's a cast
330 //
331 Operand = 0x5b5b;
332 Result = 0;
333 Status = SafeInt16ToUintn (Operand, &Result);
334 ASSERT_EQ (Status, RETURN_SUCCESS);
335 ASSERT_EQ ((UINTN)0x5b5b, Result);
336
337 //
338 // Otherwise should result in an error status
339 //
340 Operand = (-17835);
341 Status = SafeInt16ToUintn (Operand, &Result);
342 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
343 }
344
345 TEST(ConversionTestSuite, TestSafeInt16ToUint64) {
346 RETURN_STATUS Status;
347 INT16 Operand;
348 UINT64 Result;
349
350 //
351 // If Operand is non-negative, then it's a cast
352 //
353 Operand = 0x5b5b;
354 Result = 0;
355 Status = SafeInt16ToUint64 (Operand, &Result);
356 ASSERT_EQ (Status, RETURN_SUCCESS);
357 ASSERT_EQ ((UINT64)0x5b5b, Result);
358
359 //
360 // Otherwise should result in an error status
361 //
362 Operand = (-17835);
363 Status = SafeInt16ToUint64 (Operand, &Result);
364 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
365 }
366
367 TEST(ConversionTestSuite, TestSafeUint16ToInt8) {
368 RETURN_STATUS Status;
369 UINT16 Operand;
370 INT8 Result;
371
372 //
373 // If Operand is <= MAX_INT8, it's a cast
374 //
375 Operand = 0x5b;
376 Result = 0;
377 Status = SafeUint16ToInt8 (Operand, &Result);
378 ASSERT_EQ (Status, RETURN_SUCCESS);
379 ASSERT_EQ (0x5b, Result);
380
381 //
382 // Otherwise should result in an error status
383 //
384 Operand = (0x5b5b);
385 Status = SafeUint16ToInt8 (Operand, &Result);
386 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
387 }
388
389 TEST(ConversionTestSuite, TestSafeUint16ToChar8) {
390 RETURN_STATUS Status;
391 UINT16 Operand;
392 CHAR8 Result;
393
394 // CHAR8 is typedefed as char, which by default is signed, thus
395 // CHAR8 is same as INT8, so same tests as above:
396
397 //
398 // If Operand is <= MAX_INT8, it's a cast
399 //
400 Operand = 0x5b;
401 Result = 0;
402 Status = SafeUint16ToChar8 (Operand, &Result);
403 ASSERT_EQ (Status, RETURN_SUCCESS);
404 ASSERT_EQ (0x5b, Result);
405
406 //
407 // Otherwise should result in an error status
408 //
409 Operand = (0x5b5b);
410 Status = SafeUint16ToChar8 (Operand, &Result);
411 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
412 }
413
414 TEST(ConversionTestSuite, TestSafeUint16ToUint8) {
415 RETURN_STATUS Status;
416 UINT16 Operand;
417 UINT8 Result;
418
419 //
420 // If Operand is <= MAX_UINT8 (0xff), it's a cast
421 //
422 Operand = 0xab;
423 Result = 0;
424 Status = SafeUint16ToUint8 (Operand, &Result);
425 ASSERT_EQ (Status, RETURN_SUCCESS);
426 ASSERT_EQ (0xab, Result);
427
428 //
429 // Otherwise should result in an error status
430 //
431 Operand = (0x5b5b);
432 Status = SafeUint16ToUint8 (Operand, &Result);
433 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
434 }
435
436 TEST(ConversionTestSuite, TestSafeUint16ToInt16) {
437 RETURN_STATUS Status;
438 UINT16 Operand;
439 INT16 Result;
440
441 //
442 // If Operand is <= MAX_INT16 (0x7fff), it's a cast
443 //
444 Operand = 0x5b5b;
445 Result = 0;
446 Status = SafeUint16ToInt16 (Operand, &Result);
447 ASSERT_EQ (Status, RETURN_SUCCESS);
448 ASSERT_EQ (0x5b5b, Result);
449
450 //
451 // Otherwise should result in an error status
452 //
453 Operand = (0xabab);
454 Status = SafeUint16ToInt16 (Operand, &Result);
455 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
456 }
457
458 TEST(ConversionTestSuite, TestSafeInt32ToInt8) {
459 RETURN_STATUS Status;
460 INT32 Operand;
461 INT8 Result;
462
463 //
464 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
465 //
466 Operand = 0x5b;
467 Result = 0;
468 Status = SafeInt32ToInt8 (Operand, &Result);
469 ASSERT_EQ (Status, RETURN_SUCCESS);
470 ASSERT_EQ (0x5b, Result);
471
472 Operand = (-57);
473 Status = SafeInt32ToInt8 (Operand, &Result);
474 ASSERT_EQ (Status, RETURN_SUCCESS);
475 ASSERT_EQ ((-57), Result);
476
477 //
478 // Otherwise should result in an error status
479 //
480 Operand = (0x5bababab);
481 Status = SafeInt32ToInt8 (Operand, &Result);
482 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
483
484 Operand = (-1537977259);
485 Status = SafeInt32ToInt8 (Operand, &Result);
486 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
487 }
488
489 TEST(ConversionTestSuite, TestSafeInt32ToChar8) {
490 RETURN_STATUS Status;
491 INT32 Operand;
492 CHAR8 Result;
493
494 //
495 // CHAR8 is typedefed as char, which may be signed or unsigned based
496 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
497 //
498
499 //
500 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
501 //
502 Operand = 0x5b;
503 Result = 0;
504 Status = SafeInt32ToChar8 (Operand, &Result);
505 ASSERT_EQ (Status, RETURN_SUCCESS);
506 ASSERT_EQ (0x5b, Result);
507
508 Operand = 0;
509 Result = 0;
510 Status = SafeInt32ToChar8 (Operand, &Result);
511 ASSERT_EQ (Status, RETURN_SUCCESS);
512 ASSERT_EQ (0, Result);
513
514 Operand = MAX_INT8;
515 Result = 0;
516 Status = SafeInt32ToChar8 (Operand, &Result);
517 ASSERT_EQ (Status, RETURN_SUCCESS);
518 ASSERT_EQ (MAX_INT8, Result);
519
520 //
521 // Otherwise should result in an error status
522 //
523 Operand = (-57);
524 Status = SafeInt32ToChar8 (Operand, &Result);
525 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
526
527 Operand = (0x5bababab);
528 Status = SafeInt32ToChar8 (Operand, &Result);
529 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
530
531 Operand = (-1537977259);
532 Status = SafeInt32ToChar8 (Operand, &Result);
533 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
534 }
535
536 TEST(ConversionTestSuite, TestSafeInt32ToUint8) {
537 RETURN_STATUS Status;
538 INT32 Operand;
539 UINT8 Result;
540
541 //
542 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
543 //
544 Operand = 0x5b;
545 Result = 0;
546 Status = SafeInt32ToUint8 (Operand, &Result);
547 ASSERT_EQ (Status, RETURN_SUCCESS);
548 ASSERT_EQ (0x5b, Result);
549
550 //
551 // Otherwise should result in an error status
552 //
553 Operand = (-57);
554 Status = SafeInt32ToUint8 (Operand, &Result);
555 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
556
557 Operand = (0x5bababab);
558 Status = SafeInt32ToUint8 (Operand, &Result);
559 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
560
561 Operand = (-1537977259);
562 Status = SafeInt32ToUint8 (Operand, &Result);
563 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
564 }
565
566 TEST(ConversionTestSuite, TestSafeInt32ToInt16) {
567 RETURN_STATUS Status;
568 INT32 Operand;
569 INT16 Result;
570
571 //
572 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
573 //
574 Operand = 0x5b5b;
575 Result = 0;
576 Status = SafeInt32ToInt16 (Operand, &Result);
577 ASSERT_EQ (Status, RETURN_SUCCESS);
578 ASSERT_EQ (0x5b5b, Result);
579
580 Operand = (-17857);
581 Status = SafeInt32ToInt16 (Operand, &Result);
582 ASSERT_EQ (Status, RETURN_SUCCESS);
583 ASSERT_EQ ((-17857), Result);
584
585 //
586 // Otherwise should result in an error status
587 //
588 Operand = (0x5bababab);
589 Status = SafeInt32ToInt16 (Operand, &Result);
590 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
591
592 Operand = (-1537977259);
593 Status = SafeInt32ToInt16 (Operand, &Result);
594 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
595 }
596
597 TEST(ConversionTestSuite, TestSafeInt32ToUint16) {
598 RETURN_STATUS Status;
599 INT32 Operand;
600 UINT16 Result;
601
602 //
603 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
604 //
605 Operand = 0xabab;
606 Result = 0;
607 Status = SafeInt32ToUint16 (Operand, &Result);
608 ASSERT_EQ (Status, RETURN_SUCCESS);
609 ASSERT_EQ (0xabab, Result);
610
611 //
612 // Otherwise should result in an error status
613 //
614 Operand = (-17857);
615 Status = SafeInt32ToUint16 (Operand, &Result);
616 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
617
618 Operand = (0x5bababab);
619 Status = SafeInt32ToUint16 (Operand, &Result);
620 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
621
622 Operand = (-1537977259);
623 Status = SafeInt32ToUint16 (Operand, &Result);
624 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
625 }
626
627 TEST(ConversionTestSuite, TestSafeInt32ToUint32) {
628 RETURN_STATUS Status;
629 INT32 Operand;
630 UINT32 Result;
631
632 //
633 // If Operand is non-negative, then it's a cast
634 //
635 Operand = 0x5bababab;
636 Result = 0;
637 Status = SafeInt32ToUint32 (Operand, &Result);
638 ASSERT_EQ (Status, RETURN_SUCCESS);
639 ASSERT_EQ ((UINT32)0x5bababab, Result);
640
641 //
642 // Otherwise should result in an error status
643 //
644 Operand = (-1537977259);
645 Status = SafeInt32ToUint32 (Operand, &Result);
646 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
647 }
648
649 TEST(ConversionTestSuite, TestSafeInt32ToUint64) {
650 RETURN_STATUS Status;
651 INT32 Operand;
652 UINT64 Result;
653
654 //
655 // If Operand is non-negative, then it's a cast
656 //
657 Operand = 0x5bababab;
658 Result = 0;
659 Status = SafeInt32ToUint64 (Operand, &Result);
660 ASSERT_EQ (Status, RETURN_SUCCESS);
661 ASSERT_EQ ((UINT64)0x5bababab, Result);
662
663 //
664 // Otherwise should result in an error status
665 //
666 Operand = (-1537977259);
667 Status = SafeInt32ToUint64 (Operand, &Result);
668 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
669 }
670
671 TEST(ConversionTestSuite, TestSafeUint32ToInt8) {
672 RETURN_STATUS Status;
673 UINT32 Operand;
674 INT8 Result;
675
676 //
677 // If Operand is <= MAX_INT8, then it's a cast
678 //
679 Operand = 0x5b;
680 Result = 0;
681 Status = SafeUint32ToInt8 (Operand, &Result);
682 ASSERT_EQ (Status, RETURN_SUCCESS);
683 ASSERT_EQ (0x5b, Result);
684
685 //
686 // Otherwise should result in an error status
687 //
688 Operand = (0x5bababab);
689 Status = SafeUint32ToInt8 (Operand, &Result);
690 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
691 }
692
693 TEST(ConversionTestSuite, TestSafeUint32ToChar8) {
694 RETURN_STATUS Status;
695 UINT32 Operand;
696 CHAR8 Result;
697
698 // CHAR8 is typedefed as char, which by default is signed, thus
699 // CHAR8 is same as INT8, so same tests as above:
700
701 //
702 // If Operand is <= MAX_INT8, then it's a cast
703 //
704 Operand = 0x5b;
705 Result = 0;
706 Status = SafeUint32ToChar8 (Operand, &Result);
707 ASSERT_EQ (Status, RETURN_SUCCESS);
708 ASSERT_EQ (0x5b, Result);
709
710 //
711 // Otherwise should result in an error status
712 //
713 Operand = (0x5bababab);
714 Status = SafeUint32ToChar8 (Operand, &Result);
715 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
716 }
717
718 TEST(ConversionTestSuite, TestSafeUint32ToUint8) {
719 RETURN_STATUS Status;
720 UINT32 Operand;
721 UINT8 Result;
722
723 //
724 // If Operand is <= MAX_UINT8, then it's a cast
725 //
726 Operand = 0xab;
727 Result = 0;
728 Status = SafeUint32ToUint8 (Operand, &Result);
729 ASSERT_EQ (Status, RETURN_SUCCESS);
730 ASSERT_EQ (0xab, Result);
731
732 //
733 // Otherwise should result in an error status
734 //
735 Operand = (0xabababab);
736 Status = SafeUint32ToUint8 (Operand, &Result);
737 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
738 }
739
740 TEST(ConversionTestSuite, TestSafeUint32ToInt16) {
741 RETURN_STATUS Status;
742 UINT32 Operand;
743 INT16 Result;
744
745 //
746 // If Operand is <= MAX_INT16, then it's a cast
747 //
748 Operand = 0x5bab;
749 Result = 0;
750 Status = SafeUint32ToInt16 (Operand, &Result);
751 ASSERT_EQ (Status, RETURN_SUCCESS);
752 ASSERT_EQ (0x5bab, Result);
753
754 //
755 // Otherwise should result in an error status
756 //
757 Operand = (0xabababab);
758 Status = SafeUint32ToInt16 (Operand, &Result);
759 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
760 }
761
762 TEST(ConversionTestSuite, TestSafeUint32ToUint16) {
763 RETURN_STATUS Status;
764 UINT32 Operand;
765 UINT16 Result;
766
767 //
768 // If Operand is <= MAX_UINT16, then it's a cast
769 //
770 Operand = 0xabab;
771 Result = 0;
772 Status = SafeUint32ToUint16 (Operand, &Result);
773 ASSERT_EQ (Status, RETURN_SUCCESS);
774 ASSERT_EQ (0xabab, Result);
775
776 //
777 // Otherwise should result in an error status
778 //
779 Operand = (0xabababab);
780 Status = SafeUint32ToUint16 (Operand, &Result);
781 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
782 }
783
784 TEST(ConversionTestSuite, TestSafeUint32ToInt32) {
785 RETURN_STATUS Status;
786 UINT32 Operand;
787 INT32 Result;
788
789 //
790 // If Operand is <= MAX_INT32, then it's a cast
791 //
792 Operand = 0x5bababab;
793 Result = 0;
794 Status = SafeUint32ToInt32 (Operand, &Result);
795 ASSERT_EQ (Status, RETURN_SUCCESS);
796 ASSERT_EQ (0x5bababab, Result);
797
798 //
799 // Otherwise should result in an error status
800 //
801 Operand = (0xabababab);
802 Status = SafeUint32ToInt32 (Operand, &Result);
803 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
804 }
805
806 TEST(ConversionTestSuite, TestSafeIntnToInt8) {
807 RETURN_STATUS Status;
808 INTN Operand;
809 INT8 Result;
810
811 //
812 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
813 //
814 Operand = 0x5b;
815 Result = 0;
816 Status = SafeIntnToInt8 (Operand, &Result);
817 ASSERT_EQ (Status, RETURN_SUCCESS);
818 ASSERT_EQ (0x5b, Result);
819
820 Operand = (-53);
821 Status = SafeIntnToInt8 (Operand, &Result);
822 ASSERT_EQ (Status, RETURN_SUCCESS);
823 ASSERT_EQ ((-53), Result);
824
825 //
826 // Otherwise should result in an error status
827 //
828 Operand = (0x5bababab);
829 Status = SafeIntnToInt8 (Operand, &Result);
830 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
831
832 Operand = (-1537977259);
833 Status = SafeIntnToInt8 (Operand, &Result);
834 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
835 }
836
837 TEST(ConversionTestSuite, TestSafeIntnToChar8) {
838 RETURN_STATUS Status;
839 INTN Operand;
840 CHAR8 Result;
841
842 //
843 // CHAR8 is typedefed as char, which may be signed or unsigned based
844 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
845 //
846
847 //
848 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
849 //
850 Operand = 0x5b;
851 Result = 0;
852 Status = SafeIntnToChar8 (Operand, &Result);
853 ASSERT_EQ (Status, RETURN_SUCCESS);
854 ASSERT_EQ (0x5b, Result);
855
856 Operand = 0;
857 Result = 0;
858 Status = SafeIntnToChar8 (Operand, &Result);
859 ASSERT_EQ (Status, RETURN_SUCCESS);
860 ASSERT_EQ (0, Result);
861
862 Operand = MAX_INT8;
863 Result = 0;
864 Status = SafeIntnToChar8 (Operand, &Result);
865 ASSERT_EQ (Status, RETURN_SUCCESS);
866 ASSERT_EQ (MAX_INT8, Result);
867
868 //
869 // Otherwise should result in an error status
870 //
871 Operand = (-53);
872 Status = SafeIntnToChar8 (Operand, &Result);
873 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
874
875 Operand = (0x5bababab);
876 Status = SafeIntnToChar8 (Operand, &Result);
877 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
878
879 Operand = (-1537977259);
880 Status = SafeIntnToChar8 (Operand, &Result);
881 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
882 }
883
884 TEST(ConversionTestSuite, TestSafeIntnToUint8) {
885 RETURN_STATUS Status;
886 INTN Operand;
887 UINT8 Result;
888
889 //
890 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
891 //
892 Operand = 0xab;
893 Result = 0;
894 Status = SafeIntnToUint8 (Operand, &Result);
895 ASSERT_EQ (Status, RETURN_SUCCESS);
896 ASSERT_EQ (0xab, Result);
897
898 //
899 // Otherwise should result in an error status
900 //
901 Operand = (0x5bababab);
902 Status = SafeIntnToUint8 (Operand, &Result);
903 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
904
905 Operand = (-1537977259);
906 Status = SafeIntnToUint8 (Operand, &Result);
907 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
908 }
909
910 TEST(ConversionTestSuite, TestSafeIntnToInt16) {
911 RETURN_STATUS Status;
912 INTN Operand;
913 INT16 Result;
914
915 //
916 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
917 //
918 Operand = 0x5bab;
919 Result = 0;
920 Status = SafeIntnToInt16 (Operand, &Result);
921 ASSERT_EQ (Status, RETURN_SUCCESS);
922 ASSERT_EQ (0x5bab, Result);
923
924 Operand = (-23467);
925 Status = SafeIntnToInt16 (Operand, &Result);
926 ASSERT_EQ (Status, RETURN_SUCCESS);
927 ASSERT_EQ ((-23467), Result);
928
929 //
930 // Otherwise should result in an error status
931 //
932 Operand = (0x5bababab);
933 Status = SafeIntnToInt16 (Operand, &Result);
934 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
935
936 Operand = (-1537977259);
937 Status = SafeIntnToInt16 (Operand, &Result);
938 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
939 }
940
941 TEST(ConversionTestSuite, TestSafeIntnToUint16) {
942 RETURN_STATUS Status;
943 INTN Operand;
944 UINT16 Result;
945
946 //
947 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
948 //
949 Operand = 0xabab;
950 Result = 0;
951 Status = SafeIntnToUint16 (Operand, &Result);
952 ASSERT_EQ (Status, RETURN_SUCCESS);
953 ASSERT_EQ (0xabab, Result);
954
955 //
956 // Otherwise should result in an error status
957 //
958 Operand = (0x5bababab);
959 Status = SafeIntnToUint16 (Operand, &Result);
960 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
961
962 Operand = (-1537977259);
963 Status = SafeIntnToUint16 (Operand, &Result);
964 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
965 }
966
967 TEST(ConversionTestSuite, TestSafeIntnToUintn) {
968 RETURN_STATUS Status;
969 INTN Operand;
970 UINTN Result;
971
972 //
973 // If Operand is non-negative, then it's a cast
974 //
975 Operand = 0x5bababab;
976 Result = 0;
977 Status = SafeIntnToUintn (Operand, &Result);
978 ASSERT_EQ (Status, RETURN_SUCCESS);
979 ASSERT_EQ ((UINTN)0x5bababab, Result);
980
981 //
982 // Otherwise should result in an error status
983 //
984 Operand = (-1537977259);
985 Status = SafeIntnToUintn (Operand, &Result);
986 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
987 }
988
989 TEST(ConversionTestSuite, TestSafeIntnToUint64) {
990 RETURN_STATUS Status;
991 INTN Operand;
992 UINT64 Result;
993
994 //
995 // If Operand is non-negative, then it's a cast
996 //
997 Operand = 0x5bababab;
998 Result = 0;
999 Status = SafeIntnToUint64 (Operand, &Result);
1000 ASSERT_EQ (Status, RETURN_SUCCESS);
1001 ASSERT_EQ ((UINT64)0x5bababab, Result);
1002
1003 //
1004 // Otherwise should result in an error status
1005 //
1006 Operand = (-1537977259);
1007 Status = SafeIntnToUint64 (Operand, &Result);
1008 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1009 }
1010
1011 TEST(ConversionTestSuite, TestSafeUintnToInt8) {
1012 RETURN_STATUS Status;
1013 UINTN Operand;
1014 INT8 Result;
1015
1016 //
1017 // If Operand is <= MAX_INT8, then it's a cast
1018 //
1019 Operand = 0x5b;
1020 Result = 0;
1021 Status = SafeUintnToInt8 (Operand, &Result);
1022 ASSERT_EQ (Status, RETURN_SUCCESS);
1023 ASSERT_EQ (0x5b, Result);
1024
1025 //
1026 // Otherwise should result in an error status
1027 //
1028 Operand = (0xabab);
1029 Status = SafeUintnToInt8 (Operand, &Result);
1030 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1031 }
1032
1033 TEST(ConversionTestSuite, TestSafeUintnToChar8) {
1034 RETURN_STATUS Status;
1035 UINTN Operand;
1036 CHAR8 Result;
1037
1038 // CHAR8 is typedefed as char, which by default is signed, thus
1039 // CHAR8 is same as INT8, so same tests as above:
1040
1041 //
1042 // If Operand is <= MAX_INT8, then it's a cast
1043 //
1044 Operand = 0x5b;
1045 Result = 0;
1046 Status = SafeUintnToChar8 (Operand, &Result);
1047 ASSERT_EQ (Status, RETURN_SUCCESS);
1048 ASSERT_EQ (0x5b, Result);
1049
1050 //
1051 // Otherwise should result in an error status
1052 //
1053 Operand = (0xabab);
1054 Status = SafeUintnToChar8 (Operand, &Result);
1055 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1056 }
1057
1058 TEST(ConversionTestSuite, TestSafeUintnToUint8) {
1059 RETURN_STATUS Status;
1060 UINTN Operand;
1061 UINT8 Result;
1062
1063 //
1064 // If Operand is <= MAX_UINT8, then it's a cast
1065 //
1066 Operand = 0xab;
1067 Result = 0;
1068 Status = SafeUintnToUint8 (Operand, &Result);
1069 ASSERT_EQ (Status, RETURN_SUCCESS);
1070 ASSERT_EQ (0xab, Result);
1071
1072 //
1073 // Otherwise should result in an error status
1074 //
1075 Operand = (0xabab);
1076 Status = SafeUintnToUint8 (Operand, &Result);
1077 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1078 }
1079
1080 TEST(ConversionTestSuite, TestSafeUintnToInt16) {
1081 RETURN_STATUS Status;
1082 UINTN Operand;
1083 INT16 Result;
1084
1085 //
1086 // If Operand is <= MAX_INT16, then it's a cast
1087 //
1088 Operand = 0x5bab;
1089 Result = 0;
1090 Status = SafeUintnToInt16 (Operand, &Result);
1091 ASSERT_EQ (Status, RETURN_SUCCESS);
1092 ASSERT_EQ (0x5bab, Result);
1093
1094 //
1095 // Otherwise should result in an error status
1096 //
1097 Operand = (0xabab);
1098 Status = SafeUintnToInt16 (Operand, &Result);
1099 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1100 }
1101
1102 TEST(ConversionTestSuite, TestSafeUintnToUint16) {
1103 RETURN_STATUS Status;
1104 UINTN Operand;
1105 UINT16 Result;
1106
1107 //
1108 // If Operand is <= MAX_UINT16, then it's a cast
1109 //
1110 Operand = 0xabab;
1111 Result = 0;
1112 Status = SafeUintnToUint16 (Operand, &Result);
1113 ASSERT_EQ (Status, RETURN_SUCCESS);
1114 ASSERT_EQ (0xabab, Result);
1115
1116 //
1117 // Otherwise should result in an error status
1118 //
1119 Operand = (0xabababab);
1120 Status = SafeUintnToUint16 (Operand, &Result);
1121 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1122 }
1123
1124 TEST(ConversionTestSuite, TestSafeUintnToInt32) {
1125 RETURN_STATUS Status;
1126 UINTN Operand;
1127 INT32 Result;
1128
1129 //
1130 // If Operand is <= MAX_INT32, then it's a cast
1131 //
1132 Operand = 0x5bababab;
1133 Result = 0;
1134 Status = SafeUintnToInt32 (Operand, &Result);
1135 ASSERT_EQ (Status, RETURN_SUCCESS);
1136 ASSERT_EQ (0x5bababab, Result);
1137
1138 //
1139 // Otherwise should result in an error status
1140 //
1141 Operand = (0xabababab);
1142 Status = SafeUintnToInt32 (Operand, &Result);
1143 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1144 }
1145
1146 TEST(ConversionTestSuite, TestSafeInt64ToInt8) {
1147 RETURN_STATUS Status;
1148 INT64 Operand;
1149 INT8 Result;
1150
1151 //
1152 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1153 //
1154 Operand = 0x5b;
1155 Result = 0;
1156 Status = SafeInt64ToInt8 (Operand, &Result);
1157 ASSERT_EQ (Status, RETURN_SUCCESS);
1158 ASSERT_EQ (0x5b, Result);
1159
1160 Operand = (-37);
1161 Status = SafeInt64ToInt8 (Operand, &Result);
1162 ASSERT_EQ (Status, RETURN_SUCCESS);
1163 ASSERT_EQ ((-37), Result);
1164
1165 //
1166 // Otherwise should result in an error status
1167 //
1168 Operand = (0x5babababefefefef);
1169 Status = SafeInt64ToInt8 (Operand, &Result);
1170 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1171
1172 Operand = (-6605562033422200815);
1173 Status = SafeInt64ToInt8 (Operand, &Result);
1174 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1175 }
1176
1177 TEST(ConversionTestSuite, TestSafeInt64ToChar8) {
1178 RETURN_STATUS Status;
1179 INT64 Operand;
1180 CHAR8 Result;
1181
1182 //
1183 // CHAR8 is typedefed as char, which may be signed or unsigned based
1184 // on the compiler. Thus, for compatibility CHAR8 should be between 0 and MAX_INT8.
1185 //
1186
1187 //
1188 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1189 //
1190 Operand = 0x5b;
1191 Result = 0;
1192 Status = SafeInt64ToChar8 (Operand, &Result);
1193 ASSERT_EQ (Status, RETURN_SUCCESS);
1194 ASSERT_EQ (0x5b, Result);
1195
1196 Operand = 0;
1197 Result = 0;
1198 Status = SafeInt64ToChar8 (Operand, &Result);
1199 ASSERT_EQ (Status, RETURN_SUCCESS);
1200 ASSERT_EQ (0, Result);
1201
1202 Operand = MAX_INT8;
1203 Result = 0;
1204 Status = SafeInt64ToChar8 (Operand, &Result);
1205 ASSERT_EQ (Status, RETURN_SUCCESS);
1206 ASSERT_EQ (MAX_INT8, Result);
1207
1208 //
1209 // Otherwise should result in an error status
1210 //
1211 Operand = (-37);
1212 Status = SafeInt64ToChar8 (Operand, &Result);
1213 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1214
1215 Operand = (0x5babababefefefef);
1216 Status = SafeInt64ToChar8 (Operand, &Result);
1217 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1218
1219 Operand = (-6605562033422200815);
1220 Status = SafeInt64ToChar8 (Operand, &Result);
1221 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1222 }
1223
1224 TEST(ConversionTestSuite, TestSafeInt64ToUint8) {
1225 RETURN_STATUS Status;
1226 INT64 Operand;
1227 UINT8 Result;
1228
1229 //
1230 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
1231 //
1232 Operand = 0xab;
1233 Result = 0;
1234 Status = SafeInt64ToUint8 (Operand, &Result);
1235 ASSERT_EQ (Status, RETURN_SUCCESS);
1236 ASSERT_EQ (0xab, Result);
1237
1238 //
1239 // Otherwise should result in an error status
1240 //
1241 Operand = (0x5babababefefefef);
1242 Status = SafeInt64ToUint8 (Operand, &Result);
1243 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1244
1245 Operand = (-6605562033422200815);
1246 Status = SafeInt64ToUint8 (Operand, &Result);
1247 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1248 }
1249
1250 TEST(ConversionTestSuite, TestSafeInt64ToInt16) {
1251 RETURN_STATUS Status;
1252 INT64 Operand;
1253 INT16 Result;
1254
1255 //
1256 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
1257 //
1258 Operand = 0x5bab;
1259 Result = 0;
1260 Status = SafeInt64ToInt16 (Operand, &Result);
1261 ASSERT_EQ (Status, RETURN_SUCCESS);
1262 ASSERT_EQ (0x5bab, Result);
1263
1264 Operand = (-23467);
1265 Status = SafeInt64ToInt16 (Operand, &Result);
1266 ASSERT_EQ (Status, RETURN_SUCCESS);
1267 ASSERT_EQ ((-23467), Result);
1268
1269 //
1270 // Otherwise should result in an error status
1271 //
1272 Operand = (0x5babababefefefef);
1273 Status = SafeInt64ToInt16 (Operand, &Result);
1274 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1275
1276 Operand = (-6605562033422200815);
1277 Status = SafeInt64ToInt16 (Operand, &Result);
1278 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1279 }
1280
1281 TEST(ConversionTestSuite, TestSafeInt64ToUint16) {
1282 RETURN_STATUS Status;
1283 INT64 Operand;
1284 UINT16 Result;
1285
1286 //
1287 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
1288 //
1289 Operand = 0xabab;
1290 Result = 0;
1291 Status = SafeInt64ToUint16 (Operand, &Result);
1292 ASSERT_EQ (Status, RETURN_SUCCESS);
1293 ASSERT_EQ (0xabab, Result);
1294
1295 //
1296 // Otherwise should result in an error status
1297 //
1298 Operand = (0x5babababefefefef);
1299 Status = SafeInt64ToUint16 (Operand, &Result);
1300 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1301
1302 Operand = (-6605562033422200815);
1303 Status = SafeInt64ToUint16 (Operand, &Result);
1304 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1305 }
1306
1307 TEST(ConversionTestSuite, TestSafeInt64ToInt32) {
1308 RETURN_STATUS Status;
1309 INT64 Operand;
1310 INT32 Result;
1311
1312 //
1313 // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
1314 //
1315 Operand = 0x5bababab;
1316 Result = 0;
1317 Status = SafeInt64ToInt32 (Operand, &Result);
1318 ASSERT_EQ (Status, RETURN_SUCCESS);
1319 ASSERT_EQ (0x5bababab, Result);
1320
1321 Operand = (-1537977259);
1322 Status = SafeInt64ToInt32 (Operand, &Result);
1323 ASSERT_EQ (Status, RETURN_SUCCESS);
1324 ASSERT_EQ ((-1537977259), Result);
1325
1326 //
1327 // Otherwise should result in an error status
1328 //
1329 Operand = (0x5babababefefefef);
1330 Status = SafeInt64ToInt32 (Operand, &Result);
1331 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1332
1333 Operand = (-6605562033422200815);
1334 Status = SafeInt64ToInt32 (Operand, &Result);
1335 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1336 }
1337
1338 TEST(ConversionTestSuite, TestSafeInt64ToUint32) {
1339 RETURN_STATUS Status;
1340 INT64 Operand;
1341 UINT32 Result;
1342
1343 //
1344 // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
1345 //
1346 Operand = 0xabababab;
1347 Result = 0;
1348 Status = SafeInt64ToUint32 (Operand, &Result);
1349 ASSERT_EQ (Status, RETURN_SUCCESS);
1350 ASSERT_EQ (0xabababab, Result);
1351
1352 //
1353 // Otherwise should result in an error status
1354 //
1355 Operand = (0x5babababefefefef);
1356 Status = SafeInt64ToUint32 (Operand, &Result);
1357 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1358
1359 Operand = (-6605562033422200815);
1360 Status = SafeInt64ToUint32 (Operand, &Result);
1361 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1362 }
1363
1364 TEST(ConversionTestSuite, TestSafeInt64ToUint64) {
1365 RETURN_STATUS Status;
1366 INT64 Operand;
1367 UINT64 Result;
1368
1369 //
1370 // If Operand is non-negative, then it's a cast
1371 //
1372 Operand = 0x5babababefefefef;
1373 Result = 0;
1374 Status = SafeInt64ToUint64 (Operand, &Result);
1375 ASSERT_EQ (Status, RETURN_SUCCESS);
1376 ASSERT_EQ ((UINT64)0x5babababefefefef, Result);
1377
1378 //
1379 // Otherwise should result in an error status
1380 //
1381 Operand = (-6605562033422200815);
1382 Status = SafeInt64ToUint64 (Operand, &Result);
1383 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1384 }
1385
1386 TEST(ConversionTestSuite, TestSafeUint64ToInt8) {
1387 RETURN_STATUS Status;
1388 UINT64 Operand;
1389 INT8 Result;
1390
1391 //
1392 // If Operand is <= MAX_INT8, then it's a cast
1393 //
1394 Operand = 0x5b;
1395 Result = 0;
1396 Status = SafeUint64ToInt8 (Operand, &Result);
1397 ASSERT_EQ (Status, RETURN_SUCCESS);
1398 ASSERT_EQ (0x5b, Result);
1399
1400 //
1401 // Otherwise should result in an error status
1402 //
1403 Operand = (0xababababefefefef);
1404 Status = SafeUint64ToInt8 (Operand, &Result);
1405 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1406 }
1407
1408 TEST(ConversionTestSuite, TestSafeUint64ToChar8) {
1409 RETURN_STATUS Status;
1410 UINT64 Operand;
1411 CHAR8 Result;
1412
1413 // CHAR8 is typedefed as char, which by default is signed, thus
1414 // CHAR8 is same as INT8, so same tests as above:
1415
1416 //
1417 // If Operand is <= MAX_INT8, then it's a cast
1418 //
1419 Operand = 0x5b;
1420 Result = 0;
1421 Status = SafeUint64ToChar8 (Operand, &Result);
1422 ASSERT_EQ (Status, RETURN_SUCCESS);
1423 ASSERT_EQ (0x5b, Result);
1424
1425 //
1426 // Otherwise should result in an error status
1427 //
1428 Operand = (0xababababefefefef);
1429 Status = SafeUint64ToChar8 (Operand, &Result);
1430 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1431 }
1432
1433 TEST(ConversionTestSuite, TestSafeUint64ToUint8) {
1434 RETURN_STATUS Status;
1435 UINT64 Operand;
1436 UINT8 Result;
1437
1438 //
1439 // If Operand is <= MAX_UINT8, then it's a cast
1440 //
1441 Operand = 0xab;
1442 Result = 0;
1443 Status = SafeUint64ToUint8 (Operand, &Result);
1444 ASSERT_EQ (Status, RETURN_SUCCESS);
1445 ASSERT_EQ (0xab, Result);
1446
1447 //
1448 // Otherwise should result in an error status
1449 //
1450 Operand = (0xababababefefefef);
1451 Status = SafeUint64ToUint8 (Operand, &Result);
1452 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1453 }
1454
1455 TEST(ConversionTestSuite, TestSafeUint64ToInt16) {
1456 RETURN_STATUS Status;
1457 UINT64 Operand;
1458 INT16 Result;
1459
1460 //
1461 // If Operand is <= MAX_INT16, then it's a cast
1462 //
1463 Operand = 0x5bab;
1464 Result = 0;
1465 Status = SafeUint64ToInt16 (Operand, &Result);
1466 ASSERT_EQ (Status, RETURN_SUCCESS);
1467 ASSERT_EQ (0x5bab, Result);
1468
1469 //
1470 // Otherwise should result in an error status
1471 //
1472 Operand = (0xababababefefefef);
1473 Status = SafeUint64ToInt16 (Operand, &Result);
1474 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1475 }
1476
1477 TEST(ConversionTestSuite, TestSafeUint64ToUint16) {
1478 RETURN_STATUS Status;
1479 UINT64 Operand;
1480 UINT16 Result;
1481
1482 //
1483 // If Operand is <= MAX_UINT16, then it's a cast
1484 //
1485 Operand = 0xabab;
1486 Result = 0;
1487 Status = SafeUint64ToUint16 (Operand, &Result);
1488 ASSERT_EQ (Status, RETURN_SUCCESS);
1489 ASSERT_EQ (0xabab, Result);
1490
1491 //
1492 // Otherwise should result in an error status
1493 //
1494 Operand = (0xababababefefefef);
1495 Status = SafeUint64ToUint16 (Operand, &Result);
1496 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1497 }
1498
1499 TEST(ConversionTestSuite, TestSafeUint64ToInt32) {
1500 RETURN_STATUS Status;
1501 UINT64 Operand;
1502 INT32 Result;
1503
1504 //
1505 // If Operand is <= MAX_INT32, then it's a cast
1506 //
1507 Operand = 0x5bababab;
1508 Result = 0;
1509 Status = SafeUint64ToInt32 (Operand, &Result);
1510 ASSERT_EQ (Status, RETURN_SUCCESS);
1511 ASSERT_EQ (0x5bababab, Result);
1512
1513 //
1514 // Otherwise should result in an error status
1515 //
1516 Operand = (0xababababefefefef);
1517 Status = SafeUint64ToInt32 (Operand, &Result);
1518 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1519 }
1520
1521 TEST(ConversionTestSuite, TestSafeUint64ToUint32) {
1522 RETURN_STATUS Status;
1523 UINT64 Operand;
1524 UINT32 Result;
1525
1526 //
1527 // If Operand is <= MAX_UINT32, then it's a cast
1528 //
1529 Operand = 0xabababab;
1530 Result = 0;
1531 Status = SafeUint64ToUint32 (Operand, &Result);
1532 ASSERT_EQ (Status, RETURN_SUCCESS);
1533 ASSERT_EQ (0xabababab, Result);
1534
1535 //
1536 // Otherwise should result in an error status
1537 //
1538 Operand = (0xababababefefefef);
1539 Status = SafeUint64ToUint32 (Operand, &Result);
1540 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1541 }
1542
1543 TEST(ConversionTestSuite, TestSafeUint64ToInt64) {
1544 RETURN_STATUS Status;
1545 UINT64 Operand;
1546 INT64 Result;
1547
1548 //
1549 // If Operand is <= MAX_INT64, then it's a cast
1550 //
1551 Operand = 0x5babababefefefef;
1552 Result = 0;
1553 Status = SafeUint64ToInt64 (Operand, &Result);
1554 ASSERT_EQ (Status, RETURN_SUCCESS);
1555 ASSERT_EQ (0x5babababefefefef, Result);
1556
1557 //
1558 // Otherwise should result in an error status
1559 //
1560 Operand = (0xababababefefefef);
1561 Status = SafeUint64ToInt64 (Operand, &Result);
1562 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1563 }
1564
1565 //
1566 // Addition function tests:
1567 //
1568 TEST(AdditionSubtractionTestSuite, TestSafeUint8Add) {
1569 RETURN_STATUS Status;
1570 UINT8 Augend;
1571 UINT8 Addend;
1572 UINT8 Result;
1573
1574 //
1575 // If the result of addition doesn't overflow MAX_UINT8, then it's addition
1576 //
1577 Augend = 0x3a;
1578 Addend = 0x3a;
1579 Result = 0;
1580 Status = SafeUint8Add (Augend, Addend, &Result);
1581 ASSERT_EQ (Status, RETURN_SUCCESS);
1582 ASSERT_EQ (0x74, Result);
1583
1584 //
1585 // Otherwise should result in an error status
1586 //
1587 Augend = 0xab;
1588 Addend = 0xbc;
1589 Status = SafeUint8Add (Augend, Addend, &Result);
1590 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1591 }
1592
1593 TEST(AdditionSubtractionTestSuite, TestSafeUint16Add) {
1594 RETURN_STATUS Status;
1595 UINT16 Augend = 0x3a3a;
1596 UINT16 Addend = 0x3a3a;
1597 UINT16 Result = 0;
1598
1599 //
1600 // If the result of addition doesn't overflow MAX_UINT16, then it's addition
1601 //
1602 Status = SafeUint16Add (Augend, Addend, &Result);
1603 ASSERT_EQ (Status, RETURN_SUCCESS);
1604 ASSERT_EQ (0x7474, Result);
1605
1606 //
1607 // Otherwise should result in an error status
1608 //
1609 Augend = 0xabab;
1610 Addend = 0xbcbc;
1611 Status = SafeUint16Add (Augend, Addend, &Result);
1612 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1613 }
1614
1615 TEST(AdditionSubtractionTestSuite, TestSafeUint32Add) {
1616 RETURN_STATUS Status;
1617 UINT32 Augend;
1618 UINT32 Addend;
1619 UINT32 Result;
1620
1621 //
1622 // If the result of addition doesn't overflow MAX_UINT32, then it's addition
1623 //
1624 Augend = 0x3a3a3a3a;
1625 Addend = 0x3a3a3a3a;
1626 Result = 0;
1627 Status = SafeUint32Add (Augend, Addend, &Result);
1628 ASSERT_EQ (Status, RETURN_SUCCESS);
1629 ASSERT_EQ ((UINT32)0x74747474, Result);
1630
1631 //
1632 // Otherwise should result in an error status
1633 //
1634 Augend = 0xabababab;
1635 Addend = 0xbcbcbcbc;
1636 Status = SafeUint32Add (Augend, Addend, &Result);
1637 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1638 }
1639
1640 TEST(AdditionSubtractionTestSuite, TestSafeUint64Add) {
1641 RETURN_STATUS Status;
1642 UINT64 Augend;
1643 UINT64 Addend;
1644 UINT64 Result;
1645
1646 //
1647 // If the result of addition doesn't overflow MAX_UINT64, then it's addition
1648 //
1649 Augend = 0x3a3a3a3a12121212;
1650 Addend = 0x3a3a3a3a12121212;
1651 Result = 0;
1652 Status = SafeUint64Add (Augend, Addend, &Result);
1653 ASSERT_EQ (Status, RETURN_SUCCESS);
1654 ASSERT_EQ ((UINT64)0x7474747424242424, Result);
1655
1656 //
1657 // Otherwise should result in an error status
1658 //
1659 Augend = 0xababababefefefef;
1660 Addend = 0xbcbcbcbcdededede;
1661 Status = SafeUint64Add (Augend, Addend, &Result);
1662 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1663 }
1664
1665 TEST(AdditionSubtractionTestSuite, TestSafeInt8Add) {
1666 RETURN_STATUS Status;
1667 INT8 Augend;
1668 INT8 Addend;
1669 INT8 Result;
1670
1671 //
1672 // If the result of addition doesn't overflow MAX_INT8
1673 // and doesn't underflow MIN_INT8, then it's addition
1674 //
1675 Augend = 0x3a;
1676 Addend = 0x3a;
1677 Result = 0;
1678 Status = SafeInt8Add (Augend, Addend, &Result);
1679 ASSERT_EQ (Status, RETURN_SUCCESS);
1680 ASSERT_EQ (0x74, Result);
1681
1682 Augend = (-58);
1683 Addend = (-58);
1684 Status = SafeInt8Add (Augend, Addend, &Result);
1685 ASSERT_EQ (Status, RETURN_SUCCESS);
1686 ASSERT_EQ ((-116), Result);
1687
1688 //
1689 // Otherwise should result in an error status
1690 //
1691 Augend = 0x5a;
1692 Addend = 0x5a;
1693 Status = SafeInt8Add (Augend, Addend, &Result);
1694 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1695
1696 Augend = (-90);
1697 Addend = (-90);
1698 Status = SafeInt8Add (Augend, Addend, &Result);
1699 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1700 }
1701
1702 TEST(AdditionSubtractionTestSuite, TestSafeInt16Add) {
1703 RETURN_STATUS Status;
1704 INT16 Augend;
1705 INT16 Addend;
1706 INT16 Result;
1707
1708 //
1709 // If the result of addition doesn't overflow MAX_INT16
1710 // and doesn't underflow MIN_INT16, then it's addition
1711 //
1712 Augend = 0x3a3a;
1713 Addend = 0x3a3a;
1714 Result = 0;
1715 Status = SafeInt16Add (Augend, Addend, &Result);
1716 ASSERT_EQ (Status, RETURN_SUCCESS);
1717 ASSERT_EQ (0x7474, Result);
1718
1719 Augend = (-14906);
1720 Addend = (-14906);
1721 Status = SafeInt16Add (Augend, Addend, &Result);
1722 ASSERT_EQ (Status, RETURN_SUCCESS);
1723 ASSERT_EQ ((-29812), Result);
1724
1725 //
1726 // Otherwise should result in an error status
1727 //
1728 Augend = 0x5a5a;
1729 Addend = 0x5a5a;
1730 Status = SafeInt16Add (Augend, Addend, &Result);
1731 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1732
1733 Augend = (-23130);
1734 Addend = (-23130);
1735 Status = SafeInt16Add (Augend, Addend, &Result);
1736 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1737 }
1738
1739 TEST(AdditionSubtractionTestSuite, TestSafeInt32Add) {
1740 RETURN_STATUS Status;
1741 INT32 Augend;
1742 INT32 Addend;
1743 INT32 Result;
1744
1745 //
1746 // If the result of addition doesn't overflow MAX_INT32
1747 // and doesn't underflow MIN_INT32, then it's addition
1748 //
1749 Augend = 0x3a3a3a3a;
1750 Addend = 0x3a3a3a3a;
1751 Result = 0;
1752 Status = SafeInt32Add (Augend, Addend, &Result);
1753 ASSERT_EQ (Status, RETURN_SUCCESS);
1754 ASSERT_EQ (0x74747474, Result);
1755
1756 Augend = (-976894522);
1757 Addend = (-976894522);
1758 Status = SafeInt32Add (Augend, Addend, &Result);
1759 ASSERT_EQ (Status, RETURN_SUCCESS);
1760 ASSERT_EQ ((-1953789044), Result);
1761
1762 //
1763 // Otherwise should result in an error status
1764 //
1765 Augend = 0x5a5a5a5a;
1766 Addend = 0x5a5a5a5a;
1767 Status = SafeInt32Add (Augend, Addend, &Result);
1768 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1769
1770 Augend = (-1515870810);
1771 Addend = (-1515870810);
1772 Status = SafeInt32Add (Augend, Addend, &Result);
1773 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1774 }
1775
1776 TEST(AdditionSubtractionTestSuite, TestSafeInt64Add) {
1777 RETURN_STATUS Status;
1778 INT64 Augend;
1779 INT64 Addend;
1780 INT64 Result;
1781
1782 //
1783 // If the result of addition doesn't overflow MAX_INT64
1784 // and doesn't underflow MIN_INT64, then it's addition
1785 //
1786 Augend = 0x3a3a3a3a3a3a3a3a;
1787 Addend = 0x3a3a3a3a3a3a3a3a;
1788 Result = 0;
1789 Status = SafeInt64Add (Augend, Addend, &Result);
1790 ASSERT_EQ (Status, RETURN_SUCCESS);
1791 ASSERT_EQ (0x7474747474747474, Result);
1792
1793 Augend = (-4195730024608447034);
1794 Addend = (-4195730024608447034);
1795 Status = SafeInt64Add (Augend, Addend, &Result);
1796 ASSERT_EQ (Status, RETURN_SUCCESS);
1797 ASSERT_EQ ((-8391460049216894068), Result);
1798
1799 //
1800 // Otherwise should result in an error status
1801 //
1802 Augend = 0x5a5a5a5a5a5a5a5a;
1803 Addend = 0x5a5a5a5a5a5a5a5a;
1804 Status = SafeInt64Add (Augend, Addend, &Result);
1805 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1806
1807 Augend = (-6510615555426900570);
1808 Addend = (-6510615555426900570);
1809 Status = SafeInt64Add (Augend, Addend, &Result);
1810 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1811 }
1812
1813 //
1814 // Subtraction function tests:
1815 //
1816 TEST(AdditionSubtractionTestSuite, TestSafeUint8Sub) {
1817 RETURN_STATUS Status;
1818 UINT8 Minuend;
1819 UINT8 Subtrahend;
1820 UINT8 Result;
1821
1822 //
1823 // If Minuend >= Subtrahend, then it's subtraction
1824 //
1825 Minuend = 0x5a;
1826 Subtrahend = 0x3b;
1827 Result = 0;
1828 Status = SafeUint8Sub (Minuend, Subtrahend, &Result);
1829 ASSERT_EQ (Status, RETURN_SUCCESS);
1830 ASSERT_EQ (0x1f, Result);
1831
1832 //
1833 // Otherwise should result in an error status
1834 //
1835 Minuend = 0x5a;
1836 Subtrahend = 0x6d;
1837 Status = SafeUint8Sub (Minuend, Subtrahend, &Result);
1838 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1839 }
1840
1841 TEST(AdditionSubtractionTestSuite, TestSafeUint16Sub) {
1842 RETURN_STATUS Status;
1843 UINT16 Minuend;
1844 UINT16 Subtrahend;
1845 UINT16 Result;
1846
1847 //
1848 // If Minuend >= Subtrahend, then it's subtraction
1849 //
1850 Minuend = 0x5a5a;
1851 Subtrahend = 0x3b3b;
1852 Result = 0;
1853 Status = SafeUint16Sub (Minuend, Subtrahend, &Result);
1854 ASSERT_EQ (Status, RETURN_SUCCESS);
1855 ASSERT_EQ (0x1f1f, Result);
1856
1857 //
1858 // Otherwise should result in an error status
1859 //
1860 Minuend = 0x5a5a;
1861 Subtrahend = 0x6d6d;
1862 Status = SafeUint16Sub (Minuend, Subtrahend, &Result);
1863 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1864 }
1865
1866 TEST(AdditionSubtractionTestSuite, TestSafeUint32Sub) {
1867 RETURN_STATUS Status;
1868 UINT32 Minuend;
1869 UINT32 Subtrahend;
1870 UINT32 Result;
1871
1872 //
1873 // If Minuend >= Subtrahend, then it's subtraction
1874 //
1875 Minuend = 0x5a5a5a5a;
1876 Subtrahend = 0x3b3b3b3b;
1877 Result = 0;
1878 Status = SafeUint32Sub (Minuend, Subtrahend, &Result);
1879 ASSERT_EQ (Status, RETURN_SUCCESS);
1880 ASSERT_EQ ((UINT32)0x1f1f1f1f, Result);
1881
1882 //
1883 // Otherwise should result in an error status
1884 //
1885 Minuend = 0x5a5a5a5a;
1886 Subtrahend = 0x6d6d6d6d;
1887 Status = SafeUint32Sub (Minuend, Subtrahend, &Result);
1888 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1889 }
1890
1891 TEST(AdditionSubtractionTestSuite, TestSafeUint64Sub) {
1892 RETURN_STATUS Status;
1893 UINT64 Minuend;
1894 UINT64 Subtrahend;
1895 UINT64 Result;
1896
1897 //
1898 // If Minuend >= Subtrahend, then it's subtraction
1899 //
1900 Minuend = 0x5a5a5a5a5a5a5a5a;
1901 Subtrahend = 0x3b3b3b3b3b3b3b3b;
1902 Result = 0;
1903 Status = SafeUint64Sub (Minuend, Subtrahend, &Result);
1904 ASSERT_EQ (Status, RETURN_SUCCESS);
1905 ASSERT_EQ ((UINT64)0x1f1f1f1f1f1f1f1f, Result);
1906
1907 //
1908 // Otherwise should result in an error status
1909 //
1910 Minuend = 0x5a5a5a5a5a5a5a5a;
1911 Subtrahend = 0x6d6d6d6d6d6d6d6d;
1912 Status = SafeUint64Sub (Minuend, Subtrahend, &Result);
1913 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1914 }
1915
1916 TEST(AdditionSubtractionTestSuite, TestSafeInt8Sub) {
1917 RETURN_STATUS Status;
1918 INT8 Minuend;
1919 INT8 Subtrahend;
1920 INT8 Result;
1921
1922 //
1923 // If the result of subtractions doesn't overflow MAX_INT8 or
1924 // underflow MIN_INT8, then it's subtraction
1925 //
1926 Minuend = 0x5a;
1927 Subtrahend = 0x3a;
1928 Result = 0;
1929 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
1930 ASSERT_EQ (Status, RETURN_SUCCESS);
1931 ASSERT_EQ (0x20, Result);
1932
1933 Minuend = 58;
1934 Subtrahend = 78;
1935 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
1936 ASSERT_EQ (Status, RETURN_SUCCESS);
1937 ASSERT_EQ ((-20), Result);
1938
1939 //
1940 // Otherwise should result in an error status
1941 //
1942 Minuend = (-80);
1943 Subtrahend = 80;
1944 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
1945 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1946
1947 Minuend = (80);
1948 Subtrahend = (-80);
1949 Status = SafeInt8Sub (Minuend, Subtrahend, &Result);
1950 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1951 }
1952
1953 TEST(AdditionSubtractionTestSuite, TestSafeInt16Sub) {
1954 RETURN_STATUS Status;
1955 INT16 Minuend;
1956 INT16 Subtrahend;
1957 INT16 Result;
1958
1959 //
1960 // If the result of subtractions doesn't overflow MAX_INT16 or
1961 // underflow MIN_INT16, then it's subtraction
1962 //
1963 Minuend = 0x5a5a;
1964 Subtrahend = 0x3a3a;
1965 Result = 0;
1966 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
1967 ASSERT_EQ (Status, RETURN_SUCCESS);
1968 ASSERT_EQ (0x2020, Result);
1969
1970 Minuend = 0x3a3a;
1971 Subtrahend = 0x5a5a;
1972 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
1973 ASSERT_EQ (Status, RETURN_SUCCESS);
1974 ASSERT_EQ ((-8224), Result);
1975
1976 //
1977 // Otherwise should result in an error status
1978 //
1979 Minuend = (-31354);
1980 Subtrahend = 31354;
1981 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
1982 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1983
1984 Minuend = (31354);
1985 Subtrahend = (-31354);
1986 Status = SafeInt16Sub (Minuend, Subtrahend, &Result);
1987 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
1988 }
1989
1990 TEST(AdditionSubtractionTestSuite, TestSafeInt32Sub) {
1991 RETURN_STATUS Status;
1992 INT32 Minuend;
1993 INT32 Subtrahend;
1994 INT32 Result;
1995
1996 //
1997 // If the result of subtractions doesn't overflow MAX_INT32 or
1998 // underflow MIN_INT32, then it's subtraction
1999 //
2000 Minuend = 0x5a5a5a5a;
2001 Subtrahend = 0x3a3a3a3a;
2002 Result = 0;
2003 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2004 ASSERT_EQ (Status, RETURN_SUCCESS);
2005 ASSERT_EQ (0x20202020, Result);
2006
2007 Minuend = 0x3a3a3a3a;
2008 Subtrahend = 0x5a5a5a5a;
2009 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2010 ASSERT_EQ (Status, RETURN_SUCCESS);
2011 ASSERT_EQ ((-538976288), Result);
2012
2013 //
2014 // Otherwise should result in an error status
2015 //
2016 Minuend = (-2054847098);
2017 Subtrahend = 2054847098;
2018 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2019 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2020
2021 Minuend = (2054847098);
2022 Subtrahend = (-2054847098);
2023 Status = SafeInt32Sub (Minuend, Subtrahend, &Result);
2024 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2025 }
2026
2027 TEST(AdditionSubtractionTestSuite, TestSafeInt64Sub) {
2028 RETURN_STATUS Status;
2029 INT64 Minuend;
2030 INT64 Subtrahend;
2031 INT64 Result;
2032
2033 //
2034 // If the result of subtractions doesn't overflow MAX_INT64 or
2035 // underflow MIN_INT64, then it's subtraction
2036 //
2037 Minuend = 0x5a5a5a5a5a5a5a5a;
2038 Subtrahend = 0x3a3a3a3a3a3a3a3a;
2039 Result = 0;
2040 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2041 ASSERT_EQ (Status, RETURN_SUCCESS);
2042 ASSERT_EQ (0x2020202020202020, Result);
2043
2044 Minuend = 0x3a3a3a3a3a3a3a3a;
2045 Subtrahend = 0x5a5a5a5a5a5a5a5a;
2046 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2047 ASSERT_EQ (Status, RETURN_SUCCESS);
2048 ASSERT_EQ ((-2314885530818453536), Result);
2049
2050 //
2051 // Otherwise should result in an error status
2052 //
2053 Minuend = (-8825501086245354106);
2054 Subtrahend = 8825501086245354106;
2055 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2056 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2057
2058 Minuend = (8825501086245354106);
2059 Subtrahend = (-8825501086245354106);
2060 Status = SafeInt64Sub (Minuend, Subtrahend, &Result);
2061 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2062 }
2063
2064 //
2065 // Multiplication function tests:
2066 //
2067 TEST(MultiplicationTestSuite, TestSafeUint8Mult) {
2068 RETURN_STATUS Status;
2069 UINT8 Multiplicand;
2070 UINT8 Multiplier;
2071 UINT8 Result;
2072
2073 //
2074 // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed
2075 //
2076 Multiplicand = 0x12;
2077 Multiplier = 0xa;
2078 Result = 0;
2079 Status = SafeUint8Mult (Multiplicand, Multiplier, &Result);
2080 ASSERT_EQ (Status, RETURN_SUCCESS);
2081 ASSERT_EQ (0xb4, Result);
2082
2083 //
2084 // Otherwise should result in an error status
2085 //
2086 Multiplicand = 0x12;
2087 Multiplier = 0x23;
2088 Status = SafeUint8Mult (Multiplicand, Multiplier, &Result);
2089 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2090 }
2091
2092 TEST(MultiplicationTestSuite, TestSafeUint16Mult) {
2093 RETURN_STATUS Status;
2094 UINT16 Multiplicand;
2095 UINT16 Multiplier;
2096 UINT16 Result;
2097
2098 //
2099 // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed
2100 //
2101 Multiplicand = 0x212;
2102 Multiplier = 0x7a;
2103 Result = 0;
2104 Status = SafeUint16Mult (Multiplicand, Multiplier, &Result);
2105 ASSERT_EQ (Status, RETURN_SUCCESS);
2106 ASSERT_EQ (0xfc94, Result);
2107
2108 //
2109 // Otherwise should result in an error status
2110 //
2111 Multiplicand = 0x1234;
2112 Multiplier = 0x213;
2113 Status = SafeUint16Mult (Multiplicand, Multiplier, &Result);
2114 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2115 }
2116
2117 TEST(MultiplicationTestSuite, TestSafeUint32Mult) {
2118 RETURN_STATUS Status;
2119 UINT32 Multiplicand;
2120 UINT32 Multiplier;
2121 UINT32 Result;
2122
2123 //
2124 // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed
2125 //
2126 Multiplicand = 0xa122a;
2127 Multiplier = 0xd23;
2128 Result = 0;
2129 Status = SafeUint32Mult (Multiplicand, Multiplier, &Result);
2130 ASSERT_EQ (Status, RETURN_SUCCESS);
2131 ASSERT_EQ (0x844c9dbe, Result);
2132
2133 //
2134 // Otherwise should result in an error status
2135 //
2136 Multiplicand = 0xa122a;
2137 Multiplier = 0xed23;
2138 Status = SafeUint32Mult (Multiplicand, Multiplier, &Result);
2139 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2140 }
2141
2142 TEST(MultiplicationTestSuite, TestSafeUint64Mult) {
2143 RETURN_STATUS Status;
2144 UINT64 Multiplicand;
2145 UINT64 Multiplier;
2146 UINT64 Result;
2147
2148 //
2149 // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed
2150 //
2151 Multiplicand = 0x123456789a;
2152 Multiplier = 0x1234567;
2153 Result = 0;
2154 Status = SafeUint64Mult (Multiplicand, Multiplier, &Result);
2155 ASSERT_EQ (Status, RETURN_SUCCESS);
2156 ASSERT_EQ ((UINT64)0x14b66db9745a07f6, Result);
2157
2158 //
2159 // Otherwise should result in an error status
2160 //
2161 Multiplicand = 0x123456789a;
2162 Multiplier = 0x12345678;
2163 Status = SafeUint64Mult (Multiplicand, Multiplier, &Result);
2164 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2165 }
2166
2167 TEST(MultiplicationTestSuite, TestSafeInt8Mult) {
2168 RETURN_STATUS Status;
2169 INT8 Multiplicand;
2170 INT8 Multiplier;
2171 INT8 Result;
2172
2173 //
2174 // If the result of multiplication doesn't overflow MAX_INT8 and doesn't
2175 // underflow MIN_UINT8, it will succeed
2176 //
2177 Multiplicand = 0x12;
2178 Multiplier = 0x7;
2179 Result = 0;
2180 Status = SafeInt8Mult (Multiplicand, Multiplier, &Result);
2181 ASSERT_EQ (Status, RETURN_SUCCESS);
2182 ASSERT_EQ (0x7e, Result);
2183
2184 //
2185 // Otherwise should result in an error status
2186 //
2187 Multiplicand = 0x12;
2188 Multiplier = 0xa;
2189 Status = SafeInt8Mult (Multiplicand, Multiplier, &Result);
2190 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2191 }
2192
2193 TEST(MultiplicationTestSuite, TestSafeInt16Mult) {
2194 RETURN_STATUS Status;
2195 INT16 Multiplicand;
2196 INT16 Multiplier;
2197 INT16 Result;
2198
2199 //
2200 // If the result of multiplication doesn't overflow MAX_INT16 and doesn't
2201 // underflow MIN_UINT16, it will succeed
2202 //
2203 Multiplicand = 0x123;
2204 Multiplier = 0x67;
2205 Result = 0;
2206 Status = SafeInt16Mult (Multiplicand, Multiplier, &Result);
2207 ASSERT_EQ (Status, RETURN_SUCCESS);
2208 ASSERT_EQ (0x7515, Result);
2209
2210 //
2211 // Otherwise should result in an error status
2212 //
2213 Multiplicand = 0x123;
2214 Multiplier = 0xab;
2215 Status = SafeInt16Mult (Multiplicand, Multiplier, &Result);
2216 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2217 }
2218
2219 TEST(MultiplicationTestSuite, TestSafeInt32Mult) {
2220 RETURN_STATUS Status;
2221 INT32 Multiplicand;
2222 INT32 Multiplier;
2223 INT32 Result;
2224
2225 //
2226 // If the result of multiplication doesn't overflow MAX_INT32 and doesn't
2227 // underflow MIN_UINT32, it will succeed
2228 //
2229 Multiplicand = 0x123456;
2230 Multiplier = 0x678;
2231 Result = 0;
2232 Status = SafeInt32Mult (Multiplicand, Multiplier, &Result);
2233 ASSERT_EQ (Status, RETURN_SUCCESS);
2234 ASSERT_EQ (0x75c28c50, Result);
2235
2236 //
2237 // Otherwise should result in an error status
2238 //
2239 Multiplicand = 0x123456;
2240 Multiplier = 0xabc;
2241 Status = SafeInt32Mult (Multiplicand, Multiplier, &Result);
2242 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2243 }
2244
2245 TEST(MultiplicationTestSuite, TestSafeInt64Mult) {
2246 RETURN_STATUS Status;
2247 INT64 Multiplicand;
2248 INT64 Multiplier;
2249 INT64 Result;
2250
2251 //
2252 // If the result of multiplication doesn't overflow MAX_INT64 and doesn't
2253 // underflow MIN_UINT64, it will succeed
2254 //
2255 Multiplicand = 0x123456789;
2256 Multiplier = 0x6789abcd;
2257 Result = 0;
2258 Status = SafeInt64Mult (Multiplicand, Multiplier, &Result);
2259 ASSERT_EQ (Status, RETURN_SUCCESS);
2260 ASSERT_EQ (0x75cd9045220d6bb5, Result);
2261
2262 //
2263 // Otherwise should result in an error status
2264 //
2265 Multiplicand = 0x123456789;
2266 Multiplier = 0xa789abcd;
2267 Status = SafeInt64Mult (Multiplicand, Multiplier, &Result);
2268 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL, Status);
2269 }
2270
2271 int main(int argc, char* argv[]) {
2272 testing::InitGoogleTest(&argc, argv);
2273 return RUN_ALL_TESTS();
2274 }