2 UEFI OS based application for unit testing the SafeIntLib.
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
9 #include <gtest/gtest.h>
12 #include <Library/SafeIntLib.h>
16 // Conversion function tests:
18 TEST(ConversionTestSuite
, TestSafeInt8ToUint8
) {
24 // Positive UINT8 should result in just a cast
28 Status
= SafeInt8ToUint8 (Operand
, &Result
);
29 ASSERT_EQ (Status
, RETURN_SUCCESS
);
30 ASSERT_EQ (0x5b, Result
);
33 // Negative number should result in an error status
36 Status
= SafeInt8ToUint8 (Operand
, &Result
);
37 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
40 TEST(ConversionTestSuite
, TestSafeInt8ToUint16
) {
46 // Positive UINT8 should result in just a cast
50 Status
= SafeInt8ToUint16 (Operand
, &Result
);
51 ASSERT_EQ (Status
, RETURN_SUCCESS
);
52 ASSERT_EQ (0x5b, Result
);
55 // Negative number should result in an error status
58 Status
= SafeInt8ToUint16 (Operand
, &Result
);
59 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
62 TEST(ConversionTestSuite
, TestSafeInt8ToUint32
) {
68 // Positive UINT8 should result in just a cast
72 Status
= SafeInt8ToUint32 (Operand
, &Result
);
73 ASSERT_EQ (Status
, RETURN_SUCCESS
);
74 ASSERT_EQ ((UINT32
)0x5b, Result
);
77 // Negative number should result in an error status
80 Status
= SafeInt8ToUint32 (Operand
, &Result
);
81 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
84 TEST(ConversionTestSuite
, TestSafeInt8ToUintn
) {
90 // Positive UINT8 should result in just a cast
94 Status
= SafeInt8ToUintn (Operand
, &Result
);
95 ASSERT_EQ (Status
, RETURN_SUCCESS
);
96 ASSERT_EQ ((UINTN
)0x5b, Result
);
99 // Negative number should result in an error status
102 Status
= SafeInt8ToUintn (Operand
, &Result
);
103 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
106 TEST(ConversionTestSuite
, TestSafeInt8ToUint64
) {
107 RETURN_STATUS Status
;
112 // Positive UINT8 should result in just a cast
116 Status
= SafeInt8ToUint64 (Operand
, &Result
);
117 ASSERT_EQ (Status
, RETURN_SUCCESS
);
118 ASSERT_EQ ((UINT64
)0x5b, Result
);
121 // Negative number should result in an error status
124 Status
= SafeInt8ToUint64 (Operand
, &Result
);
125 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
128 TEST(ConversionTestSuite
, TestSafeUint8ToInt8
) {
129 RETURN_STATUS Status
;
134 // Operand <= 0x7F (MAX_INT8) should result in a cast
138 Status
= SafeUint8ToInt8 (Operand
, &Result
);
139 ASSERT_EQ (Status
, RETURN_SUCCESS
);
140 ASSERT_EQ (0x5b, Result
);
143 // Operand larger than 0x7f should result in an error status
146 Status
= SafeUint8ToInt8 (Operand
, &Result
);
147 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
150 TEST(ConversionTestSuite
, TestSafeUint8ToChar8
) {
151 RETURN_STATUS Status
;
156 // CHAR8 is typedefed as char, which by default is signed, thus
157 // CHAR8 is same as INT8, so same tests as above:
161 // Operand <= 0x7F (MAX_INT8) should result in a cast
165 Status
= SafeUint8ToChar8 (Operand
, &Result
);
166 ASSERT_EQ (Status
, RETURN_SUCCESS
);
167 ASSERT_EQ (0x5b, Result
);
170 // Operand larger than 0x7f should result in an error status
173 Status
= SafeUint8ToChar8 (Operand
, &Result
);
174 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
177 TEST(ConversionTestSuite
, TestSafeInt16ToInt8
) {
178 RETURN_STATUS Status
;
183 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
187 Status
= SafeInt16ToInt8 (Operand
, &Result
);
188 ASSERT_EQ (Status
, RETURN_SUCCESS
);
189 ASSERT_EQ (0x5b, Result
);
192 Status
= SafeInt16ToInt8 (Operand
, &Result
);
193 ASSERT_EQ (Status
, RETURN_SUCCESS
);
194 ASSERT_EQ ((-35), Result
);
197 // Otherwise should result in an error status
200 Status
= SafeInt16ToInt8 (Operand
, &Result
);
201 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
204 Status
= SafeInt16ToInt8 (Operand
, &Result
);
205 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
208 TEST(ConversionTestSuite
, TestSafeInt16ToChar8
) {
209 RETURN_STATUS Status
;
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.
219 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
223 Status
= SafeInt16ToChar8 (Operand
, &Result
);
224 ASSERT_EQ (Status
, RETURN_SUCCESS
);
225 ASSERT_EQ (0x5b, Result
);
229 Status
= SafeInt16ToChar8 (Operand
, &Result
);
230 ASSERT_EQ (Status
, RETURN_SUCCESS
);
231 ASSERT_EQ (0, Result
);
235 Status
= SafeInt16ToChar8 (Operand
, &Result
);
236 ASSERT_EQ (Status
, RETURN_SUCCESS
);
237 ASSERT_EQ (MAX_INT8
, Result
);
240 // Otherwise should result in an error status
243 Status
= SafeInt16ToChar8 (Operand
, &Result
);
244 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
247 Status
= SafeInt16ToChar8 (Operand
, &Result
);
248 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
251 Status
= SafeInt16ToChar8 (Operand
, &Result
);
252 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
255 TEST(ConversionTestSuite
, TestSafeInt16ToUint8
) {
256 RETURN_STATUS Status
;
261 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
265 Status
= SafeInt16ToUint8 (Operand
, &Result
);
266 ASSERT_EQ (Status
, RETURN_SUCCESS
);
267 ASSERT_EQ (0x5b, Result
);
270 // Otherwise should result in an error status
273 Status
= SafeInt16ToUint8 (Operand
, &Result
);
274 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
277 Status
= SafeInt16ToUint8 (Operand
, &Result
);
278 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
281 TEST(ConversionTestSuite
, TestSafeInt16ToUint16
) {
282 RETURN_STATUS Status
;
283 INT16 Operand
= 0x5b5b;
287 // If Operand is non-negative, then it's a cast
289 Status
= SafeInt16ToUint16 (Operand
, &Result
);
290 ASSERT_EQ (Status
, RETURN_SUCCESS
);
291 ASSERT_EQ (0x5b5b, Result
);
294 // Otherwise should result in an error status
297 Status
= SafeInt16ToUint16 (Operand
, &Result
);
298 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
301 TEST(ConversionTestSuite
, TestSafeInt16ToUint32
) {
302 RETURN_STATUS Status
;
307 // If Operand is non-negative, then it's a cast
311 Status
= SafeInt16ToUint32 (Operand
, &Result
);
312 ASSERT_EQ (Status
, RETURN_SUCCESS
);
313 ASSERT_EQ ((UINT32
)0x5b5b, Result
);
316 // Otherwise should result in an error status
319 Status
= SafeInt16ToUint32 (Operand
, &Result
);
320 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
323 TEST(ConversionTestSuite
, TestSafeInt16ToUintn
) {
324 RETURN_STATUS Status
;
329 // If Operand is non-negative, then it's a cast
333 Status
= SafeInt16ToUintn (Operand
, &Result
);
334 ASSERT_EQ (Status
, RETURN_SUCCESS
);
335 ASSERT_EQ ((UINTN
)0x5b5b, Result
);
338 // Otherwise should result in an error status
341 Status
= SafeInt16ToUintn (Operand
, &Result
);
342 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
345 TEST(ConversionTestSuite
, TestSafeInt16ToUint64
) {
346 RETURN_STATUS Status
;
351 // If Operand is non-negative, then it's a cast
355 Status
= SafeInt16ToUint64 (Operand
, &Result
);
356 ASSERT_EQ (Status
, RETURN_SUCCESS
);
357 ASSERT_EQ ((UINT64
)0x5b5b, Result
);
360 // Otherwise should result in an error status
363 Status
= SafeInt16ToUint64 (Operand
, &Result
);
364 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
367 TEST(ConversionTestSuite
, TestSafeUint16ToInt8
) {
368 RETURN_STATUS Status
;
373 // If Operand is <= MAX_INT8, it's a cast
377 Status
= SafeUint16ToInt8 (Operand
, &Result
);
378 ASSERT_EQ (Status
, RETURN_SUCCESS
);
379 ASSERT_EQ (0x5b, Result
);
382 // Otherwise should result in an error status
385 Status
= SafeUint16ToInt8 (Operand
, &Result
);
386 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
389 TEST(ConversionTestSuite
, TestSafeUint16ToChar8
) {
390 RETURN_STATUS Status
;
394 // CHAR8 is typedefed as char, which by default is signed, thus
395 // CHAR8 is same as INT8, so same tests as above:
398 // If Operand is <= MAX_INT8, it's a cast
402 Status
= SafeUint16ToChar8 (Operand
, &Result
);
403 ASSERT_EQ (Status
, RETURN_SUCCESS
);
404 ASSERT_EQ (0x5b, Result
);
407 // Otherwise should result in an error status
410 Status
= SafeUint16ToChar8 (Operand
, &Result
);
411 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
414 TEST(ConversionTestSuite
, TestSafeUint16ToUint8
) {
415 RETURN_STATUS Status
;
420 // If Operand is <= MAX_UINT8 (0xff), it's a cast
424 Status
= SafeUint16ToUint8 (Operand
, &Result
);
425 ASSERT_EQ (Status
, RETURN_SUCCESS
);
426 ASSERT_EQ (0xab, Result
);
429 // Otherwise should result in an error status
432 Status
= SafeUint16ToUint8 (Operand
, &Result
);
433 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
436 TEST(ConversionTestSuite
, TestSafeUint16ToInt16
) {
437 RETURN_STATUS Status
;
442 // If Operand is <= MAX_INT16 (0x7fff), it's a cast
446 Status
= SafeUint16ToInt16 (Operand
, &Result
);
447 ASSERT_EQ (Status
, RETURN_SUCCESS
);
448 ASSERT_EQ (0x5b5b, Result
);
451 // Otherwise should result in an error status
454 Status
= SafeUint16ToInt16 (Operand
, &Result
);
455 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
458 TEST(ConversionTestSuite
, TestSafeInt32ToInt8
) {
459 RETURN_STATUS Status
;
464 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
468 Status
= SafeInt32ToInt8 (Operand
, &Result
);
469 ASSERT_EQ (Status
, RETURN_SUCCESS
);
470 ASSERT_EQ (0x5b, Result
);
473 Status
= SafeInt32ToInt8 (Operand
, &Result
);
474 ASSERT_EQ (Status
, RETURN_SUCCESS
);
475 ASSERT_EQ ((-57), Result
);
478 // Otherwise should result in an error status
480 Operand
= (0x5bababab);
481 Status
= SafeInt32ToInt8 (Operand
, &Result
);
482 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
484 Operand
= (-1537977259);
485 Status
= SafeInt32ToInt8 (Operand
, &Result
);
486 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
489 TEST(ConversionTestSuite
, TestSafeInt32ToChar8
) {
490 RETURN_STATUS Status
;
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.
500 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
504 Status
= SafeInt32ToChar8 (Operand
, &Result
);
505 ASSERT_EQ (Status
, RETURN_SUCCESS
);
506 ASSERT_EQ (0x5b, Result
);
510 Status
= SafeInt32ToChar8 (Operand
, &Result
);
511 ASSERT_EQ (Status
, RETURN_SUCCESS
);
512 ASSERT_EQ (0, Result
);
516 Status
= SafeInt32ToChar8 (Operand
, &Result
);
517 ASSERT_EQ (Status
, RETURN_SUCCESS
);
518 ASSERT_EQ (MAX_INT8
, Result
);
521 // Otherwise should result in an error status
524 Status
= SafeInt32ToChar8 (Operand
, &Result
);
525 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
527 Operand
= (0x5bababab);
528 Status
= SafeInt32ToChar8 (Operand
, &Result
);
529 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
531 Operand
= (-1537977259);
532 Status
= SafeInt32ToChar8 (Operand
, &Result
);
533 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
536 TEST(ConversionTestSuite
, TestSafeInt32ToUint8
) {
537 RETURN_STATUS Status
;
542 // If Operand is between 0 and MAX_INT8 inclusive, then it's a cast
546 Status
= SafeInt32ToUint8 (Operand
, &Result
);
547 ASSERT_EQ (Status
, RETURN_SUCCESS
);
548 ASSERT_EQ (0x5b, Result
);
551 // Otherwise should result in an error status
554 Status
= SafeInt32ToUint8 (Operand
, &Result
);
555 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
557 Operand
= (0x5bababab);
558 Status
= SafeInt32ToUint8 (Operand
, &Result
);
559 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
561 Operand
= (-1537977259);
562 Status
= SafeInt32ToUint8 (Operand
, &Result
);
563 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
566 TEST(ConversionTestSuite
, TestSafeInt32ToInt16
) {
567 RETURN_STATUS Status
;
572 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
576 Status
= SafeInt32ToInt16 (Operand
, &Result
);
577 ASSERT_EQ (Status
, RETURN_SUCCESS
);
578 ASSERT_EQ (0x5b5b, Result
);
581 Status
= SafeInt32ToInt16 (Operand
, &Result
);
582 ASSERT_EQ (Status
, RETURN_SUCCESS
);
583 ASSERT_EQ ((-17857), Result
);
586 // Otherwise should result in an error status
588 Operand
= (0x5bababab);
589 Status
= SafeInt32ToInt16 (Operand
, &Result
);
590 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
592 Operand
= (-1537977259);
593 Status
= SafeInt32ToInt16 (Operand
, &Result
);
594 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
597 TEST(ConversionTestSuite
, TestSafeInt32ToUint16
) {
598 RETURN_STATUS Status
;
603 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
607 Status
= SafeInt32ToUint16 (Operand
, &Result
);
608 ASSERT_EQ (Status
, RETURN_SUCCESS
);
609 ASSERT_EQ (0xabab, Result
);
612 // Otherwise should result in an error status
615 Status
= SafeInt32ToUint16 (Operand
, &Result
);
616 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
618 Operand
= (0x5bababab);
619 Status
= SafeInt32ToUint16 (Operand
, &Result
);
620 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
622 Operand
= (-1537977259);
623 Status
= SafeInt32ToUint16 (Operand
, &Result
);
624 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
627 TEST(ConversionTestSuite
, TestSafeInt32ToUint32
) {
628 RETURN_STATUS Status
;
633 // If Operand is non-negative, then it's a cast
635 Operand
= 0x5bababab;
637 Status
= SafeInt32ToUint32 (Operand
, &Result
);
638 ASSERT_EQ (Status
, RETURN_SUCCESS
);
639 ASSERT_EQ ((UINT32
)0x5bababab, Result
);
642 // Otherwise should result in an error status
644 Operand
= (-1537977259);
645 Status
= SafeInt32ToUint32 (Operand
, &Result
);
646 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
649 TEST(ConversionTestSuite
, TestSafeInt32ToUint64
) {
650 RETURN_STATUS Status
;
655 // If Operand is non-negative, then it's a cast
657 Operand
= 0x5bababab;
659 Status
= SafeInt32ToUint64 (Operand
, &Result
);
660 ASSERT_EQ (Status
, RETURN_SUCCESS
);
661 ASSERT_EQ ((UINT64
)0x5bababab, Result
);
664 // Otherwise should result in an error status
666 Operand
= (-1537977259);
667 Status
= SafeInt32ToUint64 (Operand
, &Result
);
668 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
671 TEST(ConversionTestSuite
, TestSafeUint32ToInt8
) {
672 RETURN_STATUS Status
;
677 // If Operand is <= MAX_INT8, then it's a cast
681 Status
= SafeUint32ToInt8 (Operand
, &Result
);
682 ASSERT_EQ (Status
, RETURN_SUCCESS
);
683 ASSERT_EQ (0x5b, Result
);
686 // Otherwise should result in an error status
688 Operand
= (0x5bababab);
689 Status
= SafeUint32ToInt8 (Operand
, &Result
);
690 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
693 TEST(ConversionTestSuite
, TestSafeUint32ToChar8
) {
694 RETURN_STATUS Status
;
698 // CHAR8 is typedefed as char, which by default is signed, thus
699 // CHAR8 is same as INT8, so same tests as above:
702 // If Operand is <= MAX_INT8, then it's a cast
706 Status
= SafeUint32ToChar8 (Operand
, &Result
);
707 ASSERT_EQ (Status
, RETURN_SUCCESS
);
708 ASSERT_EQ (0x5b, Result
);
711 // Otherwise should result in an error status
713 Operand
= (0x5bababab);
714 Status
= SafeUint32ToChar8 (Operand
, &Result
);
715 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
718 TEST(ConversionTestSuite
, TestSafeUint32ToUint8
) {
719 RETURN_STATUS Status
;
724 // If Operand is <= MAX_UINT8, then it's a cast
728 Status
= SafeUint32ToUint8 (Operand
, &Result
);
729 ASSERT_EQ (Status
, RETURN_SUCCESS
);
730 ASSERT_EQ (0xab, Result
);
733 // Otherwise should result in an error status
735 Operand
= (0xabababab);
736 Status
= SafeUint32ToUint8 (Operand
, &Result
);
737 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
740 TEST(ConversionTestSuite
, TestSafeUint32ToInt16
) {
741 RETURN_STATUS Status
;
746 // If Operand is <= MAX_INT16, then it's a cast
750 Status
= SafeUint32ToInt16 (Operand
, &Result
);
751 ASSERT_EQ (Status
, RETURN_SUCCESS
);
752 ASSERT_EQ (0x5bab, Result
);
755 // Otherwise should result in an error status
757 Operand
= (0xabababab);
758 Status
= SafeUint32ToInt16 (Operand
, &Result
);
759 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
762 TEST(ConversionTestSuite
, TestSafeUint32ToUint16
) {
763 RETURN_STATUS Status
;
768 // If Operand is <= MAX_UINT16, then it's a cast
772 Status
= SafeUint32ToUint16 (Operand
, &Result
);
773 ASSERT_EQ (Status
, RETURN_SUCCESS
);
774 ASSERT_EQ (0xabab, Result
);
777 // Otherwise should result in an error status
779 Operand
= (0xabababab);
780 Status
= SafeUint32ToUint16 (Operand
, &Result
);
781 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
784 TEST(ConversionTestSuite
, TestSafeUint32ToInt32
) {
785 RETURN_STATUS Status
;
790 // If Operand is <= MAX_INT32, then it's a cast
792 Operand
= 0x5bababab;
794 Status
= SafeUint32ToInt32 (Operand
, &Result
);
795 ASSERT_EQ (Status
, RETURN_SUCCESS
);
796 ASSERT_EQ (0x5bababab, Result
);
799 // Otherwise should result in an error status
801 Operand
= (0xabababab);
802 Status
= SafeUint32ToInt32 (Operand
, &Result
);
803 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
806 TEST(ConversionTestSuite
, TestSafeIntnToInt8
) {
807 RETURN_STATUS Status
;
812 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
816 Status
= SafeIntnToInt8 (Operand
, &Result
);
817 ASSERT_EQ (Status
, RETURN_SUCCESS
);
818 ASSERT_EQ (0x5b, Result
);
821 Status
= SafeIntnToInt8 (Operand
, &Result
);
822 ASSERT_EQ (Status
, RETURN_SUCCESS
);
823 ASSERT_EQ ((-53), Result
);
826 // Otherwise should result in an error status
828 Operand
= (0x5bababab);
829 Status
= SafeIntnToInt8 (Operand
, &Result
);
830 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
832 Operand
= (-1537977259);
833 Status
= SafeIntnToInt8 (Operand
, &Result
);
834 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
837 TEST(ConversionTestSuite
, TestSafeIntnToChar8
) {
838 RETURN_STATUS Status
;
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.
848 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
852 Status
= SafeIntnToChar8 (Operand
, &Result
);
853 ASSERT_EQ (Status
, RETURN_SUCCESS
);
854 ASSERT_EQ (0x5b, Result
);
858 Status
= SafeIntnToChar8 (Operand
, &Result
);
859 ASSERT_EQ (Status
, RETURN_SUCCESS
);
860 ASSERT_EQ (0, Result
);
864 Status
= SafeIntnToChar8 (Operand
, &Result
);
865 ASSERT_EQ (Status
, RETURN_SUCCESS
);
866 ASSERT_EQ (MAX_INT8
, Result
);
869 // Otherwise should result in an error status
872 Status
= SafeIntnToChar8 (Operand
, &Result
);
873 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
875 Operand
= (0x5bababab);
876 Status
= SafeIntnToChar8 (Operand
, &Result
);
877 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
879 Operand
= (-1537977259);
880 Status
= SafeIntnToChar8 (Operand
, &Result
);
881 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
884 TEST(ConversionTestSuite
, TestSafeIntnToUint8
) {
885 RETURN_STATUS Status
;
890 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
894 Status
= SafeIntnToUint8 (Operand
, &Result
);
895 ASSERT_EQ (Status
, RETURN_SUCCESS
);
896 ASSERT_EQ (0xab, Result
);
899 // Otherwise should result in an error status
901 Operand
= (0x5bababab);
902 Status
= SafeIntnToUint8 (Operand
, &Result
);
903 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
905 Operand
= (-1537977259);
906 Status
= SafeIntnToUint8 (Operand
, &Result
);
907 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
910 TEST(ConversionTestSuite
, TestSafeIntnToInt16
) {
911 RETURN_STATUS Status
;
916 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
920 Status
= SafeIntnToInt16 (Operand
, &Result
);
921 ASSERT_EQ (Status
, RETURN_SUCCESS
);
922 ASSERT_EQ (0x5bab, Result
);
925 Status
= SafeIntnToInt16 (Operand
, &Result
);
926 ASSERT_EQ (Status
, RETURN_SUCCESS
);
927 ASSERT_EQ ((-23467), Result
);
930 // Otherwise should result in an error status
932 Operand
= (0x5bababab);
933 Status
= SafeIntnToInt16 (Operand
, &Result
);
934 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
936 Operand
= (-1537977259);
937 Status
= SafeIntnToInt16 (Operand
, &Result
);
938 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
941 TEST(ConversionTestSuite
, TestSafeIntnToUint16
) {
942 RETURN_STATUS Status
;
947 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
951 Status
= SafeIntnToUint16 (Operand
, &Result
);
952 ASSERT_EQ (Status
, RETURN_SUCCESS
);
953 ASSERT_EQ (0xabab, Result
);
956 // Otherwise should result in an error status
958 Operand
= (0x5bababab);
959 Status
= SafeIntnToUint16 (Operand
, &Result
);
960 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
962 Operand
= (-1537977259);
963 Status
= SafeIntnToUint16 (Operand
, &Result
);
964 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
967 TEST(ConversionTestSuite
, TestSafeIntnToUintn
) {
968 RETURN_STATUS Status
;
973 // If Operand is non-negative, then it's a cast
975 Operand
= 0x5bababab;
977 Status
= SafeIntnToUintn (Operand
, &Result
);
978 ASSERT_EQ (Status
, RETURN_SUCCESS
);
979 ASSERT_EQ ((UINTN
)0x5bababab, Result
);
982 // Otherwise should result in an error status
984 Operand
= (-1537977259);
985 Status
= SafeIntnToUintn (Operand
, &Result
);
986 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
989 TEST(ConversionTestSuite
, TestSafeIntnToUint64
) {
990 RETURN_STATUS Status
;
995 // If Operand is non-negative, then it's a cast
997 Operand
= 0x5bababab;
999 Status
= SafeIntnToUint64 (Operand
, &Result
);
1000 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1001 ASSERT_EQ ((UINT64
)0x5bababab, Result
);
1004 // Otherwise should result in an error status
1006 Operand
= (-1537977259);
1007 Status
= SafeIntnToUint64 (Operand
, &Result
);
1008 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1011 TEST(ConversionTestSuite
, TestSafeUintnToInt8
) {
1012 RETURN_STATUS Status
;
1017 // If Operand is <= MAX_INT8, then it's a cast
1021 Status
= SafeUintnToInt8 (Operand
, &Result
);
1022 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1023 ASSERT_EQ (0x5b, Result
);
1026 // Otherwise should result in an error status
1029 Status
= SafeUintnToInt8 (Operand
, &Result
);
1030 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1033 TEST(ConversionTestSuite
, TestSafeUintnToChar8
) {
1034 RETURN_STATUS Status
;
1038 // CHAR8 is typedefed as char, which by default is signed, thus
1039 // CHAR8 is same as INT8, so same tests as above:
1042 // If Operand is <= MAX_INT8, then it's a cast
1046 Status
= SafeUintnToChar8 (Operand
, &Result
);
1047 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1048 ASSERT_EQ (0x5b, Result
);
1051 // Otherwise should result in an error status
1054 Status
= SafeUintnToChar8 (Operand
, &Result
);
1055 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1058 TEST(ConversionTestSuite
, TestSafeUintnToUint8
) {
1059 RETURN_STATUS Status
;
1064 // If Operand is <= MAX_UINT8, then it's a cast
1068 Status
= SafeUintnToUint8 (Operand
, &Result
);
1069 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1070 ASSERT_EQ (0xab, Result
);
1073 // Otherwise should result in an error status
1076 Status
= SafeUintnToUint8 (Operand
, &Result
);
1077 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1080 TEST(ConversionTestSuite
, TestSafeUintnToInt16
) {
1081 RETURN_STATUS Status
;
1086 // If Operand is <= MAX_INT16, then it's a cast
1090 Status
= SafeUintnToInt16 (Operand
, &Result
);
1091 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1092 ASSERT_EQ (0x5bab, Result
);
1095 // Otherwise should result in an error status
1098 Status
= SafeUintnToInt16 (Operand
, &Result
);
1099 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1102 TEST(ConversionTestSuite
, TestSafeUintnToUint16
) {
1103 RETURN_STATUS Status
;
1108 // If Operand is <= MAX_UINT16, then it's a cast
1112 Status
= SafeUintnToUint16 (Operand
, &Result
);
1113 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1114 ASSERT_EQ (0xabab, Result
);
1117 // Otherwise should result in an error status
1119 Operand
= (0xabababab);
1120 Status
= SafeUintnToUint16 (Operand
, &Result
);
1121 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1124 TEST(ConversionTestSuite
, TestSafeUintnToInt32
) {
1125 RETURN_STATUS Status
;
1130 // If Operand is <= MAX_INT32, then it's a cast
1132 Operand
= 0x5bababab;
1134 Status
= SafeUintnToInt32 (Operand
, &Result
);
1135 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1136 ASSERT_EQ (0x5bababab, Result
);
1139 // Otherwise should result in an error status
1141 Operand
= (0xabababab);
1142 Status
= SafeUintnToInt32 (Operand
, &Result
);
1143 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1146 TEST(ConversionTestSuite
, TestSafeInt64ToInt8
) {
1147 RETURN_STATUS Status
;
1152 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1156 Status
= SafeInt64ToInt8 (Operand
, &Result
);
1157 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1158 ASSERT_EQ (0x5b, Result
);
1161 Status
= SafeInt64ToInt8 (Operand
, &Result
);
1162 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1163 ASSERT_EQ ((-37), Result
);
1166 // Otherwise should result in an error status
1168 Operand
= (0x5babababefefefef);
1169 Status
= SafeInt64ToInt8 (Operand
, &Result
);
1170 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1172 Operand
= (-6605562033422200815);
1173 Status
= SafeInt64ToInt8 (Operand
, &Result
);
1174 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1177 TEST(ConversionTestSuite
, TestSafeInt64ToChar8
) {
1178 RETURN_STATUS Status
;
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.
1188 // If Operand is between MIN_INT8 and MAX_INT8 inclusive, then it's a cast
1192 Status
= SafeInt64ToChar8 (Operand
, &Result
);
1193 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1194 ASSERT_EQ (0x5b, Result
);
1198 Status
= SafeInt64ToChar8 (Operand
, &Result
);
1199 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1200 ASSERT_EQ (0, Result
);
1204 Status
= SafeInt64ToChar8 (Operand
, &Result
);
1205 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1206 ASSERT_EQ (MAX_INT8
, Result
);
1209 // Otherwise should result in an error status
1212 Status
= SafeInt64ToChar8 (Operand
, &Result
);
1213 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1215 Operand
= (0x5babababefefefef);
1216 Status
= SafeInt64ToChar8 (Operand
, &Result
);
1217 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1219 Operand
= (-6605562033422200815);
1220 Status
= SafeInt64ToChar8 (Operand
, &Result
);
1221 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1224 TEST(ConversionTestSuite
, TestSafeInt64ToUint8
) {
1225 RETURN_STATUS Status
;
1230 // If Operand is between 0 and MAX_UINT8 inclusive, then it's a cast
1234 Status
= SafeInt64ToUint8 (Operand
, &Result
);
1235 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1236 ASSERT_EQ (0xab, Result
);
1239 // Otherwise should result in an error status
1241 Operand
= (0x5babababefefefef);
1242 Status
= SafeInt64ToUint8 (Operand
, &Result
);
1243 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1245 Operand
= (-6605562033422200815);
1246 Status
= SafeInt64ToUint8 (Operand
, &Result
);
1247 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1250 TEST(ConversionTestSuite
, TestSafeInt64ToInt16
) {
1251 RETURN_STATUS Status
;
1256 // If Operand is between MIN_INT16 and MAX_INT16 inclusive, then it's a cast
1260 Status
= SafeInt64ToInt16 (Operand
, &Result
);
1261 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1262 ASSERT_EQ (0x5bab, Result
);
1265 Status
= SafeInt64ToInt16 (Operand
, &Result
);
1266 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1267 ASSERT_EQ ((-23467), Result
);
1270 // Otherwise should result in an error status
1272 Operand
= (0x5babababefefefef);
1273 Status
= SafeInt64ToInt16 (Operand
, &Result
);
1274 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1276 Operand
= (-6605562033422200815);
1277 Status
= SafeInt64ToInt16 (Operand
, &Result
);
1278 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1281 TEST(ConversionTestSuite
, TestSafeInt64ToUint16
) {
1282 RETURN_STATUS Status
;
1287 // If Operand is between 0 and MAX_UINT16 inclusive, then it's a cast
1291 Status
= SafeInt64ToUint16 (Operand
, &Result
);
1292 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1293 ASSERT_EQ (0xabab, Result
);
1296 // Otherwise should result in an error status
1298 Operand
= (0x5babababefefefef);
1299 Status
= SafeInt64ToUint16 (Operand
, &Result
);
1300 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1302 Operand
= (-6605562033422200815);
1303 Status
= SafeInt64ToUint16 (Operand
, &Result
);
1304 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1307 TEST(ConversionTestSuite
, TestSafeInt64ToInt32
) {
1308 RETURN_STATUS Status
;
1313 // If Operand is between MIN_INT32 and MAX_INT32 inclusive, then it's a cast
1315 Operand
= 0x5bababab;
1317 Status
= SafeInt64ToInt32 (Operand
, &Result
);
1318 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1319 ASSERT_EQ (0x5bababab, Result
);
1321 Operand
= (-1537977259);
1322 Status
= SafeInt64ToInt32 (Operand
, &Result
);
1323 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1324 ASSERT_EQ ((-1537977259), Result
);
1327 // Otherwise should result in an error status
1329 Operand
= (0x5babababefefefef);
1330 Status
= SafeInt64ToInt32 (Operand
, &Result
);
1331 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1333 Operand
= (-6605562033422200815);
1334 Status
= SafeInt64ToInt32 (Operand
, &Result
);
1335 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1338 TEST(ConversionTestSuite
, TestSafeInt64ToUint32
) {
1339 RETURN_STATUS Status
;
1344 // If Operand is between 0 and MAX_UINT32 inclusive, then it's a cast
1346 Operand
= 0xabababab;
1348 Status
= SafeInt64ToUint32 (Operand
, &Result
);
1349 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1350 ASSERT_EQ (0xabababab, Result
);
1353 // Otherwise should result in an error status
1355 Operand
= (0x5babababefefefef);
1356 Status
= SafeInt64ToUint32 (Operand
, &Result
);
1357 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1359 Operand
= (-6605562033422200815);
1360 Status
= SafeInt64ToUint32 (Operand
, &Result
);
1361 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1364 TEST(ConversionTestSuite
, TestSafeInt64ToUint64
) {
1365 RETURN_STATUS Status
;
1370 // If Operand is non-negative, then it's a cast
1372 Operand
= 0x5babababefefefef;
1374 Status
= SafeInt64ToUint64 (Operand
, &Result
);
1375 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1376 ASSERT_EQ ((UINT64
)0x5babababefefefef, Result
);
1379 // Otherwise should result in an error status
1381 Operand
= (-6605562033422200815);
1382 Status
= SafeInt64ToUint64 (Operand
, &Result
);
1383 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1386 TEST(ConversionTestSuite
, TestSafeUint64ToInt8
) {
1387 RETURN_STATUS Status
;
1392 // If Operand is <= MAX_INT8, then it's a cast
1396 Status
= SafeUint64ToInt8 (Operand
, &Result
);
1397 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1398 ASSERT_EQ (0x5b, Result
);
1401 // Otherwise should result in an error status
1403 Operand
= (0xababababefefefef);
1404 Status
= SafeUint64ToInt8 (Operand
, &Result
);
1405 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1408 TEST(ConversionTestSuite
, TestSafeUint64ToChar8
) {
1409 RETURN_STATUS Status
;
1413 // CHAR8 is typedefed as char, which by default is signed, thus
1414 // CHAR8 is same as INT8, so same tests as above:
1417 // If Operand is <= MAX_INT8, then it's a cast
1421 Status
= SafeUint64ToChar8 (Operand
, &Result
);
1422 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1423 ASSERT_EQ (0x5b, Result
);
1426 // Otherwise should result in an error status
1428 Operand
= (0xababababefefefef);
1429 Status
= SafeUint64ToChar8 (Operand
, &Result
);
1430 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1433 TEST(ConversionTestSuite
, TestSafeUint64ToUint8
) {
1434 RETURN_STATUS Status
;
1439 // If Operand is <= MAX_UINT8, then it's a cast
1443 Status
= SafeUint64ToUint8 (Operand
, &Result
);
1444 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1445 ASSERT_EQ (0xab, Result
);
1448 // Otherwise should result in an error status
1450 Operand
= (0xababababefefefef);
1451 Status
= SafeUint64ToUint8 (Operand
, &Result
);
1452 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1455 TEST(ConversionTestSuite
, TestSafeUint64ToInt16
) {
1456 RETURN_STATUS Status
;
1461 // If Operand is <= MAX_INT16, then it's a cast
1465 Status
= SafeUint64ToInt16 (Operand
, &Result
);
1466 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1467 ASSERT_EQ (0x5bab, Result
);
1470 // Otherwise should result in an error status
1472 Operand
= (0xababababefefefef);
1473 Status
= SafeUint64ToInt16 (Operand
, &Result
);
1474 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1477 TEST(ConversionTestSuite
, TestSafeUint64ToUint16
) {
1478 RETURN_STATUS Status
;
1483 // If Operand is <= MAX_UINT16, then it's a cast
1487 Status
= SafeUint64ToUint16 (Operand
, &Result
);
1488 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1489 ASSERT_EQ (0xabab, Result
);
1492 // Otherwise should result in an error status
1494 Operand
= (0xababababefefefef);
1495 Status
= SafeUint64ToUint16 (Operand
, &Result
);
1496 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1499 TEST(ConversionTestSuite
, TestSafeUint64ToInt32
) {
1500 RETURN_STATUS Status
;
1505 // If Operand is <= MAX_INT32, then it's a cast
1507 Operand
= 0x5bababab;
1509 Status
= SafeUint64ToInt32 (Operand
, &Result
);
1510 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1511 ASSERT_EQ (0x5bababab, Result
);
1514 // Otherwise should result in an error status
1516 Operand
= (0xababababefefefef);
1517 Status
= SafeUint64ToInt32 (Operand
, &Result
);
1518 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1521 TEST(ConversionTestSuite
, TestSafeUint64ToUint32
) {
1522 RETURN_STATUS Status
;
1527 // If Operand is <= MAX_UINT32, then it's a cast
1529 Operand
= 0xabababab;
1531 Status
= SafeUint64ToUint32 (Operand
, &Result
);
1532 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1533 ASSERT_EQ (0xabababab, Result
);
1536 // Otherwise should result in an error status
1538 Operand
= (0xababababefefefef);
1539 Status
= SafeUint64ToUint32 (Operand
, &Result
);
1540 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1543 TEST(ConversionTestSuite
, TestSafeUint64ToInt64
) {
1544 RETURN_STATUS Status
;
1549 // If Operand is <= MAX_INT64, then it's a cast
1551 Operand
= 0x5babababefefefef;
1553 Status
= SafeUint64ToInt64 (Operand
, &Result
);
1554 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1555 ASSERT_EQ (0x5babababefefefef, Result
);
1558 // Otherwise should result in an error status
1560 Operand
= (0xababababefefefef);
1561 Status
= SafeUint64ToInt64 (Operand
, &Result
);
1562 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1566 // Addition function tests:
1568 TEST(AdditionSubtractionTestSuite
, TestSafeUint8Add
) {
1569 RETURN_STATUS Status
;
1575 // If the result of addition doesn't overflow MAX_UINT8, then it's addition
1580 Status
= SafeUint8Add (Augend
, Addend
, &Result
);
1581 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1582 ASSERT_EQ (0x74, Result
);
1585 // Otherwise should result in an error status
1589 Status
= SafeUint8Add (Augend
, Addend
, &Result
);
1590 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1593 TEST(AdditionSubtractionTestSuite
, TestSafeUint16Add
) {
1594 RETURN_STATUS Status
;
1595 UINT16 Augend
= 0x3a3a;
1596 UINT16 Addend
= 0x3a3a;
1600 // If the result of addition doesn't overflow MAX_UINT16, then it's addition
1602 Status
= SafeUint16Add (Augend
, Addend
, &Result
);
1603 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1604 ASSERT_EQ (0x7474, Result
);
1607 // Otherwise should result in an error status
1611 Status
= SafeUint16Add (Augend
, Addend
, &Result
);
1612 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1615 TEST(AdditionSubtractionTestSuite
, TestSafeUint32Add
) {
1616 RETURN_STATUS Status
;
1622 // If the result of addition doesn't overflow MAX_UINT32, then it's addition
1624 Augend
= 0x3a3a3a3a;
1625 Addend
= 0x3a3a3a3a;
1627 Status
= SafeUint32Add (Augend
, Addend
, &Result
);
1628 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1629 ASSERT_EQ ((UINT32
)0x74747474, Result
);
1632 // Otherwise should result in an error status
1634 Augend
= 0xabababab;
1635 Addend
= 0xbcbcbcbc;
1636 Status
= SafeUint32Add (Augend
, Addend
, &Result
);
1637 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1640 TEST(AdditionSubtractionTestSuite
, TestSafeUint64Add
) {
1641 RETURN_STATUS Status
;
1647 // If the result of addition doesn't overflow MAX_UINT64, then it's addition
1649 Augend
= 0x3a3a3a3a12121212;
1650 Addend
= 0x3a3a3a3a12121212;
1652 Status
= SafeUint64Add (Augend
, Addend
, &Result
);
1653 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1654 ASSERT_EQ ((UINT64
)0x7474747424242424, Result
);
1657 // Otherwise should result in an error status
1659 Augend
= 0xababababefefefef;
1660 Addend
= 0xbcbcbcbcdededede;
1661 Status
= SafeUint64Add (Augend
, Addend
, &Result
);
1662 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1665 TEST(AdditionSubtractionTestSuite
, TestSafeInt8Add
) {
1666 RETURN_STATUS Status
;
1672 // If the result of addition doesn't overflow MAX_INT8
1673 // and doesn't underflow MIN_INT8, then it's addition
1678 Status
= SafeInt8Add (Augend
, Addend
, &Result
);
1679 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1680 ASSERT_EQ (0x74, Result
);
1684 Status
= SafeInt8Add (Augend
, Addend
, &Result
);
1685 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1686 ASSERT_EQ ((-116), Result
);
1689 // Otherwise should result in an error status
1693 Status
= SafeInt8Add (Augend
, Addend
, &Result
);
1694 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1698 Status
= SafeInt8Add (Augend
, Addend
, &Result
);
1699 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1702 TEST(AdditionSubtractionTestSuite
, TestSafeInt16Add
) {
1703 RETURN_STATUS Status
;
1709 // If the result of addition doesn't overflow MAX_INT16
1710 // and doesn't underflow MIN_INT16, then it's addition
1715 Status
= SafeInt16Add (Augend
, Addend
, &Result
);
1716 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1717 ASSERT_EQ (0x7474, Result
);
1721 Status
= SafeInt16Add (Augend
, Addend
, &Result
);
1722 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1723 ASSERT_EQ ((-29812), Result
);
1726 // Otherwise should result in an error status
1730 Status
= SafeInt16Add (Augend
, Addend
, &Result
);
1731 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1735 Status
= SafeInt16Add (Augend
, Addend
, &Result
);
1736 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1739 TEST(AdditionSubtractionTestSuite
, TestSafeInt32Add
) {
1740 RETURN_STATUS Status
;
1746 // If the result of addition doesn't overflow MAX_INT32
1747 // and doesn't underflow MIN_INT32, then it's addition
1749 Augend
= 0x3a3a3a3a;
1750 Addend
= 0x3a3a3a3a;
1752 Status
= SafeInt32Add (Augend
, Addend
, &Result
);
1753 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1754 ASSERT_EQ (0x74747474, Result
);
1756 Augend
= (-976894522);
1757 Addend
= (-976894522);
1758 Status
= SafeInt32Add (Augend
, Addend
, &Result
);
1759 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1760 ASSERT_EQ ((-1953789044), Result
);
1763 // Otherwise should result in an error status
1765 Augend
= 0x5a5a5a5a;
1766 Addend
= 0x5a5a5a5a;
1767 Status
= SafeInt32Add (Augend
, Addend
, &Result
);
1768 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1770 Augend
= (-1515870810);
1771 Addend
= (-1515870810);
1772 Status
= SafeInt32Add (Augend
, Addend
, &Result
);
1773 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1776 TEST(AdditionSubtractionTestSuite
, TestSafeInt64Add
) {
1777 RETURN_STATUS Status
;
1783 // If the result of addition doesn't overflow MAX_INT64
1784 // and doesn't underflow MIN_INT64, then it's addition
1786 Augend
= 0x3a3a3a3a3a3a3a3a;
1787 Addend
= 0x3a3a3a3a3a3a3a3a;
1789 Status
= SafeInt64Add (Augend
, Addend
, &Result
);
1790 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1791 ASSERT_EQ (0x7474747474747474, Result
);
1793 Augend
= (-4195730024608447034);
1794 Addend
= (-4195730024608447034);
1795 Status
= SafeInt64Add (Augend
, Addend
, &Result
);
1796 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1797 ASSERT_EQ ((-8391460049216894068), Result
);
1800 // Otherwise should result in an error status
1802 Augend
= 0x5a5a5a5a5a5a5a5a;
1803 Addend
= 0x5a5a5a5a5a5a5a5a;
1804 Status
= SafeInt64Add (Augend
, Addend
, &Result
);
1805 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1807 Augend
= (-6510615555426900570);
1808 Addend
= (-6510615555426900570);
1809 Status
= SafeInt64Add (Augend
, Addend
, &Result
);
1810 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1814 // Subtraction function tests:
1816 TEST(AdditionSubtractionTestSuite
, TestSafeUint8Sub
) {
1817 RETURN_STATUS Status
;
1823 // If Minuend >= Subtrahend, then it's subtraction
1828 Status
= SafeUint8Sub (Minuend
, Subtrahend
, &Result
);
1829 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1830 ASSERT_EQ (0x1f, Result
);
1833 // Otherwise should result in an error status
1837 Status
= SafeUint8Sub (Minuend
, Subtrahend
, &Result
);
1838 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1841 TEST(AdditionSubtractionTestSuite
, TestSafeUint16Sub
) {
1842 RETURN_STATUS Status
;
1848 // If Minuend >= Subtrahend, then it's subtraction
1851 Subtrahend
= 0x3b3b;
1853 Status
= SafeUint16Sub (Minuend
, Subtrahend
, &Result
);
1854 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1855 ASSERT_EQ (0x1f1f, Result
);
1858 // Otherwise should result in an error status
1861 Subtrahend
= 0x6d6d;
1862 Status
= SafeUint16Sub (Minuend
, Subtrahend
, &Result
);
1863 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1866 TEST(AdditionSubtractionTestSuite
, TestSafeUint32Sub
) {
1867 RETURN_STATUS Status
;
1873 // If Minuend >= Subtrahend, then it's subtraction
1875 Minuend
= 0x5a5a5a5a;
1876 Subtrahend
= 0x3b3b3b3b;
1878 Status
= SafeUint32Sub (Minuend
, Subtrahend
, &Result
);
1879 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1880 ASSERT_EQ ((UINT32
)0x1f1f1f1f, Result
);
1883 // Otherwise should result in an error status
1885 Minuend
= 0x5a5a5a5a;
1886 Subtrahend
= 0x6d6d6d6d;
1887 Status
= SafeUint32Sub (Minuend
, Subtrahend
, &Result
);
1888 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1891 TEST(AdditionSubtractionTestSuite
, TestSafeUint64Sub
) {
1892 RETURN_STATUS Status
;
1898 // If Minuend >= Subtrahend, then it's subtraction
1900 Minuend
= 0x5a5a5a5a5a5a5a5a;
1901 Subtrahend
= 0x3b3b3b3b3b3b3b3b;
1903 Status
= SafeUint64Sub (Minuend
, Subtrahend
, &Result
);
1904 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1905 ASSERT_EQ ((UINT64
)0x1f1f1f1f1f1f1f1f, Result
);
1908 // Otherwise should result in an error status
1910 Minuend
= 0x5a5a5a5a5a5a5a5a;
1911 Subtrahend
= 0x6d6d6d6d6d6d6d6d;
1912 Status
= SafeUint64Sub (Minuend
, Subtrahend
, &Result
);
1913 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1916 TEST(AdditionSubtractionTestSuite
, TestSafeInt8Sub
) {
1917 RETURN_STATUS Status
;
1923 // If the result of subtractions doesn't overflow MAX_INT8 or
1924 // underflow MIN_INT8, then it's subtraction
1929 Status
= SafeInt8Sub (Minuend
, Subtrahend
, &Result
);
1930 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1931 ASSERT_EQ (0x20, Result
);
1935 Status
= SafeInt8Sub (Minuend
, Subtrahend
, &Result
);
1936 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1937 ASSERT_EQ ((-20), Result
);
1940 // Otherwise should result in an error status
1944 Status
= SafeInt8Sub (Minuend
, Subtrahend
, &Result
);
1945 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1949 Status
= SafeInt8Sub (Minuend
, Subtrahend
, &Result
);
1950 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1953 TEST(AdditionSubtractionTestSuite
, TestSafeInt16Sub
) {
1954 RETURN_STATUS Status
;
1960 // If the result of subtractions doesn't overflow MAX_INT16 or
1961 // underflow MIN_INT16, then it's subtraction
1964 Subtrahend
= 0x3a3a;
1966 Status
= SafeInt16Sub (Minuend
, Subtrahend
, &Result
);
1967 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1968 ASSERT_EQ (0x2020, Result
);
1971 Subtrahend
= 0x5a5a;
1972 Status
= SafeInt16Sub (Minuend
, Subtrahend
, &Result
);
1973 ASSERT_EQ (Status
, RETURN_SUCCESS
);
1974 ASSERT_EQ ((-8224), Result
);
1977 // Otherwise should result in an error status
1981 Status
= SafeInt16Sub (Minuend
, Subtrahend
, &Result
);
1982 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1985 Subtrahend
= (-31354);
1986 Status
= SafeInt16Sub (Minuend
, Subtrahend
, &Result
);
1987 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
1990 TEST(AdditionSubtractionTestSuite
, TestSafeInt32Sub
) {
1991 RETURN_STATUS Status
;
1997 // If the result of subtractions doesn't overflow MAX_INT32 or
1998 // underflow MIN_INT32, then it's subtraction
2000 Minuend
= 0x5a5a5a5a;
2001 Subtrahend
= 0x3a3a3a3a;
2003 Status
= SafeInt32Sub (Minuend
, Subtrahend
, &Result
);
2004 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2005 ASSERT_EQ (0x20202020, Result
);
2007 Minuend
= 0x3a3a3a3a;
2008 Subtrahend
= 0x5a5a5a5a;
2009 Status
= SafeInt32Sub (Minuend
, Subtrahend
, &Result
);
2010 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2011 ASSERT_EQ ((-538976288), Result
);
2014 // Otherwise should result in an error status
2016 Minuend
= (-2054847098);
2017 Subtrahend
= 2054847098;
2018 Status
= SafeInt32Sub (Minuend
, Subtrahend
, &Result
);
2019 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2021 Minuend
= (2054847098);
2022 Subtrahend
= (-2054847098);
2023 Status
= SafeInt32Sub (Minuend
, Subtrahend
, &Result
);
2024 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2027 TEST(AdditionSubtractionTestSuite
, TestSafeInt64Sub
) {
2028 RETURN_STATUS Status
;
2034 // If the result of subtractions doesn't overflow MAX_INT64 or
2035 // underflow MIN_INT64, then it's subtraction
2037 Minuend
= 0x5a5a5a5a5a5a5a5a;
2038 Subtrahend
= 0x3a3a3a3a3a3a3a3a;
2040 Status
= SafeInt64Sub (Minuend
, Subtrahend
, &Result
);
2041 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2042 ASSERT_EQ (0x2020202020202020, Result
);
2044 Minuend
= 0x3a3a3a3a3a3a3a3a;
2045 Subtrahend
= 0x5a5a5a5a5a5a5a5a;
2046 Status
= SafeInt64Sub (Minuend
, Subtrahend
, &Result
);
2047 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2048 ASSERT_EQ ((-2314885530818453536), Result
);
2051 // Otherwise should result in an error status
2053 Minuend
= (-8825501086245354106);
2054 Subtrahend
= 8825501086245354106;
2055 Status
= SafeInt64Sub (Minuend
, Subtrahend
, &Result
);
2056 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2058 Minuend
= (8825501086245354106);
2059 Subtrahend
= (-8825501086245354106);
2060 Status
= SafeInt64Sub (Minuend
, Subtrahend
, &Result
);
2061 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2065 // Multiplication function tests:
2067 TEST(MultiplicationTestSuite
, TestSafeUint8Mult
) {
2068 RETURN_STATUS Status
;
2074 // If the result of multiplication doesn't overflow MAX_UINT8, it will succeed
2076 Multiplicand
= 0x12;
2079 Status
= SafeUint8Mult (Multiplicand
, Multiplier
, &Result
);
2080 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2081 ASSERT_EQ (0xb4, Result
);
2084 // Otherwise should result in an error status
2086 Multiplicand
= 0x12;
2088 Status
= SafeUint8Mult (Multiplicand
, Multiplier
, &Result
);
2089 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2092 TEST(MultiplicationTestSuite
, TestSafeUint16Mult
) {
2093 RETURN_STATUS Status
;
2094 UINT16 Multiplicand
;
2099 // If the result of multiplication doesn't overflow MAX_UINT16, it will succeed
2101 Multiplicand
= 0x212;
2104 Status
= SafeUint16Mult (Multiplicand
, Multiplier
, &Result
);
2105 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2106 ASSERT_EQ (0xfc94, Result
);
2109 // Otherwise should result in an error status
2111 Multiplicand
= 0x1234;
2113 Status
= SafeUint16Mult (Multiplicand
, Multiplier
, &Result
);
2114 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2117 TEST(MultiplicationTestSuite
, TestSafeUint32Mult
) {
2118 RETURN_STATUS Status
;
2119 UINT32 Multiplicand
;
2124 // If the result of multiplication doesn't overflow MAX_UINT32, it will succeed
2126 Multiplicand
= 0xa122a;
2129 Status
= SafeUint32Mult (Multiplicand
, Multiplier
, &Result
);
2130 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2131 ASSERT_EQ (0x844c9dbe, Result
);
2134 // Otherwise should result in an error status
2136 Multiplicand
= 0xa122a;
2137 Multiplier
= 0xed23;
2138 Status
= SafeUint32Mult (Multiplicand
, Multiplier
, &Result
);
2139 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2142 TEST(MultiplicationTestSuite
, TestSafeUint64Mult
) {
2143 RETURN_STATUS Status
;
2144 UINT64 Multiplicand
;
2149 // If the result of multiplication doesn't overflow MAX_UINT64, it will succeed
2151 Multiplicand
= 0x123456789a;
2152 Multiplier
= 0x1234567;
2154 Status
= SafeUint64Mult (Multiplicand
, Multiplier
, &Result
);
2155 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2156 ASSERT_EQ ((UINT64
)0x14b66db9745a07f6, Result
);
2159 // Otherwise should result in an error status
2161 Multiplicand
= 0x123456789a;
2162 Multiplier
= 0x12345678;
2163 Status
= SafeUint64Mult (Multiplicand
, Multiplier
, &Result
);
2164 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2167 TEST(MultiplicationTestSuite
, TestSafeInt8Mult
) {
2168 RETURN_STATUS Status
;
2174 // If the result of multiplication doesn't overflow MAX_INT8 and doesn't
2175 // underflow MIN_UINT8, it will succeed
2177 Multiplicand
= 0x12;
2180 Status
= SafeInt8Mult (Multiplicand
, Multiplier
, &Result
);
2181 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2182 ASSERT_EQ (0x7e, Result
);
2185 // Otherwise should result in an error status
2187 Multiplicand
= 0x12;
2189 Status
= SafeInt8Mult (Multiplicand
, Multiplier
, &Result
);
2190 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2193 TEST(MultiplicationTestSuite
, TestSafeInt16Mult
) {
2194 RETURN_STATUS Status
;
2200 // If the result of multiplication doesn't overflow MAX_INT16 and doesn't
2201 // underflow MIN_UINT16, it will succeed
2203 Multiplicand
= 0x123;
2206 Status
= SafeInt16Mult (Multiplicand
, Multiplier
, &Result
);
2207 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2208 ASSERT_EQ (0x7515, Result
);
2211 // Otherwise should result in an error status
2213 Multiplicand
= 0x123;
2215 Status
= SafeInt16Mult (Multiplicand
, Multiplier
, &Result
);
2216 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2219 TEST(MultiplicationTestSuite
, TestSafeInt32Mult
) {
2220 RETURN_STATUS Status
;
2226 // If the result of multiplication doesn't overflow MAX_INT32 and doesn't
2227 // underflow MIN_UINT32, it will succeed
2229 Multiplicand
= 0x123456;
2232 Status
= SafeInt32Mult (Multiplicand
, Multiplier
, &Result
);
2233 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2234 ASSERT_EQ (0x75c28c50, Result
);
2237 // Otherwise should result in an error status
2239 Multiplicand
= 0x123456;
2241 Status
= SafeInt32Mult (Multiplicand
, Multiplier
, &Result
);
2242 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2245 TEST(MultiplicationTestSuite
, TestSafeInt64Mult
) {
2246 RETURN_STATUS Status
;
2252 // If the result of multiplication doesn't overflow MAX_INT64 and doesn't
2253 // underflow MIN_UINT64, it will succeed
2255 Multiplicand
= 0x123456789;
2256 Multiplier
= 0x6789abcd;
2258 Status
= SafeInt64Mult (Multiplicand
, Multiplier
, &Result
);
2259 ASSERT_EQ (Status
, RETURN_SUCCESS
);
2260 ASSERT_EQ (0x75cd9045220d6bb5, Result
);
2263 // Otherwise should result in an error status
2265 Multiplicand
= 0x123456789;
2266 Multiplier
= 0xa789abcd;
2267 Status
= SafeInt64Mult (Multiplicand
, Multiplier
, &Result
);
2268 ASSERT_EQ (RETURN_BUFFER_TOO_SMALL
, Status
);
2271 int main(int argc
, char* argv
[]) {
2272 testing::InitGoogleTest(&argc
, argv
);
2273 return RUN_ALL_TESTS();